summaryrefslogtreecommitdiff
path: root/src/tree.c
blob: 5edda76eaa8ca148ed3354a9ed990f5eaadd4d3d (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "config.h"

#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "logerr.h"
#include "util.h"

#include "tree.h"



struct Tree {
	const void *const value;
	const struct Tree *left;
	const struct Tree *right;
	const size_t value_size;
	size_t height;
};



int
tree_new(const size_t value_size, const struct Tree **const out) {
	int rc = -1;

	const struct Tree *ret = NULL;

	ret = malloc(sizeof(*ret));
	if (ret == NULL) {
		logerr("malloc(): %s", strerror(errno));
		goto out;
	}

	memcpy((void *)ret, &(struct Tree) {
		.value      = NULL,
		.left       = NULL,
		.right      = NULL,
		.value_size = value_size,
		.height     = 0U,
	}, sizeof(*ret));
	*out = ret;
	rc = 0;
out:
	if (rc) {
		if (ret != NULL) {
			freeit((void *)&ret);
		}
	}
	return rc;
}

void
tree_free(const struct Tree **const t) {
	const struct Tree *const tree = *t;
	assert(tree != NULL);

	const struct Tree *left  = tree->left;
	const struct Tree *right = tree->right;

	if (left != NULL) {
		tree_free(&left);
	}

	if (right != NULL) {
		tree_free(&right);
	}

	freeit((void *)t);
}

static void
insert_node(
	const struct Tree *const t,
	const struct Tree *node,
	const void *const value
) {
	struct Tree *const t_mut = (struct Tree *const)t;
	const int cmp = memcmp(value, t->value, t->value_size);
	if (cmp == 0) {
		tree_free(&node);
	} else if (cmp > 0) {
		if (t->left == NULL) {
			t_mut->left = (void *)node;
		} else {
			insert_node(t->left, node, value);
		}
		struct Tree *const t_left_mut = (struct Tree *const)t->left;
		t_left_mut->height++;
	} else {
		if (t->right == NULL) {
			t_mut->right = node;
		} else {
			insert_node(t->right, node, value);
		}
		struct Tree *const t_right_mut = (struct Tree *const)t->right;
		t_right_mut->height++;
	}
}

int
tree_insert(const struct Tree *const t, const void *const value) {
	int rc = -1;

	const struct Tree *new = NULL;
	struct Tree *const t_mut = (struct Tree *const)t;

	if (t->value == NULL) {
		memcpy((void *)t->value, value, t->value_size);
		t_mut->height++;
		goto out;
	}

	if (tree_new(t->value_size, &new)) {
		logerr("tree_new()");
		goto out;
	}

	insert_node(t, new, value);

	rc = 0;
out:
	if (rc) {
		if (new != NULL) {
			tree_free(&new);
		}
	}
	return rc;
}

static bool
remove_ref(
	const struct Tree *t,
	const void *const value,
	const struct Tree **const parent_ptr
) {
	if (t->value == NULL) {
		return false;
	}

	struct Tree *const t_mut = (struct Tree *const)t;

	const int cmp = memcmp(value, t->value, t->value_size);
	if (cmp == 0) {
		if ((t->left == NULL) && (t->right == NULL)) {
			if (parent_ptr != NULL) {
				*parent_ptr = NULL;
			}
		} else if (t->left != NULL) {
			if (parent_ptr != NULL) {
				*parent_ptr = t->left;
			}
		} else if (t->right != NULL) {
			if (parent_ptr != NULL) {
				*parent_ptr = t->right;
			}
		} else {
			// FIXME: pivot
			assert(false);
		}
		tree_free(&t);
		return true;
	} else if (cmp > 0) {
		if (t->left == NULL) {
			return false;
		}
		return remove_ref(t->left, value, &t_mut->left);
	} else {
		if (t->right == NULL) {
			return false;
		}
		return remove_ref(t->right, value, &t_mut->right);
	}
}

bool
tree_remove(const struct Tree *const t, const void *const value) {
	return remove_ref(t, value, NULL);
}

bool
tree_contains(const struct Tree *const t, const void *const value) {
	if (t->value == NULL) {
		return false;
	}

	const int cmp = memcmp(value, t->value, t->value_size);
	if (cmp == 0) {
		return true;
	} else if (cmp > 0) {
		if (t->left == NULL) {
			return false;
		}
		return tree_contains(t->left, value);
	} else {
		if (t->right == NULL) {
			return false;
		}
		return tree_contains(t->right, value);
	}
}