segment_tree 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 67bd1fc5631d255d64a5895c685058f49dabe485
4
- data.tar.gz: 4fc670352ba477a0e9241aed8af12963aa64b85d
2
+ SHA256:
3
+ metadata.gz: 59ea55a8bf39f04d9d5d8fc248c8d8eaa5cb2c65692fd25c22ccc3b5a5ac14d0
4
+ data.tar.gz: a9b683819704f99d933a04bbf89272dcb00e87908a2a876aebd798268ee1c35d
5
5
  SHA512:
6
- metadata.gz: 64564b83ddaac3ca1c860f99510793c239dd931ad25deae733dab3b79c9db265aa8be8d8ad717433a718a5cf393b586ad8b290246356836cc790a2372a3114d7
7
- data.tar.gz: 414d0aa78ee04da42ecbe555e19f756c1293c2b1055b866e411c6e735fc327f6d685599b9bef1ad2bba16f7ef8336fdbb3ce64aebcbdc73efa0f464f1b05c178
6
+ metadata.gz: 5bf8d0dbbad097d1e9f2ea887eaa71f02f253c27ee2d27bed792781ebc858a9e7e116cb42176939a10bc30a670ce820ad2eeb26be67ad40b752ce31f2d1eb455
7
+ data.tar.gz: 1cd80e31aaf356a599dea612fb6b0e21bb6551b02205a02eb982eb47a9ce6ea6d61336c17060c8b0444bc3764f8c6eb0a8fc0d0fa272c7ab90ce530605cbf1f1
@@ -1,3 +1,3 @@
1
1
  class SegmentTree
2
- VERSION = '0.1.1'
3
- end
2
+ VERSION = '0.2.0'
3
+ end
data/lib/segment_tree.rb CHANGED
@@ -31,8 +31,38 @@ class SegmentTree
31
31
  else cmp
32
32
  end
33
33
  end
34
+
35
+ def ==(other)
36
+ other.is_a?(self.class) &&
37
+ @range == other.range &&
38
+ @value == other.value
39
+ end
40
+
41
+ def eql?(other)
42
+ other.is_a?(self.class) &&
43
+ @range.eql?(other.range) &&
44
+ @value.eql?(other.value)
45
+ end
46
+
47
+ def hash
48
+ [@range, @value].hash
49
+ end
50
+
51
+ def marshal_dump
52
+ {
53
+ range: @range,
54
+ value: @value,
55
+ }
56
+ end
57
+
58
+ def marshal_load(serialized_tree)
59
+ @range = serialized_tree[:range]
60
+ @value = serialized_tree[:value]
61
+ end
34
62
  end
35
63
 
64
+ attr_reader :segments
65
+
36
66
  # Build a segment tree from +data+.
37
67
  #
38
68
  # Data can be one of the following:
@@ -83,6 +113,28 @@ class SegmentTree
83
113
  end
84
114
  end
85
115
 
116
+ def ==(other)
117
+ other.is_a?(self.class) && @segments == other.segments
118
+ end
119
+
120
+ def eql?(other)
121
+ other.is_a?(self.class) && @segments.eql?(other.segments)
122
+ end
123
+
124
+ def hash
125
+ @segments.hash
126
+ end
127
+
128
+ def marshal_dump
129
+ {
130
+ segments: @segments,
131
+ }
132
+ end
133
+
134
+ def marshal_load(serialized_tree)
135
+ @segments = serialized_tree[:segments]
136
+ end
137
+
86
138
  private
87
139
  def matches?(x, low_idx, high_idx, idx) #:nodoc:
88
140
  low, high = @segments[low_idx], @segments[high_idx]
@@ -131,4 +131,82 @@ describe SegmentTree do
131
131
  it { is_expected.to query(8).and_return(:nothing) }
132
132
  end
133
133
  end
