18 #define DBTUX_TREE_CPP
26 Dbtux::treeAdd(TuxCtx& ctx, Frag& frag, TreePos treePos, TreeEnt ent)
28 TreeHead& tree = frag.m_tree;
29 NodeHandle node(frag);
31 if (treePos.m_loc != NullTupLoc) {
33 thrjam(ctx.jamBuffer);
34 selectNode(node, treePos.m_loc);
35 unsigned pos = treePos.m_pos;
36 if (node.getOccup() < tree.m_maxOccup) {
38 thrjam(ctx.jamBuffer);
39 nodePushUp(ctx, node, pos, ent, RNIL);
42 treeAddFull(ctx, frag, node, pos, ent);
45 thrjam(ctx.jamBuffer);
47 nodePushUp(ctx, node, 0, ent, RNIL);
49 tree.m_root = node.m_loc;
60 Dbtux::treeAddFull(TuxCtx& ctx, Frag& frag, NodeHandle lubNode,
unsigned pos, TreeEnt ent)
62 TreeHead& tree = frag.m_tree;
63 TupLoc loc = lubNode.getLink(0);
64 if (loc != NullTupLoc) {
66 NodeHandle glbNode(frag);
68 thrjam(ctx.jamBuffer);
69 selectNode(glbNode, loc);
70 loc = glbNode.getLink(1);
71 }
while (loc != NullTupLoc);
72 if (glbNode.getOccup() < tree.m_maxOccup) {
74 thrjam(ctx.jamBuffer);
75 Uint32 scanList = RNIL;
77 thrjam(ctx.jamBuffer);
79 nodePushDown(ctx, lubNode, pos - 1, ent, scanList);
82 nodePushUp(ctx, glbNode, glbNode.getOccup(), ent, scanList);
85 treeAddNode(ctx, frag, lubNode, pos, ent, glbNode, 1);
88 treeAddNode(ctx, frag, lubNode, pos, ent, lubNode, 0);
97 Dbtux::treeAddNode(TuxCtx& ctx,
98 Frag& frag, NodeHandle lubNode,
unsigned pos, TreeEnt ent, NodeHandle parentNode,
unsigned i)
100 NodeHandle glbNode(frag);
103 parentNode.setLink(i, glbNode.m_loc);
104 glbNode.setLink(2, parentNode.m_loc);
106 Uint32 scanList = RNIL;
108 thrjam(ctx.jamBuffer);
110 nodePushDown(ctx, lubNode, pos - 1, ent, scanList);
113 nodePushUp(ctx, glbNode, 0, ent, scanList);
115 treeAddRebalance(ctx, frag, parentNode, i);
123 Dbtux::treeAddRebalance(TuxCtx & ctx, Frag& frag, NodeHandle node,
unsigned i)
127 int j = (i == 0 ? -1 : +1);
128 int b = node.getBalance();
131 thrjam(ctx.jamBuffer);
134 }
else if (b == -j) {
136 thrjam(ctx.jamBuffer);
142 thrjam(ctx.jamBuffer);
143 NodeHandle childNode(frag);
144 selectNode(childNode, node.getLink(i));
145 int b2 = childNode.getBalance();
147 thrjam(ctx.jamBuffer);
148 treeRotateSingle(ctx, frag, node, i);
149 }
else if (b2 == -b) {
150 thrjam(ctx.jamBuffer);
151 treeRotateDouble(ctx, frag, node, i);
161 TupLoc parentLoc = node.getLink(2);
162 if (parentLoc == NullTupLoc) {
163 thrjam(ctx.jamBuffer);
168 selectNode(node, parentLoc);
179 Dbtux::treeRemove(Frag& frag, TreePos treePos)
181 TreeHead& tree = frag.m_tree;
182 unsigned pos = treePos.m_pos;
183 NodeHandle node(frag);
184 selectNode(node, treePos.m_loc);
187 if (node.getOccup() > tree.m_minOccup) {
190 nodePopDown(c_ctx, node, pos, ent, 0);
193 if (node.getChilds() == 2) {
196 treeRemoveInner(frag, node, pos);
200 nodePopDown(c_ctx, node, pos, ent, 0);
201 if (node.getLink(0) != NullTupLoc) {
203 treeRemoveSemi(frag, node, 0);
206 if (node.getLink(1) != NullTupLoc) {
208 treeRemoveSemi(frag, node, 1);
211 treeRemoveLeaf(frag, node);
222 Dbtux::treeRemoveInner(Frag& frag, NodeHandle lubNode,
unsigned pos)
226 NodeHandle glbNode(frag);
227 TupLoc loc = lubNode.getLink(0);
230 selectNode(glbNode, loc);
231 loc = glbNode.getLink(1);
232 }
while (loc != NullTupLoc);
234 Uint32 scanList = RNIL;
235 nodePopDown(c_ctx, glbNode, glbNode.getOccup() - 1, ent, &scanList);
239 nodePopUp(c_ctx, lubNode, pos, ent, scanList);
240 if (glbNode.getLink(0) != NullTupLoc) {
242 treeRemoveSemi(frag, glbNode, 0);
245 treeRemoveLeaf(frag, glbNode);
254 Dbtux::treeRemoveSemi(Frag& frag, NodeHandle semiNode,
unsigned i)
256 TreeHead& tree = frag.m_tree;
257 ndbrequire(semiNode.getChilds() < 2);
258 TupLoc leafLoc = semiNode.getLink(i);
259 NodeHandle leafNode(frag);
260 selectNode(leafNode, leafLoc);
261 if (semiNode.getOccup() < tree.m_minOccup) {
263 unsigned cnt = min(leafNode.getOccup(), tree.m_minOccup - semiNode.getOccup());
264 nodeSlide(c_ctx, semiNode, leafNode, cnt, i);
265 if (leafNode.getOccup() == 0) {
268 treeRemoveNode(frag, leafNode);
279 Dbtux::treeRemoveLeaf(Frag& frag, NodeHandle leafNode)
281 TreeHead& tree = frag.m_tree;
282 TupLoc parentLoc = leafNode.getLink(2);
283 if (parentLoc != NullTupLoc) {
285 NodeHandle parentNode(frag);
286 selectNode(parentNode, parentLoc);
287 unsigned i = leafNode.getSide();
288 if (parentNode.getLink(1 - i) == NullTupLoc) {
291 if (parentNode.getOccup() < tree.m_minOccup) {
293 unsigned cnt = min(leafNode.getOccup(), tree.m_minOccup - parentNode.getOccup());
294 nodeSlide(c_ctx, parentNode, leafNode, cnt, i);
298 if (leafNode.getOccup() == 0) {
301 treeRemoveNode(frag, leafNode);
309 Dbtux::treeRemoveNode(Frag& frag, NodeHandle leafNode)
311 TreeHead& tree = frag.m_tree;
312 ndbrequire(leafNode.getChilds() == 0);
313 TupLoc parentLoc = leafNode.getLink(2);
314 unsigned i = leafNode.getSide();
315 deleteNode(leafNode);
316 if (parentLoc != NullTupLoc) {
318 NodeHandle parentNode(frag);
319 selectNode(parentNode, parentLoc);
320 parentNode.setLink(i, NullTupLoc);
322 treeRemoveRebalance(frag, parentNode, i);
326 tree.m_root = NullTupLoc;
328 freePreallocatedNode(frag);
336 Dbtux::treeRemoveRebalance(Frag& frag, NodeHandle node,
unsigned i)
340 int j = (i == 0 ? -1 : +1);
341 int b = node.getBalance();
353 }
else if (b == -j) {
357 NodeHandle childNode(frag);
358 selectNode(childNode, node.getLink(1 - i));
359 int b2 = childNode.getBalance();
362 treeRotateSingle(c_ctx, frag, node, 1 - i);
364 }
else if (b2 == -b) {
366 treeRotateDouble(c_ctx, frag, node, 1 - i);
370 treeRotateSingle(c_ctx, frag, node, 1 - i);
377 TupLoc parentLoc = node.getLink(2);
378 if (parentLoc == NullTupLoc) {
384 selectNode(node, parentLoc);
405 Dbtux::treeRotateSingle(TuxCtx& ctx, Frag& frag, NodeHandle& node,
unsigned i)
413 NodeHandle node5 = node;
414 const TupLoc loc5 = node5.m_loc;
415 const int bal5 = node5.getBalance();
416 const int side5 = node5.getSide();
417 ndbrequire(bal5 + (1 - i) == i);
423 TupLoc loc3 = node5.getLink(i);
424 NodeHandle node3(frag);
425 selectNode(node3, loc3);
426 const int bal3 = node3.getBalance();
431 ndbrequire(node3.getLink(i) != NullTupLoc);
438 TupLoc loc4 = node3.getLink(1 - i);
439 NodeHandle node4(frag);
440 if (loc4 != NullTupLoc) {
441 thrjam(ctx.jamBuffer);
442 selectNode(node4, loc4);
443 ndbrequire(node4.getSide() == (1 -
i) &&
444 node4.getLink(2) == loc3);
446 node4.setLink(2, loc5);
452 TupLoc loc0 = node5.getLink(2);
466 ndbrequire(node3.getLink(2) == loc5);
467 ndbrequire(node3.getSide() ==
i);
468 node3.setLink(1 - i, loc5);
469 node3.setLink(2, loc0);
470 node3.setSide(side5);
471 node5.setLink(i, loc4);
472 node5.setLink(2, loc3);
473 node5.setSide(1 - i);
474 if (loc0 != NullTupLoc) {
475 thrjam(ctx.jamBuffer);
476 NodeHandle node0(frag);
477 selectNode(node0, loc0);
478 node0.setLink(side5, loc3);
480 thrjam(ctx.jamBuffer);
481 frag.m_tree.m_root = loc3;
495 thrjam(ctx.jamBuffer);
498 }
else if (bal3 == 0) {
499 thrjam(ctx.jamBuffer);
500 node3.setBalance(-bal5);
501 node5.setBalance(bal5);
614 Dbtux::treeRotateDouble(TuxCtx& ctx, Frag& frag, NodeHandle& node,
unsigned i)
616 TreeHead& tree = frag.m_tree;
619 NodeHandle node6 = node;
620 const TupLoc loc6 = node6.m_loc;
622 const int bal6 = node6.getBalance();
623 const unsigned side6 = node6.getSide();
626 TupLoc loc2 = node6.getLink(i);
627 NodeHandle node2(frag);
628 selectNode(node2, loc2);
629 const int bal2 = node2.getBalance();
632 TupLoc loc4 = node2.getLink(1 - i);
633 NodeHandle node4(frag);
634 selectNode(node4, loc4);
635 const int bal4 = node4.getBalance();
638 ndbrequire(bal6 + (1 - i) == i);
639 ndbrequire(bal2 == -bal6);
640 ndbrequire(node2.getLink(2) == loc6);
641 ndbrequire(node2.getSide() ==
i);
642 ndbrequire(node4.getLink(2) == loc2);
645 TupLoc loc3 = node4.getLink(i);
646 TupLoc loc5 = node4.getLink(1 - i);
649 if (loc3 == NullTupLoc && loc5 == NullTupLoc) {
650 thrjam(ctx.jamBuffer);
651 if (node4.getOccup() < tree.m_minOccup) {
652 thrjam(ctx.jamBuffer);
653 unsigned cnt = tree.m_minOccup - node4.getOccup();
654 ndbrequire(cnt < node2.getOccup());
655 nodeSlide(ctx, node4, node2, cnt, i);
656 ndbrequire(node4.getOccup() >= tree.m_minOccup);
657 ndbrequire(node2.getOccup() != 0);
660 if (loc3 != NullTupLoc) {
661 thrjam(ctx.jamBuffer);
662 NodeHandle node3(frag);
663 selectNode(node3, loc3);
664 node3.setLink(2, loc2);
665 node3.setSide(1 - i);
667 if (loc5 != NullTupLoc) {
668 thrjam(ctx.jamBuffer);
669 NodeHandle node5(frag);
670 selectNode(node5, loc5);
671 node5.setLink(2, node6.m_loc);
676 TupLoc loc0 = node6.getLink(2);
677 NodeHandle node0(frag);
679 node6.setLink(i, loc5);
680 node6.setLink(2, loc4);
681 node6.setSide(1 - i);
683 node2.setLink(1 - i, loc3);
684 node2.setLink(2, loc4);
686 node4.setLink(i, loc2);
687 node4.setLink(1 - i, loc6);
688 node4.setLink(2, loc0);
689 node4.setSide(side6);
691 if (loc0 != NullTupLoc) {
692 thrjam(ctx.jamBuffer);
693 selectNode(node0, loc0);
694 node0.setLink(side6, loc4);
696 thrjam(ctx.jamBuffer);
697 frag.m_tree.m_root = loc4;
702 thrjam(ctx.jamBuffer);
705 }
else if (bal4 == -bal2) {
706 thrjam(ctx.jamBuffer);
708 node6.setBalance(bal2);
709 }
else if (bal4 == bal2) {
710 thrjam(ctx.jamBuffer);
711 node2.setBalance(-bal2);