aboutsummaryrefslogtreecommitdiff
path: root/src/tree.c
blob: 974a5bb2969bab4bf3c7f34e73170235f395c867 (plain)
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
/*
 * tree.c
 * (c) 2019 Jonas Gunz
 * License: MIT
 * */

#include "tree.h"

static int string_compare ( const char* _1, const char* _2 );

/**
 * ignore-case alphabetical string compare
 * returns:
 * 0  :: _1 == _2
 * -1 :: _1 <  _2
 * +1 :: _1 >  _2
 * */
static int string_compare ( const char* _1, const char* _2 )
{
	if ( !_1 || !_2 )
		return 99;

	int i;
	for (i = 0; _1[i] && _2[i]; i++) {
		char c1 = _1[i];
		char c2 = _2[i];

		//Convert to uppercase
		if ( c1 >= 97 && c1 <= 122 )
			c1 -= 32;
		if ( c2 >= 97 && c2 <= 122 )
			c2 -= 32;

		if (c1 > c2)
			return 1;
		if (c1 < c2)
			return -1;
	}

	if ( _1[i] == _2[i] )
		return 0;
	if ( _1[i] )
		return 1;
	if ( _2[i] )
		return -1;

	//TODO not so great
	return 99;
}

int tree_insert ( struct tree_node** _root, char* _key, void* _data )
{
	struct tree_node** node = _root;

	while( *node ) {
		int ret = string_compare ( (*node)->key, _key );
		if ( ret > 0 ) {
			node = & (*node)->above;
		} else if ( ret < 0 ) {
			node = & (*node)->below;
		} else { //Already exists
			return 1;
		}
	}

	*node = malloc (sizeof(typeof(**node)));
	if( ! *node )
		return 2;
	memset ( *node, 0, sizeof(typeof(**node)) );

	(*node)->key  = _key;
	(*node)->data = _data;

	return 0;
}

int tree_balanced_insert ( struct tree_node** _root, void*  _data[], char* _key[], unsigned int _len)
{
	// n is the smallest n, for which 2^(n+1) - 1 >= _len,
	// thus describes the minimal tree depth required to store
	// _len amount of elements
	unsigned int n = 0;
	for (n = 0; pow( 2, n+1 ) - 1 < _len; n++);


	// The maximum size of a tree with depth n;
	unsigned int virtual_len = pow( 2, n+1 ) - 1;

	unsigned int indices[ virtual_len ];
	unsigned int indices_cnt = 0;

	LOGPRINTF(_LOG_DEBUG, "Elements: %u Rounded size: %u Optimal depth: %u", _len, virtual_len, n);

	// Creates the series
	// 1/2, 1/4, 3/4, 1/8, 3/8, 5/8, 7/8, ...
	for (unsigned int i = 0; i <= n+1; i++) {
		unsigned int pow_2_i = pow(2,i);
		for (unsigned int o = 1; o < pow(2,i); o+=2) {
			indices[ indices_cnt++ ] = virtual_len * o / pow_2_i;
		}
	}

	for ( unsigned int i = 0; i < virtual_len; i++ ) {
		if ( indices[i] < _len ) {
			if (tree_insert ( _root, _key[indices[i]], _data[indices[i]] )){ 
				LOGPRINTF(_LOG_WARNING, "tree_insert failed on \"%s\". Double?", _key[indices[i]]);
			}
		}
	}

	return 0;
}

int tree_destroy ( struct tree_node** _root, uint8_t _options )
{
	//Not efficient, but this code is for testing only.
	unsigned int max_depth = 0;
	unsigned int node_cnt = 0;
	while(*_root)
	{
		struct tree_node** node = _root;
		unsigned int depth = 0;

		while( (*node)->above || (*node)->below ) {
			node= (*node)->above ? & (*node)->above : & (*node)->below ;
			depth ++;
		}

		if (_options & _TREE_FREE_DATA)
			free( (*node)->data );
		if (_options & _TREE_FREE_KEY)
			free( (*node)->key );

		if ( depth > max_depth )
			max_depth = depth;

		free ( *node );
		*node = NULL;
		node_cnt ++;
	}

	LOGPRINTF(_LOG_DEBUG, "%i nodes deleted. Max depth %i", node_cnt, max_depth);

	return 0;
}

void* tree_get ( struct  tree_node** _root, const char* _query )
{
	struct tree_node** node = _root;

	while(*node) {
		int ret = string_compare ( (*node)->key, _query );
		if ( ret > 0 ) {
			node = & (*node)->above;
		} else if ( ret < 0 ) {
			node = & (*node)->below;
		} else {
			break;
		}
	}

	return *node ? (*node)->data : NULL;
}