diff options
Diffstat (limited to 'source/b_tree.c')
| -rw-r--r-- | source/b_tree.c | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/source/b_tree.c b/source/b_tree.c new file mode 100644 index 0000000..e42f709 --- /dev/null +++ b/source/b_tree.c | |||
| @@ -0,0 +1,66 @@ | |||
| 1 | |||
| 2 | // TODO(nasr): 1. splitting the tree when getting too big? (horizontally) 2. joining trees? | ||
| 3 | |||
| 4 | internal b_tree_node * | ||
| 5 | node_alloc(mem_arena *arena) | ||
| 6 | { | ||
| 7 | b_tree_node *node = PushStructZero(arena, type); | ||
| 8 | node->leaf = 1; | ||
| 9 | return node; | ||
| 10 | } | ||
| 11 | |||
| 12 | // NOTE(nasr): @return the index of of the found element | ||
| 13 | internal s32 | ||
| 14 | node_find_pos(mem_arena *arena, string8 value) | ||
| 15 | { | ||
| 16 | s32 i = 0; | ||
| 17 | while (i < n->key_count && str8_cmp(n->keys[i], k) < 0) | ||
| 18 | { | ||
| 19 | ++i; | ||
| 20 | } | ||
| 21 | |||
| 22 | return i; | ||
| 23 | } | ||
| 24 | |||
| 25 | interal void | ||
| 26 | b_tree_create(mem_arena *arena, b_tree *tree) | ||
| 27 | { | ||
| 28 | tree->root = node_alloc(arena); | ||
| 29 | tree->root->leaf = 1; | ||
| 30 | tree->root->key_count = 0; | ||
| 31 | } | ||
| 32 | |||
| 33 | // NOTE(nasr): nodes that get passed as parameters should've already been loaded into memory | ||
| 34 | internal void | ||
| 35 | b_tree_search(b_tree_node *node, string8 key) | ||
| 36 | { | ||
| 37 | s32 found_index = node_find_pos(node, key); | ||
| 38 | |||
| 39 | if (found_index < n->key_count && string_compare(n->keys[i], key) == 0) | ||
| 40 | { | ||
| 41 | return n->rows[i]; | ||
| 42 | } | ||
| 43 | if (n->leaf) | ||
| 44 | { | ||
| 45 | return NULL; | ||
| 46 | |||
| 47 | } | ||
| 48 | |||
| 49 | return b_tree_search(n->children[i], key); | ||
| 50 | |||
| 51 | |||
| 52 | } | ||
| 53 | |||
| 54 | internal void | ||
| 55 | b_tree_insert() | ||
| 56 | { | ||
| 57 | |||
| 58 | } | ||
| 59 | |||
| 60 | internal void | ||
| 61 | b_tree_write() | ||
| 62 | { | ||
| 63 | // TODO(nasr): write the b_tree to disk | ||
| 64 | } | ||
| 65 | |||
| 66 | |||
