range_list 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +7 -3
- data/lib/range_list/version.rb +1 -1
- data/lib/range_list.rb +26 -11
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cbae62498893749767a5138f59caacd3de650bf62f566859d2ff16fc69e04a73
|
4
|
+
data.tar.gz: b5317ec465420963e3d392f89f22e2bf3a7a662a569c82084b4cbe5383eaab32
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9f39a299e682ca947eeb1518cdba35eaad08b806433a849f1651ead445a7602fcb7c290611b8c3fa2601994003b74f3edbbe7909967d02064316db27d0d03a46
|
7
|
+
data.tar.gz: 286f03c1ecc1de6b5d0b5d4d271b0fcb2d3ea2f56c5a146d26e66b68a0fe168e0a8d093e781bb7c4a4551501682e8ea4534495a86f669470b73db52aa48a2028
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
# RangeList
|
2
2
|
|
3
|
+
RangeList is a library that can handle ranges quickly and appropriately.
|
4
|
+
|
3
5
|
A pair of integers define a range, for example: [1, 5), this range includes integers: 1, 2, 3, and 4.
|
4
6
|
A range list is an aggregate of these ranges: [1, 5), [10, 11), [100, 201).
|
5
7
|
|
6
|
-
RangeList
|
7
|
-
you can handle ranges more easily.
|
8
|
+
RangeList use AVL Tree processing internally, so range operations are very fast.
|
8
9
|
|
9
10
|
[![Gem Version](https://badge.fury.io/rb/range_list.svg)](https://rubygems.org/gems/range_list)
|
10
11
|
[![Documentation](https://img.shields.io/badge/docs-YARD-blue.svg)](https://rubydoc.info/gems/range_list)
|
@@ -45,8 +46,11 @@ range_list.contains_any?([8, 10]) # false
|
|
45
46
|
range_list.contains_any?([8, 11]) # true
|
46
47
|
range_list.contains_all?([8, 11]) # false
|
47
48
|
range_list.contains_all?([10, 11]) # true
|
49
|
+
range_list.contains?(5) # false
|
50
|
+
range_list.contains?(10) # true
|
48
51
|
|
49
|
-
|
52
|
+
# RangeList has the `Enumerable` ability, feel free to use `each`, `map`, `to_a`, `min`, `max` etc.
|
53
|
+
range_list.each { |range_start, range_end| puts "#{range_start} <= x < #{range_end}" }
|
50
54
|
|
51
55
|
range_list.remove([0, 100])
|
52
56
|
range_list.print #
|
data/lib/range_list/version.rb
CHANGED
data/lib/range_list.rb
CHANGED
@@ -15,6 +15,7 @@ class RangeList
|
|
15
15
|
# @param range [Array<Integer>] the range, first element is range start, second is range end.
|
16
16
|
# Range end need be greater or equal than range start.
|
17
17
|
# @return [RangeList]
|
18
|
+
# @raise [ArgumentError] when argement invalid
|
18
19
|
def add(range)
|
19
20
|
validate_range!(range)
|
20
21
|
|
@@ -50,6 +51,7 @@ class RangeList
|
|
50
51
|
# Remove range from current range list.
|
51
52
|
# @param (see #add)
|
52
53
|
# @return [RangeList]
|
54
|
+
# @raise (see #add)
|
53
55
|
def remove(range)
|
54
56
|
validate_range!(range)
|
55
57
|
|
@@ -78,29 +80,48 @@ class RangeList
|
|
78
80
|
# Print current range list.
|
79
81
|
# @return [void]
|
80
82
|
def print
|
81
|
-
puts
|
83
|
+
puts map { |k, v| "[#{k}, #{v})" }.join(" ")
|
82
84
|
end
|
83
85
|
|
84
|
-
# Returns true if
|
86
|
+
# Returns true if current ranges contains all elements of the range.
|
85
87
|
# @param (see #add)
|
86
88
|
# @return [Boolean]
|
87
89
|
# @note return false if the argument range is empty
|
90
|
+
# @raise (see #add)
|
88
91
|
def contains_all?(range)
|
92
|
+
validate_range!(range)
|
93
|
+
|
94
|
+
# Return false when range is empty.
|
89
95
|
return false if empty_range?(range)
|
90
96
|
|
91
97
|
start_floor_entry = tree.floor_entry(range[0])
|
92
98
|
!start_floor_entry.nil? && start_floor_entry.value >= range[1]
|
93
99
|
end
|
94
100
|
|
95
|
-
# Returns true if
|
101
|
+
# Returns true if current ranges contains any element of the range.
|
96
102
|
# @param (see #add)
|
97
103
|
# @return [Boolean]
|
98
104
|
# @note return false if the argument range is empty
|
105
|
+
# @raise (see #add)
|
99
106
|
def contains_any?(range)
|
107
|
+
validate_range!(range)
|
108
|
+
|
109
|
+
# Return false when range is empty.
|
100
110
|
return false if empty_range?(range)
|
101
111
|
|
102
|
-
|
103
|
-
!
|
112
|
+
end_lower_entry = tree.lower_entry(range[1])
|
113
|
+
!end_lower_entry.nil? && end_lower_entry.value > range[0]
|
114
|
+
end
|
115
|
+
|
116
|
+
# Returns true if current ranges contains the element.
|
117
|
+
# @param element [Integer] the range element
|
118
|
+
# @return [Boolean]
|
119
|
+
# @raise (see #add)
|
120
|
+
def contains?(element)
|
121
|
+
raise ArgumentError, "`element` should be `Integer` type." unless element.is_a?(Integer)
|
122
|
+
|
123
|
+
floor_entry = tree.floor_entry(element)
|
124
|
+
!floor_entry.nil? && floor_entry.value > element
|
104
125
|
end
|
105
126
|
|
106
127
|
# Give RangeList iterative ability.
|
@@ -120,12 +141,6 @@ class RangeList
|
|
120
141
|
to_a.inspect
|
121
142
|
end
|
122
143
|
|
123
|
-
# Return the current range as an array.
|
124
|
-
# @return [Array]
|
125
|
-
def to_a
|
126
|
-
tree.empty? ? [] : tree.to_a
|
127
|
-
end
|
128
|
-
|
129
144
|
private
|
130
145
|
|
131
146
|
attr_reader :tree
|