tree_map 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cc2155ac2712f6af691fb6a799cbf4a245eb149bb6933d06b922782bd0978078
4
+ data.tar.gz: ce6d0d0cf6080b53244c4c9355f0f9d882151c71e09216b19607df9eba437ca7
5
+ SHA512:
6
+ metadata.gz: 781ce8371a4fddc23e53adf01ed5f035bae9705bfd1350b5270b28b9a74a27f61e8df8ba6478c7dc7a6207161e6953858765ae4cbedd88461b080572e5233a3c
7
+ data.tar.gz: 23d35b716b558ec8eca3810dfe6d15f905ed396ba23f2ae76072610f02b5f01a532471dfc21d5021e6f8371c718a55df3bb8a57621210c271961d7ab72f20f7f
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ tree_map-*.gem
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.3
7
+ before_install: gem install bundler -v 2.0.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at tianlu1677@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in tree_map.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,22 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ tree_map (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ minitest (5.15.0)
10
+ rake (10.5.0)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ bundler (~> 2.0)
17
+ minitest (~> 5.0)
18
+ rake (~> 10.0)
19
+ tree_map!
20
+
21
+ BUNDLED WITH
22
+ 2.0.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 tianl677
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # tree_map
2
+
3
+
4
+ This is an AVL tree based implementation of Java's java.util.TreeMap structure.
5
+
6
+ It implements Java's java.util.NavigableMap interface.
7
+
8
+ learn a lot from [treemap](https://github.com/davidkellis/treemap/)
9
+
10
+ ### References:
11
+ - Reference java implementation: https://android.googlesource.com/platform/libcore.git/+/android-6.0.1_r32/luni/src/main/java/java/util/TreeMap.java
12
+
13
+
14
+ ### Install
15
+ ```
16
+ gem install tree_map
17
+ ```
18
+
19
+
20
+ ### Usage
21
+ In IRB (some lines elided):
22
+ ```
23
+ irb(main):001:0> require 'treemap'
24
+ => true
25
+ irb(main):002:0> m = TreeMap.new; nil
26
+ => nil
27
+ irb(main):003:0> m.put(1, "foo")
28
+ => nil
29
+ irb(main):004:0> m.put(100, "baz")
30
+ => nil
31
+ irb(main):005:0> m.put(10, "bar")
32
+ => nil
33
+ irb(main):006:0> m.each {|k, v| puts "#{k} -> #{v}" }
34
+ 1 -> foo
35
+ 10 -> bar
36
+ 100 -> baz
37
+ => nil
38
+ irb(main):007:0> m.to_a
39
+ => [[1, "foo"], [10, "bar"], [100, "baz"]]
40
+ irb(main):008:0> m.keys
41
+ => #<Set: {1, 10, 100}>
42
+ irb(main):009:0> m.values
43
+ => ["foo", "bar", "baz"]
44
+ irb(main):010:0> m.first_key
45
+ => 1
46
+ irb(main):011:0> m.last_key
47
+ => 100
48
+ irb(main):012:0> m.lower_key(10)
49
+ => 1
50
+ irb(main):013:0> m.floor_key(10)
51
+ => 10
52
+ irb(main):014:0> m.ceiling_key(10)
53
+ => 10
54
+ irb(main):015:0> m.higher_key(10)
55
+ => 100
56
+ ```
57
+
58
+
59
+ ### Run Tests:
60
+ ```
61
+ rake
62
+ ```
63
+ OR
64
+ ```
65
+ rake test
66
+ ```
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "tree_map"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,380 @@
1
+ class TreeMap
2
+ module Bound
3
+ INCLUSIVE = 1
4
+ EXCLUSIVE = 2
5
+ NO_BOUND = 3
6
+ end
7
+
8
+ # A map with optional limits on its range.
9
+ # This is intended to be used only by the TreeMap class.
10
+ class BoundedMap
11
+ include Enumerable
12
+
13
+ attr_accessor :treemap, :ascending, :from, :from_bound, :to, :to_bound
14
+
15
+ def initialize(treemap, ascending, from, from_bound, to, to_bound)
16
+ @treemap = treemap
17
+ @ascending = ascending
18
+ @from = from
19
+ @from_bound = from_bound
20
+ @to = to
21
+ @to_bound = to_bound
22
+
23
+ # Validate the bounds. In addition to checking that from <= to, we verify that the comparator supports our bound objects.
24
+ if from_bound != Bound::NO_BOUND && to_bound != Bound::NO_BOUND
25
+ raise "Invalid from and to arguments: #{from} (from) > #{to} (to)" if comparator.call(from, to) > 0
26
+ elsif from_bound != Bound::NO_BOUND
27
+ comparator.call(from, from)
28
+ elsif to_bound != Bound::NO_BOUND
29
+ comparator.call(to, to)
30
+ end
31
+ end
32
+
33
+ def empty?
34
+ endpoint(true).nil?
35
+ end
36
+
37
+ def get(key)
38
+ @treemap.get(key) if in_bounds?(key)
39
+ end
40
+
41
+ def contains_key?(key)
42
+ in_bounds?(key) && @treemap.contains_key?(key)
43
+ end
44
+
45
+ def put(key, value)
46
+ raise "Key out of bounds." unless in_bounds?(key)
47
+ put_internal(key, value)
48
+ end
49
+
50
+ def remove(key)
51
+ @treemap.remove(key) if in_bounds?(key)
52
+ end
53
+
54
+ # Returns true if the key is in bounds.
55
+ # Note: The reference implementation calls this function isInBounds
56
+ def in_bounds?(key)
57
+ in_closed_bounds?(key, @from_bound, @to_bound)
58
+ end
59
+
60
+ # Returns true if the key is in bounds. Use this overload with
61
+ # NO_BOUND to skip bounds checking on either end.
62
+ # Note: The reference implementation calls this function isInBounds
63
+ def in_closed_bounds?(key, from_bound, to_bound)
64
+ if from_bound == Bound::INCLUSIVE
65
+ return false if comparator.call(key, from) < 0 # less than from
66
+ elsif from_bound == Bound::EXCLUSIVE
67
+ return false if comparator.call(key, from) <= 0 # less than or equal to from
68
+ end
69
+ if to_bound == Bound::INCLUSIVE
70
+ return false if comparator.call(key, to) > 0 # greater than 'to'
71
+ elsif to_bound == Bound::EXCLUSIVE
72
+ return false if comparator.call(key, to) >= 0 # greater than or equal to 'to'
73
+ end
74
+ true
75
+ end
76
+
77
+ # Returns the entry if it is in bounds, or null if it is out of bounds.
78
+ def bound(node, from_bound, to_bound)
79
+ node if node && in_closed_bounds?(node.key, from_bound, to_bound)
80
+ end
81
+
82
+ # Navigable methods
83
+
84
+ def first_entry
85
+ endpoint(true)
86
+ end
87
+
88
+ def poll_first_entry
89
+ result = endpoint(true)
90
+ @treemap.remove_internal(result) if result
91
+ result
92
+ end
93
+
94
+ def first_key
95
+ entry = endpoint(true)
96
+ raise "No such element" unless entry
97
+ entry.key
98
+ end
99
+
100
+ def last_entry
101
+ endpoint(false)
102
+ end
103
+
104
+ def poll_last_entry
105
+ result = endpoint(false)
106
+ @treemap.remove_internal(result) if result
107
+ result
108
+ end
109
+
110
+ def last_key
111
+ entry = endpoint(false)
112
+ raise "No such element" unless entry
113
+ entry.key
114
+ end
115
+
116
+ # <first> - true for the first element, false for the last
117
+ def endpoint(first)
118
+ node, from, to = if @ascending == first
119
+ node = case @from_bound
120
+ when Bound::NO_BOUND
121
+ @treemap.root.first if @treemap.root
122
+ when Bound::INCLUSIVE
123
+ @treemap.find(@from, Relation::CEILING)
124
+ when Bound::EXCLUSIVE
125
+ @treemap.find(@from, Relation::HIGHER)
126
+ else
127
+ raise "Undefined bound."
128
+ end
129
+ [node, Bound::NO_BOUND, @to_bound]
130
+ else
131
+ node = case @to_bound
132
+ when Bound::NO_BOUND
133
+ @treemap.root.last if @treemap.root
134
+ when Bound::INCLUSIVE
135
+ @treemap.find(@to, Relation::FLOOR)
136
+ when Bound::EXCLUSIVE
137
+ @treemap.find(@to, Relation::LOWER)
138
+ else
139
+ raise "Undefined bound."
140
+ end
141
+ [node, @from_bound, Bound::NO_BOUND]
142
+ end
143
+ bound(node, from, to)
144
+ end
145
+
146
+ # Performs a find on the underlying tree after constraining it to the
147
+ # bounds of this view. Examples:
148
+ #
149
+ # bound is (A..C)
150
+ # find_bounded(B, FLOOR) stays source.find(B, FLOOR)
151
+ #
152
+ # bound is (A..C)
153
+ # find_bounded(C, FLOOR) becomes source.find(C, LOWER)
154
+ #
155
+ # bound is (A..C)
156
+ # find_bounded(D, LOWER) becomes source.find(C, LOWER)
157
+ #
158
+ # bound is (A..C]
159
+ # find_bounded(D, FLOOR) becomes source.find(C, FLOOR)
160
+ #
161
+ # bound is (A..C]
162
+ # find_bounded(D, LOWER) becomes source.find(C, FLOOR)
163
+ def find_bounded(key, relation)
164
+ relation = Relation.for_order(relation, @ascending)
165
+ from_bound_for_check = @from_bound
166
+ to_bound_for_check = @to_bound
167
+ if @to_bound != Bound::NO_BOUND && (relation == Relation::LOWER || relation == Relation::FLOOR)
168
+ comparison = comparator.call(@to, key)
169
+ if comparison <= 0
170
+ key = @to
171
+ if @to_bound == Bound::EXCLUSIVE
172
+ relation = Relation::LOWER # 'to' is too high
173
+ elsif comparison < 0
174
+ relation = Relation::FLOOR # we already went lower
175
+ end
176
+ end
177
+ to_bound_for_check = Bound::NO_BOUND # we've already checked the upper bound
178
+ end
179
+ if @from_bound != Bound::NO_BOUND && (relation == Relation::CEILING || relation == Relation::HIGHER)
180
+ comparison = comparator.call(@from, key)
181
+ if comparison >= 0
182
+ key = @from
183
+ if @from_bound == Bound::EXCLUSIVE
184
+ relation = Relation::HIGHER # 'from' is too low
185
+ elsif comparison > 0
186
+ relation = Relation::CEILING # we already went higher
187
+ end
188
+ end
189
+ from_bound_for_check = Bound::NO_BOUND # we've already checked the lower bound
190
+ end
191
+ bound(@treemap.find(key, relation), from_bound_for_check, to_bound_for_check)
192
+ end
193
+
194
+ def lower_entry(key)
195
+ find_bounded(key, Relation::LOWER)
196
+ end
197
+
198
+ def lower_key(key)
199
+ entry = find_bounded(key, Relation::LOWER)
200
+ entry.key if entry
201
+ end
202
+
203
+ def floor_entry(key)
204
+ find_bounded(key, Relation::FLOOR)
205
+ end
206
+
207
+ def floor_key(key)
208
+ entry = find_bounded(key, Relation::FLOOR)
209
+ entry.key if entry
210
+ end
211
+
212
+ def ceiling_entry(key)
213
+ find_bounded(key, Relation::CEILING)
214
+ end
215
+
216
+ def ceiling_key(key)
217
+ entry = find_bounded(key, Relation::CEILING)
218
+ entry.key if entry
219
+ end
220
+
221
+ def higher_entry(key)
222
+ find_bounded(key, Relation::HIGHER)
223
+ end
224
+
225
+ def higher_key(key)
226
+ entry = find_bounded(key, Relation::HIGHER)
227
+ entry.key if entry
228
+ end
229
+
230
+ def comparator
231
+ if @ascending
232
+ @treemap.comparator
233
+ else
234
+ ->(this, that) { -@treemap.comparator.call(this, that) }
235
+ end
236
+ end
237
+
238
+ # View factory methods
239
+
240
+ def entry_set
241
+ Set.new(each_node.to_a)
242
+ end
243
+
244
+ def key_set
245
+ Set.new(each_node.map(&:key))
246
+ end
247
+
248
+ alias keys key_set
249
+
250
+ def values
251
+ each_node.map(&:value)
252
+ end
253
+
254
+ def descending_map
255
+ BoundedMap.new(@treemap, !@ascending, @from, @from_bound, @to, @to_bound)
256
+ end
257
+
258
+ # This can be called in 1 of 2 ways:
259
+ # sub_map(from_inclusive, to_exclusive)
260
+ # OR
261
+ # sub_map(from, from_inclusive, to, to_inclusive)
262
+ def sub_map(*args)
263
+ case args.count
264
+ when 2
265
+ from_inclusive, to_exclusive = *args
266
+ bounded_sub_map(from_inclusive, Bound::INCLUSIVE, to_exclusive, Bound::EXCLUSIVE)
267
+ when 4
268
+ from, from_inclusive, to, to_inclusive = *args
269
+ from_bound = from_inclusive ? Bound::INCLUSIVE : Bound::EXCLUSIVE
270
+ to_bound = to_inclusive ? Bound::INCLUSIVE : Bound::EXCLUSIVE
271
+ bounded_sub_map(from, from_bound, to, to_bound)
272
+ end
273
+ end
274
+
275
+ def bounded_sub_map(from, from_bound, to, to_bound)
276
+ if !@ascending
277
+ from, to = to, from
278
+ from_bound, to_bound = to_bound, from_bound
279
+ end
280
+
281
+ # If both the current and requested bounds are exclusive, the isInBounds check must be
282
+ # inclusive. For example, to create (C..F) from (A..F), the bound 'F' is in bounds.
283
+ if from_bound == Bound::NO_BOUND
284
+ from = @from
285
+ from_bound = @from_bound
286
+ else
287
+ from_bound_to_check = from_bound == @from_bound ? Bound::INCLUSIVE : @from_bound
288
+ raise out_of_bounds(to, from_bound_to_check, @to_bound) if !in_closed_bounds?(from, from_bound_to_check, @to_bound)
289
+ end
290
+ if to_bound == Bound::NO_BOUND
291
+ to = @to
292
+ to_bound = @to_bound
293
+ else
294
+ to_bound_to_check = to_bound == @to_bound ? Bound::INCLUSIVE : @to_bound
295
+ raise out_of_bounds(to, @from_bound, to_bound_to_check) if !in_closed_bounds?(to, @from_bound, to_bound_to_check)
296
+ end
297
+ BoundedMap.new(@treemap, ascending, from, from_bound, to, to_bound)
298
+ end
299
+
300
+ # This can be called in 1 of 2 ways:
301
+ # head_map(to_exclusive)
302
+ # OR
303
+ # head_map(to, inclusive)
304
+ def head_map(*args)
305
+ case args.count
306
+ when 1
307
+ to_exclusive = args.first
308
+ bounded_sub_map(nil, Bound::NO_BOUND, to_exclusive, Bound::EXCLUSIVE)
309
+ when 2
310
+ to, inclusive = *args
311
+ to_bound = inclusive ? Bound::INCLUSIVE : Bound::EXCLUSIVE
312
+ bounded_sub_map(nil, Bound::NO_BOUND, to, to_bound)
313
+ end
314
+ end
315
+
316
+ # This can be called in 1 of 2 ways:
317
+ # tail_map(from_inclusive)
318
+ # OR
319
+ # tail_map(from, inclusive)
320
+ def tail_map(*args)
321
+ case args.count
322
+ when 1
323
+ from_inclusive = args.first
324
+ bounded_sub_map(from_inclusive, Bound::INCLUSIVE, nil, Bound::NO_BOUND)
325
+ when 2
326
+ from, inclusive = *args
327
+ from_bound = inclusive ? Bound::INCLUSIVE : Bound::EXCLUSIVE
328
+ bounded_sub_map(from, from_bound, nil, Bound::NO_BOUND)
329
+ end
330
+ end
331
+
332
+ def out_of_bounds(value, from_bound, to_bound)
333
+ Exception.new("#{value} not in range #{from_bound.left_cap(@from)}..#{to_bound.right_cap(@to)}")
334
+ end
335
+
336
+ # Bounded view implementations
337
+
338
+ # in-order traversal of nodes in tree
339
+ class BoundedNodeIterator < ::TreeMap::NodeIterator
340
+ def initialize(bounded_map, next_node)
341
+ super(next_node)
342
+ @bounded_map = bounded_map
343
+ end
344
+
345
+ def step_forward
346
+ result = super
347
+ @next_node = nil if @next_node && !@bounded_map.in_closed_bounds?(@next_node.key, Bound::NO_BOUND, @bounded_map.to_bound)
348
+ result
349
+ end
350
+
351
+ def step_backward
352
+ result = super
353
+ @next_node = nil if @next_node && !@bounded_map.in_closed_bounds?(@next_node.key, @bounded_map.from_bound, Bound::NO_BOUND)
354
+ result
355
+ end
356
+ end
357
+
358
+ # each {|k,v| puts "#{k}->#{v}"}
359
+ def each(&blk)
360
+ if block_given?
361
+ each_node {|node| blk.call(node.key, node.value) }
362
+ else
363
+ enum_for(:each)
364
+ end
365
+ end
366
+
367
+ # each_node {|node| puts "#{node.key}->#{node.value}"}
368
+ def each_node
369
+ if block_given?
370
+ iter = BoundedNodeIterator.new(self, endpoint(true))
371
+ while iter.has_next?
372
+ yield iter.step_forward()
373
+ end
374
+ else
375
+ enum_for(:each_node)
376
+ end
377
+ end
378
+
379
+ end
380
+ end