Skip to content

Commit 32fb213

Browse files
committed
Add insert cases except 5/6
1 parent d2ceba7 commit 32fb213

5 files changed

Lines changed: 126 additions & 38 deletions

File tree

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,74 @@
11
insert_get_child_dir:
22
# Get child direction, set at parent.
33
# ---------------------------------------------------------------------
4-
ldxh r5, [r2 + TREE_NODE_KEY_OFF] # r5 = parent.key;
4+
ldxh r5, [r2 + TREE_NODE_KEY_OFF] # r5 = parent.key;
55
jgt r4, r5, insert_get_child_dir_branch_r
66
insert_get_child_dir_branch_l:
7-
stxdw [r2 + TREE_NODE_CHILD_L_OFF], r9 # parent.child[left] = node;
7+
stxdw [r2 + TREE_NODE_CHILD_L_OFF], r9 # parent.child[left] = node;
88
ja insert_fixup_main
99
insert_get_child_dir_branch_r:
10-
stxdw [r2 + TREE_NODE_CHILD_R_OFF], r9 # parent.child[right] = node;
10+
stxdw [r2 + TREE_NODE_CHILD_R_OFF], r9 # parent.child[right] = node;
1111

1212
insert_fixup_main:
1313
# Case 1.
1414
# ---------------------------------------------------------------------
15-
ldxb r6, [r2 + TREE_NODE_COLOR_OFF] # r6 = parent.color;
15+
ldxb r6, [r2 + TREE_NODE_COLOR_OFF] # r6 = parent.color;
1616
jne r6, TREE_COLOR_B, insert_fixup_check_case_4
1717
exit # If parent is black, tree is still valid, so exit.
1818

1919
insert_fixup_check_case_4:
2020
# Check case 4.
2121
# ---------------------------------------------------------------------
22-
ldxdw r3, [r2 + TREE_NODE_PARENT_OFF] # r3 = grandparent;
22+
ldxdw r3, [r2 + TREE_NODE_PARENT_OFF] # r3 = grandparent;
2323
jne r3, NULL, insert_fixup_check_case_5_6
24-
stb [r2 + TREE_NODE_COLOR_OFF], TREE_COLOR_B # parent.color = black;
24+
stb [r2 + TREE_NODE_COLOR_OFF], TREE_COLOR_B # parent.color = black;
2525
exit
2626

2727
insert_fixup_check_case_5_6:
28-
# Get parent's direction as a child.
28+
# Get uncle and check for case 5 or 6.
2929
# ---------------------------------------------------------------------
30-
exit
30+
ldxh r4, [r3 + TREE_NODE_KEY_OFF] # r4 = grandparent.key;
31+
jgt r5, r4, insert_fixup_check_case_5_6_dir_r
32+
33+
insert_fixup_check_case_5_6_dir_l:
34+
ldxdw r7, [r3 + TREE_NODE_CHILD_R_OFF] # r7 = uncle;
35+
jeq r7, NULL, insert_fixup_case_5_6_dir_l
36+
ldxb r8, [r7 + TREE_NODE_COLOR_OFF] # r8 = uncle.color;
37+
jne r8, TREE_COLOR_B, insert_fixup_case_2
38+
39+
insert_fixup_case_5_6_dir_l:
40+
ldxdw r8, [r2 + TREE_NODE_CHILD_R_OFF] # r8 = parent.child[right];
41+
jne r9, r8, insert_fixup_case_6_dir_l
42+
43+
insert_fixup_case_5_dir_l:
44+
# rotate_subtree(tree_header, parent, left);
45+
# ---------------------------------------------------------------------
46+
47+
insert_fixup_case_6_dir_l:
48+
exit
49+
50+
insert_fixup_check_case_5_6_dir_r:
51+
ldxdw r7, [r3 + TREE_NODE_CHILD_L_OFF] # r7 = uncle;
52+
jeq r7, NULL, insert_fixup_case_5_6_dir_r
53+
ldxb r8, [r7 + TREE_NODE_COLOR_OFF] # r8 = uncle.color;
54+
jne r8, TREE_COLOR_B, insert_fixup_case_2
55+
56+
insert_fixup_case_5_6_dir_r:
57+
ldxdw r8, [r2 + TREE_NODE_CHILD_L_OFF] # r8 = parent.child[left];
58+
jne r9, r8, insert_fixup_case_6_dir_r
59+
60+
insert_fixup_case_5_dir_r:
61+
# rotate_subtree(tree_header, parent, right);
62+
# ---------------------------------------------------------------------
63+
64+
insert_fixup_case_6_dir_r:
65+
exit
66+
67+
insert_fixup_case_2:
68+
stb [r2 + TREE_NODE_COLOR_OFF], TREE_COLOR_B # parent.color = black;
69+
stb [r7 + TREE_NODE_COLOR_OFF], TREE_COLOR_B # uncle.color = black;
70+
stb [r3 + TREE_NODE_COLOR_OFF], TREE_COLOR_R # grandparent.color = red;
71+
mov64 r9, r3 # r9 = node = grandparent;
72+
ldxdw r2, [r9 + TREE_NODE_PARENT_OFF] # r2 = parent = node.parent;
73+
jne r2, NULL, insert_fixup_main
74+
exit # Case 3.
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
insert_search:
2-
ldxh r4, [r2 + INSN_INSERT_KEY_OFF] # r4 = insn.key;
3-
mov64 r2, NULL # r2 = parent = null;
4-
ldxdw r3, [r1 + IB_TREE_DATA_ROOT_OFF] # r3 = cursor = root;
1+
insert_search: # r9 = node
2+
ldxh r4, [r2 + INSN_INSERT_KEY_OFF] # r4 = insn.key;
3+
mov64 r2, NULL # r2 = parent = null;
4+
ldxdw r3, [r1 + IB_TREE_DATA_ROOT_OFF] # r3 = cursor = root;
55

