grosser-algorithms 0.4.0 → 0.4.1
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/Manifest +45 -0
- data/VERSION +1 -0
- data/ext/containers/bst/bst.c +249 -0
- data/ext/containers/bst/extconf.rb +4 -0
- data/grosser-algorithms.gemspec +4 -4
- data/rdoc/classes/Algorithms.html +221 -0
- data/rdoc/classes/Algorithms/Algorithms.html +115 -0
- data/rdoc/classes/Algorithms/Algorithms/Sort.html +569 -0
- data/rdoc/classes/Algorithms/Containers.html +162 -0
- data/rdoc/classes/Algorithms/Containers/Heap.html +690 -0
- data/rdoc/classes/Algorithms/Containers/KDTree.html +195 -0
- data/rdoc/classes/Algorithms/Containers/MaxHeap.html +238 -0
- data/rdoc/classes/Algorithms/Containers/MinHeap.html +238 -0
- data/rdoc/classes/Algorithms/Containers/PriorityQueue.html +456 -0
- data/rdoc/classes/Algorithms/Containers/Queue.html +363 -0
- data/rdoc/classes/Algorithms/Containers/RubyDeque.html +617 -0
- data/rdoc/classes/Algorithms/Containers/RubyRBTreeMap.html +662 -0
- data/rdoc/classes/Algorithms/Containers/RubySplayTreeMap.html +623 -0
- data/rdoc/classes/Algorithms/Containers/Stack.html +363 -0
- data/rdoc/classes/Algorithms/Containers/SuffixArray.html +246 -0
- data/rdoc/classes/Algorithms/Containers/Trie.html +555 -0
- data/rdoc/classes/Algorithms/Search.html +273 -0
- data/rdoc/created.rid +1 -0
- data/rdoc/files/History_txt.html +281 -0
- data/rdoc/files/README_markdown.html +248 -0
- data/rdoc/files/lib/algorithms/search_rb.html +108 -0
- data/rdoc/files/lib/algorithms/sort_rb.html +108 -0
- data/rdoc/files/lib/algorithms/string_rb.html +115 -0
- data/rdoc/files/lib/algorithms_rb.html +252 -0
- data/rdoc/files/lib/containers/deque_rb.html +119 -0
- data/rdoc/files/lib/containers/heap_rb.html +124 -0
- data/rdoc/files/lib/containers/kd_tree_rb.html +135 -0
- data/rdoc/files/lib/containers/priority_queue_rb.html +108 -0
- data/rdoc/files/lib/containers/queue_rb.html +108 -0
- data/rdoc/files/lib/containers/rb_tree_map_rb.html +109 -0
- data/rdoc/files/lib/containers/splay_tree_map_rb.html +109 -0
- data/rdoc/files/lib/containers/stack_rb.html +108 -0
- data/rdoc/files/lib/containers/suffix_array_rb.html +113 -0
- data/rdoc/files/lib/containers/trie_rb.html +117 -0
- data/rdoc/fr_class_index.html +43 -0
- data/rdoc/fr_file_index.html +42 -0
- data/rdoc/fr_method_index.html +147 -0
- data/rdoc/index.html +24 -0
- data/rdoc/rdoc-style.css +208 -0
- data/spec/bst_gc_mark_spec.rb +25 -0
- data/spec/bst_spec.rb +25 -0
- metadata +50 -3
@@ -0,0 +1,623 @@
|
|
1
|
+
<?xml version="1.0" encoding="iso-8859-1"?>
|
2
|
+
<!DOCTYPE html
|
3
|
+
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
4
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
5
|
+
|
6
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
7
|
+
<head>
|
8
|
+
<title>Class: Algorithms::Containers::RubySplayTreeMap</title>
|
9
|
+
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
|
10
|
+
<meta http-equiv="Content-Script-Type" content="text/javascript" />
|
11
|
+
<link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
|
12
|
+
<script type="text/javascript">
|
13
|
+
// <![CDATA[
|
14
|
+
|
15
|
+
function popupCode( url ) {
|
16
|
+
window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
|
17
|
+
}
|
18
|
+
|
19
|
+
function toggleCode( id ) {
|
20
|
+
if ( document.getElementById )
|
21
|
+
elem = document.getElementById( id );
|
22
|
+
else if ( document.all )
|
23
|
+
elem = eval( "document.all." + id );
|
24
|
+
else
|
25
|
+
return false;
|
26
|
+
|
27
|
+
elemStyle = elem.style;
|
28
|
+
|
29
|
+
if ( elemStyle.display != "block" ) {
|
30
|
+
elemStyle.display = "block"
|
31
|
+
} else {
|
32
|
+
elemStyle.display = "none"
|
33
|
+
}
|
34
|
+
|
35
|
+
return true;
|
36
|
+
}
|
37
|
+
|
38
|
+
// Make codeblocks hidden by default
|
39
|
+
document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
|
40
|
+
|
41
|
+
// ]]>
|
42
|
+
</script>
|
43
|
+
|
44
|
+
</head>
|
45
|
+
<body>
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
<div id="classHeader">
|
50
|
+
<table class="header-table">
|
51
|
+
<tr class="top-aligned-row">
|
52
|
+
<td><strong>Class</strong></td>
|
53
|
+
<td class="class-name-in-header">Algorithms::Containers::RubySplayTreeMap</td>
|
54
|
+
</tr>
|
55
|
+
<tr class="top-aligned-row">
|
56
|
+
<td><strong>In:</strong></td>
|
57
|
+
<td>
|
58
|
+
<a href="../../../files/lib/containers/splay_tree_map_rb.html">
|
59
|
+
lib/containers/splay_tree_map.rb
|
60
|
+
</a>
|
61
|
+
<br />
|
62
|
+
</td>
|
63
|
+
</tr>
|
64
|
+
|
65
|
+
<tr class="top-aligned-row">
|
66
|
+
<td><strong>Parent:</strong></td>
|
67
|
+
<td>
|
68
|
+
Object
|
69
|
+
</td>
|
70
|
+
</tr>
|
71
|
+
</table>
|
72
|
+
</div>
|
73
|
+
<!-- banner header -->
|
74
|
+
|
75
|
+
<div id="bodyContent">
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
<div id="contextContent">
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
</div>
|
84
|
+
|
85
|
+
<div id="method-list">
|
86
|
+
<h3 class="section-bar">Methods</h3>
|
87
|
+
|
88
|
+
<div class="name-list">
|
89
|
+
<a href="#M000088">[]</a>
|
90
|
+
<a href="#M000082">[]=</a>
|
91
|
+
<a href="#M000084">clear</a>
|
92
|
+
<a href="#M000091">delete</a>
|
93
|
+
<a href="#M000092">each</a>
|
94
|
+
<a href="#M000087">get</a>
|
95
|
+
<a href="#M000086">has_key?</a>
|
96
|
+
<a href="#M000085">height</a>
|
97
|
+
<a href="#M000090">max</a>
|
98
|
+
<a href="#M000089">min</a>
|
99
|
+
<a href="#M000080">new</a>
|
100
|
+
<a href="#M000081">push</a>
|
101
|
+
<a href="#M000083">size</a>
|
102
|
+
</div>
|
103
|
+
</div>
|
104
|
+
|
105
|
+
</div>
|
106
|
+
|
107
|
+
|
108
|
+
<!-- if includes -->
|
109
|
+
<div id="includes">
|
110
|
+
<h3 class="section-bar">Included Modules</h3>
|
111
|
+
|
112
|
+
<div id="includes-list">
|
113
|
+
<span class="include-name">Enumerable</span>
|
114
|
+
</div>
|
115
|
+
</div>
|
116
|
+
|
117
|
+
<div id="section">
|
118
|
+
|
119
|
+
|
120
|
+
<div id="constants-list">
|
121
|
+
<h3 class="section-bar">Constants</h3>
|
122
|
+
|
123
|
+
<div class="name-list">
|
124
|
+
<table summary="Constants">
|
125
|
+
<tr class="top-aligned-row context-row">
|
126
|
+
<td class="context-item-name">Node</td>
|
127
|
+
<td>=</td>
|
128
|
+
<td class="context-item-value">Struct.new(:key, :value, :left, :right)</td>
|
129
|
+
</tr>
|
130
|
+
</table>
|
131
|
+
</div>
|
132
|
+
</div>
|
133
|
+
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
<!-- if method_list -->
|
140
|
+
<div id="methods">
|
141
|
+
<h3 class="section-bar">Public Class methods</h3>
|
142
|
+
|
143
|
+
<div id="method-M000080" class="method-detail">
|
144
|
+
<a name="M000080"></a>
|
145
|
+
|
146
|
+
<div class="method-heading">
|
147
|
+
<a href="#M000080" class="method-signature">
|
148
|
+
<span class="method-name">new</span><span class="method-args">()</span>
|
149
|
+
</a>
|
150
|
+
</div>
|
151
|
+
|
152
|
+
<div class="method-description">
|
153
|
+
<p>
|
154
|
+
Create and initialize a <a href="RubySplayTreeMap.html#M000080">new</a>
|
155
|
+
empty SplayTreeMap.
|
156
|
+
</p>
|
157
|
+
<p><a class="source-toggle" href="#"
|
158
|
+
onclick="toggleCode('M000080-source');return false;">[Source]</a></p>
|
159
|
+
<div class="method-source-code" id="M000080-source">
|
160
|
+
<pre>
|
161
|
+
<span class="ruby-comment cmt"># File lib/containers/splay_tree_map.rb, line 24</span>
|
162
|
+
24: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">initialize</span>
|
163
|
+
25: <span class="ruby-ivar">@size</span> = <span class="ruby-value">0</span>
|
164
|
+
26: <span class="ruby-identifier">clear</span>
|
165
|
+
27: <span class="ruby-keyword kw">end</span>
|
166
|
+
</pre>
|
167
|
+
</div>
|
168
|
+
</div>
|
169
|
+
</div>
|
170
|
+
|
171
|
+
<h3 class="section-bar">Public Instance methods</h3>
|
172
|
+
|
173
|
+
<div id="method-M000088" class="method-detail">
|
174
|
+
<a name="M000088"></a>
|
175
|
+
|
176
|
+
<div class="method-heading">
|
177
|
+
<span class="method-name">[]</span><span class="method-args">(key)</span>
|
178
|
+
</div>
|
179
|
+
|
180
|
+
<div class="method-description">
|
181
|
+
<p>
|
182
|
+
Alias for <a href="RubySplayTreeMap.html#M000087">get</a>
|
183
|
+
</p>
|
184
|
+
</div>
|
185
|
+
</div>
|
186
|
+
|
187
|
+
<div id="method-M000082" class="method-detail">
|
188
|
+
<a name="M000082"></a>
|
189
|
+
|
190
|
+
<div class="method-heading">
|
191
|
+
<span class="method-name">[]=</span><span class="method-args">(key, value)</span>
|
192
|
+
</div>
|
193
|
+
|
194
|
+
<div class="method-description">
|
195
|
+
<p>
|
196
|
+
Alias for <a href="RubySplayTreeMap.html#M000081">push</a>
|
197
|
+
</p>
|
198
|
+
</div>
|
199
|
+
</div>
|
200
|
+
|
201
|
+
<div id="method-M000084" class="method-detail">
|
202
|
+
<a name="M000084"></a>
|
203
|
+
|
204
|
+
<div class="method-heading">
|
205
|
+
<a href="#M000084" class="method-signature">
|
206
|
+
<span class="method-name">clear</span><span class="method-args">()</span>
|
207
|
+
</a>
|
208
|
+
</div>
|
209
|
+
|
210
|
+
<div class="method-description">
|
211
|
+
<p>
|
212
|
+
Remove all elements from the SplayTreeMap
|
213
|
+
</p>
|
214
|
+
<p>
|
215
|
+
Complexity: O(1)
|
216
|
+
</p>
|
217
|
+
<p><a class="source-toggle" href="#"
|
218
|
+
onclick="toggleCode('M000084-source');return false;">[Source]</a></p>
|
219
|
+
<div class="method-source-code" id="M000084-source">
|
220
|
+
<pre>
|
221
|
+
<span class="ruby-comment cmt"># File lib/containers/splay_tree_map.rb, line 79</span>
|
222
|
+
79: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">clear</span>
|
223
|
+
80: <span class="ruby-ivar">@root</span> = <span class="ruby-keyword kw">nil</span>
|
224
|
+
81: <span class="ruby-ivar">@size</span> = <span class="ruby-value">0</span>
|
225
|
+
82: <span class="ruby-ivar">@header</span> = <span class="ruby-constant">Node</span>.<span class="ruby-identifier">new</span>(<span class="ruby-keyword kw">nil</span>, <span class="ruby-keyword kw">nil</span>, <span class="ruby-keyword kw">nil</span>, <span class="ruby-keyword kw">nil</span>)
|
226
|
+
83: <span class="ruby-keyword kw">end</span>
|
227
|
+
</pre>
|
228
|
+
</div>
|
229
|
+
</div>
|
230
|
+
</div>
|
231
|
+
|
232
|
+
<div id="method-M000091" class="method-detail">
|
233
|
+
<a name="M000091"></a>
|
234
|
+
|
235
|
+
<div class="method-heading">
|
236
|
+
<a href="#M000091" class="method-signature">
|
237
|
+
<span class="method-name">delete</span><span class="method-args">(key)</span>
|
238
|
+
</a>
|
239
|
+
</div>
|
240
|
+
|
241
|
+
<div class="method-description">
|
242
|
+
<p>
|
243
|
+
Deletes the item and key if it‘s found, and returns the item. Returns
|
244
|
+
nil if key is not present.
|
245
|
+
</p>
|
246
|
+
<p>
|
247
|
+
Complexity: amortized O(log n)
|
248
|
+
</p>
|
249
|
+
<pre>
|
250
|
+
map = Algorithms::Containers::SplayTreeMap.new
|
251
|
+
map["MA"] = "Massachusetts"
|
252
|
+
map["GA"] = "Georgia"
|
253
|
+
map.delete("GA") #=> "Georgia"
|
254
|
+
map.delete("DE") #=> nil
|
255
|
+
</pre>
|
256
|
+
<p><a class="source-toggle" href="#"
|
257
|
+
onclick="toggleCode('M000091-source');return false;">[Source]</a></p>
|
258
|
+
<div class="method-source-code" id="M000091-source">
|
259
|
+
<pre>
|
260
|
+
<span class="ruby-comment cmt"># File lib/containers/splay_tree_map.rb, line 172</span>
|
261
|
+
172: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">delete</span>(<span class="ruby-identifier">key</span>)
|
262
|
+
173: <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">nil</span> <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@root</span>.<span class="ruby-identifier">nil?</span>
|
263
|
+
174: <span class="ruby-identifier">deleted</span> = <span class="ruby-keyword kw">nil</span>
|
264
|
+
175: <span class="ruby-identifier">splay</span>(<span class="ruby-identifier">key</span>)
|
265
|
+
176: <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">key</span> <span class="ruby-operator"><=></span> <span class="ruby-ivar">@root</span>.<span class="ruby-identifier">key</span>) <span class="ruby-operator">==</span> <span class="ruby-value">0</span> <span class="ruby-comment cmt"># The key exists</span>
|
266
|
+
177: <span class="ruby-identifier">deleted</span> = <span class="ruby-ivar">@root</span>.<span class="ruby-identifier">value</span>
|
267
|
+
178: <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@root</span>.<span class="ruby-identifier">left</span>.<span class="ruby-identifier">nil?</span>
|
268
|
+
179: <span class="ruby-ivar">@root</span> = <span class="ruby-ivar">@root</span>.<span class="ruby-identifier">right</span>
|
269
|
+
180: <span class="ruby-keyword kw">else</span>
|
270
|
+
181: <span class="ruby-identifier">x</span> = <span class="ruby-ivar">@root</span>.<span class="ruby-identifier">right</span>
|
271
|
+
182: <span class="ruby-ivar">@root</span> = <span class="ruby-ivar">@root</span>.<span class="ruby-identifier">left</span>
|
272
|
+
183: <span class="ruby-identifier">splay</span>(<span class="ruby-identifier">key</span>)
|
273
|
+
184: <span class="ruby-ivar">@root</span>.<span class="ruby-identifier">right</span> = <span class="ruby-identifier">x</span>
|
274
|
+
185: <span class="ruby-keyword kw">end</span>
|
275
|
+
186: <span class="ruby-keyword kw">end</span>
|
276
|
+
187: <span class="ruby-identifier">deleted</span>
|
277
|
+
188: <span class="ruby-keyword kw">end</span>
|
278
|
+
</pre>
|
279
|
+
</div>
|
280
|
+
</div>
|
281
|
+
</div>
|
282
|
+
|
283
|
+
<div id="method-M000092" class="method-detail">
|
284
|
+
<a name="M000092"></a>
|
285
|
+
|
286
|
+
<div class="method-heading">
|
287
|
+
<a href="#M000092" class="method-signature">
|
288
|
+
<span class="method-name">each</span><span class="method-args">() {|cursor.key, cursor.value| ...}</span>
|
289
|
+
</a>
|
290
|
+
</div>
|
291
|
+
|
292
|
+
<div class="method-description">
|
293
|
+
<p>
|
294
|
+
Iterates over the SplayTreeMap in ascending order. Uses an iterative, not
|
295
|
+
recursive, approach.
|
296
|
+
</p>
|
297
|
+
<p><a class="source-toggle" href="#"
|
298
|
+
onclick="toggleCode('M000092-source');return false;">[Source]</a></p>
|
299
|
+
<div class="method-source-code" id="M000092-source">
|
300
|
+
<pre>
|
301
|
+
<span class="ruby-comment cmt"># File lib/containers/splay_tree_map.rb, line 191</span>
|
302
|
+
191: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">each</span>
|
303
|
+
192: <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">nil</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-ivar">@root</span>
|
304
|
+
193: <span class="ruby-identifier">stack</span> = <span class="ruby-constant">Stack</span>.<span class="ruby-identifier">new</span>
|
305
|
+
194: <span class="ruby-identifier">cursor</span> = <span class="ruby-ivar">@root</span>
|
306
|
+
195: <span class="ruby-identifier">loop</span> <span class="ruby-keyword kw">do</span>
|
307
|
+
196: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">cursor</span>
|
308
|
+
197: <span class="ruby-identifier">stack</span>.<span class="ruby-identifier">push</span>(<span class="ruby-identifier">cursor</span>)
|
309
|
+
198: <span class="ruby-identifier">cursor</span> = <span class="ruby-identifier">cursor</span>.<span class="ruby-identifier">left</span>
|
310
|
+
199: <span class="ruby-keyword kw">else</span>
|
311
|
+
200: <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">stack</span>.<span class="ruby-identifier">empty?</span>
|
312
|
+
201: <span class="ruby-identifier">cursor</span> = <span class="ruby-identifier">stack</span>.<span class="ruby-identifier">pop</span>
|
313
|
+
202: <span class="ruby-keyword kw">yield</span>(<span class="ruby-identifier">cursor</span>.<span class="ruby-identifier">key</span>, <span class="ruby-identifier">cursor</span>.<span class="ruby-identifier">value</span>)
|
314
|
+
203: <span class="ruby-identifier">cursor</span> = <span class="ruby-identifier">cursor</span>.<span class="ruby-identifier">right</span>
|
315
|
+
204: <span class="ruby-keyword kw">else</span>
|
316
|
+
205: <span class="ruby-keyword kw">break</span>
|
317
|
+
206: <span class="ruby-keyword kw">end</span>
|
318
|
+
207: <span class="ruby-keyword kw">end</span>
|
319
|
+
208: <span class="ruby-keyword kw">end</span>
|
320
|
+
209: <span class="ruby-keyword kw">end</span>
|
321
|
+
</pre>
|
322
|
+
</div>
|
323
|
+
</div>
|
324
|
+
</div>
|
325
|
+
|
326
|
+
<div id="method-M000087" class="method-detail">
|
327
|
+
<a name="M000087"></a>
|
328
|
+
|
329
|
+
<div class="method-heading">
|
330
|
+
<a href="#M000087" class="method-signature">
|
331
|
+
<span class="method-name">get</span><span class="method-args">(key)</span>
|
332
|
+
</a>
|
333
|
+
</div>
|
334
|
+
|
335
|
+
<div class="method-description">
|
336
|
+
<p>
|
337
|
+
Return the item associated with the key, or nil if none found.
|
338
|
+
</p>
|
339
|
+
<p>
|
340
|
+
Complexity: amortized O(log n)
|
341
|
+
</p>
|
342
|
+
<pre>
|
343
|
+
map = Algorithms::Containers::SplayTreeMap.new
|
344
|
+
map.push("MA", "Massachusetts")
|
345
|
+
map.push("GA", "Georgia")
|
346
|
+
map.get("GA") #=> "Georgia"
|
347
|
+
</pre>
|
348
|
+
<p><a class="source-toggle" href="#"
|
349
|
+
onclick="toggleCode('M000087-source');return false;">[Source]</a></p>
|
350
|
+
<div class="method-source-code" id="M000087-source">
|
351
|
+
<pre>
|
352
|
+
<span class="ruby-comment cmt"># File lib/containers/splay_tree_map.rb, line 118</span>
|
353
|
+
118: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">get</span>(<span class="ruby-identifier">key</span>)
|
354
|
+
119: <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">nil</span> <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@root</span>.<span class="ruby-identifier">nil?</span>
|
355
|
+
120:
|
356
|
+
121: <span class="ruby-identifier">splay</span>(<span class="ruby-identifier">key</span>)
|
357
|
+
122: (<span class="ruby-ivar">@root</span>.<span class="ruby-identifier">key</span> <span class="ruby-operator"><=></span> <span class="ruby-identifier">key</span>) <span class="ruby-operator">==</span> <span class="ruby-value">0</span> <span class="ruby-operator">?</span> <span class="ruby-ivar">@root</span>.<span class="ruby-identifier">value</span> <span class="ruby-operator">:</span> <span class="ruby-keyword kw">nil</span>
|
358
|
+
123: <span class="ruby-keyword kw">end</span>
|
359
|
+
</pre>
|
360
|
+
</div>
|
361
|
+
</div>
|
362
|
+
</div>
|
363
|
+
|
364
|
+
<div id="method-M000086" class="method-detail">
|
365
|
+
<a name="M000086"></a>
|
366
|
+
|
367
|
+
<div class="method-heading">
|
368
|
+
<a href="#M000086" class="method-signature">
|
369
|
+
<span class="method-name">has_key?</span><span class="method-args">(key)</span>
|
370
|
+
</a>
|
371
|
+
</div>
|
372
|
+
|
373
|
+
<div class="method-description">
|
374
|
+
<p>
|
375
|
+
Return true if key is found in the SplayTreeMap, false otherwise.
|
376
|
+
</p>
|
377
|
+
<p>
|
378
|
+
Complexity: amortized O(log n)
|
379
|
+
</p>
|
380
|
+
<pre>
|
381
|
+
map = Algorithms::Containers::SplayTreeMap.new
|
382
|
+
map["MA"] = "Massachusetts"
|
383
|
+
map["GA"] = "Georgia"
|
384
|
+
map.has_key?("GA") #=> true
|
385
|
+
map.has_key?("DE") #=> false
|
386
|
+
</pre>
|
387
|
+
<p><a class="source-toggle" href="#"
|
388
|
+
onclick="toggleCode('M000086-source');return false;">[Source]</a></p>
|
389
|
+
<div class="method-source-code" id="M000086-source">
|
390
|
+
<pre>
|
391
|
+
<span class="ruby-comment cmt"># File lib/containers/splay_tree_map.rb, line 106</span>
|
392
|
+
106: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">key</span>)
|
393
|
+
107: <span class="ruby-operator">!</span><span class="ruby-identifier">get</span>(<span class="ruby-identifier">key</span>).<span class="ruby-identifier">nil?</span>
|
394
|
+
108: <span class="ruby-keyword kw">end</span>
|
395
|
+
</pre>
|
396
|
+
</div>
|
397
|
+
</div>
|
398
|
+
</div>
|
399
|
+
|
400
|
+
<div id="method-M000085" class="method-detail">
|
401
|
+
<a name="M000085"></a>
|
402
|
+
|
403
|
+
<div class="method-heading">
|
404
|
+
<a href="#M000085" class="method-signature">
|
405
|
+
<span class="method-name">height</span><span class="method-args">()</span>
|
406
|
+
</a>
|
407
|
+
</div>
|
408
|
+
|
409
|
+
<div class="method-description">
|
410
|
+
<p>
|
411
|
+
Return the <a href="RubySplayTreeMap.html#M000085">height</a> of the tree
|
412
|
+
structure in the SplayTreeMap.
|
413
|
+
</p>
|
414
|
+
<p>
|
415
|
+
Complexity: O(log n)
|
416
|
+
</p>
|
417
|
+
<pre>
|
418
|
+
map = Algorithms::Containers::SplayTreeMap.new
|
419
|
+
map.push("MA", "Massachusetts")
|
420
|
+
map.push("GA", "Georgia")
|
421
|
+
map.height #=> 2
|
422
|
+
</pre>
|
423
|
+
<p><a class="source-toggle" href="#"
|
424
|
+
onclick="toggleCode('M000085-source');return false;">[Source]</a></p>
|
425
|
+
<div class="method-source-code" id="M000085-source">
|
426
|
+
<pre>
|
427
|
+
<span class="ruby-comment cmt"># File lib/containers/splay_tree_map.rb, line 93</span>
|
428
|
+
93: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">height</span>
|
429
|
+
94: <span class="ruby-identifier">height_recursive</span>(<span class="ruby-ivar">@root</span>)
|
430
|
+
95: <span class="ruby-keyword kw">end</span>
|
431
|
+
</pre>
|
432
|
+
</div>
|
433
|
+
</div>
|
434
|
+
</div>
|
435
|
+
|
436
|
+
<div id="method-M000090" class="method-detail">
|
437
|
+
<a name="M000090"></a>
|
438
|
+
|
439
|
+
<div class="method-heading">
|
440
|
+
<a href="#M000090" class="method-signature">
|
441
|
+
<span class="method-name">max</span><span class="method-args">()</span>
|
442
|
+
</a>
|
443
|
+
</div>
|
444
|
+
|
445
|
+
<div class="method-description">
|
446
|
+
<p>
|
447
|
+
Return the largest [key, value] pair in the SplayTreeMap, or nil if the
|
448
|
+
tree is empty.
|
449
|
+
</p>
|
450
|
+
<p>
|
451
|
+
Complexity: amortized O(log n)
|
452
|
+
</p>
|
453
|
+
<pre>
|
454
|
+
map = Algorithms::Containers::SplayTreeMap.new
|
455
|
+
map["MA"] = "Massachusetts"
|
456
|
+
map["GA"] = "Georgia"
|
457
|
+
map.max #=> ["MA", "Massachusetts"]
|
458
|
+
</pre>
|
459
|
+
<p><a class="source-toggle" href="#"
|
460
|
+
onclick="toggleCode('M000090-source');return false;">[Source]</a></p>
|
461
|
+
<div class="method-source-code" id="M000090-source">
|
462
|
+
<pre>
|
463
|
+
<span class="ruby-comment cmt"># File lib/containers/splay_tree_map.rb, line 152</span>
|
464
|
+
152: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">max</span>
|
465
|
+
153: <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">nil</span> <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@root</span>.<span class="ruby-identifier">nil?</span>
|
466
|
+
154: <span class="ruby-identifier">n</span> = <span class="ruby-ivar">@root</span>
|
467
|
+
155: <span class="ruby-keyword kw">while</span> <span class="ruby-identifier">n</span>.<span class="ruby-identifier">right</span>
|
468
|
+
156: <span class="ruby-identifier">n</span> = <span class="ruby-identifier">n</span>.<span class="ruby-identifier">right</span>
|
469
|
+
157: <span class="ruby-keyword kw">end</span>
|
470
|
+
158: <span class="ruby-identifier">splay</span>(<span class="ruby-identifier">n</span>.<span class="ruby-identifier">key</span>)
|
471
|
+
159: <span class="ruby-keyword kw">return</span> [<span class="ruby-identifier">n</span>.<span class="ruby-identifier">key</span>, <span class="ruby-identifier">n</span>.<span class="ruby-identifier">value</span>]
|
472
|
+
160: <span class="ruby-keyword kw">end</span>
|
473
|
+
</pre>
|
474
|
+
</div>
|
475
|
+
</div>
|
476
|
+
</div>
|
477
|
+
|
478
|
+
<div id="method-M000089" class="method-detail">
|
479
|
+
<a name="M000089"></a>
|
480
|
+
|
481
|
+
<div class="method-heading">
|
482
|
+
<a href="#M000089" class="method-signature">
|
483
|
+
<span class="method-name">min</span><span class="method-args">()</span>
|
484
|
+
</a>
|
485
|
+
</div>
|
486
|
+
|
487
|
+
<div class="method-description">
|
488
|
+
<p>
|
489
|
+
Return the smallest [key, value] pair in the SplayTreeMap, or nil if the
|
490
|
+
tree is empty.
|
491
|
+
</p>
|
492
|
+
<p>
|
493
|
+
Complexity: amortized O(log n)
|
494
|
+
</p>
|
495
|
+
<pre>
|
496
|
+
map = Algorithms::Containers::SplayTreeMap.new
|
497
|
+
map["MA"] = "Massachusetts"
|
498
|
+
map["GA"] = "Georgia"
|
499
|
+
map.min #=> ["GA", "Georgia"]
|
500
|
+
</pre>
|
501
|
+
<p><a class="source-toggle" href="#"
|
502
|
+
onclick="toggleCode('M000089-source');return false;">[Source]</a></p>
|
503
|
+
<div class="method-source-code" id="M000089-source">
|
504
|
+
<pre>
|
505
|
+
<span class="ruby-comment cmt"># File lib/containers/splay_tree_map.rb, line 134</span>
|
506
|
+
134: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">min</span>
|
507
|
+
135: <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">nil</span> <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@root</span>.<span class="ruby-identifier">nil?</span>
|
508
|
+
136: <span class="ruby-identifier">n</span> = <span class="ruby-ivar">@root</span>
|
509
|
+
137: <span class="ruby-keyword kw">while</span> <span class="ruby-identifier">n</span>.<span class="ruby-identifier">left</span>
|
510
|
+
138: <span class="ruby-identifier">n</span> = <span class="ruby-identifier">n</span>.<span class="ruby-identifier">left</span>
|
511
|
+
139: <span class="ruby-keyword kw">end</span>
|
512
|
+
140: <span class="ruby-identifier">splay</span>(<span class="ruby-identifier">n</span>.<span class="ruby-identifier">key</span>)
|
513
|
+
141: <span class="ruby-keyword kw">return</span> [<span class="ruby-identifier">n</span>.<span class="ruby-identifier">key</span>, <span class="ruby-identifier">n</span>.<span class="ruby-identifier">value</span>]
|
514
|
+
142: <span class="ruby-keyword kw">end</span>
|
515
|
+
</pre>
|
516
|
+
</div>
|
517
|
+
</div>
|
518
|
+
</div>
|
519
|
+
|
520
|
+
<div id="method-M000081" class="method-detail">
|
521
|
+
<a name="M000081"></a>
|
522
|
+
|
523
|
+
<div class="method-heading">
|
524
|
+
<a href="#M000081" class="method-signature">
|
525
|
+
<span class="method-name">push</span><span class="method-args">(key, value)</span>
|
526
|
+
</a>
|
527
|
+
</div>
|
528
|
+
|
529
|
+
<div class="method-description">
|
530
|
+
<p>
|
531
|
+
Insert an item with an associated key into the SplayTreeMap, and returns
|
532
|
+
the item inserted
|
533
|
+
</p>
|
534
|
+
<p>
|
535
|
+
Complexity: amortized O(log n)
|
536
|
+
</p>
|
537
|
+
<pre>
|
538
|
+
map = Algorithms::Containers::SplayTreeMap.new
|
539
|
+
map.push("MA", "Massachusetts") #=> "Massachusetts"
|
540
|
+
map.get("MA") #=> "Massachusetts"
|
541
|
+
</pre>
|
542
|
+
<p><a class="source-toggle" href="#"
|
543
|
+
onclick="toggleCode('M000081-source');return false;">[Source]</a></p>
|
544
|
+
<div class="method-source-code" id="M000081-source">
|
545
|
+
<pre>
|
546
|
+
<span class="ruby-comment cmt"># File lib/containers/splay_tree_map.rb, line 36</span>
|
547
|
+
36: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">push</span>(<span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span>)
|
548
|
+
37: <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@root</span>.<span class="ruby-identifier">nil?</span>
|
549
|
+
38: <span class="ruby-ivar">@root</span> = <span class="ruby-constant">Node</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span>, <span class="ruby-keyword kw">nil</span>, <span class="ruby-keyword kw">nil</span>)
|
550
|
+
39: <span class="ruby-ivar">@size</span> = <span class="ruby-value">1</span>
|
551
|
+
40: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">value</span>
|
552
|
+
41: <span class="ruby-keyword kw">end</span>
|
553
|
+
42: <span class="ruby-identifier">splay</span>(<span class="ruby-identifier">key</span>)
|
554
|
+
43:
|
555
|
+
44: <span class="ruby-identifier">cmp</span> = (<span class="ruby-identifier">key</span> <span class="ruby-operator"><=></span> <span class="ruby-ivar">@root</span>.<span class="ruby-identifier">key</span>)
|
556
|
+
45: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">cmp</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span>
|
557
|
+
46: <span class="ruby-ivar">@root</span>.<span class="ruby-identifier">value</span> = <span class="ruby-identifier">value</span>
|
558
|
+
47: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">value</span>
|
559
|
+
48: <span class="ruby-keyword kw">end</span>
|
560
|
+
49: <span class="ruby-identifier">node</span> = <span class="ruby-constant">Node</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span>, <span class="ruby-keyword kw">nil</span>, <span class="ruby-keyword kw">nil</span>)
|
561
|
+
50: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">cmp</span> <span class="ruby-operator"><</span> <span class="ruby-value">1</span>
|
562
|
+
51: <span class="ruby-identifier">node</span>.<span class="ruby-identifier">left</span> = <span class="ruby-ivar">@root</span>.<span class="ruby-identifier">left</span>
|
563
|
+
52: <span class="ruby-identifier">node</span>.<span class="ruby-identifier">right</span> = <span class="ruby-ivar">@root</span>
|
564
|
+
53: <span class="ruby-ivar">@root</span>.<span class="ruby-identifier">left</span> = <span class="ruby-keyword kw">nil</span>
|
565
|
+
54: <span class="ruby-keyword kw">else</span>
|
566
|
+
55: <span class="ruby-identifier">node</span>.<span class="ruby-identifier">right</span> = <span class="ruby-ivar">@root</span>.<span class="ruby-identifier">right</span>
|
567
|
+
56: <span class="ruby-identifier">node</span>.<span class="ruby-identifier">left</span> = <span class="ruby-ivar">@root</span>
|
568
|
+
57: <span class="ruby-ivar">@root</span>.<span class="ruby-identifier">right</span> = <span class="ruby-keyword kw">nil</span>
|
569
|
+
58: <span class="ruby-keyword kw">end</span>
|
570
|
+
59: <span class="ruby-ivar">@root</span> = <span class="ruby-identifier">node</span>
|
571
|
+
60: <span class="ruby-ivar">@size</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span>
|
572
|
+
61: <span class="ruby-identifier">value</span>
|
573
|
+
62: <span class="ruby-keyword kw">end</span>
|
574
|
+
</pre>
|
575
|
+
</div>
|
576
|
+
</div>
|
577
|
+
</div>
|
578
|
+
|
579
|
+
<div id="method-M000083" class="method-detail">
|
580
|
+
<a name="M000083"></a>
|
581
|
+
|
582
|
+
<div class="method-heading">
|
583
|
+
<a href="#M000083" class="method-signature">
|
584
|
+
<span class="method-name">size</span><span class="method-args">()</span>
|
585
|
+
</a>
|
586
|
+
</div>
|
587
|
+
|
588
|
+
<div class="method-description">
|
589
|
+
<p>
|
590
|
+
Return the number of items in the SplayTreeMap.
|
591
|
+
</p>
|
592
|
+
<pre>
|
593
|
+
map = Algorithms::Containers::SplayTreeMap.new
|
594
|
+
map.push("MA", "Massachusetts")
|
595
|
+
map.push("GA", "Georgia")
|
596
|
+
map.size #=> 2
|
597
|
+
</pre>
|
598
|
+
<p><a class="source-toggle" href="#"
|
599
|
+
onclick="toggleCode('M000083-source');return false;">[Source]</a></p>
|
600
|
+
<div class="method-source-code" id="M000083-source">
|
601
|
+
<pre>
|
602
|
+
<span class="ruby-comment cmt"># File lib/containers/splay_tree_map.rb, line 71</span>
|
603
|
+
71: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">size</span>
|
604
|
+
72: <span class="ruby-ivar">@size</span>
|
605
|
+
73: <span class="ruby-keyword kw">end</span>
|
606
|
+
</pre>
|
607
|
+
</div>
|
608
|
+
</div>
|
609
|
+
</div>
|
610
|
+
|
611
|
+
|
612
|
+
</div>
|
613
|
+
|
614
|
+
|
615
|
+
</div>
|
616
|
+
|
617
|
+
|
618
|
+
<div id="validator-badges">
|
619
|
+
<p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
|
620
|
+
</div>
|
621
|
+
|
622
|
+
</body>
|
623
|
+
</html>
|