1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
|
/* IBM_PROLOG_BEGIN_TAG */
/* This is an automatically generated prolog. */
/* */
/* $Source: src/lib/splaytree.C $ */
/* */
/* OpenPOWER HostBoot Project */
/* */
/* COPYRIGHT International Business Machines Corp. 2012,2014 */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); */
/* you may not use this file except in compliance with the License. */
/* You may obtain a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or */
/* implied. See the License for the specific language governing */
/* permissions and limitations under the License. */
/* */
/* IBM_PROLOG_END_TAG */
#include <util/impl/splaytree.H>
#include <builtins.h>
/** @file splaytree.C
* Implementation of the Util::__Util_SplayTree_Impl::SplayTree class.
*/
namespace Util
{
namespace __Util_SplayTree_Impl
{
SplayTree::SplayTree(SplayTree::comparator comp,
SplayTree::copier copy,
SplayTree::deleter del) :
compare_functor(comp),
copy_functor(copy),
delete_functor(del)
{
}
SplayTree::SplayTree(const SplayTree& t)
{
(*this) = t;
}
SplayTree::~SplayTree()
{
this->clear();
}
SplayTree& SplayTree::operator=(const SplayTree& t)
{
this->clear();
compare_functor = t.compare_functor;
copy_functor = t.copy_functor;
delete_functor = t.delete_functor;
insert_range(t.front(), NULL);
if (likely(NULL != header.child[LEFT]))
{
splay(header.child[LEFT]);
}
return *this;
}
void SplayTree::insert(node* n)
{
if (unlikely(header.parent == NULL)) // First element.
{
header.parent = header.child[LEFT] = header.child[RIGHT] = n;
n->parent = n->child[LEFT] = n->child[RIGHT] = NULL;
}
else // Not first element.
{
// Find place to insert node and insert it.
node* insert_location = header.parent;
__find(insert_location, n);
__insert(insert_location, n);
// Fix up header nodes.
header.child[LEFT] = leftmost(header.child[LEFT]);
header.child[RIGHT] = rightmost(header.child[RIGHT]);
// Splay new node.
splay(n);
}
// Increment size count.
(header_n()->data)++;
}
void SplayTree::insert_range(const node* n1, const node* n2)
{
while(n1 != n2)
{
insert(copy_functor(n1));
n1 = successor(n1);
}
}
void SplayTree::remove(node* n)
{
// Fix up left-most and right-most node pointers if deleting
// them. We'll fix up the root in the node removal itself.
if (unlikely(header.child[LEFT] == n))
{
header.child[LEFT] = successor(n);
}
if (unlikely(header.child[RIGHT] == n))
{
header.child[RIGHT] = predecessor(n);
}
// Decrement size count.
(header_n()->data)--;
// Find node to splice out of the tree.
// If n has one or no child, splice itself out, otherwise the
// successor.
node* y = ((!n->child[LEFT]) || (!n->child[RIGHT])) ?
n : successor(n);
// Find the subtree of y and link it with y's parent.
node* x = y->child[LEFT] ? y->child[LEFT] : y->child[RIGHT];
if (likely(NULL != x))
{
x->parent = y->parent;
}
if (unlikely(!y->parent))
{
// Fix root.
header.parent = x;
}
else
{
y->parent->child[direction(y->parent, y)] = x;
}
// Replace n with y.
if (likely(y != n))
{
y->parent = n->parent;
if (y->parent)
{
y->parent->child[direction(y->parent, n)] = y;
}
else
{
// Removing root, so update header.
header.parent = y;
}
y->child[LEFT] = n->child[LEFT];
if (y->child[LEFT])
{
y->child[LEFT]->parent = y;
}
y->child[RIGHT] = n->child[RIGHT];
if (y->child[RIGHT])
{
y->child[RIGHT]->parent = y;
}
// Splay y up to the root.
splay(y);
}
}
bool SplayTree::find_hint(node* n, node*& hint) const
{
if (unlikely(NULL == header.parent))
{
return false;
}
hint = header.parent;
bool found = __find(hint, n);
// Splay hint up the tree to make future searches quicker.
if (likely(NULL != hint))
{
splay(hint);
}
return found;
}
void SplayTree::clear()
{
node* n = front();
while(n)
{
node* temp = n;
n = successor(n);
remove(temp);
delete_functor(temp);
}
}
void SplayTree::swap(SplayTree& tree)
{
node temp(header);
header = tree.header;
tree.header = temp;
}
bool SplayTree::insert_by_value(const void** v, node*& n)
{
n = find_by_value(v);
if (unlikely(NULL != n))
{
return false;
}
else
{
n = copy_functor(node::convertToNode(v));
insert(n);
return true;
}
}
size_t SplayTree::remove_by_value(const void** v)
{
if (unlikely(NULL == header.parent))
{
return 0;
}
node* v_node = header.parent;
if (likely(__find(v_node, node::convertToNode(v))))
{
remove(v_node);
delete_functor(v_node);
return 1;
}
return 0;
}
bool SplayTree::find_hint_by_value(const void** v, node*& hint) const
{
return find_hint(node::convertToNode(v), hint);
}
SplayTree::node* SplayTree::find_by_value(const void** v) const
{
node* n = NULL;
if (find_hint_by_value(v, n))
{
return n;
}
else
{
return NULL;
}
}
SplayTree::node* SplayTree::lower_bound_by_value(const void** v) const
{
node* n = NULL;
node* v_n = node::convertToNode(v);
if (find_hint(v_n, n) || (NULL == n))
{
return n;
}
else
{
if (-1 == compare_functor(this, n, v_n))
{
return successor(n);
}
else
{
return n;
}
}
}
SplayTree::node* SplayTree::upper_bound_by_value(const void** v) const
{
node* n = NULL;
node* v_n = node::convertToNode(v);
if (find_hint(v_n, n))
{
return successor(n);
}
else if (NULL == n)
{
return NULL;
}
else
{
if (-1 == compare_functor(this, n, v_n))
{
return successor(n);
}
else
{
return n;
}
}
}
const SplayTree::node* SplayTree::predecessor(const node* n) const
{
// If left child, predecessor is just the largest of the left
// subtree.
if (likely(NULL != n->child[LEFT]))
{
return rightmost(n->child[LEFT]);
}
// Else, need to work up the tree to find predecessor.
const node* y = n->parent;
while ((NULL != y) && (n == y->child[LEFT]))
{
n = y;
y = y->parent;
}
return y;
}
const SplayTree::node* SplayTree::successor(const node* n) const
{
// If right child, predecessor is just the smallest of the right
// subtree.
if (likely(NULL != n->child[RIGHT]))
{
return leftmost(n->child[RIGHT]);
}
// Else, need to work up the tree to find successor.
const node* y = n->parent;
while ((NULL != y) && (n == y->child[RIGHT]))
{
n = y;
y = y->parent;
}
return y;
}
void SplayTree::rotate(node* n, DIRECTION d) const
{
// Get parent node.
node* p = n->parent;
// Link n's d-subtree into p.
p->child[!d] = n->child[d];
if (likely(NULL != n->child[d]))
{
n->child[d]->parent = p;
}
// Link p's parent to n.
n->parent = p->parent;
if (unlikely(!p->parent))
{
header.parent = n;
}
else
{
p->parent->child[direction(p->parent, p)] = n;
}
// Put p onto d-subtree of n.
p->parent = n;
n->child[d] = p;
}
void SplayTree::splay(node* n) const
{
// There are three splay operations. "zig", "zig-zig" and
// "zig-zag" based on the shape of the links between child, parent
// and grand-parent.
if (unlikely(!n->parent)) // This is the root node already.
{
return;
}
if (unlikely(!n->parent->parent)) // "zig" since no grand-parent.
{
// Rotate n into parent's location.
rotate(n, !direction(n->parent, n));
}
else if (direction(n->parent, n) ==
direction(n->parent->parent, n->parent)) // "zig-zig"
{
// Rotate parent into grand-parent first, then rotate
// n into parent.
rotate(n->parent, !direction(n->parent->parent, n->parent));
rotate(n, !direction(n->parent, n));
// Continue splay.
splay(n);
}
else // "zig-zag"
{
// Rotate n into parent, then n into grand-parent.
rotate(n, !direction(n->parent, n));
// Note: grand-parent is now parent due to first rotate.
rotate(n, !direction(n->parent, n));
// Continue splay.
splay(n);
}
}
void SplayTree::__insert(node* t, node* n)
{
node*& child = (0 > (*compare_functor)(this, n, t)) ?
t->child[LEFT] : t->child[RIGHT];
if (likely(NULL == child)) // Assumption is the subtree hint is
// correct, so this should be NULL.
{
// Link node into "child" slot.
child = n;
n->parent = t;
}
else
{
// Subtree hint was wrong, recurse tree.
__insert(n, child);
}
}
bool SplayTree::__find(node*& t, node* n) const
{
int compare = (*compare_functor)(this, n, t);
if (unlikely(compare == 0)) // Node matches, return true.
{
return true;
}
node* child = (0 > compare) ? t->child[LEFT] : t->child[RIGHT];
if (unlikely(NULL == child)) // No more children, no match.
{
return false;
}
else // Recurse to child.
{
t = child;
return __find(t, n);
}
}
void Iterator::increment()
{
if (NULL == node)
{
node = tree->front(); // This causes "begin() == ++end()" but
// is necessary so that --rend() becomes
// a reverse iterator to begin().
}
else
{
node = tree->successor(node);
}
};
void Iterator::decrement()
{
if (NULL == node)
{
node = tree->back();
}
else
{
node = tree->predecessor(node);
}
}
void ConstIterator::increment()
{
if (NULL == node)
{
node = tree->front(); // This causes "begin() == ++end()" but
// is necessary so that --rend() becomes
// a reverse iterator to begin().
}
else
{
node = tree->successor(node);
}
}
void ConstIterator::decrement()
{
if (NULL == node)
{
node = tree->back();
}
else
{
node = tree->predecessor(node);
}
}
};
};
|