66
insert_search_loop:
77
jeq r3, NULL, insert_to_tree
8-
mov64 r2, r3 # r2 = parent = cursor;
9-
ldxh r5, [r3 + TREE_NODE_KEY_OFF] # r5 = cursor.key;
8+
mov64 r2, r3 # r2 = parent = cursor;
9+
ldxh r5, [r3 + TREE_NODE_KEY_OFF] # r5 = cursor.key;
1010
jlt r4, r5, insert_search_branch_l
1111
jgt r4, r5, insert_search_branch_r
1212
mov64 r0, E_KEY_EXISTS # Error if key already exists.
1313
exit
1414
insert_search_branch_l:
15-
ldxdw r3, [r3 + TREE_NODE_CHILD_L_OFF] # r3 = cursor.child[left];
15+
ldxdw r3, [r3 + TREE_NODE_CHILD_L_OFF] # r3 = cursor.child[left];
1616
ja insert_search_loop
1717
insert_search_branch_r:
18-
ldxdw r3, [r3 + TREE_NODE_CHILD_R_OFF] # r3 = cursor.child[right];
18+
ldxdw r3, [r3 + TREE_NODE_CHILD_R_OFF] # r3 = cursor.child[right];
1919
ja insert_search_loop
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
insert_to_tree:
22
# Flag new node as red and store parent pointer.
33
# ---------------------------------------------------------------------
4-
stb [r9 + TREE_NODE_COLOR_OFF], TREE_COLOR_R # node.color = red;
5-
stxdw [r9 + TREE_NODE_PARENT_OFF], r2 # node.parent = parent;
4+
stb [r9 + TREE_NODE_COLOR_OFF], TREE_COLOR_R # node.color = red;
5+
stxdw [r9 + TREE_NODE_PARENT_OFF], r2 # node.parent = parent;
66

77
# Handle case of new node at root.
88
# ---------------------------------------------------------------------
99
jne r2, NULL, insert_get_child_dir
10-
stxdw [r1 + IB_TREE_DATA_ROOT_OFF], r9 # root = node;
10+
stxdw [r1 + IB_TREE_DATA_ROOT_OFF], r9 # root = node;
1111
exit # Parent is null, new node at root.

