range-set 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dd2cdb9481ae14301f8e9725a6caafc29456fc31
4
+ data.tar.gz: e3ed01f972d39d322c6f454a79f029f38fd70e36
5
+ SHA512:
6
+ metadata.gz: bb0c5c8346dd7f53f317cdad892403198a62b1c76ac591f9e88788b374d35ab7d6e1d85e4b86292a0262e124274ab5155e6eb818bf4e99c7f736f13f83b30003
7
+ data.tar.gz: 8da9e79940622f2f5340e9a0050d35cb8ecef7120bce7d8bcf6e23f9272b50c063d2e9cf0d6831f4a313c2fe678e44232862bafcfa220529163282ba8f3bd37c
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ group :development, :test do
6
+ gem 'rake'
7
+ end
8
+
9
+ group :test do
10
+ gem 'rspec'
11
+ gem 'rr'
12
+ end
@@ -0,0 +1,3 @@
1
+ == 1.0.0
2
+
3
+ * Birthday!
data/LICENSE ADDED
@@ -0,0 +1,177 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
@@ -0,0 +1,93 @@
1
+
2
+ ## range-set [![Build Status](https://secure.travis-ci.org/camertron/range-set.png?branch=master)](http://travis-ci.org/camertron/range-set)
3
+
4
+ RangeSet is an integer set, implemented under the hood with ranges. The idea is that it's more efficient to store sequential data in ranges rather than as single elements. By definition, RangeSets contain no duplicates.
5
+
6
+ ## Installation
7
+
8
+ `gem install range-set`
9
+
10
+ ## Usage
11
+
12
+ ```ruby
13
+ require 'range-set'
14
+ ```
15
+
16
+ ### Creating Range Sets
17
+
18
+ Use the `from_array` or `new` methods to create RangeSets:
19
+
20
+ ```ruby
21
+ set = RangeSet.from_array([1, 2, 3, 6, 7, 8, 11, 14, 17, 18, 19])
22
+ set.to_a # [1..3, 6..8, 11..11, 14..14, 17..19]
23
+ ```
24
+
25
+ The `to_a` method takes a boolean argument that determines whether or not to turn isolated elements into zero-length ranges or leave them as single elements:
26
+
27
+ ```ruby
28
+ set = RangeSet.from_array([1, 2, 3, 6, 7, 8, 11, 14, 17, 18, 19])
29
+ set.to_a(true) # [1..3, 6..8, 11, 14, 17..19]
30
+ ```
31
+
32
+ ### Union
33
+
34
+ Combining two RangeSets:
35
+
36
+ ```ruby
37
+ set = RangeSet.new([3..10]).union(
38
+ RangeSet.new([9..11, 1..2, 2..3, 14..18])
39
+ )
40
+
41
+ set.to_a # [1..11, 14..18]
42
+ ```
43
+
44
+ ### Intersection
45
+
46
+ Finding the elements common to two RangeSets:
47
+
48
+ ```ruby
49
+ set = RangeSet.new([3..10]).intersection(RangeSet.new([1..7]))
50
+ set.to_a # [3..7]
51
+ ```
52
+
53
+ ### Subtract
54
+
55
+ Removing the elements contained in one RangeSet from another:
56
+
57
+ ```ruby
58
+ set = RangeSet.new([3..10]).subtract(RangeSet.new([1..5]))
59
+ set.to_a # [6..10]
60
+ ```
61
+
62
+ ### Difference
63
+
64
+ The difference here means the symmetric difference (i.e. the union without the intersection)
65
+ [http://en.wikipedia.org/wiki/Symmetric_difference](http://en.wikipedia.org/wiki/Symmetric_difference)
66
+
67
+ ```ruby
68
+ set = RangeSet.new([1..10]).difference(RangeSet.new([3..8]))
69
+ set.to_a # [1..2, 9..10]
70
+ ```
71
+
72
+ ### Include
73
+
74
+ ```ruby
75
+ set = RangeSet.new([1..5, 9..16])
76
+
77
+ set.include?(4) # true
78
+ set.include?(6) # false
79
+ set.include?(10..15) # true
80
+ set.include?(4..6) # false
81
+ ```
82
+
83
+ ## Requirements
84
+
85
+ No external requirements.
86
+
87
+ ## Running Tests
88
+
89
+ `bundle exec rake` or `bundle exec rspec`
90
+
91
+ ## License
92
+
93
+ Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0
@@ -0,0 +1,19 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'rubygems' unless ENV['NO_RUBYGEMS']
4
+
5
+ require 'bundler'
6
+
7
+ require 'rspec/core/rake_task'
8
+ require 'rubygems/package_task'
9
+
10
+ require './lib/range-set'
11
+
12
+ Bundler::GemHelper.install_tasks
13
+
14
+ task :default => :spec
15
+
16
+ desc 'Run specs'
17
+ RSpec::Core::RakeTask.new do |t|
18
+ t.pattern = './spec/**/*_spec.rb'
19
+ end
@@ -0,0 +1,234 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'set'
4
+
5
+ # An integer set, implemented under the hood with ranges. The idea is
6
+ # that it's more efficient to store sequential data in ranges rather
7
+ # than as single elements. By definition, RangeSets contain no duplicates.
8
+ class RangeSet
9
+
10
+ attr_reader :ranges
11
+
12
+ class << self
13
+
14
+ def from_array(array)
15
+ new(rangify(array))
16
+ end
17
+
18
+ # Turns an array of integers into ranges. The "compress" option indicates
19
+ # whether or not to turn isolated elements into zero-length ranges or leave
20
+ # them as single elements.
21
+ #
22
+ # For example:
23
+ # rangify([1, 2, 4], false) returns [1..2, 4..4]
24
+ # rangify([1, 2, 4], true) returns [1..2, 4]
25
+ def rangify(list, compress = false)
26
+ last_item = nil
27
+
28
+ list.sort.inject([]) do |ret, item|
29
+ if last_item
30
+ diff = item - last_item
31
+
32
+ if diff > 0
33
+ if diff == 1
34
+ ret[-1] << item
35
+ else
36
+ ret << [item]
37
+ end
38
+
39
+ last_item = item
40
+ end
41
+ else
42
+ ret << [item]
43
+ last_item = item
44
+ end
45
+
46
+ ret
47
+ end.map do |sub_list|
48
+ if compress && sub_list.size == 1
49
+ sub_list.first
50
+ else
51
+ sub_list.first..sub_list.last
52
+ end
53
+ end
54
+ end
55
+
56
+ end
57
+
58
+ def initialize(ranges)
59
+ @ranges = ranges
60
+ flatten
61
+ end
62
+
63
+ def to_a(compress = false)
64
+ if compress
65
+ ranges.map do |range|
66
+ if range.first == range.last
67
+ range.first
68
+ else
69
+ range
70
+ end
71
+ end
72
+ else
73
+ ranges.dup
74
+ end
75
+ end
76
+
77
+ def to_full_a
78
+ ranges.inject([]) do |ret, range|
79
+ ret + range.to_a
80
+ end
81
+ end
82
+
83
+ def to_set
84
+ Set.new(to_full_a)
85
+ end
86
+
87
+ def include?(obj)
88
+ case obj
89
+ when Numeric
90
+ ranges.any? { |range| range.include?(obj) }
91
+ when Range
92
+ ranges.any? do |range|
93
+ range.first <= obj.first && range.last >= obj.last
94
+ end
95
+ else
96
+ false
97
+ end
98
+ end
99
+
100
+ def empty?
101
+ ranges.empty?
102
+ end
103
+
104
+ def union(range_set)
105
+ self.class.new(range_set.ranges + ranges)
106
+ end
107
+
108
+ def intersection(range_set)
109
+ new_ranges = []
110
+
111
+ range_set.ranges.each do |their_range|
112
+ ranges.each do |our_range|
113
+ if overlap?(their_range, our_range)
114
+ if intrsc = find_intersection(our_range, their_range)
115
+ new_ranges << intrsc
116
+ end
117
+ end
118
+ end
119
+ end
120
+
121
+ self.class.new(new_ranges)
122
+ end
123
+
124
+ def subtract(range_set)
125
+ return self if range_set.empty?
126
+ remaining = range_set.ranges.dup
127
+ current_ranges = ranges.dup
128
+ new_ranges = []
129
+
130
+ while their_range = remaining.shift
131
+ new_ranges = []
132
+
133
+ current_ranges.each do |our_range|
134
+ if overlap?(their_range, our_range)
135
+ new_ranges += find_subtraction(their_range, our_range)
136
+ else
137
+ new_ranges << our_range
138
+ end
139
+ end
140
+
141
+ current_ranges = new_ranges
142
+ end
143
+
144
+ self.class.new(new_ranges)
145
+ end
146
+
147
+ # symmetric difference (the union without the intersection)
148
+ # http://en.wikipedia.org/wiki/Symmetric_difference
149
+ def difference(range_set)
150
+ union(range_set).subtract(intersection(range_set))
151
+ end
152
+
153
+ private
154
+
155
+ def flatten
156
+ return if ranges.size <= 1
157
+
158
+ sorted_ranges = ranges.sort do |a, b|
159
+ if is_numeric_range?(a) && is_numeric_range?(b)
160
+ a.first <=> b.first
161
+ else
162
+ 1
163
+ end
164
+ end
165
+
166
+ new_ranges = [sorted_ranges.first]
167
+
168
+ sorted_ranges.each do |range|
169
+ previous_range = new_ranges.pop
170
+
171
+ if adjacent?(previous_range, range) || overlap?(previous_range, range)
172
+ new_ranges.push(
173
+ [range.first, previous_range.first].min..[range.last, previous_range.last].max
174
+ )
175
+ else
176
+ new_ranges.push(previous_range)
177
+ new_ranges.push(range)
178
+ end
179
+ end
180
+
181
+ @ranges = new_ranges
182
+ end
183
+
184
+ # returns true if range1 and range2 are within 1 of each other
185
+ def adjacent?(range1, range2)
186
+ is_numeric_range?(range1) && is_numeric_range?(range2) &&
187
+ (range1.last == range2.first - 1 || range2.first == range1.last + 1)
188
+ end
189
+
190
+ def overlap?(range1, range2)
191
+ is_numeric_range?(range1) && is_numeric_range?(range2) && (
192
+ (range1.last >= range2.first && range1.last <= range2.last) ||
193
+ (range1.first >= range2.first && range1.first <= range2.last) ||
194
+ (range1.first <= range2.first && range1.last >= range2.last)
195
+ )
196
+ end
197
+
198
+ def find_intersection(range1, range2)
199
+ # range2 entirely contains range1
200
+ if range2.first <= range1.first && range1.last <= range2.last
201
+ range1.dup
202
+ elsif range1.last >= range2.first && range1.last <= range2.last
203
+ range2.first..range1.last
204
+ elsif range1.first >= range2.first && range1.first <= range2.last
205
+ range1.first..range2.last
206
+ elsif range1.first <= range2.first && range1.last >= range2.last
207
+ [range1.first, range2.first].max..[range1.last, range2.last].min
208
+ end
209
+ end
210
+
211
+ # subtracts range1 from range2 (range2 - range1)
212
+ def find_subtraction(range1, range2)
213
+ # case: range1 contains range2 entirely (also handles equal case)
214
+ result = if range1.first <= range2.first && range2.last <= range1.last
215
+ []
216
+ # case: range1 comes in the middle
217
+ elsif range2.first <= range1.first && range2.last >= range1.last
218
+ [range2.first..(range1.first - 1), (range1.last + 1)..range2.last]
219
+ # case: range1 trails
220
+ elsif range2.last >= range1.first && range1.last >= range2.last
221
+ [range2.first..(range1.first - 1)]
222
+ # case: range1 leads
223
+ elsif range1.last >= range2.first && range1.first <= range2.first
224
+ [(range1.last + 1)..range2.last]
225
+ end
226
+
227
+ result.reject { |r| r.first > r.last }
228
+ end
229
+
230
+ def is_numeric_range?(range)
231
+ range.first.is_a?(Numeric) && range.last.is_a?(Numeric)
232
+ end
233
+
234
+ end
@@ -0,0 +1,5 @@
1
+ # encoding: UTF-8
2
+
3
+ class RangeSet
4
+ VERSION = "1.0.0"
5
+ end
@@ -0,0 +1,18 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), 'lib')
2
+ require 'range-set/version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "range-set"
6
+ s.version = ::RangeSet::VERSION
7
+ s.authors = ["Cameron Dutro"]
8
+ s.email = ["camertron@gmail.com"]
9
+ s.homepage = "http://github.com/camertron"
10
+
11
+ s.description = s.summary = "An efficient set implementation that treats runs of sequential elements as ranges."
12
+
13
+ s.platform = Gem::Platform::RUBY
14
+ s.has_rdoc = true
15
+
16
+ s.require_path = 'lib'
17
+ s.files = Dir["{lib,spec}/**/*", "Gemfile", "History.txt", "LICENSE", "README.md", "Rakefile", "range-set.gemspec"]
18
+ end
@@ -0,0 +1,166 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe RangeSet do
6
+ describe "rangify" do
7
+ it "should identify runs in an array of integers and return an array of ranges" do
8
+ expect(RangeSet.rangify([1, 2, 3, 6, 7, 8, 11, 14, 17, 18, 19])).
9
+ to eq([1..3, 6..8, 11..11, 14..14, 17..19])
10
+ end
11
+
12
+ it "should not rangify zero-length values when told to compress" do
13
+ expect(RangeSet.rangify([1, 2, 3, 5, 8, 11, 12], true)).
14
+ to eq([1..3, 5, 8, 11..12])
15
+ end
16
+ end
17
+
18
+ describe "#to_a" do
19
+ it "should return a copy of the ranges in the set" do
20
+ set = RangeSet.new([1..10])
21
+ expect(set.to_a.object_id).not_to eq(set.ranges.object_id)
22
+ end
23
+
24
+ it "should return compressed ranges when asked" do
25
+ expect(RangeSet.new([1..10, 12..12]).to_a(true)).to eq([1..10, 12])
26
+ end
27
+ end
28
+
29
+ describe "#to_full_a" do
30
+ it "expands all ranges" do
31
+ expect(RangeSet.new([1..5]).to_full_a).to eq([1, 2, 3, 4, 5])
32
+ end
33
+ end
34
+
35
+ describe "#new" do
36
+ it "should flatten leading overlapping ranges" do
37
+ expect(RangeSet.new([3..10, 1..8]).to_a).to eq([1..10])
38
+ end
39
+
40
+ it "should flatten trailing overlapping ranges" do
41
+ expect(RangeSet.new([1..10, 8..12]).to_a).to eq([1..12])
42
+ end
43
+
44
+ it "should flatten completely overlapping ranges" do
45
+ expect(RangeSet.new([2..3, 1..10]).to_a).to eq([1..10])
46
+ end
47
+
48
+ it "should join ranges that are within 1 of each other" do
49
+ expect(RangeSet.new([1..10, 11..15]).to_a).to eq([1..15])
50
+ end
51
+ end
52
+
53
+ describe "#union" do
54
+ it "should aggregate the ranges and flatten when necessary" do
55
+ set = RangeSet.new([3..10]).union(
56
+ RangeSet.new([9..11, 1..2, 2..3, 14..18])
57
+ )
58
+
59
+ expect(set.to_a).to eq([1..11, 14..18])
60
+ end
61
+
62
+ it "should aggregate the ranges correctly even if they're given in reverse order" do
63
+ set = RangeSet.new([3..10]).union(RangeSet.new([1..4]))
64
+ expect(set.to_a).to eq([1..10])
65
+
66
+ set = RangeSet.new([1..4]).union(RangeSet.new([3..10]))
67
+ expect(set.to_a).to eq([1..10])
68
+ end
69
+ end
70
+
71
+ describe "#intersection" do
72
+ it "computes the intersection for leading overlapping ranges" do
73
+ set = RangeSet.new([3..10]).intersection(RangeSet.new([1..7]))
74
+ expect(set.to_a).to eq([3..7])
75
+ end
76
+
77
+ it "computes the intersection for trailing overlapping ranges" do
78
+ set = RangeSet.new([1..10]).intersection(RangeSet.new([5..15]))
79
+ expect(set.to_a).to eq([5..10])
80
+ end
81
+
82
+ it "computes the intersection for two completely overlapping ranges" do
83
+ set = RangeSet.new([1..10]).intersection(RangeSet.new([3..6]))
84
+ expect(set.to_a).to eq([3..6])
85
+ end
86
+
87
+ it "returns an empty intersection if no elements exist in both ranges" do
88
+ set = RangeSet.new([1..10]).intersection(RangeSet.new([15..20]))
89
+ expect(set.to_a).to eq([])
90
+ end
91
+
92
+ it "returns partial intersections when the range set contains multiple matching ranges" do
93
+ set = RangeSet.new([1..5, 7..10]).intersection(RangeSet.new([3..8]))
94
+ expect(set.to_a).to eq([3..5, 7..8])
95
+ end
96
+
97
+ it "doesn't matter what order the ranges are compared in" do
98
+ set = RangeSet.new([2..3]).intersection(RangeSet.new([1..4]))
99
+ expect(set.to_a).to eq([2..3])
100
+
101
+ set = RangeSet.new([1..4]).intersection(RangeSet.new([2..3]))
102
+ expect(set.to_a).to eq([2..3])
103
+ end
104
+ end
105
+
106
+ describe "#subtract" do
107
+ it "subtracts the intersection for leading overlapping ranges" do
108
+ set = RangeSet.new([3..10]).subtract(RangeSet.new([1..5]))
109
+ expect(set.to_a).to eq([6..10])
110
+ end
111
+
112
+ it "subtracts the intersection for trailing overlapping ranges" do
113
+ set = RangeSet.new([1..7]).subtract(RangeSet.new([3..9]))
114
+ expect(set.to_a).to eq([1..2])
115
+ end
116
+
117
+ it "subtracts the intersection for completely overlapping ranges (generates two ranges)" do
118
+ set = RangeSet.new([1..10]).subtract(RangeSet.new([4..6]))
119
+ expect(set.to_a).to eq([1..3, 7..10])
120
+ end
121
+
122
+ it "subtracts the intersection when the range set contians multiple matching ranges" do
123
+ set = RangeSet.new([1..5, 7..10]).subtract(RangeSet.new([3..8]))
124
+ expect(set.to_a).to eq([1..2, 9..10])
125
+ end
126
+
127
+ it "does not change object when subtracting an empty set" do
128
+ set = RangeSet.new([1..5]).subtract(RangeSet.new([]))
129
+ expect(set.to_a).to eq([1..5])
130
+ end
131
+ end
132
+
133
+ describe "#difference" do
134
+ it "returns the symmetric difference (union - intersection) between completely overlapping ranges" do
135
+ set = RangeSet.new([1..10]).difference(RangeSet.new([3..8]))
136
+ expect(set.to_a).to eq([1..2, 9..10])
137
+ end
138
+
139
+ it "returns the symmetric difference between leading overlapping ranges" do
140
+ set = RangeSet.new([3..10]).difference(RangeSet.new([1..5]))
141
+ expect(set.to_a).to eq([1..2, 6..10])
142
+ end
143
+
144
+ it "returns the symmetric difference between trailing overlapping ranges" do
145
+ set = RangeSet.new([1..5]).difference(RangeSet.new([3..8]))
146
+ expect(set.to_a).to eq([1..2, 6..8])
147
+ end
148
+ end
149
+
150
+ describe "#include?" do
151
+ let (:set) { RangeSet.new([1..5, 9..16]) }
152
+
153
+ it "returns true if the set completely includes the range, false otherwise" do
154
+ expect(set).to include(10..15)
155
+ expect(set).not_to include(3..8)
156
+ expect(set).not_to include(8..14)
157
+ end
158
+
159
+ it "returns true if the set contains the value, false otherwise" do
160
+ expect(set).to include(3)
161
+ expect(set).to include(10)
162
+ expect(set).not_to include(6)
163
+ expect(set).not_to include(8)
164
+ end
165
+ end
166
+ end
@@ -0,0 +1,8 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'rspec'
4
+ require 'range-set'
5
+
6
+ RSpec.configure do |config|
7
+ config.mock_with :rr
8
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: range-set
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Cameron Dutro
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-20 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: An efficient set implementation that treats runs of sequential elements
14
+ as ranges.
15
+ email:
16
+ - camertron@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - Gemfile
22
+ - History.txt
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - lib/range-set.rb
27
+ - lib/range-set/version.rb
28
+ - range-set.gemspec
29
+ - spec/range-set_spec.rb
30
+ - spec/spec_helper.rb
31
+ homepage: http://github.com/camertron
32
+ licenses: []
33
+ metadata: {}
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 2.2.2
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: An efficient set implementation that treats runs of sequential elements as
54
+ ranges.
55
+ test_files: []