summaryrefslogtreecommitdiff
path: root/source/b_tree.h
diff options
context:
space:
mode:
Diffstat (limited to 'source/b_tree.h')
-rw-r--r--source/b_tree.h119
1 files changed, 119 insertions, 0 deletions
diff --git a/source/b_tree.h b/source/b_tree.h
new file mode 100644
index 0000000..43a33a6
--- /dev/null
+++ b/source/b_tree.h
@@ -0,0 +1,119 @@
1/* single header b-tree implementation by Abdellah El Morabit */
2#ifndef B_TREE_H
3#define B_TREE_H
4
5// maximum height of the tree the lower the lower the lower amount
6// of disk reads which translates into faster?
7#define B_TREE_ORDER 4
8
9typedef struct b_tree_node b_tree_node;
10struct b_tree_node
11{
12
13 // store the values
14 string8 keys[B_TREE_ORDER - 1];
15
16 // TODO(nasr): replace with someting more generic?
17 // NOTE(nasr): cons of void * -> no type safety
18 // is there a way to still have some sort of that?
19 void *payload_per_key[B_TREE_ORDER - 1];
20
21 b_tree_node *parent;
22 // handle to store children faster than linked list
23 // because now we can iteratate over the nodes instead of having cache misses
24 // when jumping through linked nodes
25 b_tree_node *children[B_TREE_ORDER];
26
27 // this shouldnt change things but could be a performance improvement on a bigger scale??
28 s32 *max_children;
29
30 // NOTE(nasr): reference count ::: check how many leaves are using this node
31 // also not needed for now because we don't free individual node because of arena allocator
32 // s32 *refc;
33
34 s32 key_count;
35
36 b32 leaf;
37
38};
39
40typedef struct b_tree b_tree;
41struct b_tree
42{
43 // NOTE(nasr): make sure this always stays in memory
44 // so that initial fetch never requires a disk read
45 b_tree_node *root;
46};
47
48
49#ifdef B_TREE_IMPLEMENTATION
50///////////////////////////////////////////////////////////////////////////////
51//- b_tree.c
52
53// TODO(nasr): 1. splitting the tree when getting too big? (horizontally) 2. joining trees?
54
55internal b_tree_node *
56btree_node_alloc(mem_arena *arena)
57{
58 b_tree_node *node = PushStructZero(arena, b_tree_node);
59 node->leaf = 1;
60 return node;
61}
62
63// NOTE(nasr): @return the index of of the found element
64internal s32
65btree_node_find_pos(mem_arena *arena, string8 value, b_tree_node *node)
66{
67 for (s32 index = 0; index < node->key_count && string8_cmp(node->keys[i], value) < 0; ++index);
68 return i;
69}
70
71internal void
72b_tree_create(mem_arena *arena, b_tree *tree)
73{
74 tree->root = btree_node_alloc(arena);
75 tree->root->leaf = 1;
76 tree->root->key_count = 0;
77}
78
79// NOTE(nasr): nodes that get passed as parameters should've already been loaded into memory
80internal void
81b_tree_search(b_tree_node *node, string8 key)
82{
83 s32 found_index = btree_node_find_pos(node, key);
84 s32 i = 0;
85
86 if (found_index < node->key_count && string_compare(node->keys[index], key) == 0)
87 {
88 return node->rows[i];
89 }
90 if (node->leaf)
91 {
92 return NULL;
93
94 }
95
96 return b_tree_search(node->children[i], key);
97
98 return NULL;
99}
100
101internal void
102b_tree_insert(b_tree_node *current_node)
103{
104 if(current_node->leaf)
105 {
106
107 }
108
109}
110
111internal void
112b_tree_write()
113{
114
115 // TODO(nasr): write the b_tree to disk
116}
117
118#endif /* B_TREE_IMPLEMENTATION */
119#endif /* B_TREE_H */