134
- end
134
+
135
+ describe '#==' do
136
+ subject { SegmentTree.new(sample_overlapping) }
137
+
138
+ it { is_expected.to eq(SegmentTree.new(sample_overlapping)) }
139
+ it { is_expected.not_to eq(SegmentTree.new(sample_overlapping2)) }
140
+
141
+ it 'is equal when a range coerces' do
142
+ expect(SegmentTree.new((1..2) => "a")).to eq(SegmentTree.new(((1.0)..(2.0)) => "a"))
143
+ end
144
+
145
+ it 'is equal when a value coerces' do
146
+ expect(SegmentTree.new((1..2) => 1)).to eq(SegmentTree.new((1..2) => 1.0))
147
+ end
148
+
149
+ it "isn't equal when only a range is different" do
150
+ expect(SegmentTree.new((1..2) => "a")).not_to eq(SegmentTree.new((1..3) => "a"))
151
+ end
152
+
153
+ it "isn't equal when only a value is different" do
154
+ expect(SegmentTree.new((1..2) => "a")).not_to eq(SegmentTree.new((1..2) => "b"))
155
+ end
156
+ end
157
+
158
+ describe '#eql?' do
159
+ subject { SegmentTree.new(sample_overlapping) }
160
+
161
+ it { is_expected.to be_eql(SegmentTree.new(sample_overlapping)) }
162
+ it { is_expected.not_to be_eql(SegmentTree.new(sample_overlapping2)) }
163
+
164
+ it "isn't equal when a range coerces" do
165
+ expect(SegmentTree.new((1..2) => "a")).not_to be_eql(SegmentTree.new(((1.0)..(2.0)) => "a"))
166
+ end
167
+
168
+ it "isn't equal when a value coerces" do
169
+ expect(SegmentTree.new((1..2) => 1)).not_to be_eql(SegmentTree.new((1..2) => 1.0))
170
+ end
171
+
172
+ it "isn't equal when only a range is different" do
173
+ expect(SegmentTree.new((1..2) => "a")).not_to be_eql(SegmentTree.new((1..3) => "a"))
174
+ end
175
+
176
+ it "isn't equal when only a value is different" do
177
+ expect(SegmentTree.new((1..2) => "a")).not_to be_eql(SegmentTree.new((1..2) => "b"))
178
+ end
179
+ end
180
+
181
+ describe '#hash' do
182
+ subject { SegmentTree.new(sample_overlapping).hash }
183
+
184
+ it { is_expected.to eq(SegmentTree.new(sample_overlapping).hash) }
185
+ it { is_expected.not_to eq(SegmentTree.new(sample_overlapping2).hash) }
186
+
187
+ it "isn't equal when only a range is different" do
188
+ expect(SegmentTree.new((1..2) => "a").hash).not_to eq(SegmentTree.new((1..3) => "a").hash)
189
+ end
190
+
191
+ it "isn't equal when only a value is different" do
192
+ expect(SegmentTree.new((1..2) => "a").hash).not_to eq(SegmentTree.new((1..2) => "b").hash)
193
+ end
194
+ end
195
+
196
+ describe 'marshaling' do
197
+ it 'dumps and loads successfully' do
198
+ aggregate_failures do
199
+ [
200
+ sample_spanned,
201
+ sample_sparsed,
202
+ sample_overlapping,
203
+ sample_overlapping2,
204
+ ].each do |sample|
205
+ tree = SegmentTree.new(sample)
206
+ dumped = Marshal.dump(tree)
207
+ expect(Marshal.load(dumped)).to eq(tree)
208
+ end
209
+ end
210
+ end
211
+ end
212
+ end
metadata CHANGED
@@ -1,55 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: segment_tree
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexei Mikhailov
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-25 00:00:00.000000000 Z
11
+ date: 2023-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: 3.1.0
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: 3.1.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  description: Tree data structure for storing segments. It allows querying which of
@@ -59,7 +59,7 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
- - .rspec
62
+ - ".rspec"
63
63
  - lib/segment_tree.rb
64
64
  - lib/segment_tree/version.rb
65
65
  - segment_tree.gemspec
@@ -68,29 +68,28 @@ files:
68
68
  homepage: https://github.com/take-five/segment_tree
69
69
  licenses: []
70
70
  metadata: {}
71
- post_install_message:
71
+ post_install_message:
72
72
  rdoc_options: []
73
73
  require_paths:
74
74
  - lib
75
75
  required_ruby_version: !ruby/object:Gem::Requirement
76
76
  requirements:
77
- - - '>='
77
+ - - ">="
78
78
  - !ruby/object:Gem::Version
79
79
  version: '0'
80
80
  required_rubygems_version: !ruby/object:Gem::Requirement
81
81
  requirements:
82
- - - '>='
82
+ - - ">="
83
83
  - !ruby/object:Gem::Version
84
84
  version: '0'
85
85
  requirements: []
86
- rubyforge_project:
87
- rubygems_version: 2.2.2
88
- signing_key:
86
+ rubygems_version: 3.3.7
87
+ signing_key:
89
88
  specification_version: 4
90
89
  summary: Tree data structure for storing segments. It allows querying which of the
91
90
  stored segments contain a given point.
92
91
  test_files:
93
- - .rspec
92
+ - ".rspec"
94
93
  - segment_tree.gemspec
95
94
  - spec/segment_tree_spec.rb
96
95
  - spec/spec_helper.rb