segment_tree 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.rspec +1 -1
- data/lib/segment_tree.rb +4 -5
- data/lib/segment_tree/version.rb +2 -2
- data/segment_tree.gemspec +2 -2
- data/spec/spec_helper.rb +6 -2
- metadata +19 -13
- data/.gitignore +0 -17
- data/Gemfile +0 -6
- data/LICENSE +0 -22
- data/README.md +0 -94
- data/Rakefile +0 -2
- data/benchmark/benchmark.rb +0 -47
data/.rspec
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
--color
|
2
|
-
--format
|
2
|
+
--format progress
|
data/lib/segment_tree.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
1
|
require "forwardable"
|
2
|
-
require "segment_tree/version"
|
3
2
|
|
4
3
|
class SegmentTree
|
5
4
|
# An abstract tree node
|
6
5
|
class Node #:nodoc:all:
|
7
6
|
extend Forwardable
|
8
|
-
def_delegators :@range, :
|
7
|
+
def_delegators :@range, :include?, :begin, :end
|
9
8
|
end
|
10
9
|
|
11
10
|
# An elementary intervals or nodes container
|
@@ -24,14 +23,14 @@ class SegmentTree
|
|
24
23
|
# Find all intervals containing point +x+ within node's children. Returns array
|
25
24
|
def find(x)
|
26
25
|
[@left, @right].compact.
|
27
|
-
select { |node| node.
|
26
|
+
select { |node| node.include?(x) }.
|
28
27
|
map { |node| node.find(x) }.
|
29
28
|
flatten
|
30
29
|
end
|
31
30
|
|
32
31
|
# Find first interval containing point +x+ within node's children
|
33
32
|
def find_first(x)
|
34
|
-
subset = [@left, @right].compact.find { |node| node.
|
33
|
+
subset = [@left, @right].compact.find { |node| node.include?(x) }
|
35
34
|
subset && subset.find_first(x)
|
36
35
|
end
|
37
36
|
|
@@ -56,7 +55,7 @@ class SegmentTree
|
|
56
55
|
end
|
57
56
|
|
58
57
|
def find_first(x)
|
59
|
-
|
58
|
+
include?(x) ? self : nil
|
60
59
|
end
|
61
60
|
end
|
62
61
|
|
data/lib/segment_tree/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
class SegmentTree
|
2
|
-
VERSION = "0.0.
|
3
|
-
end
|
2
|
+
VERSION = "0.0.2"
|
3
|
+
end
|
data/segment_tree.gemspec
CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |gem|
|
|
8
8
|
gem.summary = %q{Tree data structure for storing segments. It allows querying which of the stored segments contain a given point.}
|
9
9
|
gem.homepage = "https://github.com/take-five/segment_tree"
|
10
10
|
|
11
|
-
gem.files = `git ls-files`.split($\)
|
12
|
-
gem.test_files = gem.files.grep(
|
11
|
+
gem.files = `git ls-files`.split($\).grep(/lib|spec/)
|
12
|
+
gem.test_files = gem.files.grep(/spec/)
|
13
13
|
gem.name = "segment_tree"
|
14
14
|
gem.require_paths = %W(lib)
|
15
15
|
gem.version = SegmentTree::VERSION
|
data/spec/spec_helper.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
require "bundler/setup"
|
2
|
-
require "simplecov"
|
3
2
|
|
4
3
|
RSpec.configure do |config|
|
5
4
|
# Run specs in random order to surface order dependencies. If you find an
|
@@ -9,4 +8,9 @@ RSpec.configure do |config|
|
|
9
8
|
config.order = 'random'
|
10
9
|
end
|
11
10
|
|
12
|
-
|
11
|
+
if defined?(RUBY_ENGINE) &&
|
12
|
+
RUBY_ENGINE == "ruby" &&
|
13
|
+
RUBY_VERSION > "1.9"
|
14
|
+
require "simplecov"
|
15
|
+
SimpleCov.start
|
16
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: segment_tree
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-08-
|
12
|
+
date: 2012-08-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,15 @@ dependencies:
|
|
21
21
|
version: '1.0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.0'
|
25
30
|
- !ruby/object:Gem::Dependency
|
26
31
|
name: rspec
|
27
|
-
requirement:
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
28
33
|
none: false
|
29
34
|
requirements:
|
30
35
|
- - ! '>='
|
@@ -32,7 +37,12 @@ dependencies:
|
|
32
37
|
version: '2.11'
|
33
38
|
type: :development
|
34
39
|
prerelease: false
|
35
|
-
version_requirements:
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '2.11'
|
36
46
|
description: Tree data structure for storing segments. It allows querying which of
|
37
47
|
the stored segments contain a given point.
|
38
48
|
email: amikhailov83@gmail.com
|
@@ -40,13 +50,7 @@ executables: []
|
|
40
50
|
extensions: []
|
41
51
|
extra_rdoc_files: []
|
42
52
|
files:
|
43
|
-
- .gitignore
|
44
53
|
- .rspec
|
45
|
-
- Gemfile
|
46
|
-
- LICENSE
|
47
|
-
- README.md
|
48
|
-
- Rakefile
|
49
|
-
- benchmark/benchmark.rb
|
50
54
|
- lib/segment_tree.rb
|
51
55
|
- lib/segment_tree/version.rb
|
52
56
|
- segment_tree.gemspec
|
@@ -72,11 +76,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
72
76
|
version: '0'
|
73
77
|
requirements: []
|
74
78
|
rubyforge_project:
|
75
|
-
rubygems_version: 1.8.
|
79
|
+
rubygems_version: 1.8.24
|
76
80
|
signing_key:
|
77
81
|
specification_version: 3
|
78
82
|
summary: Tree data structure for storing segments. It allows querying which of the
|
79
83
|
stored segments contain a given point.
|
80
84
|
test_files:
|
85
|
+
- .rspec
|
86
|
+
- segment_tree.gemspec
|
81
87
|
- spec/segment_tree_spec.rb
|
82
88
|
- spec/spec_helper.rb
|
data/.gitignore
DELETED
data/Gemfile
DELETED
data/LICENSE
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
Copyright (c) 2012 Alexei Mikhailov
|
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
DELETED
@@ -1,94 +0,0 @@
|
|
1
|
-
# SegmentTree
|
2
|
-
|
3
|
-
Ruby implementation of [segment tree](http://en.wikipedia.org/wiki/Segment_tree) data structure.
|
4
|
-
Segment tree is a tree data structure for storing intervals, or segments. It allows querying which of the stored segments contain a given point. It is, in principle, a static structure; that is, its content cannot be modified once the structure is built.
|
5
|
-
|
6
|
-
Segment tree storage has the complexity of <tt>O(n log n)</tt>.
|
7
|
-
Segment tree querying has the complexity of <tt>O(log n + k)</tt> where <tt>k</tt> is the number of reported intervals.
|
8
|
-
|
9
|
-
It's pretty fast on querying trees with ~ 10 millions segments, though building of such big tree will take long.
|
10
|
-
|
11
|
-
## Installation
|
12
|
-
|
13
|
-
Add this line to your application's Gemfile:
|
14
|
-
|
15
|
-
gem 'segment_tree'
|
16
|
-
|
17
|
-
And then execute:
|
18
|
-
|
19
|
-
$ bundle
|
20
|
-
|
21
|
-
Or install it yourself as:
|
22
|
-
|
23
|
-
$ gem install segment_tree
|
24
|
-
|
25
|
-
## Usage
|
26
|
-
|
27
|
-
Segment tree consists of segments (in Ruby it's <tt>Range</tt> objects) and corresponding values. The easiest way to build a segment tree is to create it from hash where segments are keys:
|
28
|
-
```ruby
|
29
|
-
tree = SegmentTree.new(1..10 => "a", 11..20 => "b", 21..30 => "c") # => #<SegmentTree:0xa47eadc @root=#<SegmentTree::Container:0x523f3b6 @range=1..30>>
|
30
|
-
```
|
31
|
-
|
32
|
-
After that you can query the tree of which segments contain a given point:
|
33
|
-
```ruby
|
34
|
-
tree.find(5) # => [#<SegmentTree::Segment:0xa47ea8c @range=1..10, @value="a">]
|
35
|
-
```
|
36
|
-
|
37
|
-
Or fetch only one segment:
|
38
|
-
```ruby
|
39
|
-
segment = tree.find_first(5) # => #<SegmentTree::Segment:0xa47ea8c @range=1..10, @value="a">
|
40
|
-
segment.value # => "a"
|
41
|
-
```
|
42
|
-
|
43
|
-
## Real world example
|
44
|
-
|
45
|
-
Segment tree can be used in applications where IP-address geocoding is needed.
|
46
|
-
|
47
|
-
```ruby
|
48
|
-
data = [
|
49
|
-
[IPAddr.new('87.224.241.0/24').to_range, {:city => "YEKT"}],
|
50
|
-
[IPAddr.new('195.58.18.0/24').to_range, {:city => "MSK"}]
|
51
|
-
# and so on
|
52
|
-
]
|
53
|
-
ip_tree = SegmentTree.new(data)
|
54
|
-
|
55
|
-
client_ip = IPAddr.new("87.224.241.66")
|
56
|
-
ip_tree.find_first(client_ip).value # => {:city=>"YEKT"}
|
57
|
-
```
|
58
|
-
|
59
|
-
## Contributing
|
60
|
-
|
61
|
-
1. Fork it
|
62
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
63
|
-
3. Commit your changes (`git commit -am 'Added some feature'`)
|
64
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
65
|
-
5. Create new Pull Request
|
66
|
-
|
67
|
-
## TODO
|
68
|
-
1. Fix README typos and grammatical errors (english speaking contributors are welcomed)
|
69
|
-
2. Implement C binding for MRI.
|
70
|
-
3. Test on different versions of Ruby.
|
71
|
-
|
72
|
-
## LICENSE
|
73
|
-
Copyright (c) 2012 Alexei Mikhailov
|
74
|
-
|
75
|
-
MIT License
|
76
|
-
|
77
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
78
|
-
a copy of this software and associated documentation files (the
|
79
|
-
"Software"), to deal in the Software without restriction, including
|
80
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
81
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
82
|
-
permit persons to whom the Software is furnished to do so, subject to
|
83
|
-
the following conditions:
|
84
|
-
|
85
|
-
The above copyright notice and this permission notice shall be
|
86
|
-
included in all copies or substantial portions of the Software.
|
87
|
-
|
88
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
89
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
90
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
91
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
92
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
93
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
94
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
DELETED
data/benchmark/benchmark.rb
DELETED
@@ -1,47 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
require "bundler/setup"
|
3
|
-
require "benchmark"
|
4
|
-
require "segment_tree"
|
5
|
-
|
6
|
-
# generate a tree with +n+ number of intervals
|
7
|
-
def tree(n)
|
8
|
-
SegmentTree.new list(n)
|
9
|
-
end
|
10
|
-
def list(n)
|
11
|
-
(0..n).map { |num| [(num * 10)..(num + 1) * 10 - 1, num] }
|
12
|
-
end
|
13
|
-
|
14
|
-
puts "Pregenerating data..."
|
15
|
-
tests = [100, 1000, 10_000, 100_000, 1_000_000]
|
16
|
-
|
17
|
-
lists = Hash[tests.map { |n| [n, list(n)] }]
|
18
|
-
trees = Hash[tests.map { |n| [n, tree(n)] }]
|
19
|
-
|
20
|
-
puts "Done"
|
21
|
-
puts
|
22
|
-
|
23
|
-
puts "Building a tree of N intervals"
|
24
|
-
Benchmark.bmbm do |x|
|
25
|
-
tests.each do |n|
|
26
|
-
x.report(n.to_s) { tree(n) }
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
puts "Finding matching interval in tree of N intervals"
|
31
|
-
Benchmark.bmbm do |x|
|
32
|
-
tests.each do |n|
|
33
|
-
t = trees[n]
|
34
|
-
|
35
|
-
x.report(n.to_s) { t.find_first(rand(n)) }
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
puts
|
40
|
-
puts "Finding matching interval in list of N intervals"
|
41
|
-
Benchmark.bmbm do |x|
|
42
|
-
tests.each do |n|
|
43
|
-
data = lists[n]
|
44
|
-
|
45
|
-
x.report(n.to_s) { data.find { |range, _| range.cover?(rand(n)) } }
|
46
|
-
end
|
47
|
-
end
|