Debian Codegraph @

cextern/astrometry.net/bl-nl.c

raw
1 /*
2 # This file is part of the Astrometry.net suite.
3 # Licensed under a 3-clause BSD style license - see LICENSE
4 */
5
6 /**
7 Defined:
8
9 --nl
10 --number
11 --NL_PRINT(x) prints number 'x'
12
13 Note:
14 --You can't declare multiple "number" variables like this:
15 number n1, n2;
16 Instead, do:
17 number n1;
18 number n2;
19 This is because "number" may be a pointer type.
20 */
21
22 #include "bl-nl.ph"
23
24 #define NODE_NUMDATA(node) ((number*)NODE_DATA(node))
25
26 number* NLF(to_array)(nl* list) {
27 number* arr;
28 size_t N;
29 if (!list)
30 return NULL;
31 N = NLF(size)(list);
32 arr = malloc(N * sizeof(number));
33 bl_copy(list, 0, N, arr);
34 return arr;
35 }
36
37 #define InlineDefine InlineDefineC
38 #include "bl-nl.inc"
39 #undef InlineDefine
40
41 static int NLF(compare_ascending)(const void* v1, const void* v2) {
42 number i1 = *(number*)v1;
43 number i2 = *(number*)v2;
44 if (i1 > i2) return 1;
45 else if (i1 < i2) return -1;
46 else return 0;
47 }
48
49 static int NLF(compare_descending)(const void* v1, const void* v2) {
50 number i1 = *(number*)v1;
51 number i2 = *(number*)v2;
52 if (i1 > i2) return -1;
53 else if (i1 < i2) return 1;
54 else return 0;
55 }
56
57 void NLF(reverse)(nl* list) {
58 bl_reverse(list);
59 }
60
61 void NLF(append_array)(nl* list, const number* data, size_t ndata) {
62 size_t i;
63 for (i=0; i<ndata; i++)
64 NLF(append)(list, data[i]);
65 }
66
67 nl* NLF(merge_ascending)(nl* list1, nl* list2) {
68 nl* res;
69 size_t i1, i2, N1, N2;
70 number v1 = 0;
71 number v2 = 0;
72 unsigned char getv1, getv2;
73 if (!list1)
74 return NLF(dupe)(list2);
75 if (!list2)
76 return NLF(dupe)(list1);
77 if (!NLF(size)(list1))
78 return NLF(dupe)(list2);
79 if (!NLF(size)(list2))
80 return NLF(dupe)(list1);
81
82 res = NLF(new)(list1->blocksize);
83 N1 = NLF(size)(list1);
84 N2 = NLF(size)(list2);
85 i1 = i2 = 0;
86 getv1 = getv2 = 1;
87 while (i1 < N1 && i2 < N2) {
88 if (getv1) {
89 v1 = NLF(get)(list1, i1);
90 getv1 = 0;
91 }
92 if (getv2) {
93 getv2 = 0;
94 v2 = NLF(get)(list2, i2);
95 }
96 if (v1 <= v2) {
97 NLF(append)(res, v1);
98 i1++;
99 getv1 = 1;
100 } else {
101 NLF(append)(res, v2);
102 i2++;
103 getv2 = 1;
104 }
105 }
106 for (; i1<N1; i1++)
107 NLF(append)(res, NLF(get)(list1, i1));
108 for (; i2<N2; i2++)
109 NLF(append)(res, NLF(get)(list2, i2));
110 return res;
111 }
112
113 void NLF(remove_all_reuse)(nl* list) {
114 bl_remove_all_but_first(list);
115 }
116
117 ptrdiff_t NLF(find_index_ascending)(nl* list, const number value) {
118 return bl_find_index(list, &value, NLF(compare_ascending));
119 }
120
121 int NLF(check_consistency)(nl* list) {
122 return bl_check_consistency(list);
123 }
124
125 int NLF(check_sorted_ascending)(nl* list,
126 int isunique) {
127 return bl_check_sorted(list, NLF(compare_ascending), isunique);
128 }
129
130 int NLF(check_sorted_descending)(nl* list,
131 int isunique) {
132 return bl_check_sorted(list, NLF(compare_descending), isunique);
133 }
134
135 void NLF(remove)(nl* nlist, size_t index) {
136 bl_remove_index(nlist, index);
137 }
138
139 number NLF(pop)(nl* nlist) {
140 number ret = NLF(get)(nlist, nlist->N-1);
141 bl_remove_index(nlist, nlist->N-1);
142 return ret;
143 }
144
145 nl* NLF(dupe)(nl* nlist) {
146 nl* ret = NLF(new)(nlist->blocksize);
147 size_t i;
148 for (i=0; i<nlist->N; i++)
149 NLF(push)(ret, NLF(get)(nlist, i));
150 return ret;
151 }
152
153 ptrdiff_t NLF(remove_value)(nl* nlist, const number value) {
154 bl* list = nlist;
155 bl_node *node, *prev;
156 size_t istart = 0;
157 for (node=list->head, prev=NULL;
158 node;
159 prev=node, node=node->next) {
160 int i;
161 number* idat;
162 idat = NODE_DATA(node);
163 for (i=0; i<node->N; i++)
164 if (idat[i] == value) {
165 bl_remove_from_node(list, node, prev, i);
166 list->last_access = prev;
167 list->last_access_n = istart;
168 return istart + i;
169 }
170 istart += node->N;
171 }
172 return BL_NOT_FOUND;
173 }
174
175 void NLF(remove_all)(nl* list) {
176 bl_remove_all(list);
177 }
178
179 void NLF(remove_index_range)(nl* list, size_t start, size_t length) {
180 bl_remove_index_range(list, start, length);
181 }
182
183 void NLF(set)(nl* list, size_t index, const number value) {
184 bl_set(list, index, &value);
185 }
186
187 /*
188 void dl_set(dl* list, int index, double value) {
189 int i;
190 int nadd = (index+1) - list->N;
191 if (nadd > 0) {
192 // enlarge the list to hold 'nadd' more entries.
193 for (i=0; i<nadd; i++) {
194 dl_append(list, 0.0);
195 }
196 }
197 bl_set(list, index, &value);
198 }
199 */
200
201 nl* NLF(new)(int blocksize) {
202 return bl_new(blocksize, sizeof(number));
203 }
204
205 void NLF(new_existing)(nl* list, int blocksize) {
206 bl_init(list, blocksize, sizeof(number));
207 }
208
209 void NLF(init)(nl* list, int blocksize) {
210 bl_init(list, blocksize, sizeof(number));
211 }
212
213 void NLF(free)(nl* list) {
214 bl_free(list);
215 }
216
217 void NLF(push)(nl* list, const number data) {
218 bl_append(list, &data);
219 }
220
221 number* NLF(append)(nl* list, const number data) {
222 return bl_append(list, &data);
223 }
224
225 void NLF(append_list)(nl* list, nl* list2) {
226 size_t i, N;
227 N = NLF(size)(list2);
228 for (i=0; i<N; i++)
229 NLF(append)(list, NLF(get)(list2, i));
230 }
231
232 void NLF(merge_lists)(nl* list1, nl* list2) {
233 bl_append_list(list1, list2);
234 }
235
236 static ptrdiff_t NLF(binarysearch)(bl_node* node, const number n) {
237 number* iarray = NODE_NUMDATA(node);
238 ptrdiff_t lower = -1;
239 ptrdiff_t upper = node->N;
240 ptrdiff_t mid;
241 while (lower < (upper-1)) {
242 mid = (upper + lower) / 2;
243 if (n >= iarray[mid])
244 lower = mid;
245 else
246 upper = mid;
247 }
248 return lower;
249 }
250
251 // find the first node for which n <= the last element.
252 static bl_node* NLF(findnodecontainingsorted)(const nl* list, const number n,
253 size_t* p_nskipped) {
254 bl_node *node;
255 size_t nskipped;
256 //bl_node *prev;
257 //int prevnskipped;
258
259 // check if we can use the jump accessor or if we have to start at
260 // the beginning...
261 if (list->last_access && list->last_access->N &&
262 // is the value we're looking for >= the first element?
263 (n >= *NODE_NUMDATA(list->last_access))) {
264 node = list->last_access;
265 nskipped = list->last_access_n;
266 } else {
267 node = list->head;
268 nskipped = 0;
269 }
270
271 /*
272 // find the first node for which n < the first element. The
273 // previous node will contain the value (if it exists).
274 for (prev=node, prevnskipped=nskipped;
275 node && (n < *NODE_NUMDATA(node));) {
276 prev=node;
277 prevnskipped=nskipped;
278 nskipped+=node->N;
279 node=node->next;
280 }
281 if (prev && n <= NODE_NUMDATA(prev)[prev->N-1]) {
282 if (p_nskipped)
283 *p_nskipped = prevnskipped;
284 return prev;
285 }
286 if (node && n <= NODE_NUMDATA(node)[node->N-1]) {
287 if (p_nskipped)
288 *p_nskipped = nskipped;
289 return node;
290 }
291 return NULL;
292 */
293 /*
294 if (!node && prev && n > NODE_NUMDATA(prev)[prev->N-1])
295 return NULL;
296 if (p_nskipped)
297 *p_nskipped = prevnskipped;
298 return prev;
299 */
300
301 for (; node && (n > NODE_NUMDATA(node)[node->N-1]); node=node->next)
302 nskipped += node->N;
303 if (p_nskipped)
304 *p_nskipped = nskipped;
305 return node;
306 }
307
308 static ptrdiff_t NLF(insertascending)(nl* list, const number n, int unique) {
309 bl_node *node;
310 size_t ind;
311 size_t nskipped;
312
313 node = NLF(findnodecontainingsorted)(list, n, &nskipped);
314 if (!node) {
315 NLF(append)(list, n);
316 return list->N-1;
317 }
318
319 /*
320 for (; node && (n > NODE_NUMDATA(node)[node->N-1]); node=node->next)
321 nskipped += node->N;
322 if (!node) {
323 // either we're adding the first element, or we're appending since
324 // n is bigger than the largest element in the list.
325 NLF(append)(list, n);
326 return list->N-1;
327 }
328 */
329
330 // find where in the node it should be inserted...
331 ind = 1 + NLF(binarysearch)(node, n);
332
333 // check if it's a duplicate...
334 if (unique && ind > 0 && (n == NODE_NUMDATA(node)[ind-1]))
335 return BL_NOT_FOUND;
336
337 // set the jump accessors...
338 list->last_access = node;
339 list->last_access_n = nskipped;
340 // ... so that this runs in O(1).
341 bl_insert(list, nskipped + ind, &n);
342 return nskipped + ind;
343 }
344
345 size_t NLF(insert_ascending)(nl* list, const number n) {
346 return NLF(insertascending)(list, n, 0);
347 }
348
349 ptrdiff_t NLF(insert_unique_ascending)(nl* list, const number n) {
350 return NLF(insertascending)(list, n, 1);
351 }
352
353 size_t NLF(insert_descending)(nl* list, const number n) {
354 return bl_insert_sorted(list, &n, NLF(compare_descending));
355 }
356
357 void NLF(insert)(nl* list, size_t indx, const number data) {
358 bl_insert(list, indx, &data);
359 }
360
361 void NLF(copy)(nl* list, size_t start, size_t length, number* vdest) {
362 bl_copy(list, start, length, vdest);
363 }
364
365 void NLF(print)(nl* list) {
366 bl_node* n;
367 for (n=list->head; n; n=n->next) {
368 int i;
369 printf("[ ");
370 for (i=0; i<n->N; i++) {
371 if (i > 0)
372 printf(", ");
373 NL_PRINT(NODE_NUMDATA(n)[i]);
374 }
375 printf("] ");
376 }
377 }
378
379 ptrdiff_t NLF(index_of)(nl* list, const number data) {
380 bl_node* n;
381 number* idata;
382 size_t npast = 0;
383 for (n=list->head; n; n=n->next) {
384 int i;
385 idata = NODE_NUMDATA(n);
386 for (i=0; i<n->N; i++)
387 if (idata[i] == data)
388 return npast + i;
389 npast += n->N;
390 }
391 return BL_NOT_FOUND;
392 }
393
394 int NLF(contains)(nl* list, const number data) {
395 return (NLF(index_of)(list, data) != BL_NOT_FOUND);
396 }
397
398 int NLF(sorted_contains)(nl* list, const number n) {
399 return NLF(sorted_index_of)(list, n) != BL_NOT_FOUND;
400 }
401
402 ptrdiff_t NLF(sorted_index_of)(nl* list, const number n) {
403 bl_node *node;
404 ptrdiff_t lower;
405 size_t nskipped;
406
407 node = NLF(findnodecontainingsorted)(list, n, &nskipped);
408 if (!node)
409 return BL_NOT_FOUND;
410 //if (!node && (n > NODE_NUMDATA(prev)[prev->N-1]))
411 //return -1;
412 //node = prev;
413
414 /*
415 // find the first node for which n <= the last element. That node
416 // will contain the value (if it exists)
417 for (; node && (n > NODE_NUMDATA(node)[node->N-1]); node=node->next)
418 nskipped += node->N;
419 if (!node)
420 return -1;
421 */
422
423 // update jump accessors...
424 list->last_access = node;
425 list->last_access_n = nskipped;
426
427 // find within the node...
428 lower = NLF(binarysearch)(node, n);
429 if (lower == BL_NOT_FOUND)
430 return BL_NOT_FOUND;
431 if (n == NODE_NUMDATA(node)[lower])
432 return nskipped + lower;
433 return BL_NOT_FOUND;
434 }
435
436 #undef NLF
437 #undef NODE_NUMDATA