-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBtreePA.java
3514 lines (3181 loc) · 203 KB
/
BtreePA.java
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//------------------------------------------------------------------------------
// BtreeSA in pseudo assembler
// Philip R Brenan at appaapps dot com, Appa Apps Ltd Inc., 2024
//------------------------------------------------------------------------------
package com.AppaApps.Silicon; // Btree in a block on the surface of a silicon chip.
// Copy entire stuck in one go rather than keys and data separately
import java.util.*;
import java.nio.file.*;
abstract class BtreePA extends Test // Manipulate a btree using static methods and memory
{final MemoryLayoutPA M; // The memory layout of the btree
final MemoryLayoutPA T; // The memory used to hold temporary variable used during a transaction on the btree
final ProgramPA P = new ProgramPA(); // Program in which to generate instructions
final boolean Assert = false; // Execute asserts if true
final boolean Halt = false; // Execute tests that result in a halt
abstract int maxSize(); // The maximum number of leaves plus branches in the bree
abstract int bitsPerKey(); // The number of bits per key
abstract int bitsPerData(); // The number of bits per data
abstract int maxKeysPerLeaf(); // Maximum number of leafs in a key
abstract int maxKeysPerBranch(); // Maximum number of keys in a branch
final int splitLeafSize; // The number of key, data pairs to split out of a leaf
final int splitBranchSize; // The number of key, next pairs to split out of a branch
final int bitsPerAddress; // The number of bits required to address a bit in memory
final int bitsPerNext; // The number of bits in a next field
final int bitsPerSize; // The number of bits in stuck size field
Layout.Field leaf; // Layout of a leaf in the memory used by btree
Layout.Field branch; // Layout of a branch in the memory used by btree
Layout.Union branchOrLeaf; // Layout of either a leaf or a branch in the memory used by btree
Layout.Bit isLeaf; // Whether the current node is a leaf or a branch
Layout.Variable free; // Free list chain
Layout.Structure Node; // Layout of a node in the memory used by btree
Layout.Array nodes; // Layout of an array of nodes in the memory used by btree
Layout.Variable freeList; // Single linked list of nodes that have been freed and so can be reused without fragmenting memory
Layout.Structure bTree; // Btree
final static int
linesToPrintABranch = 4, // The number of lines required to print a branch
maxPrintLevels = 10, // Maximum number of levels to print in a tree
maxDepth = 99, // Maximum depth of any realistic tree
testMaxSize = github_actions ? 1000 : 50; // Maximum number of leaves plus branches during testing
int nodeUsed = 0; // Number of nodes currently in use
int maxNodeUsed = 0; // Maximum number of branches plus leaves used
final int root = 0; // The root of the tree is always node zero
final int Branch_Size = 0; // Get the size of a stuck
final int Branch_Leaf = 0; // Check whether a node has leaves for children
final int Branch_Top = 0; // Get the top element of a branch
final int Branch_FirstBranch = 0; // Locate the first greater or equal key in a branch
final int Branch_T = 1; // Process a parent node
final int Branch_tl = 2; // Process a left node
final int Branch_tr = 3; // Process a right node
final int Branch_length = 4; // Number of transaction types
final int Leaf_Size = 0; // Get the size of a stuck
final int Leaf_Leaf = 0; // Check whether a node has leaves for children
final int Leaf_Equal = 0; // Get the top element of a branch
final int Leaf_FirstLeaf = 0; // Locate the first greater or equal key in a branch
final int Leaf_T = 1; // Process a parent node
final int Leaf_tl = 2; // Process a left node
final int Leaf_tr = 3; // Process a right node
final int Leaf_length = 4; // Number of transaction types
final StuckPA[]branchTransactions; // Transactions to use on branch stucks
final StuckPA[] leafTransactions; // Transactions to use on leaf stucks
final StuckPA bSize; // Branch size
final StuckPA bLeaf; // Check whether a node has leaves for children
final StuckPA bTop; // Get the size of a stuck
final StuckPA bFirstBranch; // Locate the first greater or equal key in a branch
final StuckPA bT; // Process a parent node
final StuckPA bL; // Process a left node
final StuckPA bR; // Process a right node
final StuckPA lSize; // Branch size
final StuckPA lLeaf; // Check whether a node has leaves for children
final StuckPA lEqual; // Locate an equal key
final StuckPA lFirstLeaf; // Locate the first greater or equal key in a leaf
final StuckPA lT; // Process a parent node as a leaf
final StuckPA lL; // Process a left node
final StuckPA lR; // Process a right node
boolean debug = false; // Debugging enabled
//D1 Construction // Create a Btree from nodes which can be branches or leaves. The data associated with the BTree is stored only in the leaves opposite the keys
BtreePA() // Define a Btree with user specified dimensions
{z();
splitLeafSize = maxKeysPerLeaf() >> 1; // The number of key, data pairs to split out of a leaf
splitBranchSize = maxKeysPerBranch() >> 1; // The number of key, next pairs to split out of a branch
bitsPerNext = logTwo(maxSize()); // The number of bits in a next field sufficient to index any node
bitsPerSize = logTwo(max(bitsPerKey(), bitsPerData())+1); // The number of bits in stuck size field sufficient to index an key or data element including top
M = new MemoryLayoutPA(layout(), "M"); // The memory layout of the btree
bitsPerAddress = logTwo(M.memory().size()); // Number of bits to address any bit in memory
T = new MemoryLayoutPA(transactionLayout(), "T"); // The memory used to hold temporary variable used during a transaction on the btree
M.program(P);
T.at(maxKeysPerLeaf) .setInt(maxKeysPerLeaf());
T.at(maxKeysPerBranch).setInt(maxKeysPerBranch());
T.at(two) .setInt(2);
T.at(MaxDepth) .setInt(maxDepth); // Prevent runaway searches of the btree by limiting the number of levels to be searched
T.program(P);
{final int N = Branch_length; // Preallocate transactions used on branch stucks
branchTransactions = new StuckPA[N];
for (int i = 0; i < N; i++)
{final StuckPA b = branchTransactions[i] = new StuckPA("branch_"+i, M) // Based stucks
{int maxSize() {return BtreePA.this.maxKeysPerBranch()+1;} // Not forgetting top next
int bitsPerKey() {return BtreePA.this.bitsPerKey();}
int bitsPerData() {return BtreePA.this.bitsPerNext;}
int bitsPerSize() {return BtreePA.this.bitsPerSize;}
};
b.M.layout.layoutName = "branchMain"+i;
b.T.layout.layoutName = "branch"+i;
b.program(P);
}
}
{final int N = Leaf_length; // Preallocate transactions used on leaf stucks
leafTransactions = new StuckPA[N];
for (int i = 0; i < N; i++)
{final StuckPA l = leafTransactions[i] = new StuckPA("leaf_"+i, M) // Based stucks
{int maxSize() {return BtreePA.this.maxKeysPerLeaf();}
int bitsPerKey() {return BtreePA.this.bitsPerKey();}
int bitsPerData() {return BtreePA.this.bitsPerData();}
int bitsPerSize() {return BtreePA.this.bitsPerSize;}
};
l.M.layout.layoutName = "leafMain"+i;
l.T.layout.layoutName = "leaf"+i;
l.program(P);
}
}
bSize = branchTransactions[Branch_Size ]; // Branch size
bLeaf = branchTransactions[Branch_Leaf ]; // Check whether a node has leaves for children
bTop = branchTransactions[Branch_Top ]; // Get the size of a stuck
bFirstBranch = branchTransactions[Branch_FirstBranch]; // Locate the first greater or equal key in a branch
bT = branchTransactions[Branch_T ]; // Process a parent node
bL = branchTransactions[Branch_tl ]; // Process a left node
bR = branchTransactions[Branch_tr ]; // Process a right node
lSize = leafTransactions[Leaf_Size ]; // Leaf size
lLeaf = leafTransactions[Leaf_Leaf ]; // Print a leaf
lEqual = leafTransactions[Leaf_Equal ]; // Locate an equal key
lFirstLeaf = leafTransactions[Leaf_FirstLeaf ]; // Locate the first greater or equal key in a leaf
lT = leafTransactions[Leaf_T ]; // Process a parent node
lL = leafTransactions[Leaf_tl ]; // Process a left node
lR = leafTransactions[Leaf_tr ]; // Process a right node
P.new I()
{void a()
{final int N = maxSize(); // Put all the nodes on the free chain at the start with low nodes first
for (int i = N; i > 0; --i) setInt(free, (i == N ? 0 : i), i - 1); // Link this node to the previous node
setInt(freeList, root); // Root is first on free chain
}
String v() {return "/* Construct Free list */";}
};
allocate(false); // The root is always at zero, which frees zero to act as the end of list marker on the free chain
T.setIntInstruction(node_setLeaf, root);
setLeaf(); // The root starts as a leaf
}
static BtreePA btreePA(final int leafKeys, int branchKeys) // Define a test btree with the specified dimensions
{return new BtreePA()
{int maxSize () {return testMaxSize;}
int maxKeysPerLeaf () {return leafKeys;}
int maxKeysPerBranch() {return branchKeys;}
int bitsPerKey () {return 32;}
int bitsPerData () {return 32;}
};
}
static BtreePA btreePA_small() // Define a small test btree
{return new BtreePA()
{int maxSize () {return 8;}
int maxKeysPerLeaf () {return 2;}
int maxKeysPerBranch() {return 3;}
int bitsPerKey () {return 4;}
int bitsPerData () {return 4;}
};
}
Layout layout() // Layout describing memory used by btree
{z();
final BtreePA btree = this;
final StuckPA leafStuck = new StuckPA("leaf", M) // Leaf
{int maxSize() {return btree.maxKeysPerLeaf();}
int bitsPerKey() {return btree.bitsPerKey();}
int bitsPerData() {return btree.bitsPerData();}
int bitsPerSize() {return btree.bitsPerSize;}
};
leafStuck.T.layout.layoutName = "leaf";
final StuckPA branchStuck = new StuckPA("branch", M) // Branch
{int maxSize() {return btree.maxKeysPerBranch()+1;} // Not forgetting top next
int bitsPerKey() {return btree.bitsPerKey();}
int bitsPerData() {return btree.bitsPerNext;}
int bitsPerSize() {return btree.bitsPerSize;}
};
branchStuck.T.layout.layoutName = "branch";
final Layout l = Layout.layout();
leaf = l.duplicate("leaf", leafStuck.layout());
branch = l.duplicate("branch", branchStuck.layout());
branchOrLeaf = l.union ("branchOrLeaf", leaf, branch);
isLeaf = l.bit ("isLeaf");
free = l.variable ("free", btree.bitsPerNext);
Node = l.structure("node", isLeaf, free, branchOrLeaf);
nodes = l.array ("nodes", Node, maxSize());
freeList = l.variable ("freeList", btree.bitsPerNext);
bTree = l.structure("bTree", freeList , nodes);
return l.compile();
}
//D1 Control // Testing, control and integrity
private void ok(String expected) {Test.ok(toString(), expected);} // Confirm tree is as expected
private void stop() {Test.stop(toString());} // Stop after printing the tree
public String toString() {return print();} // Print the tree
//D1 Memory access // Access to memory
private void checkMainField(Layout.Field field) // Check that a variable is in main memory
{z();
if (field.container() != M.layout)
{final String name = field.container().layoutName;
stop("Field:", field.name, "is part of memory layout:", name, "not main");
}
}
private void checkTransactionField(Layout.Field field) // Check that a variable is in transaction memory
{z();
if (field.container() != T.layout)
{final String name = field.container().layoutName;
stop("Field:", field.name, "is part of memory layout:",
name, "not transaction");
}
}
private int getInt(Layout.Field field) // Get an integer from main memory
{z(); checkMainField(field); return M.getInt(field);
}
private int getInt(Layout.Field field, int index)
{z(); checkMainField(field); return M.getInt(field, index);
}
private void setInt(Layout.Field field, int value) // Set an integer in main memory
{z(); checkMainField(field); M.setInt(field, value);
}
private void setInt(Layout.Field field, int value, int index)
{z(); checkMainField(field); M.setInt(field, value, index);
}
private int tGetInt(Layout.Field field) // Get an integer from transaction memory
{z(); checkTransactionField(field); return T.getInt(field);
}
private int tGetInt(Layout.Field field, int index)
{z(); checkTransactionField(field); return T.getInt(field, index);
}
private void tSetInt(Layout.Field field, int value) // Set an integer in transaction memory
{z(); checkTransactionField(field); T.setInt(field, value);
}
private void tSetInt(Layout.Field field, int value, int index)
{z(); checkTransactionField(field); T.setInt(field, value, index);
}
void tt(Layout.Variable target, Layout.Variable source) // Copy the value of one transaction variable into another
{z(); checkTransactionField(target); checkTransactionField(source);
T.at(target).move(T.at(source));
}
void tm(Layout.Variable target, Layout.Variable source) // Copy the value of a main memory variable into transaction memory
{z(); checkTransactionField(target); checkMainField(source);
T.at(target).move(M.at(source));
}
void mt(Layout.Variable target, Layout.Variable source) // Copy the value of a transaction memory variable into main memory
{z(); checkMainField(target); checkTransactionField(source);
M.at(target).move(T.at(source));
}
//D1 Memory allocation // Allocate and free memory
private void allocate(boolean check) // Allocate a node with or without checking for sufficient free space
{tm(allocate, freeList); // Node at head of free nodes list
if (check)
{P.new If (T.at(allocate))
{void Else()
{P.halt("No more memory available"); // No more free nodes available
}
};
}
M.at(freeList).move(M.at(free, T.at(allocate))); // Second node on free list
tt(node_clear, allocate);
clear(); // Construct and clear the node
// tt(node_clear, allocate);
// clear(T.at(allocate)); // Construct and clear the node
// maxNodeUsed = max(maxNodeUsed, ++nodeUsed); // Number of nodes in use
}
private void allocate() {z(); allocate(true);} // Allocate a node checking for free space
//D1 Components // A branch or leaf in the tree
Layout.Variable Key; // Key being found, inserted or deleted
Layout.Variable Data; // Data associated with the key being inserted
Layout.Bit found; // Whether the key was found
Layout.Variable key; // Key to insert
Layout.Variable data; // Data associated with the key found
Layout.Variable allocate; // The latest allocation result
Layout.Variable nextFree; // Next element of the free chain
Layout.Bit success; // Inserted or updated if true
Layout.Bit inserted; // Inserted if true
Layout.Variable first; // Index of first key greater than or equal to the search key
Layout.Variable next; // The corresponding next field or top if no such key was found
// Find equal in leaf
Layout.Variable search; // Search key
Layout.Variable firstKey; // First of right leaf
Layout.Variable lastKey; // Last of left leaf
Layout.Variable flKey; // Key mid way between last of left and first of right
Layout.Variable parentKey; // Parent key
Layout.Variable lk; // Left child key
Layout.Variable ld; // Left child data
Layout.Variable rk; // Right child key
Layout.Variable rd; // Right child data
Layout.Variable index; // Index of a slot in a node
Layout.Variable nl; // Number in the left child
Layout.Variable nr; // Number in the right child
Layout.Variable l; // Left node
Layout.Variable r; // Right node
Layout.Variable splitParent; // The parent during a splitting operation
Layout.Bit IsLeaf; // On a leaf
Layout.Bit isFull; // The node is full
Layout.Bit leafIsFull; // The leaf node is full
Layout.Bit branchIsFull; // The branch node is full
Layout.Bit parentIsFull; // The parent branch node is full
Layout.Bit isEmpty; // The node is empty
Layout.Bit isLow; // The node has too few children for a delete
Layout.Bit hasLeavesForChildren; // The node has leaves for children
Layout.Bit stolenOrMerged; // A merge or steal operation succeeded
Layout.Bit pastMaxDepth; // A merge or steal operation succeeded
Layout.Bit nodeMerged; // All sequential pairs of siblings have been offered a chance to merge
Layout.Bit mergeable; // The left and right children are mergable
Layout.Bit deleted; // Whether the delete request actually deleted the specified key
Layout.Variable leafBase, leafBase1, leafBase2, leafBase3; // The offset of a leaf in memory
Layout.Variable branchBase, branchBase1, branchBase2, branchBase3; // The offset of a branch in memory
Layout.Variable leafSize; // Number of children in body of leaf
Layout.Variable branchSize; // Number of children in body of branch taking top for granted as it is always there
Layout.Variable top; // The top next element of a branch - only used in printing
// Find, insert, delete - the public entry points to this module
Layout.Variable find; // Results of a find operation
Layout.Variable findAndInsert; // Results of a find and insert operation
Layout.Variable parent; // Parent node in a descent through the tree
Layout.Variable child; // Child node in a descent through the tree
Layout.Variable leafFound; // Leaf found by find
Layout.Variable maxKeysPerLeaf; // Maximum keys per leaf
Layout.Variable maxKeysPerBranch; // Maximum keys per branch
Layout.Variable MaxDepth; // Maximum depth of a search
Layout.Variable two; // The value two
Layout.Variable findDepth; // Current level being searched by find
Layout.Variable putDepth; // Current level being traversed by put
Layout.Variable deleteDepth; // Current level being traversed by delete
Layout.Variable mergeDepth; // Current level being traversed by merge
Layout.Variable mergeIndex; // Current index of node being merged across
Layout.Variable node_isLeaf; // The node to be used to implicitly parameterize each method call
Layout.Variable node_setLeaf;
Layout.Variable node_setBranch;
Layout.Variable node_assertLeaf;
Layout.Variable node_assertBranch;
Layout.Variable allocLeaf;
Layout.Variable allocBranch;
Layout.Variable node_free;
Layout.Variable node_clear;
Layout.Variable node_erase;
Layout.Variable node_leafBase, node_leafBase1, node_leafBase2, node_leafBase3;
Layout.Variable node_branchBase, node_branchBase1, node_branchBase2, node_branchBase3;
Layout.Variable node_leafSize;
Layout.Variable node_branchSize;
Layout.Variable node_isFull;
Layout.Variable node_leafIsFull;
Layout.Variable node_branchIsFull;
Layout.Variable node_parentIsFull;
Layout.Variable node_isEmpty;
Layout.Variable node_isLow;
Layout.Variable node_hasLeavesForChildren;
Layout.Variable node_top;
Layout.Variable node_findEqualInLeaf;
Layout.Variable node_findFirstGreaterThanOrEqualInLeaf;
Layout.Variable node_findFirstGreaterThanOrEqualInBranch;
Layout.Variable node_splitLeaf;
Layout.Variable node_splitBranch;
Layout.Variable node_stealFromLeft;
Layout.Variable node_stealFromRight;
Layout.Variable node_mergeRoot;
Layout.Variable node_mergeLeftSibling;
Layout.Variable node_mergeRightSibling;
Layout.Variable node_balance;
Layout transactionLayout() // Layout of temporary storage used during a transaction against the btree
{final Layout L = new Layout();
allocate = L.variable ("allocate" , bitsPerNext);
nextFree = L.variable ("nextFree" , bitsPerNext);
success = L.bit ("success" );
inserted = L.bit ("inserted" );
first = L.variable ("first" , bitsPerSize);
next = L.variable ("next" , bitsPerNext);
search = L.variable ("search" , bitsPerKey());
found = L.bit ("found" );
key = L.variable ("key" , bitsPerKey());
data = L.variable ("data" , bitsPerData());
firstKey = L.variable ("firstKey" , bitsPerKey());
lastKey = L.variable ("lastKey" , bitsPerKey());
flKey = L.variable ("flKey" , bitsPerKey());
parentKey = L.variable ("parentKey" , bitsPerKey());
lk = L.variable ("lk" , bitsPerKey());
ld = L.variable ("ld" , bitsPerData());
rk = L.variable ("rk" , bitsPerKey());
rd = L.variable ("rd" , bitsPerData());
index = L.variable ("index" , bitsPerSize);
nl = L.variable ("nl" , bitsPerSize);
nr = L.variable ("nr" , bitsPerSize);
l = L.variable ("l" , bitsPerNext);
r = L.variable ("r" , bitsPerNext);
splitParent = L.variable ("splitParent" , bitsPerNext);
IsLeaf = L.bit ("IsLeaf" );
isFull = L.bit ("isFull" );
leafIsFull = L.bit ("leafIsFull" );
branchIsFull = L.bit ("branchIsFull" );
parentIsFull = L.bit ("parentIsFull" );
isEmpty = L.bit ("isEmpty" );
isLow = L.bit ("isLow" );
hasLeavesForChildren = L.bit ("hasLeavesForChildren" );
stolenOrMerged = L.bit ("stolenOrMerged" );
pastMaxDepth = L.bit ("pastMaxDepth" );
nodeMerged = L.bit ("nodeMerged" );
mergeable = L.bit ("mergeable" );
deleted = L.bit ("deleted" );
leafBase = L.variable ("leafBase" , bitsPerAddress);
leafBase1 = L.variable ("leafBase1" , bitsPerAddress);
leafBase2 = L.variable ("leafBase2" , bitsPerAddress);
leafBase3 = L.variable ("leafBase3" , bitsPerAddress);
branchBase = L.variable ("branchBase" , bitsPerAddress);
branchBase1 = L.variable ("branchBase1" , bitsPerAddress);
branchBase2 = L.variable ("branchBase2" , bitsPerAddress);
branchBase3 = L.variable ("branchBase3" , bitsPerAddress);
leafSize = L.variable ("leafSize" , bitsPerSize);
branchSize = L.variable ("branchSize" , bitsPerSize);
top = L.variable ("top" , bitsPerNext);
Key = L.variable ("Key" , bitsPerKey());
Data = L.variable ("Data" , bitsPerData());
find = L.variable ("find" , bitsPerNext);
findAndInsert = L.variable ("findAndInsert" , bitsPerNext);
parent = L.variable ("parent" , bitsPerNext);
child = L.variable ("child" , bitsPerNext);
leafFound = L.variable ("leafFound" , bitsPerNext);
maxKeysPerLeaf = L.variable ("maxKeysPerLeaf" , bitsPerSize);
maxKeysPerBranch = L.variable ("maxKeysPerBranch" , bitsPerSize);
MaxDepth = L.variable ("maxDepth" , bitsPerNext);
two = L.variable ("two" , bitsPerSize);
findDepth = L.variable ("findDepth" , bitsPerNext);
putDepth = L.variable ("putDepth" , bitsPerNext);
deleteDepth = L.variable ("deleteDepth" , bitsPerNext);
mergeDepth = L.variable ("mergeDepth" , bitsPerNext);
mergeIndex = L.variable ("mergeIndex" , bitsPerSize);
node_isLeaf = L.variable ("node_isLeaf" , bitsPerNext);
node_setLeaf = L.variable ("node_setLeaf" , bitsPerNext);
node_setBranch = L.variable ("node_setBranch" , bitsPerNext);
node_assertLeaf = L.variable ("node_assertLeaf" , bitsPerNext);
node_assertBranch = L.variable ("node_assertBranch" , bitsPerNext);
allocLeaf = L.variable ("allocLeaf" , bitsPerNext);
allocBranch = L.variable ("allocBranch" , bitsPerNext);
node_free = L.variable ("node_free" , bitsPerNext);
node_clear = L.variable ("node_clear" , bitsPerNext);
node_erase = L.variable ("node_erase" , bitsPerNext);
node_leafBase = L.variable ("node_leafBase" , bitsPerNext);
node_leafBase1 = L.variable ("node_leafBase1" , bitsPerNext);
node_leafBase2 = L.variable ("node_leafBase2" , bitsPerNext);
node_leafBase3 = L.variable ("node_leafBase3" , bitsPerNext);
node_branchBase = L.variable ("node_branchBase" , bitsPerNext);
node_branchBase1 = L.variable ("node_branchBase1" , bitsPerNext);
node_branchBase2 = L.variable ("node_branchBase2" , bitsPerNext);
node_branchBase3 = L.variable ("node_branchBase3" , bitsPerNext);
node_leafSize = L.variable ("node_leafSize" , bitsPerNext);
node_branchSize = L.variable ("node_branchSize" , bitsPerNext);
node_isFull = L.variable ("node_isFull" , bitsPerNext);
node_leafIsFull = L.variable ("node_leafIsFull" , bitsPerNext);
node_branchIsFull = L.variable ("node_branchIsFull" , bitsPerNext);
node_parentIsFull = L.variable ("node_parentIsFull" , bitsPerNext);
node_isEmpty = L.variable ("node_isEmpty" , bitsPerNext);
node_isLow = L.variable ("node_isLow" , bitsPerNext);
node_hasLeavesForChildren = L.variable ("node_hasLeavesForChildren" , bitsPerNext);
node_top = L.variable ("node_top" , bitsPerNext);
node_findEqualInLeaf = L.variable ("node_findEqualInLeaf" , bitsPerNext);
node_findFirstGreaterThanOrEqualInLeaf = L.variable ("node_findFirstGreaterThanOrEqualInLeaf" , bitsPerNext);
node_findFirstGreaterThanOrEqualInBranch = L.variable ("node_findFirstGreaterThanOrEqualInBranch" , bitsPerNext);
node_splitLeaf = L.variable ("node_splitLeaf" , bitsPerNext);
node_splitBranch = L.variable ("node_splitBranch" , bitsPerNext);
node_stealFromLeft = L.variable ("node_stealFromLeft" , bitsPerNext);
node_stealFromRight = L.variable ("node_stealFromRight" , bitsPerNext);
node_mergeRoot = L.variable ("node_mergeRoot" , bitsPerNext);
node_mergeLeftSibling = L.variable ("node_mergeLeftSibling" , bitsPerNext);
node_mergeRightSibling = L.variable ("node_mergeRightSibling" , bitsPerNext);
node_balance = L.variable ("node_balance" , bitsPerNext);
final Layout.Structure transaction = L.structure("transaction", allocate,
nextFree, success, inserted, first, next, search, found, key, data,
firstKey, lastKey, flKey, parentKey, lk, ld, rk, rd, index, nl, nr, l, r,
splitParent, IsLeaf, isFull, leafIsFull, branchIsFull, parentIsFull, isEmpty, isLow,
hasLeavesForChildren, stolenOrMerged, pastMaxDepth, nodeMerged,
mergeable, deleted,
leafBase, leafBase1, leafBase2, leafBase3,
branchBase, branchBase1, branchBase2, branchBase3,
leafSize, branchSize, top, Key,
Data, find, findAndInsert, parent, child, leafFound, maxKeysPerLeaf,
maxKeysPerBranch, two, MaxDepth, findDepth, putDepth, deleteDepth,
mergeDepth, mergeIndex, node_isLeaf, node_setLeaf, node_setBranch,
node_assertLeaf, node_assertBranch, allocLeaf, allocBranch, node_free,
node_clear, node_erase,
node_leafBase, node_leafBase1, node_leafBase2, node_leafBase3,
node_branchBase, node_branchBase1, node_branchBase2, node_branchBase3,
node_leafSize,
node_branchSize, node_isFull, node_branchIsFull, node_parentIsFull, node_leafIsFull,
node_isEmpty, node_isLow, node_hasLeavesForChildren, node_top,
node_findEqualInLeaf, node_findFirstGreaterThanOrEqualInLeaf,
node_findFirstGreaterThanOrEqualInBranch, node_splitLeaf,
node_splitBranch, node_stealFromLeft, node_stealFromRight,
node_mergeRoot, node_mergeLeftSibling, node_mergeRightSibling,
node_balance);
return L.compile();
}
private void isLeaf() {z(); T.at(IsLeaf).move(M.at(isLeaf, T.at(node_isLeaf)));} // A leaf if true
private void setLeaf() {z(); M.at(isLeaf, T.at(node_setLeaf)) .ones();} // Set as leaf
private void setBranch() {z(); M.at(isLeaf, T.at(node_setBranch)).zero();} // Set as branch
private void isLeaf(MemoryLayoutPA.At node) // A leaf if true
{z();
T.at(IsLeaf).move(M.at(isLeaf, node));
}
private void assertLeaf()
{z();
tt(node_isLeaf, node_assertLeaf); isLeaf();
P.new If (T.at(IsLeaf))
{void Else()
{P.halt("Leaf required");
}
};
}
private void assertBranch()
{z();
tt(node_isLeaf, node_assertBranch);
isLeaf();
P.new If (T.at(IsLeaf))
{void Then()
{P.halt("Branch required");
}
};
}
private void allocLeaf() // Allocate leaf
{z();
allocate();
tt(allocLeaf, allocate);
tt(node_setLeaf, allocate);
setLeaf();
}
private void allocBranch() // Allocate branch
{z();
allocate();
tt(allocBranch , allocate);
tt(node_setBranch, allocate);
setBranch();
}
private void free() // Free a new node to make it available for reuse
{z();
P.new If (T.at(node_free)) {void Else() {P.halt("Cannot free root");}}; // The root is never freed
z(); tt(node_erase, node_free); erase(); // Clear the node to encourage erroneous frees to do damage that shows up quickly.
M.at(free, T.at(node_free)).move(M.at(freeList)); // Chain this node in front of the last freed node
M.at(freeList).move(T.at(node_free)); // Make this node the head of the free chain
maxNodeUsed = max(maxNodeUsed, --nodeUsed); // Number of nodes in use
}
private void clear() {z(); clear(T.at(node_clear));} // Clear a new node to zeros ready for use
private void clear(MemoryLayoutPA.At node) // Clear a new node to zeros ready for use
{z(); M.at(Node, node).zero();
}
private void erase() // Clear a new node to ones as this is likely to create invalid values that will be easily detected in the case of erroneous frees
{z();
M.at(Node, T.at(node_erase)).ones();
}
private void leafBase(Layout.Variable leafBase, // Base of leaf stuck in memory
Layout.Variable node_leafBase)
{z();
P.new I()
{void a()
{final MemoryLayoutPA.At a = M.at(leaf, T.at(node_leafBase)).setOff();
T.at(leafBase).setInt(a.at);
}
String v() {return T.at(leafBase).verilogLoad() + " <= " + M.at(leaf, T.at(node_leafBase)).verilogAddr() + ";";}
};
}
private void leafBase () {leafBase(leafBase , node_leafBase );}
private void leafBase1() {leafBase(leafBase1, node_leafBase1);}
private void leafBase2() {leafBase(leafBase2, node_leafBase2);}
private void leafBase3() {leafBase(leafBase3, node_leafBase3);}
private void branchBase(Layout.Variable branchBase, // Base of branch stuck in memory
Layout.Variable node_branchBase)
{z();
P.new I()
{void a()
{final MemoryLayoutPA.At a = M.at(branch, T.at(node_branchBase)).setOff();
T.at(branchBase).setInt(a.at);
}
String v() {return T.at(branchBase).verilogLoad() + " <= " + M.at(branch, T.at(node_branchBase)).verilogAddr() + ";";}
};
}
private void branchBase () {branchBase(branchBase, node_branchBase );}
private void branchBase1() {branchBase(branchBase1, node_branchBase1);}
private void branchBase2() {branchBase(branchBase2, node_branchBase2);}
private void branchBase3() {branchBase(branchBase3, node_branchBase3);}
private void leafSize() // Number of children in body of leaf
{z();
tt(node_leafBase, node_leafSize); leafBase(); lSize.base(T.at(leafBase));
lSize.size(); T.at(leafSize).move(lSize.T.at(lSize.size));
}
private void branchSize() // Number of children in body of branch taking top for granted as it is always there
{z();
tt(node_branchBase, node_branchSize); branchBase(); bSize.base(T.at(branchBase));
bSize.size(); T.at(branchSize).move(bSize.T.at(bSize.size)); // Changed order here to match leafSize more closely
T.at(branchSize).dec(); // Account for top which will always be present
}
private void isEmpty() // The node is empty
{z();
tt(node_isLeaf, node_isEmpty);
isLeaf();
P.new If (T.at(IsLeaf))
{void Then()
{tt(node_leafSize, node_isEmpty); leafSize();
T.at(leafSize).isZero(T.at(isEmpty));
}
void Else()
{z();
tt(node_branchSize, node_isEmpty); branchSize();
T.at(branchSize).isZero(T.at(isEmpty)); // Allow for top which must always be present
}
};
}
private void isFull() // The node is full
{z();
tt(node_isLeaf, node_isFull);
isLeaf();
P.new If (T.at(IsLeaf))
{void Then()
{tt(node_leafSize, node_isFull);
leafSize();
T.at(leafSize) .equal(T.at(maxKeysPerLeaf), T.at(isFull));
}
void Else()
{tt(node_branchSize, node_isFull);
branchSize();
T.at(branchSize).equal(T.at(maxKeysPerBranch), T.at(isFull));
}
};
}
private void leafIsFull() // Whether a node known to be a leaf is full
{tt(node_leafSize, node_leafIsFull);
leafSize();
T.at(leafSize) .equal(T.at(maxKeysPerLeaf), T.at(leafIsFull));
}
private void branchIsFull() // Whether a node known to be a branch is full
{tt(node_branchSize, node_branchIsFull);
branchSize();
T.at(branchSize).equal(T.at(maxKeysPerBranch), T.at(branchIsFull));
}
private void isLow() // The node is low on children making it impossible to merge two sibling children
{z(); tt(node_isLeaf, node_isLow); isLeaf();
P.new If (T.at(IsLeaf))
{void Then()
{tt(node_leafSize, node_isLow);
leafSize();
T.at(leafSize).lessThan(T.at(two), T.at(isLow));
}
void Else()
{tt(node_branchSize, node_isLow);
branchSize();
T.at(branchSize).lessThan(T.at(two), T.at(isLow));
}
};
}
private void hasLeavesForChildren() // The node has leaves for children
{if (Assert) {tt(node_assertBranch, node_hasLeavesForChildren); assertBranch();}
tt(node_branchBase, node_hasLeavesForChildren); branchBase(); bLeaf.base(T.at(branchBase));
bLeaf.lastElement();
T.at(node_isLeaf).move(bLeaf.T.at(bLeaf.tData)); isLeaf(bLeaf.T.at(bLeaf.tData));
tt(hasLeavesForChildren, IsLeaf);
}
private void top() // The top next element of a branch - only used in printing
{if (Assert) {tt(node_assertBranch, node_top); assertBranch();}
tt(node_branchBase, node_top); branchBase(); bTop.base(T.at(branchBase));
tt(node_branchSize, node_top); branchSize(); bTop.T.at(bTop.index).move(T.at(branchSize));
bTop.elementAt();
T.at(top).move(bTop.T.at(bTop.tData));
}
//D2 Search // Search within a node and update the node description with the results
private void findEqualInLeaf() // Find the first key in the leaf that is equal to the search key
{if (Assert) {tt(node_assertLeaf, node_findEqualInLeaf); assertLeaf();}
tt(node_leafBase, node_findEqualInLeaf); leafBase();
lEqual.base(T.at(leafBase));
lEqual.T.at(lEqual.search).move(T.at(search));
lEqual.T.setIntInstruction(lEqual.limit, 0);
lEqual.search();
M.moveParallel
(T.at(found), lEqual.T.at(lEqual.found), /// Parallel possible
T.at(index), lEqual.T.at(lEqual.index),
T.at(data ), lEqual.T.at(lEqual.tData));
}
public String findEqualInLeaf_toString() // Print details of find equal in leaf node
{final StringBuilder s = new StringBuilder();
s.append("FindEqualInLeaf(");
s.append( "Leaf:"+T.at(node_findEqualInLeaf).getInt());
s.append( " Key:"+T.at(search) .getInt());
s.append(" found:"+T.at(found) .getInt());
if (T.at(found).isOnes())
{s.append(" data:"+T.at(data).getInt()+" index:"+T.at(index).getInt());
}
s.append(")\n");
return s.toString();
}
private void findFirstGreaterThanOrEqualInLeaf() // Find the first key in the leaf that is equal to or greater than the search key
{if (Assert) {tt(node_assertLeaf, node_findFirstGreaterThanOrEqualInLeaf); assertLeaf();}
tt(node_leafBase, node_findFirstGreaterThanOrEqualInLeaf); leafBase();
lFirstLeaf.base(T.at(leafBase));
lFirstLeaf.T.at(lFirstLeaf.search).move(T.at(search));
lFirstLeaf.T.setIntInstruction(lFirstLeaf.limit, 0);
lFirstLeaf.searchFirstGreaterThanOrEqual();
M.moveParallel
(T.at(found), lFirstLeaf.T.at(lFirstLeaf.found), /// Parallel possible
T.at(first), lFirstLeaf.T.at(lFirstLeaf.index));
}
private void findFirstGreaterThanOrEqualInBranch() // Find the first key in the branch that is equal to or greater than the search key
{if (Assert) {tt(node_assertBranch, node_findFirstGreaterThanOrEqualInBranch); assertBranch();}
tt(node_branchBase, node_findFirstGreaterThanOrEqualInBranch); branchBase();
bFirstBranch.base(T.at(branchBase));
bFirstBranch.T.at(bFirstBranch.search).move(T.at(search));
bFirstBranch.T.setIntInstruction(bFirstBranch.limit, 1);
bFirstBranch.searchFirstGreaterThanOrEqual();
M.moveParallel
(T.at(found), bFirstBranch.T.at(bFirstBranch.found), /// Parallel possible
T.at(first), bFirstBranch.T.at(bFirstBranch.index));
P.new If (T.at(found)) // Next if key matches else top
{void Then()
{T.at(next).move(bFirstBranch.T.at(bFirstBranch.tData));
}
void Else() // Top as no key matched
{z();
bFirstBranch.lastElement();
T.at(next).move(bFirstBranch.T.at(bFirstBranch.tData));
}
};
}
//D2 Array // Represent the contents of the tree as an array
private void leafToArray(int node, Stack<ArrayElement> s) // Leaf as an array
{if (Assert) {T.at(node_assertLeaf).setInt(node); assertLeaf();}
T.at(node_leafSize).setInt(node);
leafSize();
final int K = T.at(leafSize).getInt();
final StuckPA t = lLeaf.copy();
T.at(node_leafBase).setInt(node); leafBase(); t.base(T.at(leafBase));
for (int i = 0; i < K; i++)
{z();
t.T.at(t.index).setInt(i); t.elementAt();
s.push(new ArrayElement(i, t.T.at(t.tKey).getInt(), t.T.at(t.tData).getInt()));
}
}
private void branchToArray(int node, Stack<ArrayElement> s) // Branch to array
{if (Assert) {T.at(node_assertBranch).setInt(node); assertBranch();}
T.at(node_branchSize ).setInt(node); branchSize();
final int K = T.at(branchSize).getInt()+1; // Include top next
if (K > 0) // Branch has key, next pairs
{z();
final StuckPA t = bLeaf.copy();
T.at(node_branchBase).setInt(node); branchBase(); t.base(T.at(branchBase));
for (int i = 0; i < K; i++)
{z();
t.T.at(t.index).setInt(i); t.elementAt(); // Each node in the branch
T.at(node_isLeaf).move(t.T.at(t.tData)); isLeaf(t.T.at(t.tData));
if (T.at(IsLeaf).isOnes())
{z();
leafToArray(t.T.at(t.tData).getInt(), s);
}
else
{z();
if (t.T.at(t.tData).isZero())
{say("Cannot descend through root from index", i,
"in branch", node);
break;
}
z(); branchToArray(t.T.at(t.tData).getInt(), s);
}
}
}
}
//D2 Print // Print the contents of the tree
public String find_toString() // Print find result
{final StringBuilder s = new StringBuilder();
s.append("Find(");
s.append( " search:"+T.at(search).getInt());
s.append( " found:"+T.at(found) .getInt());
s.append( " data:"+T.at(data) .getInt());
s.append( " index:"+T.at(index) .getInt());
s.append(")\n");
return s.toString();
}
public String findAndInsert_toString() // Print find and insert result
{final StringBuilder s = new StringBuilder();
s.append("FindAndInsert(");
s.append( " key:"+T.at(key) .getInt());
s.append( " data:"+T.at(data) .getInt());
s.append(" success:"+T.at(success).getInt());
if (T.at(success).isOnes()) s.append(" inserted:"+T.at(inserted));
s.append(")\n" );
return s.toString();
}
public String findFirstGreaterThanOrEqualInLeaf_toString() // Print results of search
{final StringBuilder s = new StringBuilder();
s.append("FindFirstGreaterThanOrEqualInLeaf(");
s.append( "Leaf:"+T.at(node_findFirstGreaterThanOrEqualInLeaf).getInt());
s.append( " Key:"+T.at(search).getInt());
s.append(" found:"+T.at(found).getInt());
if (T.at(found).isOnes()) s.append(" first:"+T.at(first).getInt());
s.append(")\n");
return s.toString();
}
public String findFirstGreaterThanOrEqualInBranch_toString() // Print search results
{final StringBuilder s = new StringBuilder();
s.append("FindFirstGreaterThanOrEqualInBranch(");
s.append("branch:"+T.at(node_findFirstGreaterThanOrEqualInBranch).getInt());
s.append( " Key:"+T.at(search).getInt());
s.append(" found:"+T.at(found).getInt());
s.append( " next:"+T.at(next).getInt());
if (T.at(found).isOnes()) s.append(" first:"+T.at(first).getInt());
s.append(")\n");
return s.toString();
}
//D2 Split // Split nodes in half to increase the number of nodes in the tree
private void splitLeafRoot() // Split a leaf which happens to be a full root into two half full leaves while transforming the root leaf into a branch
{if (Assert) {T.setIntInstruction(node_assertLeaf, root); assertLeaf();}
T.setIntInstruction(node_leafIsFull, root); leafIsFull();
if (Halt) P.new If (T.at(leafIsFull))
{void Else()
{P.halt("Root is not full");
}
};
allocLeaf(); tt(l, allocLeaf); // New left leaf
allocLeaf(); tt(r, allocLeaf); // New right leaf
T.at(node_leafBase1).zero(); leafBase1(); lT.base(T.at(leafBase1)); // Set address of the referenced leaf stuck
tt (node_leafBase2, l); leafBase2(); lL.base(T.at(leafBase2)); // Set address of the referenced leaf stuck
tt (node_leafBase3, r); leafBase3(); lR.base(T.at(leafBase3)); // Set address of the referenced leaf stuck
for (int i = 0; i < splitLeafSize; i++) // Build left leaf from parent
{z(); lT.shift();
M.moveParallel
(lL.T.at(lL.tKey ), lT.T.at(lT.tKey ), /// Parallel possible
lL.T.at(lL.tData), lT.T.at(lT.tData));
lL.push();
}
for (int i = 0; i < splitLeafSize; i++) // Build right leaf from parent
{z(); lT.shift();
M.moveParallel
(lR.T.at(lR.tKey ), lT.T.at(lT.tKey), /// Parallel possible
lR.T.at(lR.tData), lT.T.at(lT.tData));
lR.push();
}
lR.firstElement();
lL. lastElement();
T.setIntInstruction(node_setBranch, root); setBranch(); // The root is now a branch
T.setIntInstruction(node_branchBase, root); branchBase(); // Set address of the referenced leaf stuck
bT.base(T.at(branchBase)); // Set address of the referenced leaf stuck
bT.clear(); // Clear the branch
T.at(firstKey).move(lR.T.at(lR.tKey)); // First of right leaf
T.at(lastKey ).move(lL.T.at(lL.tKey)); // Last of left leaf
P.new I() // Mid key - keys are likely to be bigger than 31 bits
{void a()
{T.at(flKey).setInt((T.at(firstKey).getInt()+T.at(lastKey).getInt())/2);
}
String v()
{return T.at(flKey) .verilogLoad() + "<= " +
"("+T.at(firstKey).verilogLoad() + " + " +
T.at(lastKey) .verilogLoad() + ") / 2;";
}
};
M.moveParallel
(bT.T.at(bT.tKey ), T.at(flKey), /// Parallel possible
bT.T.at(bT.tData), T.at(l));
bT.push(); // Insert left leaf into root
bT.T.at(bT.tKey).zero();
bT.T.at(bT.tData).move(T.at(r));
bT.push(); // Insert right into root. This will be the top node and so ignored by search ... except last.
}
private void splitBranchRoot() // Split a branch which happens to be a full root into two half full branches while retaining the current branch as the root
{if (Assert) {T.setIntInstruction(node_assertBranch, root); assertBranch();}
T.setIntInstruction(node_branchIsFull, root); branchIsFull();
if (Halt) P.new If (T.at(branchIsFull))
{void Else()
{P.halt("Root is not full");
}
};
z();
allocBranch(); tt(l, allocBranch); // New left branch
allocBranch(); tt(r, allocBranch); // New right branch
T.setIntInstruction(node_branchBase1, root);
branchBase1(); bT.base(T.at(branchBase1)); // Set address of the referenced branch stuck
tt(node_branchBase2, l); branchBase2(); bL.base(T.at(branchBase2)); // Set address of the referenced branch stuck
tt(node_branchBase3, r); branchBase3(); bR.base(T.at(branchBase3)); // Set address of the referenced branch stuck
for (int i = 0; i < splitBranchSize; i++) // Build left child from parent
{z(); bT.shift();
bL.T.at(bL.tKey ).move(bT.T.at(bT.tKey ));
bL.T.at(bL.tData).move(bT.T.at(bT.tData));
bL.push();
}
bT.shift(); // This key, next pair will be part of the root
T.at(parentKey).move(bT.T.at(bT.tKey));
bL.T.at(bL.tKey).zero();
bL.T.at(bL.tData).move(bT.T.at(bT.tData));