diff options
author | iphydf <iphydf@users.noreply.github.com> | 2018-07-07 21:40:17 +0000 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2018-07-08 19:03:52 +0000 |
commit | 2b49f803959cf47f81d1b49032f7c6f30ccc37f7 (patch) | |
tree | de26ad6da901c7d951e66745ee80545b71300abb /toxcore/list.c | |
parent | ebdc43285a4fdf1f9d76f8839a9ba572a35cbb80 (diff) |
Rename `BS_LIST` to `BS_List` to follow the naming conventions.
`BS_LIST` would be a constant. `BS_List` is a type name.
Diffstat (limited to 'toxcore/list.c')
-rw-r--r-- | toxcore/list.c | 80 |
1 files changed, 42 insertions, 38 deletions
diff --git a/toxcore/list.c b/toxcore/list.c index 7b65e184..4846ce25 100644 --- a/toxcore/list.c +++ b/toxcore/list.c | |||
@@ -45,31 +45,35 @@ | |||
45 | * -some considerations since the array size is never perfect | 45 | * -some considerations since the array size is never perfect |
46 | */ | 46 | */ |
47 | 47 | ||
48 | #define INDEX(i) (~i) | 48 | static int32_t |
49 | list_index(uint32_t i) | ||
50 | { | ||
51 | return ~i; | ||
52 | } | ||
49 | 53 | ||
50 | /* Find data in list | 54 | /* Find data in list |
51 | * | 55 | * |
52 | * return value: | 56 | * return value: |
53 | * >= 0 : index of data in array | 57 | * >= 0 : index of data in array |
54 | * < 0 : no match, returns index (return value is INDEX(index)) where | 58 | * < 0 : no match, returns index (return value is list_index(index)) where |
55 | * the data should be inserted | 59 | * the data should be inserted |
56 | */ | 60 | */ |
57 | static int find(const BS_LIST *list, const uint8_t *data) | 61 | static int find(const BS_List *list, const uint8_t *data) |
58 | { | 62 | { |
59 | //should work well, but could be improved | 63 | // should work well, but could be improved |
60 | if (list->n == 0) { | 64 | if (list->n == 0) { |
61 | return INDEX(0); | 65 | return list_index(0); |
62 | } | 66 | } |
63 | 67 | ||
64 | uint32_t i = list->n / 2; //current position in the array | 68 | uint32_t i = list->n / 2; // current position in the array |
65 | uint32_t delta = i / 2; //how much we move in the array | 69 | uint32_t delta = i / 2; // how much we move in the array |
66 | 70 | ||
67 | if (!delta) { | 71 | if (!delta) { |
68 | delta = 1; | 72 | delta = 1; |
69 | } | 73 | } |
70 | 74 | ||
71 | int d = -1; //used to determine if closest match is found | 75 | int d = -1; // used to determine if closest match is found |
72 | //closest match is found if we move back to where we have already been | 76 | // closest match is found if we move back to where we have already been |
73 | 77 | ||
74 | while (1) { | 78 | while (1) { |
75 | int r = memcmp(data, list->data + list->element_size * i, list->element_size); | 79 | int r = memcmp(data, list->data + list->element_size * i, list->element_size); |
@@ -79,13 +83,13 @@ static int find(const BS_LIST *list, const uint8_t *data) | |||
79 | } | 83 | } |
80 | 84 | ||
81 | if (r > 0) { | 85 | if (r > 0) { |
82 | //data is greater | 86 | // data is greater |
83 | //move down | 87 | // move down |
84 | i += delta; | 88 | i += delta; |
85 | 89 | ||
86 | if (d == 0 || i == list->n) { | 90 | if (d == 0 || i == list->n) { |
87 | //reached bottom of list, or closest match | 91 | // reached bottom of list, or closest match |
88 | return INDEX(i); | 92 | return list_index(i); |
89 | } | 93 | } |
90 | 94 | ||
91 | delta = (delta) / 2; | 95 | delta = (delta) / 2; |
@@ -95,13 +99,13 @@ static int find(const BS_LIST *list, const uint8_t *data) | |||
95 | d = 1; | 99 | d = 1; |
96 | } | 100 | } |
97 | } else { | 101 | } else { |
98 | //data is smaller | 102 | // data is smaller |
99 | if (d == 1 || i == 0) { | 103 | if (d == 1 || i == 0) { |
100 | //reached top or list or closest match | 104 | // reached top or list or closest match |
101 | return INDEX(i); | 105 | return list_index(i); |
102 | } | 106 | } |
103 | 107 | ||
104 | //move up | 108 | // move up |
105 | i -= delta; | 109 | i -= delta; |
106 | 110 | ||
107 | delta = (delta) / 2; | 111 | delta = (delta) / 2; |
@@ -120,7 +124,7 @@ static int find(const BS_LIST *list, const uint8_t *data) | |||
120 | * 1 : success | 124 | * 1 : success |
121 | * 0 : failure | 125 | * 0 : failure |
122 | */ | 126 | */ |
123 | static int resize(BS_LIST *list, uint32_t new_size) | 127 | static int resize(BS_List *list, uint32_t new_size) |
124 | { | 128 | { |
125 | if (new_size == 0) { | 129 | if (new_size == 0) { |
126 | bs_list_free(list); | 130 | bs_list_free(list); |
@@ -147,9 +151,9 @@ static int resize(BS_LIST *list, uint32_t new_size) | |||
147 | } | 151 | } |
148 | 152 | ||
149 | 153 | ||
150 | int bs_list_init(BS_LIST *list, uint32_t element_size, uint32_t initial_capacity) | 154 | int bs_list_init(BS_List *list, uint32_t element_size, uint32_t initial_capacity) |
151 | { | 155 | { |
152 | //set initial values | 156 | // set initial values |
153 | list->n = 0; | 157 | list->n = 0; |
154 | list->element_size = element_size; | 158 | list->element_size = element_size; |
155 | list->capacity = 0; | 159 | list->capacity = 0; |
@@ -167,9 +171,9 @@ int bs_list_init(BS_LIST *list, uint32_t element_size, uint32_t initial_capacity | |||
167 | return 1; | 171 | return 1; |
168 | } | 172 | } |
169 | 173 | ||
170 | void bs_list_free(BS_LIST *list) | 174 | void bs_list_free(BS_List *list) |
171 | { | 175 | { |
172 | //free both arrays | 176 | // free both arrays |
173 | free(list->data); | 177 | free(list->data); |
174 | list->data = nullptr; | 178 | list->data = nullptr; |
175 | 179 | ||
@@ -177,11 +181,11 @@ void bs_list_free(BS_LIST *list) | |||
177 | list->ids = nullptr; | 181 | list->ids = nullptr; |
178 | } | 182 | } |
179 | 183 | ||
180 | int bs_list_find(const BS_LIST *list, const uint8_t *data) | 184 | int bs_list_find(const BS_List *list, const uint8_t *data) |
181 | { | 185 | { |
182 | int r = find(list, data); | 186 | int r = find(list, data); |
183 | 187 | ||
184 | //return only -1 and positive values | 188 | // return only -1 and positive values |
185 | if (r < 0) { | 189 | if (r < 0) { |
186 | return -1; | 190 | return -1; |
187 | } | 191 | } |
@@ -189,20 +193,20 @@ int bs_list_find(const BS_LIST *list, const uint8_t *data) | |||
189 | return list->ids[r]; | 193 | return list->ids[r]; |
190 | } | 194 | } |
191 | 195 | ||
192 | int bs_list_add(BS_LIST *list, const uint8_t *data, int id) | 196 | int bs_list_add(BS_List *list, const uint8_t *data, int id) |
193 | { | 197 | { |
194 | //find where the new element should be inserted | 198 | // find where the new element should be inserted |
195 | //see: return value of find() | 199 | // see: return value of find() |
196 | int i = find(list, data); | 200 | int i = find(list, data); |
197 | 201 | ||
198 | if (i >= 0) { | 202 | if (i >= 0) { |
199 | //already in list | 203 | // already in list |
200 | return 0; | 204 | return 0; |
201 | } | 205 | } |
202 | 206 | ||
203 | i = ~i; | 207 | i = ~i; |
204 | 208 | ||
205 | //increase the size of the arrays if needed | 209 | // increase the size of the arrays if needed |
206 | if (list->n == list->capacity) { | 210 | if (list->n == list->capacity) { |
207 | // 1.5 * n + 1 | 211 | // 1.5 * n + 1 |
208 | const uint32_t new_capacity = list->n + list->n / 2 + 1; | 212 | const uint32_t new_capacity = list->n + list->n / 2 + 1; |
@@ -214,22 +218,22 @@ int bs_list_add(BS_LIST *list, const uint8_t *data, int id) | |||
214 | list->capacity = new_capacity; | 218 | list->capacity = new_capacity; |
215 | } | 219 | } |
216 | 220 | ||
217 | //insert data to element array | 221 | // insert data to element array |
218 | memmove(list->data + (i + 1) * list->element_size, list->data + i * list->element_size, | 222 | memmove(list->data + (i + 1) * list->element_size, list->data + i * list->element_size, |
219 | (list->n - i) * list->element_size); | 223 | (list->n - i) * list->element_size); |
220 | memcpy(list->data + i * list->element_size, data, list->element_size); | 224 | memcpy(list->data + i * list->element_size, data, list->element_size); |
221 | 225 | ||
222 | //insert id to id array | 226 | // insert id to id array |
223 | memmove(&list->ids[i + 1], &list->ids[i], (list->n - i) * sizeof(int)); | 227 | memmove(&list->ids[i + 1], &list->ids[i], (list->n - i) * sizeof(int)); |
224 | list->ids[i] = id; | 228 | list->ids[i] = id; |
225 | 229 | ||
226 | //increase n | 230 | // increase n |
227 | list->n++; | 231 | ++list->n; |
228 | 232 | ||
229 | return 1; | 233 | return 1; |
230 | } | 234 | } |
231 | 235 | ||
232 | int bs_list_remove(BS_LIST *list, const uint8_t *data, int id) | 236 | int bs_list_remove(BS_List *list, const uint8_t *data, int id) |
233 | { | 237 | { |
234 | int i = find(list, data); | 238 | int i = find(list, data); |
235 | 239 | ||
@@ -238,11 +242,11 @@ int bs_list_remove(BS_LIST *list, const uint8_t *data, int id) | |||
238 | } | 242 | } |
239 | 243 | ||
240 | if (list->ids[i] != id) { | 244 | if (list->ids[i] != id) { |
241 | //this should never happen | 245 | // this should never happen |
242 | return 0; | 246 | return 0; |
243 | } | 247 | } |
244 | 248 | ||
245 | //decrease the size of the arrays if needed | 249 | // decrease the size of the arrays if needed |
246 | if (list->n < list->capacity / 2) { | 250 | if (list->n < list->capacity / 2) { |
247 | const uint32_t new_capacity = list->capacity / 2; | 251 | const uint32_t new_capacity = list->capacity / 2; |
248 | 252 | ||
@@ -251,7 +255,7 @@ int bs_list_remove(BS_LIST *list, const uint8_t *data, int id) | |||
251 | } | 255 | } |
252 | } | 256 | } |
253 | 257 | ||
254 | list->n--; | 258 | --list->n; |
255 | 259 | ||
256 | memmove(list->data + i * list->element_size, list->data + (i + 1) * list->element_size, | 260 | memmove(list->data + i * list->element_size, list->data + (i + 1) * list->element_size, |
257 | (list->n - i) * list->element_size); | 261 | (list->n - i) * list->element_size); |
@@ -260,7 +264,7 @@ int bs_list_remove(BS_LIST *list, const uint8_t *data, int id) | |||
260 | return 1; | 264 | return 1; |
261 | } | 265 | } |
262 | 266 | ||
263 | int bs_list_trim(BS_LIST *list) | 267 | int bs_list_trim(BS_List *list) |
264 | { | 268 | { |
265 | if (!resize(list, list->n)) { | 269 | if (!resize(list, list->n)) { |
266 | return 0; | 270 | return 0; |