‎examples/tree/src/tests.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,5 +200,5 @@ fn test_insert_search() {
200200

201201
#[test]
202202
fn test_insert_to_tree() {
203-
print_comparison_table(insert_tree::InsertTreeCase::TREE_CASES, true, false);
203+
print_comparison_table(insert_tree::InsertTreeCase::TREE_CASES, false, false);
204204
}

‎examples/tree/src/tree/tree.s‎

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -662,72 +662,116 @@ insert_store_key_value_pair:
662662
# ANCHOR_END: insert-allocate
663663

664664
# ANCHOR: insert-search
665-
insert_search:
666-
ldxh r4, [r2 + INSN_INSERT_KEY_OFF] # r4 = insn.key;
667-
mov64 r2, NULL # r2 = parent = null;
668-
ldxdw r3, [r1 + IB_TREE_DATA_ROOT_OFF] # r3 = cursor = root;
665+
insert_search: # r9 = node
666+
ldxh r4, [r2 + INSN_INSERT_KEY_OFF] # r4 = insn.key;
667+
mov64 r2, NULL # r2 = parent = null;
668+
ldxdw r3, [r1 + IB_TREE_DATA_ROOT_OFF] # r3 = cursor = root;
669669

670670
insert_search_loop:
671671
jeq r3, NULL, insert_to_tree
672-
mov64 r2, r3 # r2 = parent = cursor;
673-
ldxh r5, [r3 + TREE_NODE_KEY_OFF] # r5 = cursor.key;
672+
mov64 r2, r3 # r2 = parent = cursor;
673+
ldxh r5, [r3 + TREE_NODE_KEY_OFF] # r5 = cursor.key;
674674
jlt r4, r5, insert_search_branch_l
675675
jgt r4, r5, insert_search_branch_r
676676
mov64 r0, E_KEY_EXISTS # Error if key already exists.
677677
exit
678678
insert_search_branch_l:
679-
ldxdw r3, [r3 + TREE_NODE_CHILD_L_OFF] # r3 = cursor.child[left];
679+
ldxdw r3, [r3 + TREE_NODE_CHILD_L_OFF] # r3 = cursor.child[left];
680680
ja insert_search_loop
681681
insert_search_branch_r:
682-
ldxdw r3, [r3 + TREE_NODE_CHILD_R_OFF] # r3 = cursor.child[right];
682+
ldxdw r3, [r3 + TREE_NODE_CHILD_R_OFF] # r3 = cursor.child[right];
683683
ja insert_search_loop
684684
# ANCHOR_END: insert-search
685685

686686
# ANCHOR: insert-to-tree
687687
insert_to_tree:
688688
# Flag new node as red and store parent pointer.
689689
# ---------------------------------------------------------------------
690-
stb [r9 + TREE_NODE_COLOR_OFF], TREE_COLOR_R # node.color = red;
691-
stxdw [r9 + TREE_NODE_PARENT_OFF], r2 # node.parent = parent;
690+
stb [r9 + TREE_NODE_COLOR_OFF], TREE_COLOR_R # node.color = red;
691+
stxdw [r9 + TREE_NODE_PARENT_OFF], r2 # node.parent = parent;
692692

693693
# Handle case of new node at root.
694694
# ---------------------------------------------------------------------
695695
jne r2, NULL, insert_get_child_dir
696-
stxdw [r1 + IB_TREE_DATA_ROOT_OFF], r9 # root = node;
696+
stxdw [r1 + IB_TREE_DATA_ROOT_OFF], r9 # root = node;
697697
exit # Parent is null, new node at root.
698698
# ANCHOR_END: insert-to-tree
699699

700700
# ANCHOR: insert-fixup
701701
insert_get_child_dir:
702702
# Get child direction, set at parent.
703703
# ---------------------------------------------------------------------
704-
ldxh r5, [r2 + TREE_NODE_KEY_OFF] # r5 = parent.key;
704+
ldxh r5, [r2 + TREE_NODE_KEY_OFF] # r5 = parent.key;
705705
jgt r4, r5, insert_get_child_dir_branch_r
706706
insert_get_child_dir_branch_l:
707-
stxdw [r2 + TREE_NODE_CHILD_L_OFF], r9 # parent.child[left] = node;
707+
stxdw [r2 + TREE_NODE_CHILD_L_OFF], r9 # parent.child[left] = node;
708708
ja insert_fixup_main
709709
insert_get_child_dir_branch_r:
710-
stxdw [r2 + TREE_NODE_CHILD_R_OFF], r9 # parent.child[right] = node;
710+
stxdw [r2 + TREE_NODE_CHILD_R_OFF], r9 # parent.child[right] = node;
711711

712712
insert_fixup_main:
713713
# Case 1.
714714
# ---------------------------------------------------------------------
715-
ldxb r6, [r2 + TREE_NODE_COLOR_OFF] # r6 = parent.color;
715+
ldxb r6, [r2 + TREE_NODE_COLOR_OFF] # r6 = parent.color;
716716
jne r6, TREE_COLOR_B, insert_fixup_check_case_4
717717
exit # If parent is black, tree is still valid, so exit.
718718

719719
insert_fixup_check_case_4:
720720
# Check case 4.
721721
# ---------------------------------------------------------------------
722-
ldxdw r3, [r2 + TREE_NODE_PARENT_OFF] # r3 = grandparent;
722+
ldxdw r3, [r2 + TREE_NODE_PARENT_OFF] # r3 = grandparent;
723723
jne r3, NULL, insert_fixup_check_case_5_6
724-
stb [r2 + TREE_NODE_COLOR_OFF], TREE_COLOR_B # parent.color = black;
724+
stb [r2 + TREE_NODE_COLOR_OFF], TREE_COLOR_B # parent.color = black;
725725
exit
726726

727727
insert_fixup_check_case_5_6:
728-
# Get parent's direction as a child.
728+
# Get uncle and check for case 5 or 6.
729729
# ---------------------------------------------------------------------
730+
ldxh r4, [r3 + TREE_NODE_KEY_OFF] # r4 = grandparent.key;
731+
jgt r5, r4, insert_fixup_check_case_5_6_dir_r
732+
733+
insert_fixup_check_case_5_6_dir_l:
734+
ldxdw r7, [r3 + TREE_NODE_CHILD_R_OFF] # r7 = uncle;
735+
jeq r7, NULL, insert_fixup_case_5_6_dir_l
736+
ldxb r8, [r7 + TREE_NODE_COLOR_OFF] # r8 = uncle.color;
737+
jne r8, TREE_COLOR_B, insert_fixup_case_2
738+
739+
insert_fixup_case_5_6_dir_l:
740+
ldxdw r8, [r2 + TREE_NODE_CHILD_R_OFF] # r8 = parent.child[right];
741+
jne r9, r8, insert_fixup_case_6_dir_l
742+
743+
insert_fixup_case_5_dir_l:
744+
# rotate_subtree(tree_header, parent, left);
745+
# ---------------------------------------------------------------------
746+
747+
insert_fixup_case_6_dir_l:
748+
exit
749+
750+
insert_fixup_check_case_5_6_dir_r:
751+
ldxdw r7, [r3 + TREE_NODE_CHILD_L_OFF] # r7 = uncle;
752+
jeq r7, NULL, insert_fixup_case_5_6_dir_r
753+
ldxb r8, [r7 + TREE_NODE_COLOR_OFF] # r8 = uncle.color;
754+
jne r8, TREE_COLOR_B, insert_fixup_case_2
755+
756+
insert_fixup_case_5_6_dir_r:
757+
ldxdw r8, [r2 + TREE_NODE_CHILD_L_OFF] # r8 = parent.child[left];
758+
jne r9, r8, insert_fixup_case_6_dir_r
759+
760+
insert_fixup_case_5_dir_r:
761+
# rotate_subtree(tree_header, parent, right);
762+
# ---------------------------------------------------------------------
763+
764+
insert_fixup_case_6_dir_r:
730765
exit
766+
767+
insert_fixup_case_2:
768+
stb [r2 + TREE_NODE_COLOR_OFF], TREE_COLOR_B # parent.color = black;
769+
stb [r7 + TREE_NODE_COLOR_OFF], TREE_COLOR_B # uncle.color = black;
770+
stb [r3 + TREE_NODE_COLOR_OFF], TREE_COLOR_R # grandparent.color = red;
771+
mov64 r9, r3 # r9 = node = grandparent;
772+
ldxdw r2, [r9 + TREE_NODE_PARENT_OFF] # r2 = parent = node.parent;
773+
jne r2, NULL, insert_fixup_main
774+
exit # Case 3.
731775
# ANCHOR_END: insert-fixup
732776

733777

0 commit comments

Comments
 (0)