summaryrefslogtreecommitdiffstats
path: root/klib/rbtree.h
blob: 43767132647858148ee4fa1cc7d29d09a35bfa5f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#ifndef JOVE_LIB_RBTREE_H
#define JOVE_LIB_RBTREE_H 1

#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include "memory.h"

typedef struct rbnode
{
    struct rbnode *parent;
    struct rbnode *children[2];
    intmax_t key;
    bool color;
    char data[];
} rbnode_t;

typedef struct rbtree
{
    rbnode_t *root;
    size_t objw;
} rbtree_t;

/**Instantiate a new tree based on the given type.
 * @param tree to instantiate into.
 * @param type to instantiate for.*/
#define rbtree_new(tree, type) __rbtree_new(tree, sizeof(type))
void __rbtree_new(rbtree_t *tree, size_t objw);

/**Find the data corresponding to the key in the tree.
 * usage: rbtree_find(type)(tree, key);
 * @param tree to search through.
 * @param key to search for.
 * @return */
void *rbtree_find(rbtree_t *tree, intmax_t key);

/**Insert a value into the given tree using the given key.
 * If the key is already present within the tree, the existing data is
 * overwritten.
 * @param tree to insert into.
 * @param key to insert.
 * @param value to insert.
 * @return pointer to inserted data.
 * */
void *rbtree_insert(rbtree_t *tree, intmax_t key, void *value);
void *rbtree_reserve(rbtree_t *tree, intmax_t key);

#endif