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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f27b8cd8551942e4ff09f2cc32c525e7fd5724cdbea8e322203cd01df681fb46
4
- data.tar.gz: c24ffbe52e439561c2010fccdf67f68d9700703b23c4ed45d34c143ba5e6de46
3
+ metadata.gz: cbae62498893749767a5138f59caacd3de650bf62f566859d2ff16fc69e04a73
4
+ data.tar.gz: b5317ec465420963e3d392f89f22e2bf3a7a662a569c82084b4cbe5383eaab32
5
5
  SHA512:
6
- metadata.gz: 62a1a66f4607c3e17424d8bd76cd8371626e8ff0d64961f189f09534fb33cc6e60537e821b3bb743389057db0c76a4e5e83cd7cbe8d3a6872c6c808891237f16
7
- data.tar.gz: 9fb515423d8048f124e15a88cb3c5b718e7d273694bb8e9fe128ce2e37ebda636ba1da330af406abfc57d8566686fa71eff91d88c7af1eee55cd9b5b4ad75ad1
6
+ metadata.gz: 9f39a299e682ca947eeb1518cdba35eaad08b806433a849f1651ead445a7602fcb7c290611b8c3fa2601994003b74f3edbbe7909967d02064316db27d0d03a46
7
+ data.tar.gz: 286f03c1ecc1de6b5d0b5d4d271b0fcb2d3ea2f56c5a146d26e66b68a0fe168e0a8d093e781bb7c4a4551501682e8ea4534495a86f669470b73db52aa48a2028
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- range_list (1.0.0)
4
+ range_list (1.1.0)
5
5
  treemap (~> 1.0)
6
6
 
7
7
  GEM
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 offers `add`, `remove`, `print`, `containsAll?`, `containsAny?` methods, and offers the `Enumerable` ability,
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
- range_list.each { |range_start, range_end| puts "#{range_start} <= x < #{range_end}" } # print all range in order
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 #
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class RangeList
4
- VERSION = "1.0.0"
4
+ VERSION = "1.1.0"
5
5
  end
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 to_a.map { |k, v| "[#{k}, #{v})" }.join(" ")
83
+ puts map { |k, v| "[#{k}, #{v})" }.join(" ")
82
84
  end
83
85
 
84
- # Returns true if this set contains all elements of the range.
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 this set contains any element of the range.
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
- lower_floor_entry = tree.lower_entry(range[1])
103
- !lower_floor_entry.nil? && lower_floor_entry.value > range[0]
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: range_list
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Song Huang