IntervalSet
IntervalSet implements a set of sorted non-overlapping ranges. A range's start is always interpreted as inclusive while the end is exclusive.
Installation
Add this line to your application's Gemfile:
gem 'interval_set'
And then execute:
$ bundle
Or install it yourself as:
$ gem install interval_set
Documentation
http://www.rubydoc.info/gems/interval_set
Usage
Create a interval set:
IntervalSet.new
IntervalSet[]
IntervalSet[0...1]
IntervalSet[0...1, 2...3]
IntervalSet[0...1, 1...2]
array = [0...1, 2...3]
IntervalSet[*array]
Add a range:
IntervalSet.new << (0...1)
IntervalSet.new.add(0...1)
i = IntervalSet.new
i << (0...1)
i << (2...3)
i << (1...2)
i << (-1...4)
Remove a range:
i = IntervalSet[0...10]
i >> (2...8)
i.remove(0...2)
Get bounds:
i = IntervalSet[0...1, 2...3]
i.min
i.max
i.bounds
Check empty:
IntervalSet[].empty?
i = IntervalSet[0...1]
i.empty?
i >> (0...1)
i.empty?
Count ranges:
i = IntervalSet[]
i.count
i << (0...1)
i.count
i << (2...3)
i.count
i << (1...2)
i.count
Check inclusion:
i = IntervalSet[0...1]
i.include?(0)
i.include?(0.5)
i.include?(1)
Check intersection:
i = IntervalSet[0...1]
i.intersect?(0...1)
i.intersect?(0...2)
i.intersect?(1...2)
i.intersect?(IntervalSet[0...1])
i.intersect?(IntervalSet[0...1, 2...3])
i.intersect?(IntervalSet[2...3])
Calculate union:
IntervalSet[0...1, 2...3] | IntervalSet[1...2, 4...5] # -> [0...3, 4...5]
Calculate intersection:
IntervalSet[0...2, 3...5] & IntervalSet[1...4, 5...6]
Calculate difference:
IntervalSet[0...2, 3...5] - IntervalSet[1...4, 5...6]
Calculate exclusive set:
IntervalSet[0...1] ^ IntervalSet[1...2]
IntervalSet[0...2, 4...6] ^ IntervalSet[1...5, 7...8]
IntervalSet[0...1] ^ IntervalSet[0...1]
Compare sets:
IntervalSet[0...1] > IntervalSet[0...1]
IntervalSet[0...2] > IntervalSet[0...1]
IntervalSet[0...1] > IntervalSet[1...3]
IntervalSet[0...1] >= IntervalSet[0...1]
IntervalSet[0...2] >= IntervalSet[0...1]
IntervalSet[0...1] >= IntervalSet[0...1, 2...3]
IntervalSet[0...3] >= IntervalSet[0...1, 2...3]
IntervalSet[0...1] < IntervalSet[0...2]
IntervalSet[1...3] < IntervalSet[0...2]
IntervalSet[1...3] < IntervalSet[0...2]
IntervalSet[0...1] <= IntervalSet[0...1]
IntervalSet[0...1] <= IntervalSet[0...2]
IntervalSet[0...1, 2...3] <= IntervalSet[0...1]
IntervalSet[0...1, 2...3] <= IntervalSet[0...3]
IntervalSet[0...1] == IntervalSet[0...1]
IntervalSet[0...1] == IntervalSet[1...2]
Use in case statements:
case 2.5
when IntervalSet[0...2] then 'between 0 and 2'
when IntervalSet[2...3] then 'between 2 and 3'
end
Shift by a given amount:
IntervalSet[0...1].shift(1)
Note that shift(0)
will not be optimized since IntervalSet does not assume numbers as element type.
Buffer left and right:
IntervalSet[1...2].buffer(1, 2)
IntervalSet[0...4].buffer(-1, -2)
IntervalSet[1...2].buffer(-0.5, -0.5)
Convolve sets: A ∗ B = { a + b | a ∈ A ∧ b ∈ B }
IntervalSet[0...4] * (-1...2)
IntervalSet[0...4] * (0...0)
IntervalSet[0...4] * (1...0)
IntervalSet[0...1, 10...12] * IntervalSet[-2...1, 1...2]
Copy another interval set:
a = IntervalSet[0...1]
b = IntervalSet[2...3]
a.copy(b)
a
b
Clone another interval set:
a = IntervalSet[0...1]
b = a.clone
b << (2...3)
b
Use other types:
a = Date.parse('2000-01-01')
b = Date.parse('2000-01-02')
c = Date.parse('2000-01-03')
i = IntervalSet[a...b]
i << (b...c)
i.shift!(1)
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the specs. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/rjasper/ruby-interval_set. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the IntervalSet project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.