rbtree-pure 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.project ADDED
@@ -0,0 +1,17 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <projectDescription>
3
+ <name>rbtree-pure</name>
4
+ <comment></comment>
5
+ <projects>
6
+ </projects>
7
+ <buildSpec>
8
+ <buildCommand>
9
+ <name>com.aptana.ide.core.unifiedBuilder</name>
10
+ <arguments>
11
+ </arguments>
12
+ </buildCommand>
13
+ </buildSpec>
14
+ <natures>
15
+ <nature>com.aptana.ruby.core.rubynature</nature>
16
+ </natures>
17
+ </projectDescription>
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'http://rubygems.org'
2
+ # Add dependencies required to use your gem here.
3
+
4
+ # Add dependencies to develop your gem here.
5
+ # Include everything needed to run rake, tests, features, etc.
6
+ group :development do
7
+ gem 'bundler', '~> 1.0.0'
8
+ gem 'jeweler', '~> 1.6.2'
9
+ gem 'rcov', '>= 0', :platform => :mri
10
+ gem 'rdoc', '>= 0'
11
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.6.4)
6
+ bundler (~> 1.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rake (0.9.2)
10
+ rcov (0.9.9)
11
+ rdoc (3.8)
12
+
13
+ PLATFORMS
14
+ ruby
15
+
16
+ DEPENDENCIES
17
+ bundler (~> 1.0.0)
18
+ jeweler (~> 1.6.2)
19
+ rcov
20
+ rdoc
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Victor Costan
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = rbtree-pure
2
+
3
+ Pure-ruby implementation of the rbtree gem, providing red-black trees.
4
+
5
+ == Contributing to rbtree-pure
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Victor Costan. See LICENSE.txt for further details.
data/Rakefile ADDED
@@ -0,0 +1,57 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "rbtree-pure"
18
+ gem.homepage = "http://github.com/pwnall/rbtree-pure"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Pure-ruby implementation of red-black trees.}
21
+ gem.description = %Q{This is a pure-ruby implementation of the rbtree gem.}
22
+ gem.email = "victor@costan.us"
23
+ gem.authors = ["Victor Costan"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/*_test.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ begin
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
41
+ test.rcov_opts << '--exclude "gems/*"'
42
+ end
43
+ rescue LoadError
44
+ # No rcov outside MRI.
45
+ end
46
+
47
+ task :default => :test
48
+
49
+ require 'rdoc/task'
50
+ Rake::RDocTask.new do |rdoc|
51
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
52
+
53
+ rdoc.rdoc_dir = 'rdoc'
54
+ rdoc.title = "rbtree-pure #{version}"
55
+ rdoc.rdoc_files.include('README*')
56
+ rdoc.rdoc_files.include('lib/**/*.rb')
57
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/rbtree.rb ADDED
@@ -0,0 +1,10 @@
1
+ # :nodoc: namespace
2
+ class RBTree
3
+ end
4
+
5
+ require 'rbtree/node.rb'
6
+ require 'rbtree/guard_node.rb'
7
+ require 'rbtree/tree.rb'
8
+ require 'rbtree/tree_cmp.rb'
9
+ require 'rbtree/multi_rb_tree.rb'
10
+ require 'rbtree/rb_tree.rb'
@@ -0,0 +1,28 @@
1
+ # :nodoc: namespace
2
+ class RBTree
3
+
4
+ # Node instance used as a guard.
5
+ class GuardNode < Node
6
+ def initialize
7
+ @color = :black
8
+ @key = nil
9
+ @value = nil
10
+ @left = nil
11
+ @right = nil
12
+ @parent = nil
13
+ end
14
+
15
+ def nil?
16
+ true
17
+ end
18
+
19
+ def to_a
20
+ nil
21
+ end
22
+
23
+ def inspect
24
+ 'RBTree::GuardNode'
25
+ end
26
+ end
27
+
28
+ end # namespace RBTree
@@ -0,0 +1,317 @@
1
+ # Sorted hash that supports multiple keys for its values.
2
+ class MultiRBTree < RBTree
3
+ def initialize(default = nil, &default_proc)
4
+ super(default, &default_proc)
5
+ @size = 0
6
+ end
7
+
8
+ def lower_bound(key)
9
+ node = @tree.lower_bound(key)
10
+ [node.key, node.value.first]
11
+ end
12
+
13
+ def upper_bound(key)
14
+ node = @tree.lower_bound(key)
15
+ [node.key, node.value.last]
16
+ end
17
+
18
+ def bound(lower_key, upper_key = nil)
19
+ result = []
20
+ bound_nodes lower_key, upper_key do |node|
21
+ if block_given?
22
+ node.value.each { |value| yield node.key, value }
23
+ else
24
+ node.value.each { |value| result << [node.key, value] }
25
+ end
26
+ end
27
+ block_given? ? self : result
28
+ end
29
+
30
+ def to_rbtree
31
+ self
32
+ end
33
+
34
+ def replace(other)
35
+ raise TypeError, 'cannot modify rbtree in iteration' if @lock_count > 0
36
+ unless other.kind_of? RBTree
37
+ raise TypeError, "expected RBTree, got #{other.class}"
38
+ end
39
+
40
+ @tree = other.tree.dup
41
+ @default_proc = other.default_proc
42
+ @default = other.default
43
+ @cmp_proc = other.cmp_proc
44
+ @size = other.size
45
+
46
+ unless other.instance_of? MultiRBTree
47
+ # Wrap values in arrays to convert RBTree -> MultiRBTree.
48
+ @tree.inorder do |node|
49
+ node.value = [node.value]
50
+ end
51
+ end
52
+
53
+ self
54
+ end
55
+ end
56
+
57
+ # :nodoc: array behavior
58
+ class MultiRBTree
59
+ # The [key, value] for the smallest key in the tree.
60
+ def first
61
+ node = @tree.minimum
62
+ node.nil? ? default : [node.key, node.value.first]
63
+ end
64
+
65
+ # The [key, value] for the largest key in the tree.
66
+ def last
67
+ node = @tree.maximum
68
+ node.nil? ? default : [node.key, node.value.last]
69
+ end
70
+
71
+ # Removes the largest key in the tree.
72
+ def pop
73
+ return default if (node = @tree.maximum).nil?
74
+ value = node.value.pop
75
+ @tree.delete node if node.value.empty?
76
+ @size -= 1
77
+ [node.key, value]
78
+ end
79
+
80
+ # Removes the smallest key in the tree.
81
+ def shift
82
+ return default if (node = @tree.minimum).nil?
83
+ value = node.value.shift
84
+ @tree.delete node if node.value.empty?
85
+ @size -= 1
86
+ [node.key, value]
87
+ end
88
+ end
89
+
90
+ # :nodoc: hash behavior
91
+ class MultiRBTree
92
+ # See Hash#[]
93
+ def [](key)
94
+ node = tree.search key
95
+ node ? node.value.first : default(key)
96
+ end
97
+
98
+ # See Hash#[]=
99
+ def []=(key, value)
100
+ raise TypeError, 'cannot modify rbtree in iteration' if @lock_count > 0
101
+
102
+ key = key.clone.freeze if key.kind_of? String
103
+ @tree.insert(@tree.node(key, [])).value << value
104
+ @size += 1
105
+ value
106
+ end
107
+
108
+ # See Hash#size
109
+ attr_reader :size
110
+
111
+ # See Hash#empty
112
+ def empty?
113
+ @tree.empty?
114
+ end
115
+
116
+ # See Hash#clear
117
+ def clear
118
+ super
119
+ @size = 0
120
+ end
121
+
122
+ # See Hash#each
123
+ def each
124
+ if block_given?
125
+ lock_changes do
126
+ @tree.inorder do |node|
127
+ node.value.each { |value| yield node.key, value }
128
+ end
129
+ end
130
+ else
131
+ Enumerator.new self, :each
132
+ end
133
+ end
134
+ alias :each_pair :each
135
+
136
+ # See Hash#reverse_each
137
+ def reverse_each
138
+ if block_given?
139
+ lock_changes do
140
+ @tree.reverse_inorder do |node|
141
+ node.value.each { |value| yield node.key, value }
142
+ end
143
+ end
144
+ else
145
+ Enumerator.new self, :reverse_each
146
+ end
147
+ end
148
+
149
+ # See Hash#index
150
+ def index(value)
151
+ each { |k, v| return k if v.include? value }
152
+ nil
153
+ end
154
+
155
+ # See Hash#fetch
156
+ def fetch(key, *default)
157
+ if default.length > 1
158
+ raise ArgumentError, "expected at most 1 default, got #{default.length}"
159
+ end
160
+ if default.length == 1 && block_given?
161
+ $stderr << "warning: block supersedes default value argument"
162
+ end
163
+
164
+ node = tree.search key
165
+ return node.value.first if node
166
+ if block_given?
167
+ yield key
168
+ else
169
+ if default.length == 1
170
+ default.first
171
+ else
172
+ raise IndexError, 'key not found'
173
+ end
174
+ end
175
+ end
176
+
177
+ # See Hash#delete
178
+ def delete(key)
179
+ node = @tree.search key
180
+ unless node
181
+ return block_given? ? yield : nil
182
+ end
183
+ value = node.value.shift
184
+ @tree.delete node if node.value.empty?
185
+ @size -= 1
186
+ value
187
+ end
188
+
189
+ # See Hash#reject!
190
+ def reject!
191
+ if block_given?
192
+ dead_nodes = []
193
+ lock_changes do
194
+ @tree.inorder do |node|
195
+ node.value.reject! do |value|
196
+ @size -= 1 if result = yield(node.key, value)
197
+ result
198
+ end
199
+ dead_nodes << node if node.value.empty?
200
+ end
201
+ end
202
+ dead_nodes.each { |node| @tree.delete node }
203
+ dead_nodes.empty? ? nil : self
204
+ else
205
+ Enumerator.new self, :each
206
+ end
207
+ end
208
+
209
+ # See Hash#reject
210
+ def reject(&block)
211
+ copy = self.dup
212
+ copy.reject!(&block)
213
+ # NOTE: the correct answer should be "copy", but we're copying RBTree
214
+ # bug-for-bug
215
+ # copy
216
+ end
217
+
218
+ # See Hash#each_key.
219
+ def each_key
220
+ if block_given?
221
+ lock_changes do
222
+ @tree.inorder { |node| node.value.each { yield node.key } }
223
+ end
224
+ else
225
+ Enumerator.new self, :each_key
226
+ end
227
+ end
228
+
229
+
230
+ # See Hash#each_value.
231
+ def each_value
232
+ if block_given?
233
+ lock_changes do
234
+ @tree.inorder { |node| node.value.each { |value| yield value } }
235
+ end
236
+ else
237
+ Enumerator.new self, :each_value
238
+ end
239
+ end
240
+
241
+ # See Hash#merge!
242
+ def merge!(other)
243
+ unless other.instance_of? RBTree
244
+ raise TypeError, "wrong argument type #{other.class} (expected RBTree)"
245
+ end
246
+
247
+ if block_given?
248
+ other.each do |key, value|
249
+ if node = @tree.search(key)
250
+ self[key] = yield key, node.value.first, value
251
+ else
252
+ self[key] = value
253
+ end
254
+ end
255
+ else
256
+ other.each { |key, value| self[key] = value }
257
+ end
258
+ self
259
+ end
260
+ alias :update :merge!
261
+
262
+ # See Hash#merge
263
+ def merge(other)
264
+ copy = self.dup
265
+ copy.merge! other
266
+ copy
267
+ end
268
+
269
+ # A new Hash with the same contents and defaults as this RBTree instance.
270
+ def to_hash
271
+ raise TypeError, "can't convert MultiRBTree to Hash"
272
+ end
273
+
274
+ # :nodoc:
275
+ def inspect
276
+ contents = map { |k, v|
277
+ k_inspect = k.equal?(self) ? '#<RBTree: ...>' : k.inspect
278
+ v_inspect = v.equal?(self) ? '#<RBTree: ...>' : v.inspect
279
+ "#{k_inspect}=>#{v_inspect}"
280
+ }.join(', ')
281
+ default_inspect = default.equal?(self) ? '#<RBTree: ...>' : default.inspect
282
+ "#<MultiRBTree: {#{contents}}, default=#{default_inspect}, cmp_proc=#{@cmp_proc.inspect}>"
283
+ end
284
+
285
+ # :nodoc: custom pp output
286
+ def pretty_print(q)
287
+ q.group(1, "#<#{self.class.name}: ", '>') do
288
+ q.group(1, '{', '}') do
289
+ first = true
290
+ each do |key, value|
291
+ if first
292
+ first = false
293
+ else
294
+ q.text ','
295
+ q.breakable ' '
296
+ end
297
+ q.pp key
298
+ q.text '=>'
299
+ q.pp value
300
+ end
301
+ end
302
+ q.text ','
303
+ q.breakable ' '
304
+ q.text 'default='
305
+ q.pp default
306
+ q.text ','
307
+ q.breakable ' '
308
+ q.text 'cmp_proc='
309
+ q.pp cmp_proc
310
+ end
311
+ end
312
+
313
+ # :nodoc: custom pp output
314
+ def pretty_print_cycle(q)
315
+ q.text '"#<MultiRBTree: ...>"'
316
+ end
317
+ end # class RBTree::MultiRBTree