-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtree_vectors.c
201 lines (164 loc) · 4.37 KB
/
tree_vectors.c
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
/*
* tree_vectors.c
*
* MathMap
*
* Copyright (C) 2009 Mark Probst
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <string.h>
#include <stdlib.h>
#include <glib.h>
#include "tree_vectors.h"
#define LENGTH_FOR_DEPTH(d) (1 << (((d) + 1) * TREE_VECTOR_SHIFT))
static int
get_depth_for_length (int length)
{
int depth = 0;
--length;
while (length >= TREE_VECTOR_ARITY)
{
length >>= TREE_VECTOR_SHIFT;
++depth;
}
return depth;
}
static void
populate (mathmap_pools_t *pools, tree_vector_node_t *node, int length, int depth, float *data)
{
int i;
if (depth == 0)
{
g_assert(length <= TREE_VECTOR_ARITY);
memcpy(node->data, data, sizeof(float) * length);
return;
}
for (i = 0; i < TREE_VECTOR_ARITY; ++i)
{
if (length > 0)
{
int sub_length = MIN(length, LENGTH_FOR_DEPTH(depth - 1));
node->subs[i] = mathmap_pools_alloc(pools, sizeof(tree_vector_node_t));
populate(pools, node->subs[i], sub_length, depth - 1, data);
length -= sub_length;
data += sub_length;
}
else
node->subs[i] = NULL;
}
g_assert(length == 0);
}
tree_vector_t*
new_tree_vector (mathmap_pools_t *pools, int length, float *data)
{
tree_vector_t *tv = mathmap_pools_alloc(pools, sizeof(tree_vector_t));
g_assert(length > 0);
tv->length = length;
tv->depth = get_depth_for_length(length);
populate(pools, &tv->root, length, tv->depth, data);
return tv;
}
float
tree_vector_get (tree_vector_t *tv, int index)
{
int depth = tv->depth;
tree_vector_node_t *node = &tv->root;
if (index < 0)
index = 0;
else if (index >= tv->length)
index = tv->length - 1;
while (depth > 0)
{
node = node->subs[index >> (depth * TREE_VECTOR_SHIFT)];
index &= (1 << (depth * TREE_VECTOR_SHIFT)) - 1;
--depth;
}
return node->data[index];
}
tree_vector_t*
tree_vector_set (mathmap_pools_t *pools, tree_vector_t *tv, int index, float value)
{
int depth = tv->depth;
tree_vector_t *new;
tree_vector_node_t *node;
if (index < 0)
index = 0;
else if (index >= tv->length)
index = tv->length - 1;
if (depth == 0)
{
new = new_tree_vector(pools, tv->length, tv->root.data);
new->root.data[index] = value;
return new;
}
new = mathmap_pools_alloc(pools, sizeof(tree_vector_t));
new->length = tv->length;
new->depth = tv->depth;
memcpy(new->root.subs, tv->root.subs, sizeof(tree_vector_node_t*) * TREE_VECTOR_ARITY);
node = &new->root;
while (depth > 0)
{
tree_vector_node_t *new_sub = mathmap_pools_alloc(pools, sizeof(tree_vector_node_t));
int sub_index = index >> (depth * TREE_VECTOR_SHIFT);
memcpy(new_sub->subs, node->subs[sub_index]->subs, sizeof(tree_vector_node_t*) * TREE_VECTOR_ARITY);
node = node->subs[sub_index] = new_sub;
index &= (1 << (depth * TREE_VECTOR_SHIFT)) - 1;
--depth;
}
node->data[index] = value;
return new;
}
#ifdef TEST_TREE_VECTORS
static void
check (int length, tree_vector_t *tv, float *data)
{
int i;
for (i = 0; i < length; ++i)
g_assert(tree_vector_get(tv, i) == data[i]);
}
static void
run_test (int length)
{
mathmap_pools_t pools;
float data[length];
tree_vector_t *tv;
int i;
g_print("testing %d\n", length);
for (i = 0; i < length; ++i)
data[i] = (float)i;
mathmap_pools_init_local(&pools);
tv = new_tree_vector(&pools, length, data);
for (i = 0; i < 1024; ++i)
{
int index = random() % length;
float value = (float)random();
tree_vector_t *new = tree_vector_set(&pools, tv, index, value);
check(length, tv, data);
data[index] = value;
tv = new;
}
check(length, tv, data);
mathmap_pools_free(&pools);
}
int
main (void)
{
int i;
for (i = 1; i < 1024; ++i)
run_test(i);
return 0;
}
#endif