Debian Codegraph @

libmowgli-2/src/libmowgli/container/index.c

raw
1 /*
2 * Copyright 2009-2011 John Lindgren
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice is present in all copies.
7 *
8 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
9 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
11 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
12 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
13 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
14 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
15 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
16 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
17 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
18 * POSSIBILITY OF SUCH DAMAGE.
19 */
20
21 #include "mowgli.h"
22 #include "core/bootstrap_internal.h"
23
24 struct mowgli_index_
25 {
26 void **data;
27 int count, size;
28 int (*compare)(const void *a, const void *b, void *data);
29 void *compare_data;
30 };
31
32 static mowgli_heap_t *index_heap = NULL;
33
34 void
35 mowgli_index_bootstrap(void)
36 {
37 if (! (index_heap = mowgli_heap_create(sizeof(mowgli_index_t), 32, BH_NOW)))
38 abort();
39 }
40
41 mowgli_index_t *
42 mowgli_index_create(void)
43 {
44 mowgli_index_t *index = mowgli_heap_alloc(index_heap);
45
46 index->data = NULL;
47 index->count = 0;
48 index->size = 0;
49 index->compare = NULL;
50 index->compare_data = NULL;
51
52 return index;
53 }
54
55 void
56 mowgli_index_destroy(mowgli_index_t *index)
57 {
58 mowgli_free(index->data);
59 mowgli_heap_free(index_heap, index);
60 }
61
62 int
63 mowgli_index_count(mowgli_index_t *index)
64 {
65 return index->count;
66 }
67
68 void
69 mowgli_index_allocate(mowgli_index_t *index, int size)
70 {
71 size_t oldsize;
72 void *new_ptr;
73
74 if (size <= index->size)
75 return;
76
77 if (!index->size)
78 index->size = 64;
79
80 oldsize = index->size;
81
82 while (size > index->size)
83 {
84 index->size <<= 1;
85 }
86
87 new_ptr = mowgli_alloc_array(sizeof(void *), index->size);
88
89 if (index->data != NULL)
90 {
91 memcpy(new_ptr, index->data, oldsize);
92 mowgli_free(index->data);
93 }
94
95 index->data = new_ptr;
96 }
97
98 void
99 mowgli_index_set(mowgli_index_t *index, int at, void *value)
100 {
101 index->data[at] = value;
102 }
103
104 void *
105 mowgli_index_get(mowgli_index_t *index, int at)
106 {
107 return index->data[at];
108 }
109
110 static void
111 make_room(mowgli_index_t *index, int at, int count)
112 {
113 mowgli_index_allocate(index, index->count + count);
114
115 if (at < index->count)
116 memmove(index->data + at + count, index->data + at, sizeof(void *) *
117 (index->count - at));
118
119 index->count += count;
120 }
121
122 void
123 mowgli_index_insert(mowgli_index_t *index, int at, void *value)
124 {
125 make_room(index, at, 1);
126 index->data[at] = value;
127 }
128
129 void
130 mowgli_index_append(mowgli_index_t *index, void *value)
131 {
132 mowgli_index_insert(index, index->count, value);
133 }
134
135 void
136 mowgli_index_copy_set(mowgli_index_t *source, int from, mowgli_index_t *target, int to, int count)
137 {
138 memcpy(target->data + to, source->data + from, sizeof(void *) * count);
139 }
140
141 void
142 mowgli_index_copy_insert(mowgli_index_t *source, int from, mowgli_index_t *target, int to, int count)
143 {
144 make_room(target, to, count);
145 memcpy(target->data + to, source->data + from, sizeof(void *) * count);
146 }
147
148 void
149 mowgli_index_copy_append(mowgli_index_t *source, int from, mowgli_index_t *target, int count)
150 {
151 mowgli_index_copy_insert(source, from, target, target->count, count);
152 }
153
154 void
155 mowgli_index_merge_insert(mowgli_index_t *first, int at, mowgli_index_t *second)
156 {
157 mowgli_index_copy_insert(second, 0, first, at, second->count);
158 }
159
160 void
161 mowgli_index_merge_append(mowgli_index_t *first, mowgli_index_t *second)
162 {
163 mowgli_index_copy_insert(second, 0, first, first->count, second->count);
164 }
165
166 void
167 mowgli_index_move(mowgli_index_t *index, int from, int to, int count)
168 {
169 memmove(index->data + to, index->data + from, sizeof(void *) * count);
170 }
171
172 void
173 mowgli_index_delete(mowgli_index_t *index, int at, int count)
174 {
175 index->count -= count;
176 memmove(index->data + at, index->data + at + count, sizeof(void *) *
177 (index->count - at));
178 }
179
180 void
181 mowgli_index_sort(mowgli_index_t *index, int (*compare)(const void *, const void *))
182 {
183 qsort(index->data, index->count, sizeof(void *), compare);
184 }
185
186 #ifdef NOTYET
187 static int
188 mowgli_index_compare_with_data(const void *a, const void *b, void *_index)
189 {
190 mowgli_index_t *index = _index;
191
192 return index->compare(*(const void **) a, *(const void **) b,
193 index->compare_data);
194 }
195
196 void
197 mowgli_index_sort_with_data(mowgli_index_t *index, int(*compare)
198 (const void *a, const void *b, void *data), void *data)
199 {
200 index->compare = compare;
201 index->compare_data = data;
202 g_qsort_with_data(index->data, index->count, sizeof(void *),
203 mowgli_index_compare_with_data, index);
204 index->compare = NULL;
205 index->compare_data = NULL;
206 }
207
208 #endif