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