Rev 1483 | Rev 1704 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1483 | Rev 1702 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2006 Jakub Jermar |
2 | * Copyright (C) 2006 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
- | 29 | /** @addtogroup genericadt |
|
- | 30 | * @{ |
|
- | 31 | */ |
|
- | 32 | ||
29 | /** |
33 | /** |
30 | * @file btree.c |
34 | * @file btree.c |
31 | * @brief B+tree implementation. |
35 | * @brief B+tree implementation. |
32 | * |
36 | * |
33 | * This file implements B+tree type and operations. |
37 | * This file implements B+tree type and operations. |
34 | * |
38 | * |
35 | * The B+tree has the following properties: |
39 | * The B+tree has the following properties: |
36 | * @li it is a ballanced 3-4-5 tree (i.e. BTREE_M = 5) |
40 | * @li it is a ballanced 3-4-5 tree (i.e. BTREE_M = 5) |
37 | * @li values (i.e. pointers to values) are stored only in leaves |
41 | * @li values (i.e. pointers to values) are stored only in leaves |
38 | * @li leaves are linked in a list |
42 | * @li leaves are linked in a list |
39 | * |
43 | * |
40 | * Be carefull when using these trees. They need to allocate |
44 | * Be carefull when using these trees. They need to allocate |
41 | * and deallocate memory for their index nodes and as such |
45 | * and deallocate memory for their index nodes and as such |
42 | * can sleep. |
46 | * can sleep. |
43 | */ |
47 | */ |
44 | 48 | ||
45 | #include <adt/btree.h> |
49 | #include <adt/btree.h> |
46 | #include <adt/list.h> |
50 | #include <adt/list.h> |
47 | #include <mm/slab.h> |
51 | #include <mm/slab.h> |
48 | #include <debug.h> |
52 | #include <debug.h> |
49 | #include <panic.h> |
53 | #include <panic.h> |
50 | #include <typedefs.h> |
54 | #include <typedefs.h> |
51 | #include <print.h> |
55 | #include <print.h> |
52 | 56 | ||
53 | static void btree_destroy_subtree(btree_node_t *root); |
57 | static void btree_destroy_subtree(btree_node_t *root); |
54 | static void _btree_insert(btree_t *t, btree_key_t key, void *value, btree_node_t *rsubtree, btree_node_t *node); |
58 | static void _btree_insert(btree_t *t, btree_key_t key, void *value, btree_node_t *rsubtree, btree_node_t *node); |
55 | static void _btree_remove(btree_t *t, btree_key_t key, btree_node_t *node); |
59 | static void _btree_remove(btree_t *t, btree_key_t key, btree_node_t *node); |
56 | static void node_initialize(btree_node_t *node); |
60 | static void node_initialize(btree_node_t *node); |
57 | static void node_insert_key_and_lsubtree(btree_node_t *node, btree_key_t key, void *value, btree_node_t *lsubtree); |
61 | static void node_insert_key_and_lsubtree(btree_node_t *node, btree_key_t key, void *value, btree_node_t *lsubtree); |
58 | static void node_insert_key_and_rsubtree(btree_node_t *node, btree_key_t key, void *value, btree_node_t *rsubtree); |
62 | static void node_insert_key_and_rsubtree(btree_node_t *node, btree_key_t key, void *value, btree_node_t *rsubtree); |
59 | static void node_remove_key_and_lsubtree(btree_node_t *node, btree_key_t key); |
63 | static void node_remove_key_and_lsubtree(btree_node_t *node, btree_key_t key); |
60 | static void node_remove_key_and_rsubtree(btree_node_t *node, btree_key_t key); |
64 | static void node_remove_key_and_rsubtree(btree_node_t *node, btree_key_t key); |
61 | static btree_node_t *node_split(btree_node_t *node, btree_key_t key, void *value, btree_node_t *rsubtree, btree_key_t *median); |
65 | static btree_node_t *node_split(btree_node_t *node, btree_key_t key, void *value, btree_node_t *rsubtree, btree_key_t *median); |
62 | static btree_node_t *node_combine(btree_node_t *node); |
66 | static btree_node_t *node_combine(btree_node_t *node); |
63 | static index_t find_key_by_subtree(btree_node_t *node, btree_node_t *subtree, bool right); |
67 | static index_t find_key_by_subtree(btree_node_t *node, btree_node_t *subtree, bool right); |
64 | static void rotate_from_right(btree_node_t *lnode, btree_node_t *rnode, index_t idx); |
68 | static void rotate_from_right(btree_node_t *lnode, btree_node_t *rnode, index_t idx); |
65 | static void rotate_from_left(btree_node_t *lnode, btree_node_t *rnode, index_t idx); |
69 | static void rotate_from_left(btree_node_t *lnode, btree_node_t *rnode, index_t idx); |
66 | static bool try_insert_by_rotation_to_left(btree_node_t *node, btree_key_t key, void *value, btree_node_t *rsubtree); |
70 | static bool try_insert_by_rotation_to_left(btree_node_t *node, btree_key_t key, void *value, btree_node_t *rsubtree); |
67 | static bool try_insert_by_rotation_to_right(btree_node_t *node, btree_key_t key, void *value, btree_node_t *rsubtree); |
71 | static bool try_insert_by_rotation_to_right(btree_node_t *node, btree_key_t key, void *value, btree_node_t *rsubtree); |
68 | static bool try_rotation_from_left(btree_node_t *rnode); |
72 | static bool try_rotation_from_left(btree_node_t *rnode); |
69 | static bool try_rotation_from_right(btree_node_t *lnode); |
73 | static bool try_rotation_from_right(btree_node_t *lnode); |
70 | 74 | ||
71 | #define ROOT_NODE(n) (!(n)->parent) |
75 | #define ROOT_NODE(n) (!(n)->parent) |
72 | #define INDEX_NODE(n) ((n)->subtree[0] != NULL) |
76 | #define INDEX_NODE(n) ((n)->subtree[0] != NULL) |
73 | #define LEAF_NODE(n) ((n)->subtree[0] == NULL) |
77 | #define LEAF_NODE(n) ((n)->subtree[0] == NULL) |
74 | 78 | ||
75 | #define FILL_FACTOR ((BTREE_M-1)/2) |
79 | #define FILL_FACTOR ((BTREE_M-1)/2) |
76 | 80 | ||
77 | #define MEDIAN_LOW_INDEX(n) (((n)->keys-1)/2) |
81 | #define MEDIAN_LOW_INDEX(n) (((n)->keys-1)/2) |
78 | #define MEDIAN_HIGH_INDEX(n) ((n)->keys/2) |
82 | #define MEDIAN_HIGH_INDEX(n) ((n)->keys/2) |
79 | #define MEDIAN_LOW(n) ((n)->key[MEDIAN_LOW_INDEX((n))]); |
83 | #define MEDIAN_LOW(n) ((n)->key[MEDIAN_LOW_INDEX((n))]); |
80 | #define MEDIAN_HIGH(n) ((n)->key[MEDIAN_HIGH_INDEX((n))]); |
84 | #define MEDIAN_HIGH(n) ((n)->key[MEDIAN_HIGH_INDEX((n))]); |
81 | 85 | ||
82 | static slab_cache_t *btree_node_slab; |
86 | static slab_cache_t *btree_node_slab; |
83 | 87 | ||
84 | /** Initialize B-trees. */ |
88 | /** Initialize B-trees. */ |
85 | void btree_init(void) |
89 | void btree_init(void) |
86 | { |
90 | { |
87 | btree_node_slab = slab_cache_create("btree_node_slab", sizeof(btree_node_t), 0, NULL, NULL, SLAB_CACHE_MAGDEFERRED); |
91 | btree_node_slab = slab_cache_create("btree_node_slab", sizeof(btree_node_t), 0, NULL, NULL, SLAB_CACHE_MAGDEFERRED); |
88 | } |
92 | } |
89 | 93 | ||
90 | /** Create empty B-tree. |
94 | /** Create empty B-tree. |
91 | * |
95 | * |
92 | * @param t B-tree. |
96 | * @param t B-tree. |
93 | */ |
97 | */ |
94 | void btree_create(btree_t *t) |
98 | void btree_create(btree_t *t) |
95 | { |
99 | { |
96 | list_initialize(&t->leaf_head); |
100 | list_initialize(&t->leaf_head); |
97 | t->root = (btree_node_t *) slab_alloc(btree_node_slab, 0); |
101 | t->root = (btree_node_t *) slab_alloc(btree_node_slab, 0); |
98 | node_initialize(t->root); |
102 | node_initialize(t->root); |
99 | list_append(&t->root->leaf_link, &t->leaf_head); |
103 | list_append(&t->root->leaf_link, &t->leaf_head); |
100 | } |
104 | } |
101 | 105 | ||
102 | /** Destroy empty B-tree. */ |
106 | /** Destroy empty B-tree. */ |
103 | void btree_destroy(btree_t *t) |
107 | void btree_destroy(btree_t *t) |
104 | { |
108 | { |
105 | btree_destroy_subtree(t->root); |
109 | btree_destroy_subtree(t->root); |
106 | } |
110 | } |
107 | 111 | ||
108 | /** Insert key-value pair into B-tree. |
112 | /** Insert key-value pair into B-tree. |
109 | * |
113 | * |
110 | * @param t B-tree. |
114 | * @param t B-tree. |
111 | * @param key Key to be inserted. |
115 | * @param key Key to be inserted. |
112 | * @param value Value to be inserted. |
116 | * @param value Value to be inserted. |
113 | * @param leaf_node Leaf node where the insertion should begin. |
117 | * @param leaf_node Leaf node where the insertion should begin. |
114 | */ |
118 | */ |
115 | void btree_insert(btree_t *t, btree_key_t key, void *value, btree_node_t *leaf_node) |
119 | void btree_insert(btree_t *t, btree_key_t key, void *value, btree_node_t *leaf_node) |
116 | { |
120 | { |
117 | btree_node_t *lnode; |
121 | btree_node_t *lnode; |
118 | 122 | ||
119 | ASSERT(value); |
123 | ASSERT(value); |
120 | 124 | ||
121 | lnode = leaf_node; |
125 | lnode = leaf_node; |
122 | if (!lnode) { |
126 | if (!lnode) { |
123 | if (btree_search(t, key, &lnode)) { |
127 | if (btree_search(t, key, &lnode)) { |
124 | panic("B-tree %p already contains key %d\n", t, key); |
128 | panic("B-tree %p already contains key %d\n", t, key); |
125 | } |
129 | } |
126 | } |
130 | } |
127 | 131 | ||
128 | _btree_insert(t, key, value, NULL, lnode); |
132 | _btree_insert(t, key, value, NULL, lnode); |
129 | } |
133 | } |
130 | 134 | ||
131 | /** Destroy subtree rooted in a node. |
135 | /** Destroy subtree rooted in a node. |
132 | * |
136 | * |
133 | * @param root Root of the subtree. |
137 | * @param root Root of the subtree. |
134 | */ |
138 | */ |
135 | void btree_destroy_subtree(btree_node_t *root) |
139 | void btree_destroy_subtree(btree_node_t *root) |
136 | { |
140 | { |
137 | int i; |
141 | int i; |
138 | 142 | ||
139 | if (root->keys) { |
143 | if (root->keys) { |
140 | for (i = 0; i < root->keys + 1; i++) { |
144 | for (i = 0; i < root->keys + 1; i++) { |
141 | if (root->subtree[i]) |
145 | if (root->subtree[i]) |
142 | btree_destroy_subtree(root->subtree[i]); |
146 | btree_destroy_subtree(root->subtree[i]); |
143 | } |
147 | } |
144 | } |
148 | } |
145 | slab_free(btree_node_slab, root); |
149 | slab_free(btree_node_slab, root); |
146 | } |
150 | } |
147 | 151 | ||
148 | /** Recursively insert into B-tree. |
152 | /** Recursively insert into B-tree. |
149 | * |
153 | * |
150 | * @param t B-tree. |
154 | * @param t B-tree. |
151 | * @param key Key to be inserted. |
155 | * @param key Key to be inserted. |
152 | * @param value Value to be inserted. |
156 | * @param value Value to be inserted. |
153 | * @param rsubtree Right subtree of the inserted key. |
157 | * @param rsubtree Right subtree of the inserted key. |
154 | * @param node Start inserting into this node. |
158 | * @param node Start inserting into this node. |
155 | */ |
159 | */ |
156 | void _btree_insert(btree_t *t, btree_key_t key, void *value, btree_node_t *rsubtree, btree_node_t *node) |
160 | void _btree_insert(btree_t *t, btree_key_t key, void *value, btree_node_t *rsubtree, btree_node_t *node) |
157 | { |
161 | { |
158 | if (node->keys < BTREE_MAX_KEYS) { |
162 | if (node->keys < BTREE_MAX_KEYS) { |
159 | /* |
163 | /* |
160 | * Node conatins enough space, the key can be stored immediately. |
164 | * Node conatins enough space, the key can be stored immediately. |
161 | */ |
165 | */ |
162 | node_insert_key_and_rsubtree(node, key, value, rsubtree); |
166 | node_insert_key_and_rsubtree(node, key, value, rsubtree); |
163 | } else if (try_insert_by_rotation_to_left(node, key, value, rsubtree)) { |
167 | } else if (try_insert_by_rotation_to_left(node, key, value, rsubtree)) { |
164 | /* |
168 | /* |
165 | * The key-value-rsubtree triplet has been inserted because |
169 | * The key-value-rsubtree triplet has been inserted because |
166 | * some keys could have been moved to the left sibling. |
170 | * some keys could have been moved to the left sibling. |
167 | */ |
171 | */ |
168 | } else if (try_insert_by_rotation_to_right(node, key, value, rsubtree)) { |
172 | } else if (try_insert_by_rotation_to_right(node, key, value, rsubtree)) { |
169 | /* |
173 | /* |
170 | * The key-value-rsubtree triplet has been inserted because |
174 | * The key-value-rsubtree triplet has been inserted because |
171 | * some keys could have been moved to the right sibling. |
175 | * some keys could have been moved to the right sibling. |
172 | */ |
176 | */ |
173 | } else { |
177 | } else { |
174 | btree_node_t *rnode; |
178 | btree_node_t *rnode; |
175 | btree_key_t median; |
179 | btree_key_t median; |
176 | 180 | ||
177 | /* |
181 | /* |
178 | * Node is full and both siblings (if both exist) are full too. |
182 | * Node is full and both siblings (if both exist) are full too. |
179 | * Split the node and insert the smallest key from the node containing |
183 | * Split the node and insert the smallest key from the node containing |
180 | * bigger keys (i.e. the new node) into its parent. |
184 | * bigger keys (i.e. the new node) into its parent. |
181 | */ |
185 | */ |
182 | 186 | ||
183 | rnode = node_split(node, key, value, rsubtree, &median); |
187 | rnode = node_split(node, key, value, rsubtree, &median); |
184 | 188 | ||
185 | if (LEAF_NODE(node)) { |
189 | if (LEAF_NODE(node)) { |
186 | list_prepend(&rnode->leaf_link, &node->leaf_link); |
190 | list_prepend(&rnode->leaf_link, &node->leaf_link); |
187 | } |
191 | } |
188 | 192 | ||
189 | if (ROOT_NODE(node)) { |
193 | if (ROOT_NODE(node)) { |
190 | /* |
194 | /* |
191 | * We split the root node. Create new root. |
195 | * We split the root node. Create new root. |
192 | */ |
196 | */ |
193 | t->root = (btree_node_t *) slab_alloc(btree_node_slab, 0); |
197 | t->root = (btree_node_t *) slab_alloc(btree_node_slab, 0); |
194 | node->parent = t->root; |
198 | node->parent = t->root; |
195 | rnode->parent = t->root; |
199 | rnode->parent = t->root; |
196 | node_initialize(t->root); |
200 | node_initialize(t->root); |
197 | 201 | ||
198 | /* |
202 | /* |
199 | * Left-hand side subtree will be the old root (i.e. node). |
203 | * Left-hand side subtree will be the old root (i.e. node). |
200 | * Right-hand side subtree will be rnode. |
204 | * Right-hand side subtree will be rnode. |
201 | */ |
205 | */ |
202 | t->root->subtree[0] = node; |
206 | t->root->subtree[0] = node; |
203 | 207 | ||
204 | t->root->depth = node->depth + 1; |
208 | t->root->depth = node->depth + 1; |
205 | } |
209 | } |
206 | _btree_insert(t, median, NULL, rnode, node->parent); |
210 | _btree_insert(t, median, NULL, rnode, node->parent); |
207 | } |
211 | } |
208 | 212 | ||
209 | } |
213 | } |
210 | 214 | ||
211 | /** Remove B-tree node. |
215 | /** Remove B-tree node. |
212 | * |
216 | * |
213 | * @param B-tree. |
217 | * @param B-tree. |
214 | * @param key Key to be removed from the B-tree along with its associated value. |
218 | * @param key Key to be removed from the B-tree along with its associated value. |
215 | * @param leaf_node If not NULL, pointer to the leaf node where the key is found. |
219 | * @param leaf_node If not NULL, pointer to the leaf node where the key is found. |
216 | */ |
220 | */ |
217 | void btree_remove(btree_t *t, btree_key_t key, btree_node_t *leaf_node) |
221 | void btree_remove(btree_t *t, btree_key_t key, btree_node_t *leaf_node) |
218 | { |
222 | { |
219 | btree_node_t *lnode; |
223 | btree_node_t *lnode; |
220 | 224 | ||
221 | lnode = leaf_node; |
225 | lnode = leaf_node; |
222 | if (!lnode) { |
226 | if (!lnode) { |
223 | if (!btree_search(t, key, &lnode)) { |
227 | if (!btree_search(t, key, &lnode)) { |
224 | panic("B-tree %p does not contain key %d\n", t, key); |
228 | panic("B-tree %p does not contain key %d\n", t, key); |
225 | } |
229 | } |
226 | } |
230 | } |
227 | 231 | ||
228 | _btree_remove(t, key, lnode); |
232 | _btree_remove(t, key, lnode); |
229 | } |
233 | } |
230 | 234 | ||
231 | /** Recursively remove B-tree node. |
235 | /** Recursively remove B-tree node. |
232 | * |
236 | * |
233 | * @param B-tree. |
237 | * @param B-tree. |
234 | * @param key Key to be removed from the B-tree along with its associated value. |
238 | * @param key Key to be removed from the B-tree along with its associated value. |
235 | * @param node Node where the key being removed resides. |
239 | * @param node Node where the key being removed resides. |
236 | */ |
240 | */ |
237 | void _btree_remove(btree_t *t, btree_key_t key, btree_node_t *node) |
241 | void _btree_remove(btree_t *t, btree_key_t key, btree_node_t *node) |
238 | { |
242 | { |
239 | if (ROOT_NODE(node)) { |
243 | if (ROOT_NODE(node)) { |
240 | if (node->keys == 1 && node->subtree[0]) { |
244 | if (node->keys == 1 && node->subtree[0]) { |
241 | /* |
245 | /* |
242 | * Free the current root and set new root. |
246 | * Free the current root and set new root. |
243 | */ |
247 | */ |
244 | t->root = node->subtree[0]; |
248 | t->root = node->subtree[0]; |
245 | t->root->parent = NULL; |
249 | t->root->parent = NULL; |
246 | slab_free(btree_node_slab, node); |
250 | slab_free(btree_node_slab, node); |
247 | } else { |
251 | } else { |
248 | /* |
252 | /* |
249 | * Remove the key from the root node. |
253 | * Remove the key from the root node. |
250 | * Note that the right subtree is removed because when |
254 | * Note that the right subtree is removed because when |
251 | * combining two nodes, the left-side sibling is preserved |
255 | * combining two nodes, the left-side sibling is preserved |
252 | * and the right-side sibling is freed. |
256 | * and the right-side sibling is freed. |
253 | */ |
257 | */ |
254 | node_remove_key_and_rsubtree(node, key); |
258 | node_remove_key_and_rsubtree(node, key); |
255 | } |
259 | } |
256 | return; |
260 | return; |
257 | } |
261 | } |
258 | 262 | ||
259 | if (node->keys <= FILL_FACTOR) { |
263 | if (node->keys <= FILL_FACTOR) { |
260 | /* |
264 | /* |
261 | * If the node is below the fill factor, |
265 | * If the node is below the fill factor, |
262 | * try to borrow keys from left or right sibling. |
266 | * try to borrow keys from left or right sibling. |
263 | */ |
267 | */ |
264 | if (!try_rotation_from_left(node)) |
268 | if (!try_rotation_from_left(node)) |
265 | try_rotation_from_right(node); |
269 | try_rotation_from_right(node); |
266 | } |
270 | } |
267 | 271 | ||
268 | if (node->keys > FILL_FACTOR) { |
272 | if (node->keys > FILL_FACTOR) { |
269 | int i; |
273 | int i; |
270 | 274 | ||
271 | /* |
275 | /* |
272 | * The key can be immediatelly removed. |
276 | * The key can be immediatelly removed. |
273 | * |
277 | * |
274 | * Note that the right subtree is removed because when |
278 | * Note that the right subtree is removed because when |
275 | * combining two nodes, the left-side sibling is preserved |
279 | * combining two nodes, the left-side sibling is preserved |
276 | * and the right-side sibling is freed. |
280 | * and the right-side sibling is freed. |
277 | */ |
281 | */ |
278 | node_remove_key_and_rsubtree(node, key); |
282 | node_remove_key_and_rsubtree(node, key); |
279 | for (i = 0; i < node->parent->keys; i++) { |
283 | for (i = 0; i < node->parent->keys; i++) { |
280 | if (node->parent->key[i] == key) |
284 | if (node->parent->key[i] == key) |
281 | node->parent->key[i] = node->key[0]; |
285 | node->parent->key[i] = node->key[0]; |
282 | } |
286 | } |
283 | 287 | ||
284 | } else { |
288 | } else { |
285 | index_t idx; |
289 | index_t idx; |
286 | btree_node_t *rnode, *parent; |
290 | btree_node_t *rnode, *parent; |
287 | 291 | ||
288 | /* |
292 | /* |
289 | * The node is below the fill factor as well as its left and right sibling. |
293 | * The node is below the fill factor as well as its left and right sibling. |
290 | * Resort to combining the node with one of its siblings. |
294 | * Resort to combining the node with one of its siblings. |
291 | * The node which is on the left is preserved and the node on the right is |
295 | * The node which is on the left is preserved and the node on the right is |
292 | * freed. |
296 | * freed. |
293 | */ |
297 | */ |
294 | parent = node->parent; |
298 | parent = node->parent; |
295 | node_remove_key_and_rsubtree(node, key); |
299 | node_remove_key_and_rsubtree(node, key); |
296 | rnode = node_combine(node); |
300 | rnode = node_combine(node); |
297 | if (LEAF_NODE(rnode)) |
301 | if (LEAF_NODE(rnode)) |
298 | list_remove(&rnode->leaf_link); |
302 | list_remove(&rnode->leaf_link); |
299 | idx = find_key_by_subtree(parent, rnode, true); |
303 | idx = find_key_by_subtree(parent, rnode, true); |
300 | ASSERT((int) idx != -1); |
304 | ASSERT((int) idx != -1); |
301 | slab_free(btree_node_slab, rnode); |
305 | slab_free(btree_node_slab, rnode); |
302 | _btree_remove(t, parent->key[idx], parent); |
306 | _btree_remove(t, parent->key[idx], parent); |
303 | } |
307 | } |
304 | } |
308 | } |
305 | 309 | ||
306 | /** Search key in a B-tree. |
310 | /** Search key in a B-tree. |
307 | * |
311 | * |
308 | * @param t B-tree. |
312 | * @param t B-tree. |
309 | * @param key Key to be searched. |
313 | * @param key Key to be searched. |
310 | * @param leaf_node Address where to put pointer to visited leaf node. |
314 | * @param leaf_node Address where to put pointer to visited leaf node. |
311 | * |
315 | * |
312 | * @return Pointer to value or NULL if there is no such key. |
316 | * @return Pointer to value or NULL if there is no such key. |
313 | */ |
317 | */ |
314 | void *btree_search(btree_t *t, btree_key_t key, btree_node_t **leaf_node) |
318 | void *btree_search(btree_t *t, btree_key_t key, btree_node_t **leaf_node) |
315 | { |
319 | { |
316 | btree_node_t *cur, *next; |
320 | btree_node_t *cur, *next; |
317 | 321 | ||
318 | /* |
322 | /* |
319 | * Iteratively descend to the leaf that can contain the searched key. |
323 | * Iteratively descend to the leaf that can contain the searched key. |
320 | */ |
324 | */ |
321 | for (cur = t->root; cur; cur = next) { |
325 | for (cur = t->root; cur; cur = next) { |
322 | 326 | ||
323 | /* Last iteration will set this with proper leaf node address. */ |
327 | /* Last iteration will set this with proper leaf node address. */ |
324 | *leaf_node = cur; |
328 | *leaf_node = cur; |
325 | 329 | ||
326 | /* |
330 | /* |
327 | * The key can be in the leftmost subtree. |
331 | * The key can be in the leftmost subtree. |
328 | * Test it separately. |
332 | * Test it separately. |
329 | */ |
333 | */ |
330 | if (key < cur->key[0]) { |
334 | if (key < cur->key[0]) { |
331 | next = cur->subtree[0]; |
335 | next = cur->subtree[0]; |
332 | continue; |
336 | continue; |
333 | } else { |
337 | } else { |
334 | void *val; |
338 | void *val; |
335 | int i; |
339 | int i; |
336 | 340 | ||
337 | /* |
341 | /* |
338 | * Now if the key is smaller than cur->key[i] |
342 | * Now if the key is smaller than cur->key[i] |
339 | * it can only mean that the value is in cur->subtree[i] |
343 | * it can only mean that the value is in cur->subtree[i] |
340 | * or it is not in the tree at all. |
344 | * or it is not in the tree at all. |
341 | */ |
345 | */ |
342 | for (i = 1; i < cur->keys; i++) { |
346 | for (i = 1; i < cur->keys; i++) { |
343 | if (key < cur->key[i]) { |
347 | if (key < cur->key[i]) { |
344 | next = cur->subtree[i]; |
348 | next = cur->subtree[i]; |
345 | val = cur->value[i - 1]; |
349 | val = cur->value[i - 1]; |
346 | 350 | ||
347 | if (LEAF_NODE(cur)) |
351 | if (LEAF_NODE(cur)) |
348 | return key == cur->key[i - 1] ? val : NULL; |
352 | return key == cur->key[i - 1] ? val : NULL; |
349 | 353 | ||
350 | goto descend; |
354 | goto descend; |
351 | } |
355 | } |
352 | } |
356 | } |
353 | 357 | ||
354 | /* |
358 | /* |
355 | * Last possibility is that the key is in the rightmost subtree. |
359 | * Last possibility is that the key is in the rightmost subtree. |
356 | */ |
360 | */ |
357 | next = cur->subtree[i]; |
361 | next = cur->subtree[i]; |
358 | val = cur->value[i - 1]; |
362 | val = cur->value[i - 1]; |
359 | if (LEAF_NODE(cur)) |
363 | if (LEAF_NODE(cur)) |
360 | return key == cur->key[i - 1] ? val : NULL; |
364 | return key == cur->key[i - 1] ? val : NULL; |
361 | } |
365 | } |
362 | descend: |
366 | descend: |
363 | ; |
367 | ; |
364 | } |
368 | } |
365 | 369 | ||
366 | /* |
370 | /* |
367 | * The key was not found in the *leaf_node and is smaller than any of its keys. |
371 | * The key was not found in the *leaf_node and is smaller than any of its keys. |
368 | */ |
372 | */ |
369 | return NULL; |
373 | return NULL; |
370 | } |
374 | } |
371 | 375 | ||
372 | /** Return pointer to B-tree leaf node's left neighbour. |
376 | /** Return pointer to B-tree leaf node's left neighbour. |
373 | * |
377 | * |
374 | * @param t B-tree. |
378 | * @param t B-tree. |
375 | * @param node Node whose left neighbour will be returned. |
379 | * @param node Node whose left neighbour will be returned. |
376 | * |
380 | * |
377 | * @return Left neighbour of the node or NULL if the node does not have the left neighbour. |
381 | * @return Left neighbour of the node or NULL if the node does not have the left neighbour. |
378 | */ |
382 | */ |
379 | btree_node_t *btree_leaf_node_left_neighbour(btree_t *t, btree_node_t *node) |
383 | btree_node_t *btree_leaf_node_left_neighbour(btree_t *t, btree_node_t *node) |
380 | { |
384 | { |
381 | ASSERT(LEAF_NODE(node)); |
385 | ASSERT(LEAF_NODE(node)); |
382 | if (node->leaf_link.prev != &t->leaf_head) |
386 | if (node->leaf_link.prev != &t->leaf_head) |
383 | return list_get_instance(node->leaf_link.prev, btree_node_t, leaf_link); |
387 | return list_get_instance(node->leaf_link.prev, btree_node_t, leaf_link); |
384 | else |
388 | else |
385 | return NULL; |
389 | return NULL; |
386 | } |
390 | } |
387 | 391 | ||
388 | /** Return pointer to B-tree leaf node's right neighbour. |
392 | /** Return pointer to B-tree leaf node's right neighbour. |
389 | * |
393 | * |
390 | * @param t B-tree. |
394 | * @param t B-tree. |
391 | * @param node Node whose right neighbour will be returned. |
395 | * @param node Node whose right neighbour will be returned. |
392 | * |
396 | * |
393 | * @return Right neighbour of the node or NULL if the node does not have the right neighbour. |
397 | * @return Right neighbour of the node or NULL if the node does not have the right neighbour. |
394 | */ |
398 | */ |
395 | btree_node_t *btree_leaf_node_right_neighbour(btree_t *t, btree_node_t *node) |
399 | btree_node_t *btree_leaf_node_right_neighbour(btree_t *t, btree_node_t *node) |
396 | { |
400 | { |
397 | ASSERT(LEAF_NODE(node)); |
401 | ASSERT(LEAF_NODE(node)); |
398 | if (node->leaf_link.next != &t->leaf_head) |
402 | if (node->leaf_link.next != &t->leaf_head) |
399 | return list_get_instance(node->leaf_link.next, btree_node_t, leaf_link); |
403 | return list_get_instance(node->leaf_link.next, btree_node_t, leaf_link); |
400 | else |
404 | else |
401 | return NULL; |
405 | return NULL; |
402 | } |
406 | } |
403 | 407 | ||
404 | /** Initialize B-tree node. |
408 | /** Initialize B-tree node. |
405 | * |
409 | * |
406 | * @param node B-tree node. |
410 | * @param node B-tree node. |
407 | */ |
411 | */ |
408 | void node_initialize(btree_node_t *node) |
412 | void node_initialize(btree_node_t *node) |
409 | { |
413 | { |
410 | int i; |
414 | int i; |
411 | 415 | ||
412 | node->keys = 0; |
416 | node->keys = 0; |
413 | 417 | ||
414 | /* Clean also space for the extra key. */ |
418 | /* Clean also space for the extra key. */ |
415 | for (i = 0; i < BTREE_MAX_KEYS + 1; i++) { |
419 | for (i = 0; i < BTREE_MAX_KEYS + 1; i++) { |
416 | node->key[i] = 0; |
420 | node->key[i] = 0; |
417 | node->value[i] = NULL; |
421 | node->value[i] = NULL; |
418 | node->subtree[i] = NULL; |
422 | node->subtree[i] = NULL; |
419 | } |
423 | } |
420 | node->subtree[i] = NULL; |
424 | node->subtree[i] = NULL; |
421 | 425 | ||
422 | node->parent = NULL; |
426 | node->parent = NULL; |
423 | 427 | ||
424 | link_initialize(&node->leaf_link); |
428 | link_initialize(&node->leaf_link); |
425 | 429 | ||
426 | link_initialize(&node->bfs_link); |
430 | link_initialize(&node->bfs_link); |
427 | node->depth = 0; |
431 | node->depth = 0; |
428 | } |
432 | } |
429 | 433 | ||
430 | /** Insert key-value-lsubtree triplet into B-tree node. |
434 | /** Insert key-value-lsubtree triplet into B-tree node. |
431 | * |
435 | * |
432 | * It is actually possible to have more keys than BTREE_MAX_KEYS. |
436 | * It is actually possible to have more keys than BTREE_MAX_KEYS. |
433 | * This feature is used during insert by right rotation. |
437 | * This feature is used during insert by right rotation. |
434 | * |
438 | * |
435 | * @param node B-tree node into wich the new key is to be inserted. |
439 | * @param node B-tree node into wich the new key is to be inserted. |
436 | * @param key The key to be inserted. |
440 | * @param key The key to be inserted. |
437 | * @param value Pointer to value to be inserted. |
441 | * @param value Pointer to value to be inserted. |
438 | * @param lsubtree Pointer to the left subtree. |
442 | * @param lsubtree Pointer to the left subtree. |
439 | */ |
443 | */ |
440 | void node_insert_key_and_lsubtree(btree_node_t *node, btree_key_t key, void *value, btree_node_t *lsubtree) |
444 | void node_insert_key_and_lsubtree(btree_node_t *node, btree_key_t key, void *value, btree_node_t *lsubtree) |
441 | { |
445 | { |
442 | int i; |
446 | int i; |
443 | 447 | ||
444 | for (i = 0; i < node->keys; i++) { |
448 | for (i = 0; i < node->keys; i++) { |
445 | if (key < node->key[i]) { |
449 | if (key < node->key[i]) { |
446 | int j; |
450 | int j; |
447 | 451 | ||
448 | for (j = node->keys; j > i; j--) { |
452 | for (j = node->keys; j > i; j--) { |
449 | node->key[j] = node->key[j - 1]; |
453 | node->key[j] = node->key[j - 1]; |
450 | node->value[j] = node->value[j - 1]; |
454 | node->value[j] = node->value[j - 1]; |
451 | node->subtree[j + 1] = node->subtree[j]; |
455 | node->subtree[j + 1] = node->subtree[j]; |
452 | } |
456 | } |
453 | node->subtree[j + 1] = node->subtree[j]; |
457 | node->subtree[j + 1] = node->subtree[j]; |
454 | break; |
458 | break; |
455 | } |
459 | } |
456 | } |
460 | } |
457 | node->key[i] = key; |
461 | node->key[i] = key; |
458 | node->value[i] = value; |
462 | node->value[i] = value; |
459 | node->subtree[i] = lsubtree; |
463 | node->subtree[i] = lsubtree; |
460 | 464 | ||
461 | node->keys++; |
465 | node->keys++; |
462 | } |
466 | } |
463 | 467 | ||
464 | /** Insert key-value-rsubtree triplet into B-tree node. |
468 | /** Insert key-value-rsubtree triplet into B-tree node. |
465 | * |
469 | * |
466 | * It is actually possible to have more keys than BTREE_MAX_KEYS. |
470 | * It is actually possible to have more keys than BTREE_MAX_KEYS. |
467 | * This feature is used during splitting the node when the |
471 | * This feature is used during splitting the node when the |
468 | * number of keys is BTREE_MAX_KEYS + 1. Insert by left rotation |
472 | * number of keys is BTREE_MAX_KEYS + 1. Insert by left rotation |
469 | * also makes use of this feature. |
473 | * also makes use of this feature. |
470 | * |
474 | * |
471 | * @param node B-tree node into wich the new key is to be inserted. |
475 | * @param node B-tree node into wich the new key is to be inserted. |
472 | * @param key The key to be inserted. |
476 | * @param key The key to be inserted. |
473 | * @param value Pointer to value to be inserted. |
477 | * @param value Pointer to value to be inserted. |
474 | * @param rsubtree Pointer to the right subtree. |
478 | * @param rsubtree Pointer to the right subtree. |
475 | */ |
479 | */ |
476 | void node_insert_key_and_rsubtree(btree_node_t *node, btree_key_t key, void *value, btree_node_t *rsubtree) |
480 | void node_insert_key_and_rsubtree(btree_node_t *node, btree_key_t key, void *value, btree_node_t *rsubtree) |
477 | { |
481 | { |
478 | int i; |
482 | int i; |
479 | 483 | ||
480 | for (i = 0; i < node->keys; i++) { |
484 | for (i = 0; i < node->keys; i++) { |
481 | if (key < node->key[i]) { |
485 | if (key < node->key[i]) { |
482 | int j; |
486 | int j; |
483 | 487 | ||
484 | for (j = node->keys; j > i; j--) { |
488 | for (j = node->keys; j > i; j--) { |
485 | node->key[j] = node->key[j - 1]; |
489 | node->key[j] = node->key[j - 1]; |
486 | node->value[j] = node->value[j - 1]; |
490 | node->value[j] = node->value[j - 1]; |
487 | node->subtree[j + 1] = node->subtree[j]; |
491 | node->subtree[j + 1] = node->subtree[j]; |
488 | } |
492 | } |
489 | break; |
493 | break; |
490 | } |
494 | } |
491 | } |
495 | } |
492 | node->key[i] = key; |
496 | node->key[i] = key; |
493 | node->value[i] = value; |
497 | node->value[i] = value; |
494 | node->subtree[i + 1] = rsubtree; |
498 | node->subtree[i + 1] = rsubtree; |
495 | 499 | ||
496 | node->keys++; |
500 | node->keys++; |
497 | } |
501 | } |
498 | 502 | ||
499 | /** Remove key and its left subtree pointer from B-tree node. |
503 | /** Remove key and its left subtree pointer from B-tree node. |
500 | * |
504 | * |
501 | * Remove the key and eliminate gaps in node->key array. |
505 | * Remove the key and eliminate gaps in node->key array. |
502 | * Note that the value pointer and the left subtree pointer |
506 | * Note that the value pointer and the left subtree pointer |
503 | * is removed from the node as well. |
507 | * is removed from the node as well. |
504 | * |
508 | * |
505 | * @param node B-tree node. |
509 | * @param node B-tree node. |
506 | * @param key Key to be removed. |
510 | * @param key Key to be removed. |
507 | */ |
511 | */ |
508 | void node_remove_key_and_lsubtree(btree_node_t *node, btree_key_t key) |
512 | void node_remove_key_and_lsubtree(btree_node_t *node, btree_key_t key) |
509 | { |
513 | { |
510 | int i, j; |
514 | int i, j; |
511 | 515 | ||
512 | for (i = 0; i < node->keys; i++) { |
516 | for (i = 0; i < node->keys; i++) { |
513 | if (key == node->key[i]) { |
517 | if (key == node->key[i]) { |
514 | for (j = i + 1; j < node->keys; j++) { |
518 | for (j = i + 1; j < node->keys; j++) { |
515 | node->key[j - 1] = node->key[j]; |
519 | node->key[j - 1] = node->key[j]; |
516 | node->value[j - 1] = node->value[j]; |
520 | node->value[j - 1] = node->value[j]; |
517 | node->subtree[j - 1] = node->subtree[j]; |
521 | node->subtree[j - 1] = node->subtree[j]; |
518 | } |
522 | } |
519 | node->subtree[j - 1] = node->subtree[j]; |
523 | node->subtree[j - 1] = node->subtree[j]; |
520 | node->keys--; |
524 | node->keys--; |
521 | return; |
525 | return; |
522 | } |
526 | } |
523 | } |
527 | } |
524 | panic("node %p does not contain key %d\n", node, key); |
528 | panic("node %p does not contain key %d\n", node, key); |
525 | } |
529 | } |
526 | 530 | ||
527 | /** Remove key and its right subtree pointer from B-tree node. |
531 | /** Remove key and its right subtree pointer from B-tree node. |
528 | * |
532 | * |
529 | * Remove the key and eliminate gaps in node->key array. |
533 | * Remove the key and eliminate gaps in node->key array. |
530 | * Note that the value pointer and the right subtree pointer |
534 | * Note that the value pointer and the right subtree pointer |
531 | * is removed from the node as well. |
535 | * is removed from the node as well. |
532 | * |
536 | * |
533 | * @param node B-tree node. |
537 | * @param node B-tree node. |
534 | * @param key Key to be removed. |
538 | * @param key Key to be removed. |
535 | */ |
539 | */ |
536 | void node_remove_key_and_rsubtree(btree_node_t *node, btree_key_t key) |
540 | void node_remove_key_and_rsubtree(btree_node_t *node, btree_key_t key) |
537 | { |
541 | { |
538 | int i, j; |
542 | int i, j; |
539 | 543 | ||
540 | for (i = 0; i < node->keys; i++) { |
544 | for (i = 0; i < node->keys; i++) { |
541 | if (key == node->key[i]) { |
545 | if (key == node->key[i]) { |
542 | for (j = i + 1; j < node->keys; j++) { |
546 | for (j = i + 1; j < node->keys; j++) { |
543 | node->key[j - 1] = node->key[j]; |
547 | node->key[j - 1] = node->key[j]; |
544 | node->value[j - 1] = node->value[j]; |
548 | node->value[j - 1] = node->value[j]; |
545 | node->subtree[j] = node->subtree[j + 1]; |
549 | node->subtree[j] = node->subtree[j + 1]; |
546 | } |
550 | } |
547 | node->keys--; |
551 | node->keys--; |
548 | return; |
552 | return; |
549 | } |
553 | } |
550 | } |
554 | } |
551 | panic("node %p does not contain key %d\n", node, key); |
555 | panic("node %p does not contain key %d\n", node, key); |
552 | } |
556 | } |
553 | 557 | ||
554 | /** Split full B-tree node and insert new key-value-right-subtree triplet. |
558 | /** Split full B-tree node and insert new key-value-right-subtree triplet. |
555 | * |
559 | * |
556 | * This function will split a node and return pointer to a newly created |
560 | * This function will split a node and return pointer to a newly created |
557 | * node containing keys greater than or equal to the greater of medians |
561 | * node containing keys greater than or equal to the greater of medians |
558 | * (or median) of the old keys and the newly added key. It will also write |
562 | * (or median) of the old keys and the newly added key. It will also write |
559 | * the median key to a memory address supplied by the caller. |
563 | * the median key to a memory address supplied by the caller. |
560 | * |
564 | * |
561 | * If the node being split is an index node, the median will not be |
565 | * If the node being split is an index node, the median will not be |
562 | * included in the new node. If the node is a leaf node, |
566 | * included in the new node. If the node is a leaf node, |
563 | * the median will be copied there. |
567 | * the median will be copied there. |
564 | * |
568 | * |
565 | * @param node B-tree node wich is going to be split. |
569 | * @param node B-tree node wich is going to be split. |
566 | * @param key The key to be inserted. |
570 | * @param key The key to be inserted. |
567 | * @param value Pointer to the value to be inserted. |
571 | * @param value Pointer to the value to be inserted. |
568 | * @param rsubtree Pointer to the right subtree of the key being added. |
572 | * @param rsubtree Pointer to the right subtree of the key being added. |
569 | * @param median Address in memory, where the median key will be stored. |
573 | * @param median Address in memory, where the median key will be stored. |
570 | * |
574 | * |
571 | * @return Newly created right sibling of node. |
575 | * @return Newly created right sibling of node. |
572 | */ |
576 | */ |
573 | btree_node_t *node_split(btree_node_t *node, btree_key_t key, void *value, btree_node_t *rsubtree, btree_key_t *median) |
577 | btree_node_t *node_split(btree_node_t *node, btree_key_t key, void *value, btree_node_t *rsubtree, btree_key_t *median) |
574 | { |
578 | { |
575 | btree_node_t *rnode; |
579 | btree_node_t *rnode; |
576 | int i, j; |
580 | int i, j; |
577 | 581 | ||
578 | ASSERT(median); |
582 | ASSERT(median); |
579 | ASSERT(node->keys == BTREE_MAX_KEYS); |
583 | ASSERT(node->keys == BTREE_MAX_KEYS); |
580 | 584 | ||
581 | /* |
585 | /* |
582 | * Use the extra space to store the extra node. |
586 | * Use the extra space to store the extra node. |
583 | */ |
587 | */ |
584 | node_insert_key_and_rsubtree(node, key, value, rsubtree); |
588 | node_insert_key_and_rsubtree(node, key, value, rsubtree); |
585 | 589 | ||
586 | /* |
590 | /* |
587 | * Compute median of keys. |
591 | * Compute median of keys. |
588 | */ |
592 | */ |
589 | *median = MEDIAN_HIGH(node); |
593 | *median = MEDIAN_HIGH(node); |
590 | 594 | ||
591 | /* |
595 | /* |
592 | * Allocate and initialize new right sibling. |
596 | * Allocate and initialize new right sibling. |
593 | */ |
597 | */ |
594 | rnode = (btree_node_t *) slab_alloc(btree_node_slab, 0); |
598 | rnode = (btree_node_t *) slab_alloc(btree_node_slab, 0); |
595 | node_initialize(rnode); |
599 | node_initialize(rnode); |
596 | rnode->parent = node->parent; |
600 | rnode->parent = node->parent; |
597 | rnode->depth = node->depth; |
601 | rnode->depth = node->depth; |
598 | 602 | ||
599 | /* |
603 | /* |
600 | * Copy big keys, values and subtree pointers to the new right sibling. |
604 | * Copy big keys, values and subtree pointers to the new right sibling. |
601 | * If this is an index node, do not copy the median. |
605 | * If this is an index node, do not copy the median. |
602 | */ |
606 | */ |
603 | i = (int) INDEX_NODE(node); |
607 | i = (int) INDEX_NODE(node); |
604 | for (i += MEDIAN_HIGH_INDEX(node), j = 0; i < node->keys; i++, j++) { |
608 | for (i += MEDIAN_HIGH_INDEX(node), j = 0; i < node->keys; i++, j++) { |
605 | rnode->key[j] = node->key[i]; |
609 | rnode->key[j] = node->key[i]; |
606 | rnode->value[j] = node->value[i]; |
610 | rnode->value[j] = node->value[i]; |
607 | rnode->subtree[j] = node->subtree[i]; |
611 | rnode->subtree[j] = node->subtree[i]; |
608 | 612 | ||
609 | /* |
613 | /* |
610 | * Fix parent links in subtrees. |
614 | * Fix parent links in subtrees. |
611 | */ |
615 | */ |
612 | if (rnode->subtree[j]) |
616 | if (rnode->subtree[j]) |
613 | rnode->subtree[j]->parent = rnode; |
617 | rnode->subtree[j]->parent = rnode; |
614 | 618 | ||
615 | } |
619 | } |
616 | rnode->subtree[j] = node->subtree[i]; |
620 | rnode->subtree[j] = node->subtree[i]; |
617 | if (rnode->subtree[j]) |
621 | if (rnode->subtree[j]) |
618 | rnode->subtree[j]->parent = rnode; |
622 | rnode->subtree[j]->parent = rnode; |
619 | 623 | ||
620 | rnode->keys = j; /* Set number of keys of the new node. */ |
624 | rnode->keys = j; /* Set number of keys of the new node. */ |
621 | node->keys /= 2; /* Shrink the old node. */ |
625 | node->keys /= 2; /* Shrink the old node. */ |
622 | 626 | ||
623 | return rnode; |
627 | return rnode; |
624 | } |
628 | } |
625 | 629 | ||
626 | /** Combine node with any of its siblings. |
630 | /** Combine node with any of its siblings. |
627 | * |
631 | * |
628 | * The siblings are required to be below the fill factor. |
632 | * The siblings are required to be below the fill factor. |
629 | * |
633 | * |
630 | * @param node Node to combine with one of its siblings. |
634 | * @param node Node to combine with one of its siblings. |
631 | * |
635 | * |
632 | * @return Pointer to the rightmost of the two nodes. |
636 | * @return Pointer to the rightmost of the two nodes. |
633 | */ |
637 | */ |
634 | btree_node_t *node_combine(btree_node_t *node) |
638 | btree_node_t *node_combine(btree_node_t *node) |
635 | { |
639 | { |
636 | index_t idx; |
640 | index_t idx; |
637 | btree_node_t *rnode; |
641 | btree_node_t *rnode; |
638 | int i; |
642 | int i; |
639 | 643 | ||
640 | ASSERT(!ROOT_NODE(node)); |
644 | ASSERT(!ROOT_NODE(node)); |
641 | 645 | ||
642 | idx = find_key_by_subtree(node->parent, node, false); |
646 | idx = find_key_by_subtree(node->parent, node, false); |
643 | if (idx == node->parent->keys) { |
647 | if (idx == node->parent->keys) { |
644 | /* |
648 | /* |
645 | * Rightmost subtree of its parent, combine with the left sibling. |
649 | * Rightmost subtree of its parent, combine with the left sibling. |
646 | */ |
650 | */ |
647 | idx--; |
651 | idx--; |
648 | rnode = node; |
652 | rnode = node; |
649 | node = node->parent->subtree[idx]; |
653 | node = node->parent->subtree[idx]; |
650 | } else { |
654 | } else { |
651 | rnode = node->parent->subtree[idx + 1]; |
655 | rnode = node->parent->subtree[idx + 1]; |
652 | } |
656 | } |
653 | 657 | ||
654 | /* Index nodes need to insert parent node key in between left and right node. */ |
658 | /* Index nodes need to insert parent node key in between left and right node. */ |
655 | if (INDEX_NODE(node)) |
659 | if (INDEX_NODE(node)) |
656 | node->key[node->keys++] = node->parent->key[idx]; |
660 | node->key[node->keys++] = node->parent->key[idx]; |
657 | 661 | ||
658 | /* Copy the key-value-subtree triplets from the right node. */ |
662 | /* Copy the key-value-subtree triplets from the right node. */ |
659 | for (i = 0; i < rnode->keys; i++) { |
663 | for (i = 0; i < rnode->keys; i++) { |
660 | node->key[node->keys + i] = rnode->key[i]; |
664 | node->key[node->keys + i] = rnode->key[i]; |
661 | node->value[node->keys + i] = rnode->value[i]; |
665 | node->value[node->keys + i] = rnode->value[i]; |
662 | if (INDEX_NODE(node)) { |
666 | if (INDEX_NODE(node)) { |
663 | node->subtree[node->keys + i] = rnode->subtree[i]; |
667 | node->subtree[node->keys + i] = rnode->subtree[i]; |
664 | rnode->subtree[i]->parent = node; |
668 | rnode->subtree[i]->parent = node; |
665 | } |
669 | } |
666 | } |
670 | } |
667 | if (INDEX_NODE(node)) { |
671 | if (INDEX_NODE(node)) { |
668 | node->subtree[node->keys + i] = rnode->subtree[i]; |
672 | node->subtree[node->keys + i] = rnode->subtree[i]; |
669 | rnode->subtree[i]->parent = node; |
673 | rnode->subtree[i]->parent = node; |
670 | } |
674 | } |
671 | 675 | ||
672 | node->keys += rnode->keys; |
676 | node->keys += rnode->keys; |
673 | 677 | ||
674 | return rnode; |
678 | return rnode; |
675 | } |
679 | } |
676 | 680 | ||
677 | /** Find key by its left or right subtree. |
681 | /** Find key by its left or right subtree. |
678 | * |
682 | * |
679 | * @param node B-tree node. |
683 | * @param node B-tree node. |
680 | * @param subtree Left or right subtree of a key found in node. |
684 | * @param subtree Left or right subtree of a key found in node. |
681 | * @param right If true, subtree is a right subtree. If false, subtree is a left subtree. |
685 | * @param right If true, subtree is a right subtree. If false, subtree is a left subtree. |
682 | * |
686 | * |
683 | * @return Index of the key associated with the subtree. |
687 | * @return Index of the key associated with the subtree. |
684 | */ |
688 | */ |
685 | index_t find_key_by_subtree(btree_node_t *node, btree_node_t *subtree, bool right) |
689 | index_t find_key_by_subtree(btree_node_t *node, btree_node_t *subtree, bool right) |
686 | { |
690 | { |
687 | int i; |
691 | int i; |
688 | 692 | ||
689 | for (i = 0; i < node->keys + 1; i++) { |
693 | for (i = 0; i < node->keys + 1; i++) { |
690 | if (subtree == node->subtree[i]) |
694 | if (subtree == node->subtree[i]) |
691 | return i - (int) (right != false); |
695 | return i - (int) (right != false); |
692 | } |
696 | } |
693 | panic("node %p does not contain subtree %p\n", node, subtree); |
697 | panic("node %p does not contain subtree %p\n", node, subtree); |
694 | } |
698 | } |
695 | 699 | ||
696 | /** Rotate one key-value-rsubtree triplet from the left sibling to the right sibling. |
700 | /** Rotate one key-value-rsubtree triplet from the left sibling to the right sibling. |
697 | * |
701 | * |
698 | * The biggest key and its value and right subtree is rotated from the left node |
702 | * The biggest key and its value and right subtree is rotated from the left node |
699 | * to the right. If the node is an index node, than the parent node key belonging to |
703 | * to the right. If the node is an index node, than the parent node key belonging to |
700 | * the left node takes part in the rotation. |
704 | * the left node takes part in the rotation. |
701 | * |
705 | * |
702 | * @param lnode Left sibling. |
706 | * @param lnode Left sibling. |
703 | * @param rnode Right sibling. |
707 | * @param rnode Right sibling. |
704 | * @param idx Index of the parent node key that is taking part in the rotation. |
708 | * @param idx Index of the parent node key that is taking part in the rotation. |
705 | */ |
709 | */ |
706 | void rotate_from_left(btree_node_t *lnode, btree_node_t *rnode, index_t idx) |
710 | void rotate_from_left(btree_node_t *lnode, btree_node_t *rnode, index_t idx) |
707 | { |
711 | { |
708 | btree_key_t key; |
712 | btree_key_t key; |
709 | 713 | ||
710 | key = lnode->key[lnode->keys - 1]; |
714 | key = lnode->key[lnode->keys - 1]; |
711 | 715 | ||
712 | if (LEAF_NODE(lnode)) { |
716 | if (LEAF_NODE(lnode)) { |
713 | void *value; |
717 | void *value; |
714 | 718 | ||
715 | value = lnode->value[lnode->keys - 1]; |
719 | value = lnode->value[lnode->keys - 1]; |
716 | node_remove_key_and_rsubtree(lnode, key); |
720 | node_remove_key_and_rsubtree(lnode, key); |
717 | node_insert_key_and_lsubtree(rnode, key, value, NULL); |
721 | node_insert_key_and_lsubtree(rnode, key, value, NULL); |
718 | lnode->parent->key[idx] = key; |
722 | lnode->parent->key[idx] = key; |
719 | } else { |
723 | } else { |
720 | btree_node_t *rsubtree; |
724 | btree_node_t *rsubtree; |
721 | 725 | ||
722 | rsubtree = lnode->subtree[lnode->keys]; |
726 | rsubtree = lnode->subtree[lnode->keys]; |
723 | node_remove_key_and_rsubtree(lnode, key); |
727 | node_remove_key_and_rsubtree(lnode, key); |
724 | node_insert_key_and_lsubtree(rnode, lnode->parent->key[idx], NULL, rsubtree); |
728 | node_insert_key_and_lsubtree(rnode, lnode->parent->key[idx], NULL, rsubtree); |
725 | lnode->parent->key[idx] = key; |
729 | lnode->parent->key[idx] = key; |
726 | 730 | ||
727 | /* Fix parent link of the reconnected right subtree. */ |
731 | /* Fix parent link of the reconnected right subtree. */ |
728 | rsubtree->parent = rnode; |
732 | rsubtree->parent = rnode; |
729 | } |
733 | } |
730 | 734 | ||
731 | } |
735 | } |
732 | 736 | ||
733 | /** Rotate one key-value-lsubtree triplet from the right sibling to the left sibling. |
737 | /** Rotate one key-value-lsubtree triplet from the right sibling to the left sibling. |
734 | * |
738 | * |
735 | * The smallest key and its value and left subtree is rotated from the right node |
739 | * The smallest key and its value and left subtree is rotated from the right node |
736 | * to the left. If the node is an index node, than the parent node key belonging to |
740 | * to the left. If the node is an index node, than the parent node key belonging to |
737 | * the right node takes part in the rotation. |
741 | * the right node takes part in the rotation. |
738 | * |
742 | * |
739 | * @param lnode Left sibling. |
743 | * @param lnode Left sibling. |
740 | * @param rnode Right sibling. |
744 | * @param rnode Right sibling. |
741 | * @param idx Index of the parent node key that is taking part in the rotation. |
745 | * @param idx Index of the parent node key that is taking part in the rotation. |
742 | */ |
746 | */ |
743 | void rotate_from_right(btree_node_t *lnode, btree_node_t *rnode, index_t idx) |
747 | void rotate_from_right(btree_node_t *lnode, btree_node_t *rnode, index_t idx) |
744 | { |
748 | { |
745 | btree_key_t key; |
749 | btree_key_t key; |
746 | 750 | ||
747 | key = rnode->key[0]; |
751 | key = rnode->key[0]; |
748 | 752 | ||
749 | if (LEAF_NODE(rnode)) { |
753 | if (LEAF_NODE(rnode)) { |
750 | void *value; |
754 | void *value; |
751 | 755 | ||
752 | value = rnode->value[0]; |
756 | value = rnode->value[0]; |
753 | node_remove_key_and_lsubtree(rnode, key); |
757 | node_remove_key_and_lsubtree(rnode, key); |
754 | node_insert_key_and_rsubtree(lnode, key, value, NULL); |
758 | node_insert_key_and_rsubtree(lnode, key, value, NULL); |
755 | rnode->parent->key[idx] = rnode->key[0]; |
759 | rnode->parent->key[idx] = rnode->key[0]; |
756 | } else { |
760 | } else { |
757 | btree_node_t *lsubtree; |
761 | btree_node_t *lsubtree; |
758 | 762 | ||
759 | lsubtree = rnode->subtree[0]; |
763 | lsubtree = rnode->subtree[0]; |
760 | node_remove_key_and_lsubtree(rnode, key); |
764 | node_remove_key_and_lsubtree(rnode, key); |
761 | node_insert_key_and_rsubtree(lnode, rnode->parent->key[idx], NULL, lsubtree); |
765 | node_insert_key_and_rsubtree(lnode, rnode->parent->key[idx], NULL, lsubtree); |
762 | rnode->parent->key[idx] = key; |
766 | rnode->parent->key[idx] = key; |
763 | 767 | ||
764 | /* Fix parent link of the reconnected left subtree. */ |
768 | /* Fix parent link of the reconnected left subtree. */ |
765 | lsubtree->parent = lnode; |
769 | lsubtree->parent = lnode; |
766 | } |
770 | } |
767 | 771 | ||
768 | } |
772 | } |
769 | 773 | ||
770 | /** Insert key-value-rsubtree triplet and rotate the node to the left, if this operation can be done. |
774 | /** Insert key-value-rsubtree triplet and rotate the node to the left, if this operation can be done. |
771 | * |
775 | * |
772 | * Left sibling of the node (if it exists) is checked for free space. |
776 | * Left sibling of the node (if it exists) is checked for free space. |
773 | * If there is free space, the key is inserted and the smallest key of |
777 | * If there is free space, the key is inserted and the smallest key of |
774 | * the node is moved there. The index node which is the parent of both |
778 | * the node is moved there. The index node which is the parent of both |
775 | * nodes is fixed. |
779 | * nodes is fixed. |
776 | * |
780 | * |
777 | * @param node B-tree node. |
781 | * @param node B-tree node. |
778 | * @param inskey Key to be inserted. |
782 | * @param inskey Key to be inserted. |
779 | * @param insvalue Value to be inserted. |
783 | * @param insvalue Value to be inserted. |
780 | * @param rsubtree Right subtree of inskey. |
784 | * @param rsubtree Right subtree of inskey. |
781 | * |
785 | * |
782 | * @return True if the rotation was performed, false otherwise. |
786 | * @return True if the rotation was performed, false otherwise. |
783 | */ |
787 | */ |
784 | bool try_insert_by_rotation_to_left(btree_node_t *node, btree_key_t inskey, void *insvalue, btree_node_t *rsubtree) |
788 | bool try_insert_by_rotation_to_left(btree_node_t *node, btree_key_t inskey, void *insvalue, btree_node_t *rsubtree) |
785 | { |
789 | { |
786 | index_t idx; |
790 | index_t idx; |
787 | btree_node_t *lnode; |
791 | btree_node_t *lnode; |
788 | 792 | ||
789 | /* |
793 | /* |
790 | * If this is root node, the rotation can not be done. |
794 | * If this is root node, the rotation can not be done. |
791 | */ |
795 | */ |
792 | if (ROOT_NODE(node)) |
796 | if (ROOT_NODE(node)) |
793 | return false; |
797 | return false; |
794 | 798 | ||
795 | idx = find_key_by_subtree(node->parent, node, true); |
799 | idx = find_key_by_subtree(node->parent, node, true); |
796 | if ((int) idx == -1) { |
800 | if ((int) idx == -1) { |
797 | /* |
801 | /* |
798 | * If this node is the leftmost subtree of its parent, |
802 | * If this node is the leftmost subtree of its parent, |
799 | * the rotation can not be done. |
803 | * the rotation can not be done. |
800 | */ |
804 | */ |
801 | return false; |
805 | return false; |
802 | } |
806 | } |
803 | 807 | ||
804 | lnode = node->parent->subtree[idx]; |
808 | lnode = node->parent->subtree[idx]; |
805 | if (lnode->keys < BTREE_MAX_KEYS) { |
809 | if (lnode->keys < BTREE_MAX_KEYS) { |
806 | /* |
810 | /* |
807 | * The rotaion can be done. The left sibling has free space. |
811 | * The rotaion can be done. The left sibling has free space. |
808 | */ |
812 | */ |
809 | node_insert_key_and_rsubtree(node, inskey, insvalue, rsubtree); |
813 | node_insert_key_and_rsubtree(node, inskey, insvalue, rsubtree); |
810 | rotate_from_right(lnode, node, idx); |
814 | rotate_from_right(lnode, node, idx); |
811 | return true; |
815 | return true; |
812 | } |
816 | } |
813 | 817 | ||
814 | return false; |
818 | return false; |
815 | } |
819 | } |
816 | 820 | ||
817 | /** Insert key-value-rsubtree triplet and rotate the node to the right, if this operation can be done. |
821 | /** Insert key-value-rsubtree triplet and rotate the node to the right, if this operation can be done. |
818 | * |
822 | * |
819 | * Right sibling of the node (if it exists) is checked for free space. |
823 | * Right sibling of the node (if it exists) is checked for free space. |
820 | * If there is free space, the key is inserted and the biggest key of |
824 | * If there is free space, the key is inserted and the biggest key of |
821 | * the node is moved there. The index node which is the parent of both |
825 | * the node is moved there. The index node which is the parent of both |
822 | * nodes is fixed. |
826 | * nodes is fixed. |
823 | * |
827 | * |
824 | * @param node B-tree node. |
828 | * @param node B-tree node. |
825 | * @param inskey Key to be inserted. |
829 | * @param inskey Key to be inserted. |
826 | * @param insvalue Value to be inserted. |
830 | * @param insvalue Value to be inserted. |
827 | * @param rsubtree Right subtree of inskey. |
831 | * @param rsubtree Right subtree of inskey. |
828 | * |
832 | * |
829 | * @return True if the rotation was performed, false otherwise. |
833 | * @return True if the rotation was performed, false otherwise. |
830 | */ |
834 | */ |
831 | bool try_insert_by_rotation_to_right(btree_node_t *node, btree_key_t inskey, void *insvalue, btree_node_t *rsubtree) |
835 | bool try_insert_by_rotation_to_right(btree_node_t *node, btree_key_t inskey, void *insvalue, btree_node_t *rsubtree) |
832 | { |
836 | { |
833 | index_t idx; |
837 | index_t idx; |
834 | btree_node_t *rnode; |
838 | btree_node_t *rnode; |
835 | 839 | ||
836 | /* |
840 | /* |
837 | * If this is root node, the rotation can not be done. |
841 | * If this is root node, the rotation can not be done. |
838 | */ |
842 | */ |
839 | if (ROOT_NODE(node)) |
843 | if (ROOT_NODE(node)) |
840 | return false; |
844 | return false; |
841 | 845 | ||
842 | idx = find_key_by_subtree(node->parent, node, false); |
846 | idx = find_key_by_subtree(node->parent, node, false); |
843 | if (idx == node->parent->keys) { |
847 | if (idx == node->parent->keys) { |
844 | /* |
848 | /* |
845 | * If this node is the rightmost subtree of its parent, |
849 | * If this node is the rightmost subtree of its parent, |
846 | * the rotation can not be done. |
850 | * the rotation can not be done. |
847 | */ |
851 | */ |
848 | return false; |
852 | return false; |
849 | } |
853 | } |
850 | 854 | ||
851 | rnode = node->parent->subtree[idx + 1]; |
855 | rnode = node->parent->subtree[idx + 1]; |
852 | if (rnode->keys < BTREE_MAX_KEYS) { |
856 | if (rnode->keys < BTREE_MAX_KEYS) { |
853 | /* |
857 | /* |
854 | * The rotaion can be done. The right sibling has free space. |
858 | * The rotaion can be done. The right sibling has free space. |
855 | */ |
859 | */ |
856 | node_insert_key_and_rsubtree(node, inskey, insvalue, rsubtree); |
860 | node_insert_key_and_rsubtree(node, inskey, insvalue, rsubtree); |
857 | rotate_from_left(node, rnode, idx); |
861 | rotate_from_left(node, rnode, idx); |
858 | return true; |
862 | return true; |
859 | } |
863 | } |
860 | 864 | ||
861 | return false; |
865 | return false; |
862 | } |
866 | } |
863 | 867 | ||
864 | /** Rotate in a key from the left sibling or from the index node, if this operation can be done. |
868 | /** Rotate in a key from the left sibling or from the index node, if this operation can be done. |
865 | * |
869 | * |
866 | * @param rnode Node into which to add key from its left sibling or from the index node. |
870 | * @param rnode Node into which to add key from its left sibling or from the index node. |
867 | * |
871 | * |
868 | * @return True if the rotation was performed, false otherwise. |
872 | * @return True if the rotation was performed, false otherwise. |
869 | */ |
873 | */ |
870 | bool try_rotation_from_left(btree_node_t *rnode) |
874 | bool try_rotation_from_left(btree_node_t *rnode) |
871 | { |
875 | { |
872 | index_t idx; |
876 | index_t idx; |
873 | btree_node_t *lnode; |
877 | btree_node_t *lnode; |
874 | 878 | ||
875 | /* |
879 | /* |
876 | * If this is root node, the rotation can not be done. |
880 | * If this is root node, the rotation can not be done. |
877 | */ |
881 | */ |
878 | if (ROOT_NODE(rnode)) |
882 | if (ROOT_NODE(rnode)) |
879 | return false; |
883 | return false; |
880 | 884 | ||
881 | idx = find_key_by_subtree(rnode->parent, rnode, true); |
885 | idx = find_key_by_subtree(rnode->parent, rnode, true); |
882 | if ((int) idx == -1) { |
886 | if ((int) idx == -1) { |
883 | /* |
887 | /* |
884 | * If this node is the leftmost subtree of its parent, |
888 | * If this node is the leftmost subtree of its parent, |
885 | * the rotation can not be done. |
889 | * the rotation can not be done. |
886 | */ |
890 | */ |
887 | return false; |
891 | return false; |
888 | } |
892 | } |
889 | 893 | ||
890 | lnode = rnode->parent->subtree[idx]; |
894 | lnode = rnode->parent->subtree[idx]; |
891 | if (lnode->keys > FILL_FACTOR) { |
895 | if (lnode->keys > FILL_FACTOR) { |
892 | rotate_from_left(lnode, rnode, idx); |
896 | rotate_from_left(lnode, rnode, idx); |
893 | return true; |
897 | return true; |
894 | } |
898 | } |
895 | 899 | ||
896 | return false; |
900 | return false; |
897 | } |
901 | } |
898 | 902 | ||
899 | /** Rotate in a key from the right sibling or from the index node, if this operation can be done. |
903 | /** Rotate in a key from the right sibling or from the index node, if this operation can be done. |
900 | * |
904 | * |
901 | * @param rnode Node into which to add key from its right sibling or from the index node. |
905 | * @param rnode Node into which to add key from its right sibling or from the index node. |
902 | * |
906 | * |
903 | * @return True if the rotation was performed, false otherwise. |
907 | * @return True if the rotation was performed, false otherwise. |
904 | */ |
908 | */ |
905 | bool try_rotation_from_right(btree_node_t *lnode) |
909 | bool try_rotation_from_right(btree_node_t *lnode) |
906 | { |
910 | { |
907 | index_t idx; |
911 | index_t idx; |
908 | btree_node_t *rnode; |
912 | btree_node_t *rnode; |
909 | 913 | ||
910 | /* |
914 | /* |
911 | * If this is root node, the rotation can not be done. |
915 | * If this is root node, the rotation can not be done. |
912 | */ |
916 | */ |
913 | if (ROOT_NODE(lnode)) |
917 | if (ROOT_NODE(lnode)) |
914 | return false; |
918 | return false; |
915 | 919 | ||
916 | idx = find_key_by_subtree(lnode->parent, lnode, false); |
920 | idx = find_key_by_subtree(lnode->parent, lnode, false); |
917 | if (idx == lnode->parent->keys) { |
921 | if (idx == lnode->parent->keys) { |
918 | /* |
922 | /* |
919 | * If this node is the rightmost subtree of its parent, |
923 | * If this node is the rightmost subtree of its parent, |
920 | * the rotation can not be done. |
924 | * the rotation can not be done. |
921 | */ |
925 | */ |
922 | return false; |
926 | return false; |
923 | } |
927 | } |
924 | 928 | ||
925 | rnode = lnode->parent->subtree[idx + 1]; |
929 | rnode = lnode->parent->subtree[idx + 1]; |
926 | if (rnode->keys > FILL_FACTOR) { |
930 | if (rnode->keys > FILL_FACTOR) { |
927 | rotate_from_right(lnode, rnode, idx); |
931 | rotate_from_right(lnode, rnode, idx); |
928 | return true; |
932 | return true; |
929 | } |
933 | } |
930 | 934 | ||
931 | return false; |
935 | return false; |
932 | } |
936 | } |
933 | 937 | ||
934 | /** Print B-tree. |
938 | /** Print B-tree. |
935 | * |
939 | * |
936 | * @param t Print out B-tree. |
940 | * @param t Print out B-tree. |
937 | */ |
941 | */ |
938 | void btree_print(btree_t *t) |
942 | void btree_print(btree_t *t) |
939 | { |
943 | { |
940 | int i, depth = t->root->depth; |
944 | int i, depth = t->root->depth; |
941 | link_t head, *cur; |
945 | link_t head, *cur; |
942 | 946 | ||
943 | printf("Printing B-tree:\n"); |
947 | printf("Printing B-tree:\n"); |
944 | list_initialize(&head); |
948 | list_initialize(&head); |
945 | list_append(&t->root->bfs_link, &head); |
949 | list_append(&t->root->bfs_link, &head); |
946 | 950 | ||
947 | /* |
951 | /* |
948 | * Use BFS search to print out the tree. |
952 | * Use BFS search to print out the tree. |
949 | * Levels are distinguished from one another by node->depth. |
953 | * Levels are distinguished from one another by node->depth. |
950 | */ |
954 | */ |
951 | while (!list_empty(&head)) { |
955 | while (!list_empty(&head)) { |
952 | link_t *hlp; |
956 | link_t *hlp; |
953 | btree_node_t *node; |
957 | btree_node_t *node; |
954 | 958 | ||
955 | hlp = head.next; |
959 | hlp = head.next; |
956 | ASSERT(hlp != &head); |
960 | ASSERT(hlp != &head); |
957 | node = list_get_instance(hlp, btree_node_t, bfs_link); |
961 | node = list_get_instance(hlp, btree_node_t, bfs_link); |
958 | list_remove(hlp); |
962 | list_remove(hlp); |
959 | 963 | ||
960 | ASSERT(node); |
964 | ASSERT(node); |
961 | 965 | ||
962 | if (node->depth != depth) { |
966 | if (node->depth != depth) { |
963 | printf("\n"); |
967 | printf("\n"); |
964 | depth = node->depth; |
968 | depth = node->depth; |
965 | } |
969 | } |
966 | 970 | ||
967 | printf("("); |
971 | printf("("); |
968 | for (i = 0; i < node->keys; i++) { |
972 | for (i = 0; i < node->keys; i++) { |
969 | printf("%lld%s", node->key[i], i < node->keys - 1 ? "," : ""); |
973 | printf("%lld%s", node->key[i], i < node->keys - 1 ? "," : ""); |
970 | if (node->depth && node->subtree[i]) { |
974 | if (node->depth && node->subtree[i]) { |
971 | list_append(&node->subtree[i]->bfs_link, &head); |
975 | list_append(&node->subtree[i]->bfs_link, &head); |
972 | } |
976 | } |
973 | } |
977 | } |
974 | if (node->depth && node->subtree[i]) { |
978 | if (node->depth && node->subtree[i]) { |
975 | list_append(&node->subtree[i]->bfs_link, &head); |
979 | list_append(&node->subtree[i]->bfs_link, &head); |
976 | } |
980 | } |
977 | printf(")"); |
981 | printf(")"); |
978 | } |
982 | } |
979 | printf("\n"); |
983 | printf("\n"); |
980 | 984 | ||
981 | printf("Printing list of leaves:\n"); |
985 | printf("Printing list of leaves:\n"); |
982 | for (cur = t->leaf_head.next; cur != &t->leaf_head; cur = cur->next) { |
986 | for (cur = t->leaf_head.next; cur != &t->leaf_head; cur = cur->next) { |
983 | btree_node_t *node; |
987 | btree_node_t *node; |
984 | 988 | ||
985 | node = list_get_instance(cur, btree_node_t, leaf_link); |
989 | node = list_get_instance(cur, btree_node_t, leaf_link); |
986 | 990 | ||
987 | ASSERT(node); |
991 | ASSERT(node); |
988 | 992 | ||
989 | printf("("); |
993 | printf("("); |
990 | for (i = 0; i < node->keys; i++) |
994 | for (i = 0; i < node->keys; i++) |
991 | printf("%lld%s", node->key[i], i < node->keys - 1 ? "," : ""); |
995 | printf("%lld%s", node->key[i], i < node->keys - 1 ? "," : ""); |
992 | printf(")"); |
996 | printf(")"); |
993 | } |
997 | } |
994 | printf("\n"); |
998 | printf("\n"); |
995 | } |
999 | } |
- | 1000 | ||
- | 1001 | /** @} |
|
- | 1002 | */ |
|
- | 1003 | ||
996 | 1004 |