set_of_ranges 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +22 -0
- data/README.md +49 -0
- data/Rakefile +5 -0
- data/lib/set_of_ranges/set.rb +59 -0
- data/lib/set_of_ranges/version.rb +3 -0
- data/lib/set_of_ranges.rb +5 -0
- data/spec/set_of_ranges/set_spec.rb +103 -0
- data/spec/set_of_ranges/version_spec.rb +7 -0
- data/spec/spec_helper.rb +8 -0
- metadata +70 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9142960fa98dfc74d66581a09fffd896fae62a28
|
4
|
+
data.tar.gz: f63d71318e3c2968282dc83c81d27e7344f916ec
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e9469ebb965c96ea42e8c046b180f0232271e73be1f95ba5451454143d2209f2f3af16d2dd130bceccf1d5b76d87e2a287681ef7b499b02f0dc2bcbedc6b567a
|
7
|
+
data.tar.gz: 3fdb8eb4abaf4535a05ad4f319ed3cf81cfd39e8269881637e8f6b44b59afc834489fcc48c63c06b51d05faf4f670fc4115d56da1faac7b788382951476c3fd1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Galen Palmer
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# SetOfRanges [![Build Status](https://secure.travis-ci.org/palmergs/set_of_ranges.png?branch=master)](http://travis-ci.org/palmergs/set_of_ranges)
|
2
|
+
|
3
|
+
`SetOfRanges::Set` is a simple gem that inherits from `Set` and allows `Range` instances as values. It behaves like `Set` except that the add method attempts to merge adjacent and overlapping ranges. It was designed for use with integer values. Insertion of new range values is proportional to the number of existing values in the set.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'set_of_ranges'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install set_of_ranges
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
require 'set_of_ranges'
|
22
|
+
|
23
|
+
set = SetOfRanges::Set.new
|
24
|
+
set << (5..10)
|
25
|
+
set << (15..20)
|
26
|
+
set.to_a #=> [5..10, 15..20]
|
27
|
+
|
28
|
+
set << (2..6)
|
29
|
+
set.to_a #=> [2..10, 15..20]
|
30
|
+
|
31
|
+
set << (3..9)
|
32
|
+
set.to_a #=> [2..10, 15..20]
|
33
|
+
|
34
|
+
set << (11...12)
|
35
|
+
set.to_a #=> [2..11, 15..20]
|
36
|
+
|
37
|
+
set << (12..14)
|
38
|
+
set.to_a #=> [2..20]
|
39
|
+
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
## Contributing
|
44
|
+
|
45
|
+
1. Fork it
|
46
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
47
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
48
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
49
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
class SetOfRanges::Set < Set
|
4
|
+
|
5
|
+
def add(obj)
|
6
|
+
raise ArgumentError if obj.nil?
|
7
|
+
raise ArgumentError unless obj.is_a? Range
|
8
|
+
rng = obj.exclude_end? ? (obj.begin..(obj.end-1)) : obj
|
9
|
+
|
10
|
+
if empty?
|
11
|
+
super(rng)
|
12
|
+
else
|
13
|
+
|
14
|
+
overlapping_ranges = Set.new
|
15
|
+
each do |r|
|
16
|
+
if r.begin <= rng.begin and r.end >= rng.end
|
17
|
+
|
18
|
+
# new range is completely contained within an
|
19
|
+
# existing range don't need to modify anything
|
20
|
+
return self
|
21
|
+
elsif (rng.begin < r.begin and rng.end >= r.begin - 1) or
|
22
|
+
(rng.end > r.end and rng.begin <= r.end + 1)
|
23
|
+
overlapping_ranges << r
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
if overlapping_ranges.size == 0
|
28
|
+
|
29
|
+
super(rng)
|
30
|
+
|
31
|
+
else
|
32
|
+
|
33
|
+
subtract(overlapping_ranges)
|
34
|
+
overlapping_ranges << rng
|
35
|
+
min = overlapping_ranges.map(&:begin).min
|
36
|
+
max = overlapping_ranges.map(&:end).max
|
37
|
+
super(min..max)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
self
|
42
|
+
end
|
43
|
+
|
44
|
+
def << obj
|
45
|
+
add(obj)
|
46
|
+
end
|
47
|
+
|
48
|
+
def merge enum
|
49
|
+
enum.each do |e|
|
50
|
+
add(e)
|
51
|
+
end
|
52
|
+
self
|
53
|
+
end
|
54
|
+
|
55
|
+
def replace enum
|
56
|
+
clear
|
57
|
+
merge(enum)
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SetOfRanges::Set do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
|
7
|
+
@r = SetOfRanges::Set.new
|
8
|
+
@r << (10..12)
|
9
|
+
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'raises an error unless a range is added' do
|
13
|
+
expect { @r << nil }.to raise_error(ArgumentError)
|
14
|
+
expect { @r << "test" }.to raise_error(ArgumentError)
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'ignores ranges within existing ranges' do
|
18
|
+
@r << (10..11)
|
19
|
+
@r.count.should == 1
|
20
|
+
@r.should include(10..12)
|
21
|
+
|
22
|
+
@r << (11..12)
|
23
|
+
@r.count.should == 1
|
24
|
+
@r.should include(10..12)
|
25
|
+
|
26
|
+
@r << (10..12)
|
27
|
+
@r.count.should == 1
|
28
|
+
@r.should include(10..12)
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'adds disjoint ranges' do
|
32
|
+
@r << (1..8)
|
33
|
+
@r << (14..15)
|
34
|
+
@r.count.should == 3
|
35
|
+
@r.should include (1..8)
|
36
|
+
@r.should include (14..15)
|
37
|
+
@r.should include (10..12)
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'adds overlapping ranges' do
|
41
|
+
|
42
|
+
@r << (9..13)
|
43
|
+
@r.count.should == 1
|
44
|
+
@r.should include (9..13)
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'adds overlapping ranges on beginning' do
|
49
|
+
@r << (9..11)
|
50
|
+
@r.count.should == 1
|
51
|
+
@r.should include (9..12)
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'adds overlapping ranges on the end' do
|
55
|
+
@r << (11..13)
|
56
|
+
@r.count.should == 1
|
57
|
+
@r.should include (10..13)
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'converts open ranges to closed ranges' do
|
61
|
+
@r << (1...5)
|
62
|
+
@r.count.should == 2
|
63
|
+
@r.should include (1..4)
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'merges multiple ranges if overlapping' do
|
67
|
+
@r << (14..18)
|
68
|
+
@r << (1..3)
|
69
|
+
@r.count.should == 3
|
70
|
+
@r << (3..14)
|
71
|
+
@r.count.should == 1
|
72
|
+
@r.should include (1..18)
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'merges adjacent ranges' do
|
76
|
+
@r << (8..9)
|
77
|
+
@r.count.should == 1
|
78
|
+
@r << (13..14)
|
79
|
+
@r.count.should == 1
|
80
|
+
@r.should include (8..14)
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'merges sets' do
|
84
|
+
r2 = SetOfRanges::Set.new
|
85
|
+
r2 << (1..10)
|
86
|
+
r2 << (20..30)
|
87
|
+
@r.merge(r2)
|
88
|
+
@r.count.should == 2
|
89
|
+
@r.should include (1..12)
|
90
|
+
@r.should include (20..30)
|
91
|
+
r2.count.should == 2
|
92
|
+
r2.should include (1..10)
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'replaces sets' do
|
96
|
+
r2 = SetOfRanges::Set.new
|
97
|
+
r2 << (20..30)
|
98
|
+
@r.replace(r2)
|
99
|
+
@r.count.should == 1
|
100
|
+
@r.should include (20..30)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: set_of_ranges
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Galen Palmer
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-12-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.10'
|
27
|
+
description: A subclass of Set that attempts to merge overlapping or adjacent Range
|
28
|
+
values.
|
29
|
+
email:
|
30
|
+
- palmergs@gmail.com
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- Rakefile
|
36
|
+
- lib/set_of_ranges/set.rb
|
37
|
+
- lib/set_of_ranges/version.rb
|
38
|
+
- lib/set_of_ranges.rb
|
39
|
+
- spec/set_of_ranges/set_spec.rb
|
40
|
+
- spec/set_of_ranges/version_spec.rb
|
41
|
+
- spec/spec_helper.rb
|
42
|
+
- README.md
|
43
|
+
- LICENSE.txt
|
44
|
+
homepage: https://github.com/palmergs/set_of_ranges
|
45
|
+
licenses: []
|
46
|
+
metadata: {}
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
requirements: []
|
62
|
+
rubyforge_project: set_of_ranges
|
63
|
+
rubygems_version: 2.0.3
|
64
|
+
signing_key:
|
65
|
+
specification_version: 4
|
66
|
+
summary: Developed to enable iteration through a set of discontiguous ranges.
|
67
|
+
test_files:
|
68
|
+
- spec/set_of_ranges/set_spec.rb
|
69
|
+
- spec/set_of_ranges/version_spec.rb
|
70
|
+
- spec/spec_helper.rb
|