segment_tree 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  class SegmentTree
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/segment_tree.rb CHANGED
@@ -1,23 +1,45 @@
1
- require "forwardable"
2
-
1
+ # == Synopsys
2
+ # Segment tree is a tree data structure for storing intervals, or segments.
3
+ # It allows querying which of the stored segments contain a given point.
4
+ # It is, in principle, a static structure; that is, its content cannot be modified once the structure is built.
5
+ #
6
+ # == Example
7
+ # data = [
8
+ # [IPAddr.new('87.224.241.0/24').to_range, {:city => "YEKT"}],
9
+ # [IPAddr.new('195.58.18.0/24').to_range, {:city => "MSK"}]
10
+ # # and so on
11
+ # ]
12
+ # ip_tree = SegmentTree.new(data)
13
+ #
14
+ # client_ip = IPAddr.new("87.224.241.66")
15
+ # ip_tree.find_first(client_ip).value # => {:city=>"YEKT"}
3
16
  class SegmentTree
4
17
  # An abstract tree node
5
18
  class Node #:nodoc:all:
6
- extend Forwardable
7
- def_delegators :@range, :include?, :begin, :end
19
+ attr_reader :begin, :end
20
+
21
+ def initialize(*)
22
+ @begin, @end = @range.begin, @range.end
23
+ end
24
+ protected :initialize
25
+
26
+ def include?(x)
27
+ @range.include?(x)
28
+ end
8
29
  end
9
30
 
10
31
  # An elementary intervals or nodes container
11
32
  class Container < Node #:nodoc:all:
12
33
  extend Forwardable
13
34
 
14
- attr_reader :left, :right
35
+ #attr_reader :left, :right
15
36
 
16
37
  # Node constructor, accepts both +Node+ and +Segment+
17
38
  def initialize(left, right)
18
39
  @left, @right = left, right
19
-
20
40
  @range = left.begin..(right || left).end
41
+
42
+ super
21
43
  end
22
44
 
23
45
  # Find all intervals containing point +x+ within node's children. Returns array
@@ -48,6 +70,7 @@ class SegmentTree
48
70
  raise ArgumentError, 'Range expected, %s given' % range.class.name unless range.is_a?(Range)
49
71
 
50
72
  @range, @value = range, value
73
+ super
51
74
  end
52
75
 
53
76
  def find(x)
@@ -55,7 +78,7 @@ class SegmentTree
55
78
  end
56
79
 
57
80
  def find_first(x)
58
- include?(x) ? self : nil
81
+ @range.include?(x) ? self : nil
59
82
  end
60
83
  end
61
84
 
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.2
4
+ version: 0.0.3
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-02 00:00:00.000000000 Z
12
+ date: 2012-08-04 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
16
- requirement: !ruby/object:Gem::Requirement
16
+ requirement: &82956300 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,15 +21,10 @@ dependencies:
21
21
  version: '1.0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: '1.0'
24
+ version_requirements: *82956300
30
25
  - !ruby/object:Gem::Dependency
31
26
  name: rspec
32
- requirement: !ruby/object:Gem::Requirement
27
+ requirement: &82956050 !ruby/object:Gem::Requirement
33
28
  none: false
34
29
  requirements:
35
30
  - - ! '>='
@@ -37,12 +32,7 @@ dependencies:
37
32
  version: '2.11'
38
33
  type: :development
39
34
  prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ! '>='
44
- - !ruby/object:Gem::Version
45
- version: '2.11'
35
+ version_requirements: *82956050
46
36
  description: Tree data structure for storing segments. It allows querying which of
47
37
  the stored segments contain a given point.
48
38
  email: amikhailov83@gmail.com
@@ -76,7 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
66
  version: '0'
77
67
  requirements: []
78
68
  rubyforge_project:
79
- rubygems_version: 1.8.24
69
+ rubygems_version: 1.8.17
80
70
  signing_key:
81
71
  specification_version: 3
82
72
  summary: Tree data structure for storing segments. It allows querying which of the