diff options
Diffstat (limited to 'toxcore/list.c')
-rw-r--r-- | toxcore/list.c | 96 |
1 files changed, 75 insertions, 21 deletions
diff --git a/toxcore/list.c b/toxcore/list.c index c513afab..301e56f8 100644 --- a/toxcore/list.c +++ b/toxcore/list.c | |||
@@ -63,7 +63,7 @@ static int find(const BS_LIST *list, const void *data) | |||
63 | //closest match is found if we move back to where we have already been | 63 | //closest match is found if we move back to where we have already been |
64 | 64 | ||
65 | while (1) { | 65 | while (1) { |
66 | int r = memcmp(data, list->data + list->size * i, list->size); | 66 | int r = memcmp(data, list->data + list->element_size * i, list->element_size); |
67 | 67 | ||
68 | if (r == 0) { | 68 | if (r == 0) { |
69 | return i; | 69 | return i; |
@@ -105,14 +105,54 @@ static int find(const BS_LIST *list, const void *data) | |||
105 | } | 105 | } |
106 | } | 106 | } |
107 | 107 | ||
108 | /* Resized the list list | ||
109 | * | ||
110 | * return value: | ||
111 | * 1 : success | ||
112 | * 0 : failure | ||
113 | */ | ||
114 | static int resize(BS_LIST *list, uint32_t new_size) | ||
115 | { | ||
116 | void *p; | ||
117 | |||
118 | p = realloc(list->data, list->element_size * new_size); | ||
119 | |||
120 | if (!p) { | ||
121 | return 0; | ||
122 | } else { | ||
123 | list->data = p; | ||
124 | } | ||
125 | |||
126 | p = realloc(list->ids, sizeof(int) * new_size); | ||
108 | 127 | ||
109 | void bs_list_init(BS_LIST *list, uint32_t element_size) | 128 | if (!p) { |
129 | return 0; | ||
130 | } else { | ||
131 | list->ids = p; | ||
132 | } | ||
133 | |||
134 | return 1; | ||
135 | } | ||
136 | |||
137 | |||
138 | int bs_list_init(BS_LIST *list, uint32_t element_size, uint32_t initial_capacity) | ||
110 | { | 139 | { |
111 | //set initial values | 140 | //set initial values |
112 | list->n = 0; | 141 | list->n = 0; |
113 | list->size = element_size; | 142 | list->element_size = element_size; |
143 | list->capacity = 0; | ||
114 | list->data = NULL; | 144 | list->data = NULL; |
115 | list->ids = NULL; | 145 | list->ids = NULL; |
146 | |||
147 | if (initial_capacity != 0) { | ||
148 | if (!resize(list, initial_capacity)) { | ||
149 | return 0; | ||
150 | } | ||
151 | } | ||
152 | |||
153 | list->capacity = initial_capacity; | ||
154 | |||
155 | return 1; | ||
116 | } | 156 | } |
117 | 157 | ||
118 | void bs_list_free(BS_LIST *list) | 158 | void bs_list_free(BS_LIST *list) |
@@ -147,28 +187,22 @@ int bs_list_add(BS_LIST *list, const void *data, int id) | |||
147 | 187 | ||
148 | i = ~i; | 188 | i = ~i; |
149 | 189 | ||
150 | //increase the size of the arrays by one | 190 | //increase the size of the arrays if needed |
151 | void *p; | 191 | if (list->n == list->capacity) { |
152 | 192 | // 1.5 * n + 1 | |
153 | p = realloc(list->data, list->size * (list->n + 1)); | 193 | const uint32_t new_capacity = list->n + list->n / 2 + 1; |
154 | 194 | ||
155 | if (!p) { | 195 | if (!resize(list, new_capacity)) { |
156 | return 0; | 196 | return 0; |
157 | } else { | 197 | } |
158 | list->data = p; | ||
159 | } | ||
160 | |||
161 | p = realloc(list->ids, sizeof(int) * (list->n + 1)); | ||
162 | 198 | ||
163 | if (!p) { | 199 | list->capacity = new_capacity; |
164 | return 0; | ||
165 | } else { | ||
166 | list->ids = p; | ||
167 | } | 200 | } |
168 | 201 | ||
169 | //insert data to element array | 202 | //insert data to element array |
170 | memmove(list->data + (i + 1) * list->size, list->data + i * list->size, (list->n - i) * list->size); | 203 | memmove(list->data + (i + 1) * list->element_size, list->data + i * list->element_size, |
171 | memcpy(list->data + i * list->size, data, list->size); | 204 | (list->n - i) * list->element_size); |
205 | memcpy(list->data + i * list->element_size, data, list->element_size); | ||
172 | 206 | ||
173 | //insert id to id array | 207 | //insert id to id array |
174 | memmove(&list->ids[i + 1], &list->ids[i], (list->n - i) * sizeof(int)); | 208 | memmove(&list->ids[i + 1], &list->ids[i], (list->n - i) * sizeof(int)); |
@@ -193,10 +227,30 @@ int bs_list_remove(BS_LIST *list, const void *data, int id) | |||
193 | return 0; | 227 | return 0; |
194 | } | 228 | } |
195 | 229 | ||
230 | //decrease the size of the arrays if needed | ||
231 | if (list->n < list->capacity / 2) { | ||
232 | const uint32_t new_capacity = list->capacity / 2; | ||
233 | |||
234 | if (resize(list, new_capacity)) { | ||
235 | list->capacity = new_capacity; | ||
236 | } | ||
237 | } | ||
238 | |||
196 | list->n--; | 239 | list->n--; |
197 | 240 | ||
198 | memmove(list->data + i * list->size, list->data + (i + 1) * list->size, (list->n - i) * list->size); | 241 | memmove(list->data + i * list->element_size, list->data + (i + 1) * list->element_size, |
242 | (list->n - i) * list->element_size); | ||
199 | memmove(&list->ids[i], &list->ids[i + 1], (list->n - i) * sizeof(int)); | 243 | memmove(&list->ids[i], &list->ids[i + 1], (list->n - i) * sizeof(int)); |
200 | 244 | ||
201 | return 1; | 245 | return 1; |
202 | } | 246 | } |
247 | |||
248 | int bs_list_trim(BS_LIST *list) | ||
249 | { | ||
250 | if (!resize(list, list->n)) { | ||
251 | return 0; | ||
252 | } | ||
253 | |||
254 | list->capacity = list->n; | ||
255 | return 1; | ||
256 | } | ||