autoc 1.4 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGES.md +3 -0
- data/README.md +149 -0
- data/cmake/AutoC.cmake +39 -0
- data/lib/autoc/allocators.rb +51 -0
- data/lib/autoc/association.rb +126 -0
- data/lib/autoc/box.rb +311 -0
- data/lib/autoc/cmake.rb +54 -0
- data/lib/autoc/collection.rb +83 -110
- data/lib/autoc/composite.rb +333 -0
- data/lib/autoc/cstring.rb +263 -0
- data/lib/autoc/function.rb +247 -0
- data/lib/autoc/hash_map.rb +328 -0
- data/lib/autoc/hash_set.rb +339 -0
- data/lib/autoc/hashers.rb +102 -0
- data/lib/autoc/list.rb +444 -0
- data/lib/autoc/module.rb +434 -0
- data/lib/autoc/openmp.rb +15 -0
- data/lib/autoc/primitive.rb +27 -0
- data/lib/autoc/ranges.rb +707 -0
- data/lib/autoc/record.rb +247 -0
- data/lib/autoc/scaffold/docs.rb +117 -0
- data/lib/autoc/scaffold/generic_value.rb +86 -0
- data/lib/autoc/scaffold/project.rb +75 -0
- data/lib/autoc/scaffold/test_cstring.rb +113 -0
- data/lib/autoc/scaffold/test_cstring_hash_set.rb +35 -0
- data/lib/autoc/scaffold/test_int_box.rb +22 -0
- data/lib/autoc/scaffold/test_int_hash_set.rb +448 -0
- data/lib/autoc/scaffold/test_int_list.rb +106 -0
- data/lib/autoc/scaffold/test_int_vector.rb +83 -0
- data/lib/autoc/scaffold/test_v2v_hash_map.rb +83 -0
- data/lib/autoc/scaffold/test_value_hash_set.rb +60 -0
- data/lib/autoc/scaffold/test_value_vector.rb +146 -0
- data/{test/test.rb → lib/autoc/scaffold/tests.rb} +179 -158
- data/lib/autoc/scaffold.rb +12 -0
- data/lib/autoc/sequential.rb +99 -0
- data/lib/autoc/set.rb +331 -0
- data/lib/autoc/std.rb +149 -0
- data/lib/autoc/type.rb +93 -531
- data/lib/autoc/vector.rb +290 -0
- data/lib/autoc.rb +4 -35
- metadata +55 -85
- data/.yardopts +0 -4
- data/CHANGES +0 -23
- data/README +0 -28
- data/doc/AutoC/Code.html +0 -523
- data/doc/AutoC/Collection.html +0 -1214
- data/doc/AutoC/HashMap.html +0 -1441
- data/doc/AutoC/HashSet.html +0 -916
- data/doc/AutoC/Iterators/Bidirectional.html +0 -204
- data/doc/AutoC/Iterators/Unidirectional.html +0 -200
- data/doc/AutoC/Iterators.html +0 -126
- data/doc/AutoC/List.html +0 -1039
- data/doc/AutoC/Maps.html +0 -290
- data/doc/AutoC/Module/File.html +0 -415
- data/doc/AutoC/Module/Header.html +0 -437
- data/doc/AutoC/Module/Source.html +0 -707
- data/doc/AutoC/Module.html +0 -948
- data/doc/AutoC/Priority.html +0 -138
- data/doc/AutoC/Queue.html +0 -1172
- data/doc/AutoC/Reference.html +0 -735
- data/doc/AutoC/Sets.html +0 -520
- data/doc/AutoC/String.html +0 -1394
- data/doc/AutoC/TreeMap.html +0 -1565
- data/doc/AutoC/TreeSet.html +0 -1447
- data/doc/AutoC/Type.html +0 -2148
- data/doc/AutoC/UserDefinedType.html +0 -1047
- data/doc/AutoC/Vector.html +0 -987
- data/doc/AutoC.html +0 -331
- data/doc/_index.html +0 -388
- data/doc/class_list.html +0 -51
- data/doc/css/common.css +0 -1
- data/doc/css/full_list.css +0 -58
- data/doc/css/style.css +0 -481
- data/doc/file.CHANGES.html +0 -117
- data/doc/file.README.html +0 -116
- data/doc/file_list.html +0 -61
- data/doc/frames.html +0 -17
- data/doc/index.html +0 -116
- data/doc/js/app.js +0 -243
- data/doc/js/full_list.js +0 -216
- data/doc/js/jquery.js +0 -4
- data/doc/method_list.html +0 -1307
- data/doc/top-level-namespace.html +0 -112
- data/lib/autoc/code.rb +0 -237
- data/lib/autoc/collection/hash_map.rb +0 -385
- data/lib/autoc/collection/hash_set.rb +0 -337
- data/lib/autoc/collection/iterator.rb +0 -39
- data/lib/autoc/collection/list.rb +0 -429
- data/lib/autoc/collection/map.rb +0 -41
- data/lib/autoc/collection/queue.rb +0 -517
- data/lib/autoc/collection/set.rb +0 -134
- data/lib/autoc/collection/tree_map.rb +0 -464
- data/lib/autoc/collection/tree_set.rb +0 -611
- data/lib/autoc/collection/vector.rb +0 -336
- data/lib/autoc/string.rb +0 -492
- data/test/test_auto.c +0 -7141
- data/test/test_auto.h +0 -753
- data/test/test_char_string.rb +0 -270
- data/test/test_int_list.rb +0 -35
- data/test/test_int_tree_set.rb +0 -111
- data/test/test_int_vector.rb +0 -34
- data/test/test_value_hash_map.rb +0 -162
- data/test/test_value_hash_set.rb +0 -173
- data/test/test_value_list.rb +0 -193
- data/test/test_value_queue.rb +0 -275
- data/test/test_value_tree_map.rb +0 -176
- data/test/test_value_tree_set.rb +0 -173
- data/test/test_value_vector.rb +0 -155
- data/test/value.rb +0 -80
@@ -1,611 +0,0 @@
|
|
1
|
-
require "autoc/collection"
|
2
|
-
|
3
|
-
|
4
|
-
require "autoc/collection/set"
|
5
|
-
require "autoc/collection/iterator"
|
6
|
-
|
7
|
-
|
8
|
-
module AutoC
|
9
|
-
|
10
|
-
|
11
|
-
=begin
|
12
|
-
|
13
|
-
TreeSet< *_E_* > is a sorted container holding unique elements.
|
14
|
-
|
15
|
-
The TreeSet implements the Red-Black Tree algorithm.
|
16
|
-
|
17
|
-
This code is an adaptation of the rbtree code from the http://www.nlnetlabs.nl/projects/ldns[NLNetLabs LDNS] project.
|
18
|
-
|
19
|
-
The collection's C++ counterpart is +std::set<>+ template class.
|
20
|
-
|
21
|
-
== Generated C interface
|
22
|
-
|
23
|
-
=== Collection management
|
24
|
-
|
25
|
-
[cols=2*]
|
26
|
-
|===
|
27
|
-
|*_void_* ~type~Copy(*_Type_* * +dst+, *_Type_* * +src+)
|
28
|
-
|
|
29
|
-
Create a new set +dst+ filled with the contents of +src+.
|
30
|
-
A copy operation is performed on every element in +src+.
|
31
|
-
|
32
|
-
NOTE: Previous contents of +dst+ is overwritten.
|
33
|
-
|
34
|
-
|*_void_* ~type~Ctor(*_Type_* * +self+)
|
35
|
-
|
|
36
|
-
Create a new empty set +self+.
|
37
|
-
|
38
|
-
NOTE: Previous contents of +self+ is overwritten.
|
39
|
-
|
40
|
-
|*_void_* ~type~Dtor(*_Type_* * +self+)
|
41
|
-
|
|
42
|
-
Destroy set +self+.
|
43
|
-
Stored elements are destroyed as well by calling the respective destructors.
|
44
|
-
|
45
|
-
|*_int_* ~type~Equal(*_Type_* * +lt+, *_Type_* * +rt+)
|
46
|
-
|
|
47
|
-
Return non-zero value if sets +lt+ and +rt+ are considered equal by contents and zero value otherwise.
|
48
|
-
|
49
|
-
|*_size_t_* ~type~Identify(*_Type_* * +self+)
|
50
|
-
|
|
51
|
-
Return hash code for set +self+.
|
52
|
-
|===
|
53
|
-
|
54
|
-
=== Basic operations
|
55
|
-
|
56
|
-
[cols=2*]
|
57
|
-
|===
|
58
|
-
|*_int_* ~type~Contains(*_Type_* * +self+, *_E_* +what+)
|
59
|
-
|
|
60
|
-
Return non-zero value if set +self+ contains an element considered equal to the element +what+ and zero value otherwise.
|
61
|
-
|
62
|
-
|*_int_* ~type~Empty(*_Type_* * +self+)
|
63
|
-
|
|
64
|
-
Return non-zero value if set +self+ contains no elements and zero value otherwise.
|
65
|
-
|
66
|
-
|*_E_* ~type~Get(*_Type_* * +self+, *_E_* +what+)
|
67
|
-
|
|
68
|
-
Return a _copy_ of the element in +self+ considered equal to the element +what+.
|
69
|
-
|
70
|
-
WARNING: +self+ *must* contain such element otherwise the behavior is undefined. See ~type~Contains().
|
71
|
-
|
72
|
-
|*_E_* ~type~PeekLowest(*_Type_* * +self+)
|
73
|
-
|
|
74
|
-
Return a _copy_ of the lowest element in +self+.
|
75
|
-
|
76
|
-
WARNING: +self+ *must not* be empty otherwise the behavior is undefined. See ~type~Empty().
|
77
|
-
|
78
|
-
|*_E_* ~type~PeekHighest(*_Type_* * +self+)
|
79
|
-
|
|
80
|
-
Return a _copy_ of the highest element in +self+.
|
81
|
-
|
82
|
-
WARNING: +self+ *must not* be empty otherwise the behavior is undefined. See ~type~Empty().
|
83
|
-
|
84
|
-
|*_void_* ~type~Purge(*_Type_* * +self+)
|
85
|
-
|
|
86
|
-
Remove and destroy all elements stored in +self+.
|
87
|
-
|
88
|
-
|*_int_* ~type~Put(*_Type_* * +self+, *_E_* +what+)
|
89
|
-
|
|
90
|
-
Put a _copy_ of the element +what+ into +self+ *only if* there is no such element in +self+ which is considered equal to +what+.
|
91
|
-
|
92
|
-
Return non-zero value on successful element put (that is there was not such element in +self+) and zero value otherwise.
|
93
|
-
|
94
|
-
|*_int_* ~type~Replace(*_Type_* * +self+, *_E_* +with+)
|
95
|
-
|
|
96
|
-
If +self+ contains an element which is considered equal to the element +with+,
|
97
|
-
replace that element with a _copy_ of +with+, otherwise do nothing.
|
98
|
-
Replaced element is destroyed.
|
99
|
-
|
100
|
-
Return non-zero value if the replacement was actually performed and zero value otherwise.
|
101
|
-
|
102
|
-
|*_int_* ~type~Remove(*_Type_* * +self+, *_E_* +what+)
|
103
|
-
|
|
104
|
-
Remove and destroy an element in +self+ which is considered equal to the element +what+.
|
105
|
-
|
106
|
-
Return non-zero value on successful element removal and zero value otherwise.
|
107
|
-
|
108
|
-
|*_size_t_* ~type~Size(*_Type_* * +self+)
|
109
|
-
|
|
110
|
-
Return number of elements stored in +self+.
|
111
|
-
|===
|
112
|
-
|
113
|
-
=== Logical operations
|
114
|
-
|
115
|
-
[cols=2*]
|
116
|
-
|===
|
117
|
-
|*_void_* ~type~Exclude(*_Type_* * +self+, *_Type_* * +other+)
|
118
|
-
|
|
119
|
-
Perform the difference operation that is +self+ will retain only the elements not contained in +other+.
|
120
|
-
|
121
|
-
Removed elements are destroyed.
|
122
|
-
|*_void_* ~type~Include(*_Type_* * +self+, *_Type_* * +other+)
|
123
|
-
|
|
124
|
-
Perform the union operation that is +self+ will contain the elements from both +self+ and +other+.
|
125
|
-
|
126
|
-
+self+ receives the _copies_ of extra elements in +other+.
|
127
|
-
|
128
|
-
|*_void_* ~type~Invert(*_Type_* * +self+, *_Type_* * +other+)
|
129
|
-
|
|
130
|
-
Perform the symmetric difference operation that is +self+ will retain the elements contained in either +self+ or +other+, but not in both.
|
131
|
-
|
132
|
-
Removed elements are destroyed, extra elements are _copied_.
|
133
|
-
|
134
|
-
|*_void_* ~type~Retain(*_Type_* * +self+, *_Type_* * +other+)
|
135
|
-
|
|
136
|
-
Perform the intersection operation that is +self+ will retain only the elements contained in both +self+ and +other+.
|
137
|
-
|
138
|
-
Removed elements are destroyed.
|
139
|
-
|===
|
140
|
-
|
141
|
-
=== Iteration
|
142
|
-
|
143
|
-
[cols=2*]
|
144
|
-
|===
|
145
|
-
|*_void_* ~it~Ctor(*_IteratorType_* * +it+, *_Type_* * +self+)
|
146
|
-
|
|
147
|
-
Create a new ascending iterator +it+ on tree +self+. See ~it~CtorEx().
|
148
|
-
|
149
|
-
NOTE: Previous contents of +it+ is overwritten.
|
150
|
-
|
151
|
-
|*_void_* ~it~CtorEx(*_IteratorType_* * +it+, *_Type_* * +self+, *_int_* +ascending+)
|
152
|
-
|
|
153
|
-
Create a new iterator +it+ on tree +self+.
|
154
|
-
Non-zero value of +ascending+ specifies an ascending (+lowest to highest element traversal+) iterator, zero value specifies a descending (+highest to lowest element traversal+) iterator.
|
155
|
-
|
156
|
-
NOTE: Previous contents of +it+ is overwritten.
|
157
|
-
|
158
|
-
|*_int_* ~it~Move(*_IteratorType_* * +it+)
|
159
|
-
|
|
160
|
-
Advance iterator position of +it+ *and* return non-zero value if new position is valid and zero value otherwise.
|
161
|
-
|
162
|
-
|*_E_* ~it~Get(*_IteratorType_* * +it+)
|
163
|
-
|
|
164
|
-
Return a _copy_ of current element pointed to by the iterator +it+.
|
165
|
-
|
166
|
-
WARNING: current position *must* be valid otherwise the behavior is undefined. See ~it~Move().
|
167
|
-
|===
|
168
|
-
|
169
|
-
=end
|
170
|
-
class TreeSet < Collection
|
171
|
-
|
172
|
-
include Sets
|
173
|
-
include Iterators::Bidirectional
|
174
|
-
|
175
|
-
def initialize(*args)
|
176
|
-
super
|
177
|
-
key_requirement(element)
|
178
|
-
end
|
179
|
-
|
180
|
-
def write_intf_types(stream)
|
181
|
-
super
|
182
|
-
stream << %$
|
183
|
-
typedef struct #{type} #{type};
|
184
|
-
typedef struct #{node} #{node};
|
185
|
-
typedef struct #{it} #{it};
|
186
|
-
struct #{type} {
|
187
|
-
#{node}* root;
|
188
|
-
size_t size;
|
189
|
-
};
|
190
|
-
struct #{it} {
|
191
|
-
int start, ascending;
|
192
|
-
#{node}* node;
|
193
|
-
};
|
194
|
-
struct #{node} {
|
195
|
-
int color;
|
196
|
-
#{node}* left;
|
197
|
-
#{node}* right;
|
198
|
-
#{node}* parent;
|
199
|
-
#{element.type} element;
|
200
|
-
};
|
201
|
-
$
|
202
|
-
end
|
203
|
-
|
204
|
-
def write_intf_decls(stream, declare, define)
|
205
|
-
super
|
206
|
-
stream << %$
|
207
|
-
#{declare} #{element.type} #{peekLowest}(#{type_ref});
|
208
|
-
#{declare} #{element.type} #{peekHighest}(#{type_ref});
|
209
|
-
$
|
210
|
-
end
|
211
|
-
|
212
|
-
def write_impls(stream, define)
|
213
|
-
super
|
214
|
-
stream << %$
|
215
|
-
#define #{isRed}(x) (x->color)
|
216
|
-
#define #{isBlack}(x) !#{isRed}(x)
|
217
|
-
#define #{setRed}(x) (x->color = 1)
|
218
|
-
#define #{setBlack}(x) (x->color = 0)
|
219
|
-
#define #{compare}(lt, rt) (#{element.equal(:lt, :rt)} ? 0 : (#{element.less(:lt, :rt)} ? -1 : +1))
|
220
|
-
static #{node} #{nullNode} = {0, NULL, NULL, NULL};
|
221
|
-
static #{node}* #{null} = &#{nullNode};
|
222
|
-
static void #{destroyNode}(#{node}* node) {
|
223
|
-
#{assert}(node);
|
224
|
-
#{assert}(node != #{null});
|
225
|
-
#{element.dtor("node->element")};
|
226
|
-
#{free}(node);
|
227
|
-
}
|
228
|
-
#{define} #{ctor.definition} {
|
229
|
-
#{assert}(self);
|
230
|
-
self->size = 0;
|
231
|
-
self->root = #{null};
|
232
|
-
}
|
233
|
-
static void #{destroy}(#{node}* node) {
|
234
|
-
if(node != #{null}) {
|
235
|
-
#{destroy}(node->left);
|
236
|
-
#{destroy}(node->right);
|
237
|
-
#{destroyNode}(node);
|
238
|
-
}
|
239
|
-
}
|
240
|
-
#{define} #{dtor.definition} {
|
241
|
-
#{assert}(self);
|
242
|
-
#{destroy}(self->root); /* FIXME recursive algorithm might be inefficient */
|
243
|
-
}
|
244
|
-
#{define} void #{purge}(#{type_ref} self) {
|
245
|
-
#{assert}(self);
|
246
|
-
#{dtor}(self);
|
247
|
-
#{ctor}(self);
|
248
|
-
}
|
249
|
-
static void #{rotateLeft}(#{type_ref} self, #{node}* node) {
|
250
|
-
#{node}* right = node->right;
|
251
|
-
node->right = right->left;
|
252
|
-
if(right->left != #{null}) right->left->parent = node;
|
253
|
-
right->parent = node->parent;
|
254
|
-
if(node->parent != #{null}) {
|
255
|
-
if(node == node->parent->left) {
|
256
|
-
node->parent->left = right;
|
257
|
-
} else {
|
258
|
-
node->parent->right = right;
|
259
|
-
}
|
260
|
-
} else {
|
261
|
-
self->root = right;
|
262
|
-
}
|
263
|
-
right->left = node;
|
264
|
-
node->parent = right;
|
265
|
-
}
|
266
|
-
static void #{rotateRight}(#{type_ref} self, #{node}* node) {
|
267
|
-
#{node}* left = node->left;
|
268
|
-
node->left = left->right;
|
269
|
-
if(left->right != #{null}) left->right->parent = node;
|
270
|
-
left->parent = node->parent;
|
271
|
-
if(node->parent != #{null}) {
|
272
|
-
if(node == node->parent->right) {
|
273
|
-
node->parent->right = left;
|
274
|
-
} else {
|
275
|
-
node->parent->left = left;
|
276
|
-
}
|
277
|
-
} else {
|
278
|
-
self->root = left;
|
279
|
-
}
|
280
|
-
left->right = node;
|
281
|
-
node->parent = left;
|
282
|
-
}
|
283
|
-
static void #{insertFixup}(#{type_ref} self, #{node}* node) {
|
284
|
-
#{node}* uncle;
|
285
|
-
while(node != self->root && #{isRed}(node->parent)) {
|
286
|
-
if(node->parent == node->parent->parent->left) {
|
287
|
-
uncle = node->parent->parent->right;
|
288
|
-
if(#{isRed}(uncle)) {
|
289
|
-
#{setBlack}(node->parent);
|
290
|
-
#{setBlack}(uncle);
|
291
|
-
#{setRed}(node->parent->parent);
|
292
|
-
node = node->parent->parent;
|
293
|
-
} else {
|
294
|
-
if(node == node->parent->right) {
|
295
|
-
node = node->parent;
|
296
|
-
#{rotateLeft}(self, node);
|
297
|
-
}
|
298
|
-
#{setBlack}(node->parent);
|
299
|
-
#{setRed}(node->parent->parent);
|
300
|
-
#{rotateRight}(self, node->parent->parent);
|
301
|
-
}
|
302
|
-
} else {
|
303
|
-
uncle = node->parent->parent->left;
|
304
|
-
if(#{isRed}(uncle)) {
|
305
|
-
#{setBlack}(node->parent);
|
306
|
-
#{setBlack}(uncle);
|
307
|
-
#{setRed}(node->parent->parent);
|
308
|
-
node = node->parent->parent;
|
309
|
-
} else {
|
310
|
-
if(node == node->parent->left) {
|
311
|
-
node = node->parent;
|
312
|
-
#{rotateRight}(self, node);
|
313
|
-
}
|
314
|
-
#{setBlack}(node->parent);
|
315
|
-
#{setRed}(node->parent->parent);
|
316
|
-
#{rotateLeft}(self, node->parent->parent);
|
317
|
-
}
|
318
|
-
}
|
319
|
-
}
|
320
|
-
#{setBlack}(self->root);
|
321
|
-
}
|
322
|
-
static void #{deleteFixup}(#{type_ref} self, #{node}* child, #{node}* child_parent) {
|
323
|
-
#{node}* sibling;
|
324
|
-
int go_up = 1;
|
325
|
-
if(child_parent->right == child) sibling = child_parent->left; else sibling = child_parent->right;
|
326
|
-
while(go_up) {
|
327
|
-
if(child_parent == #{null}) return;
|
328
|
-
if(#{isRed}(sibling)) {
|
329
|
-
#{setRed}(child_parent);
|
330
|
-
#{setBlack}(sibling);
|
331
|
-
if(child_parent->right == child) #{rotateRight}(self, child_parent); else #{rotateLeft}(self, child_parent);
|
332
|
-
if(child_parent->right == child) sibling = child_parent->left; else sibling = child_parent->right;
|
333
|
-
}
|
334
|
-
if(#{isBlack}(child_parent) && #{isBlack}(sibling) && #{isBlack}(sibling->left) && #{isBlack}(sibling->right)) {
|
335
|
-
if(sibling != #{null}) #{setRed}(sibling);
|
336
|
-
child = child_parent;
|
337
|
-
child_parent = child_parent->parent;
|
338
|
-
if(child_parent->right == child) sibling = child_parent->left; else sibling = child_parent->right;
|
339
|
-
} else go_up = 0;
|
340
|
-
}
|
341
|
-
if(#{isRed}(child_parent) && #{isBlack}(sibling) && #{isBlack}(sibling->left) && #{isBlack}(sibling->right)) {
|
342
|
-
if(sibling != #{null}) #{setRed}(sibling);
|
343
|
-
#{setBlack}(child_parent);
|
344
|
-
return;
|
345
|
-
}
|
346
|
-
if(child_parent->right == child && #{isBlack}(sibling) && #{isRed}(sibling->right) && #{isBlack}(sibling->left)) {
|
347
|
-
#{setRed}(sibling);
|
348
|
-
#{setBlack}(sibling->right);
|
349
|
-
#{rotateLeft}(self, sibling);
|
350
|
-
if(child_parent->right == child) sibling = child_parent->left; else sibling = child_parent->right;
|
351
|
-
} else if(child_parent->left == child && #{isBlack}(sibling) && #{isRed}(sibling->left) && #{isBlack}(sibling->right)) {
|
352
|
-
#{setRed}(sibling);
|
353
|
-
#{setBlack}(sibling->left);
|
354
|
-
#{rotateRight}(self, sibling);
|
355
|
-
if(child_parent->right == child) sibling = child_parent->left; else sibling = child_parent->right;
|
356
|
-
}
|
357
|
-
sibling->color = child_parent->color;
|
358
|
-
#{setBlack}(child_parent);
|
359
|
-
if(child_parent->right == child) {
|
360
|
-
#{setBlack}(sibling->left);
|
361
|
-
#{rotateRight}(self, child_parent);
|
362
|
-
} else {
|
363
|
-
#{setBlack}(sibling->right);
|
364
|
-
#{rotateLeft}(self, child_parent);
|
365
|
-
}
|
366
|
-
}
|
367
|
-
static #{node}* #{findNode}(#{type_ref} self, #{element.type} element) {
|
368
|
-
int r;
|
369
|
-
#{node}* node;
|
370
|
-
#{assert}(self);
|
371
|
-
node = self->root;
|
372
|
-
while(node != #{null}) {
|
373
|
-
if((r = #{compare}(element, node->element)) == 0) {
|
374
|
-
return node;
|
375
|
-
}
|
376
|
-
if(r < 0) {
|
377
|
-
node = node->left;
|
378
|
-
} else {
|
379
|
-
node = node->right;
|
380
|
-
}
|
381
|
-
}
|
382
|
-
return NULL;
|
383
|
-
}
|
384
|
-
#{define} int #{contains}(#{type_ref} self, #{element.type} element) {
|
385
|
-
#{assert}(self);
|
386
|
-
return #{findNode}(self, element) != NULL;
|
387
|
-
}
|
388
|
-
#{define} #{element.type} #{get}(#{type_ref} self, #{element.type} element) {
|
389
|
-
#{node} *node;
|
390
|
-
#{element.type} result;
|
391
|
-
#{assert}(self);
|
392
|
-
#{assert}(#{contains}(self, element));
|
393
|
-
node = #{findNode}(self, element);
|
394
|
-
#{element.copy("result", "node->element")}; /* Here we rely on NULL pointer dereference to manifest the failure! */
|
395
|
-
return result;
|
396
|
-
}
|
397
|
-
#{define} int #{put}(#{type_ref} self, #{element.type} element) {
|
398
|
-
int r;
|
399
|
-
#{node}* data;
|
400
|
-
#{node}* node;
|
401
|
-
#{node}* parent;
|
402
|
-
#{assert}(self);
|
403
|
-
node = self->root;
|
404
|
-
parent = #{null};
|
405
|
-
while(node != #{null}) {
|
406
|
-
if((r = #{compare}(element, node->element)) == 0) {
|
407
|
-
return 0;
|
408
|
-
}
|
409
|
-
parent = node;
|
410
|
-
if (r < 0) {
|
411
|
-
node = node->left;
|
412
|
-
} else {
|
413
|
-
node = node->right;
|
414
|
-
}
|
415
|
-
}
|
416
|
-
data = #{malloc}(sizeof(#{node})); #{assert}(data);
|
417
|
-
#{element.copy("data->element", "element")};
|
418
|
-
data->parent = parent;
|
419
|
-
data->left = data->right = #{null};
|
420
|
-
#{setRed}(data);
|
421
|
-
++self->size;
|
422
|
-
if(parent != #{null}) {
|
423
|
-
if(r < 0) {
|
424
|
-
parent->left = data;
|
425
|
-
} else {
|
426
|
-
parent->right = data;
|
427
|
-
}
|
428
|
-
} else {
|
429
|
-
self->root = data;
|
430
|
-
}
|
431
|
-
#{insertFixup}(self, data);
|
432
|
-
return 1;
|
433
|
-
}
|
434
|
-
#{define} int #{replace}(#{type_ref} self, #{element.type} element) {
|
435
|
-
int removed;
|
436
|
-
#{assert}(self);
|
437
|
-
/* FIXME removing followed by putting might be inefficient */
|
438
|
-
if((removed = #{remove}(self, element))) #{put}(self, element);
|
439
|
-
return removed;
|
440
|
-
}
|
441
|
-
static void #{swapColors}(#{node}* x, #{node}* y) {
|
442
|
-
int t = x->color;
|
443
|
-
#{assert}(x);
|
444
|
-
#{assert}(y);
|
445
|
-
x->color = y->color;
|
446
|
-
y->color = t;
|
447
|
-
}
|
448
|
-
static void #{swapNodes}(#{node}** x, #{node}** y) {
|
449
|
-
#{node}* t = *x; *x = *y; *y = t;
|
450
|
-
}
|
451
|
-
static void #{changeParent}(#{type_ref} self, #{node}* parent, #{node}* old_node, #{node}* new_node) {
|
452
|
-
if(parent == #{null}) {
|
453
|
-
if(self->root == old_node) self->root = new_node;
|
454
|
-
return;
|
455
|
-
}
|
456
|
-
if(parent->left == old_node) parent->left = new_node;
|
457
|
-
if(parent->right == old_node) parent->right = new_node;
|
458
|
-
}
|
459
|
-
static void #{changeChild}(#{node}* child, #{node}* old_node, #{node}* new_node) {
|
460
|
-
if(child == #{null}) return;
|
461
|
-
if(child->parent == old_node) child->parent = new_node;
|
462
|
-
}
|
463
|
-
int #{remove}(#{type_ref} self, #{element.type} element) {
|
464
|
-
#{node}* to_delete;
|
465
|
-
#{node}* child;
|
466
|
-
#{assert}(self);
|
467
|
-
if((to_delete = #{findNode}(self, element)) == NULL) return 0;
|
468
|
-
if(to_delete->left != #{null} && to_delete->right != #{null}) {
|
469
|
-
#{node} *smright = to_delete->right;
|
470
|
-
while(smright->left != #{null}) smright = smright->left;
|
471
|
-
#{swapColors}(to_delete, smright);
|
472
|
-
#{changeParent}(self, to_delete->parent, to_delete, smright);
|
473
|
-
if(to_delete->right != smright) #{changeParent}(self, smright->parent, smright, to_delete);
|
474
|
-
#{changeChild}(smright->left, smright, to_delete);
|
475
|
-
#{changeChild}(smright->left, smright, to_delete);
|
476
|
-
#{changeChild}(smright->right, smright, to_delete);
|
477
|
-
#{changeChild}(smright->right, smright, to_delete);
|
478
|
-
#{changeChild}(to_delete->left, to_delete, smright);
|
479
|
-
if(to_delete->right != smright) #{changeChild}(to_delete->right, to_delete, smright);
|
480
|
-
if(to_delete->right == smright) {
|
481
|
-
to_delete->right = to_delete;
|
482
|
-
smright->parent = smright;
|
483
|
-
}
|
484
|
-
#{swapNodes}(&to_delete->parent, &smright->parent);
|
485
|
-
#{swapNodes}(&to_delete->left, &smright->left);
|
486
|
-
#{swapNodes}(&to_delete->right, &smright->right);
|
487
|
-
}
|
488
|
-
if(to_delete->left != #{null}) child = to_delete->left; else child = to_delete->right;
|
489
|
-
#{changeParent}(self, to_delete->parent, to_delete, child);
|
490
|
-
#{changeChild}(child, to_delete, to_delete->parent);
|
491
|
-
if(#{isRed}(to_delete)) {} else if(#{isRed}(child)) {
|
492
|
-
if(child != #{null}) #{setBlack}(child);
|
493
|
-
} else #{deleteFixup}(self, child, to_delete->parent);
|
494
|
-
#{destroyNode}(to_delete);
|
495
|
-
--self->size;
|
496
|
-
return 1;
|
497
|
-
}
|
498
|
-
static #{node}* #{lowestNode}(#{type_ref} self) {
|
499
|
-
#{node}* node;
|
500
|
-
#{assert}(self);
|
501
|
-
node = self->root;
|
502
|
-
if(self->root != #{null}) {
|
503
|
-
for(node = self->root; node->left != #{null}; node = node->left);
|
504
|
-
}
|
505
|
-
return node;
|
506
|
-
}
|
507
|
-
static #{node}* #{highestNode}(#{type_ref} self) {
|
508
|
-
#{node}* node;
|
509
|
-
#{assert}(self);
|
510
|
-
node = self->root;
|
511
|
-
if(self->root != #{null}) {
|
512
|
-
for(node = self->root; node->right != #{null}; node = node->right);
|
513
|
-
}
|
514
|
-
return node;
|
515
|
-
}
|
516
|
-
static #{node}* #{nextNode}(#{node}* node) {
|
517
|
-
#{node}* parent;
|
518
|
-
#{assert}(node);
|
519
|
-
if(node->right != #{null}) {
|
520
|
-
for(node = node->right;
|
521
|
-
node->left != #{null};
|
522
|
-
node = node->left);
|
523
|
-
} else {
|
524
|
-
parent = node->parent;
|
525
|
-
while(parent != #{null} && node == parent->right) {
|
526
|
-
node = parent;
|
527
|
-
parent = parent->parent;
|
528
|
-
}
|
529
|
-
node = parent;
|
530
|
-
}
|
531
|
-
return node;
|
532
|
-
}
|
533
|
-
static #{node}* #{prevNode}(#{node}* node) {
|
534
|
-
#{node}* parent;
|
535
|
-
#{assert}(node);
|
536
|
-
if(node->left != #{null}) {
|
537
|
-
for(node = node->left;
|
538
|
-
node->right != #{null};
|
539
|
-
node = node->right);
|
540
|
-
} else {
|
541
|
-
parent = node->parent;
|
542
|
-
while(parent != #{null} && node == parent->left) {
|
543
|
-
node = parent;
|
544
|
-
parent = parent->parent;
|
545
|
-
}
|
546
|
-
node = parent;
|
547
|
-
}
|
548
|
-
return node;
|
549
|
-
}
|
550
|
-
#{define} #{element.type} #{peekLowest}(#{type_ref} self) {
|
551
|
-
#{node}* node;
|
552
|
-
#{element.type} result;
|
553
|
-
#{assert}(self);
|
554
|
-
#{assert}(!#{empty}(self));
|
555
|
-
node = #{lowestNode}(self);
|
556
|
-
#{assert}(node);
|
557
|
-
#{assert}(node != #{null});
|
558
|
-
#{element.copy("result", "node->element")};
|
559
|
-
return result;
|
560
|
-
}
|
561
|
-
#{define} #{element.type} #{peekHighest}(#{type_ref} self) {
|
562
|
-
#{node}* node;
|
563
|
-
#{element.type} result;
|
564
|
-
#{assert}(self);
|
565
|
-
#{assert}(!#{empty}(self));
|
566
|
-
node = #{highestNode}(self);
|
567
|
-
#{assert}(node);
|
568
|
-
#{assert}(node != #{null});
|
569
|
-
#{element.copy("result", "node->element")};
|
570
|
-
return result;
|
571
|
-
}
|
572
|
-
#{define} void #{itCtorEx}(#{it_ref} self, #{type_ref} tree, int ascending) {
|
573
|
-
#{assert}(self);
|
574
|
-
#{assert}(tree);
|
575
|
-
self->node = (self->ascending = ascending) ? #{lowestNode}(tree) : #{highestNode}(tree);
|
576
|
-
self->start = 1;
|
577
|
-
}
|
578
|
-
#{define} int #{itMove}(#{it_ref} self) {
|
579
|
-
#{assert}(self);
|
580
|
-
if(self->start) {
|
581
|
-
self->start = 0;
|
582
|
-
} else {
|
583
|
-
self->node = self->ascending ? #{nextNode}(self->node) : #{prevNode}(self->node);
|
584
|
-
}
|
585
|
-
return self->node != #{null};
|
586
|
-
}
|
587
|
-
static #{element.type_ref} #{itGetRef}(#{it_ref} self) {
|
588
|
-
#{assert}(self);
|
589
|
-
#{assert}(self->node);
|
590
|
-
#{assert}(self->node != #{null});
|
591
|
-
return &self->node->element;
|
592
|
-
}
|
593
|
-
#{define} #{element.type} #{itGet}(#{it_ref} self) {
|
594
|
-
#{element.type} result;
|
595
|
-
#{assert}(self);
|
596
|
-
#{element.copy("result", "*#{itGetRef}(self)")};
|
597
|
-
return result;
|
598
|
-
}
|
599
|
-
$
|
600
|
-
end
|
601
|
-
|
602
|
-
private
|
603
|
-
|
604
|
-
def key_requirement(obj)
|
605
|
-
element_requirement(obj)
|
606
|
-
raise "type #{obj.type} (#{obj}) must be sortable" unless obj.sortable?
|
607
|
-
end
|
608
|
-
|
609
|
-
end # TreeSet
|
610
|
-
|
611
|
-
end # AutoC
|