Debian Codegraph @

cextern/astrometry.net/bl.h

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 A linked list of arrays, which allows
8 more rapid traversal of the list, and fairly
9 efficient insertions and deletions.
10 */
11
12 #ifndef BL_H
13 #define BL_H
14
15 #include <stddef.h>
16 #include <stdlib.h>
17 #include <stdarg.h>
18
19 #ifdef _MSC_VER
20 #define strcasecmp _stricmp
21 #endif
22
23 #ifdef _MSC_VER
24 #if _MSC_VER >= 1600
25 #include <stdint.h>
26 #else
27 #include <stdint_msc.h>
28 #endif
29 #else
30 #include <stdint.h>
31 #endif
32
33 #include "keywords.h"
34
35 struct bl_node {
36 // number of elements filled.
37 int N;
38 struct bl_node* next;
39 // (data block implicitly follows this struct).
40 };
41 typedef struct bl_node bl_node;
42
43 // the top-level data structure of a blocklist.
44 typedef struct {
45 bl_node* head;
46 bl_node* tail;
47 // the total number of data elements
48 size_t N;
49 // the number of elements per block
50 int blocksize;
51 // the size in bytes of each data element
52 int datasize;
53 // rapid accessors for "jumping in" at the last block accessed
54 bl_node* last_access;
55 size_t last_access_n;
56 } bl;
57
58 #define BL_NOT_FOUND (ptrdiff_t)(-1)
59
60
61 Malloc bl* bl_new(int blocksize, int datasize);
62 void bl_init(bl* l, int blocksize, int datasize);
63 void bl_free(bl* list);
64 void bl_remove_all(bl* list);
65 Pure InlineDeclare size_t bl_size(const bl* list);
66 Pure int bl_datasize(const bl* list);
67 /** Appends an element, returning the location whereto it was copied. */
68 void* bl_append(bl* list, const void* data);
69 // Copies the nth element into the destination location.
70 void bl_get(bl* list, size_t n, void* dest);
71 // Returns a pointer to the nth element.
72 InlineDeclare void* bl_access(bl* list, size_t n);
73
74 void* bl_access_const(const bl* list, size_t n);
75
76 void* bl_push(bl* list, const void* data);
77 // Pops a data item into the given "into" memory.
78 void bl_pop(bl* list, void* into);
79
80 // allocates space for a new object and returns a pointer to it
81 void* bl_extend(bl* list);
82
83 /**
84 Removes elements from \c split
85 to the end of the list from \c src and appends them to \c dest.
86 */
87 void bl_split(bl* src, bl* dest, size_t split);
88
89 void bl_reverse(bl* list);
90
91 /*
92 * Appends "list2" to the end of "list1", and removes all elements
93 * from "list2".
94 */
95 void bl_append_list(bl* list1, bl* list2);
96 void bl_insert(bl* list, size_t indx, const void* data);
97 void bl_set(bl* list, size_t indx, const void* data);
98 void bl_print_structure(bl* list);
99 void bl_copy(bl* list, size_t start, size_t length, void* vdest);
100
101 /**
102 * Inserts the given datum into the list in such a way that the list
103 * stays sorted in ascending order according to the given comparison
104 * function (assuming it was sorted to begin with!).
105 *
106 * The inserted element will be placed _after_ existing elements with
107 * the same value.
108 *
109 * The comparison function is the same as qsort's: it should return
110 * 1 if the first arg is greater than the second arg
111 * 0 if they're equal
112 * -1 if the first arg is smaller.
113 *
114 * The index where the element was inserted is returned.
115 */
116 size_t bl_insert_sorted(bl* list, const void* data, int (*compare)(const void* v1, const void* v2));
117
118 /**
119 If the item already existed in the list (ie, the compare function
120 returned zero), then -1 is returned. Otherwise, the index at which
121 the item was inserted is returned.
122 */
123 ptrdiff_t bl_insert_unique_sorted(bl* list, const void* data,
124 int (*compare)(const void* v1, const void* v2));
125
126 /*
127 Removes all the elements, but doesn't free the first block, which makes
128 it slightly faster for the case when you're going to add more elements
129 right away, since you don't have to free() the old block then immediately
130 malloc() a new block.
131 */
132 void bl_remove_all_but_first(bl* list);
133 void bl_remove_index(bl* list, size_t indx);
134 void bl_remove_index_range(bl* list, size_t start, size_t length);
135 void* bl_find(bl* list, const void* data, int (*compare)(const void* v1, const void* v2));
136 ptrdiff_t bl_find_index(bl* list, const void* data, int (*compare)(const void* v1, const void* v2));
137
138 // returns 0 if okay, 1 if an error is detected.
139 int bl_check_consistency(bl* list);
140
141 // returns 0 if okay, 1 if an error is detected.
142 int bl_check_sorted(bl* list, int (*compare)(const void* v1, const void* v2), int isunique);
143
144 ///////////////////////////////////////////////
145 // special-case functions for string lists. //
146 ///////////////////////////////////////////////
147 /*
148 sl makes a copy of the string using strdup().
149 It will be freed when the string is removed from the list or the list is
150 freed.
151 */
152 typedef bl sl;
153 sl* sl_new(int blocksize);
154
155 /*
156 The functions:
157 sl_init() ---> sl_init2()
158 sl_free() ---> sl_free2()
159 sl_add() ---> sl_add2()
160 sl_find() ---> sl_find2()
161 are defined by BSD, where they live in libc.
162
163 We therefore avoid these names, which breaks the principle of least surprise, but
164 makes life a bit easier.
165 */
166
167 void sl_init2(sl* list, int blocksize);
168
169 // free this list and all the strings it contains.
170 void sl_free2(sl* list);
171
172 void sl_append_contents(sl* dest, sl* src);
173
174 // Searches the sl for the given string. Comparisons use strcmp().
175 // Returns -1 if the string is not found, or the first index where it was found.
176 ptrdiff_t sl_index_of(sl* lst, const char* str);
177 ptrdiff_t sl_last_index_of(sl* lst, const char* str);
178
179 // Returns 0 if the string is not in the sl, 1 otherwise.
180 // (same as sl_index_of(lst, str) > -1)
181 int sl_contains(sl* lst, const char* str);
182
183 // just free the list structure, not the strings in it.
184 void sl_free_nonrecursive(sl* list);
185
186 Pure InlineDeclare size_t sl_size(const sl* list);
187
188 // copies the string and enqueues it; returns the newly-allocate string.
189 char* sl_append(sl* list, const char* string);
190 // appends the string; doesn't copy it.
191 void sl_append_nocopy(sl* list, const char* string);
192
193 void sl_append_array(sl* list, const char** strings, size_t n);
194
195 // copies the string and pushes the copy. Returns the copy.
196 char* sl_push(sl* list, const char* data);
197 // returns the last string: it's your responsibility to free it.
198 char* sl_pop(sl* list);
199 char* sl_get(sl* list, size_t n);
200 char* sl_get_const(const sl* list, size_t n);
201 // sets the string at the given index to the given value.
202 // if there is already a string at that index, frees it.
203 char* sl_set(sl* list, size_t n, const char* val);
204 int sl_check_consistency(sl* list);
205 // inserts a copy of the given string.
206 char* sl_insert(sl* list, size_t indx, const char* str);
207 // inserts the given string.
208 void sl_insert_nocopy(sl* list, size_t indx, const char* str);
209 // frees all the strings and removes them from the list.
210 void sl_remove_all(sl* list);
211
212 // inserts the string; doesn't copy it.
213 void sl_insert_sorted_nocopy(sl* list, const char* string);
214
215 // inserts a copy of the string; returns the newly-allocated string.
216 char* sl_insert_sorted(sl* list, const char* string);
217
218 // Inserts the (newly-allocated) formatted string and returns it.
219 char*
220 #ifdef __GNUC__
221 ATTRIB_FORMAT(printf,2,3)
222 #endif
223 sl_insert_sortedf(sl* list, const char* format, ...);
224
225 void sl_remove_index_range(sl* list, size_t start, size_t length);
226
227 void sl_remove(sl* list, size_t index);
228
229 // Removes "string" if it is found in the list.
230 // Note that this checks pointer match, not a strcmp() match.
231 // Returns the index where the string was found, or -1 if it wasn't found.
232 ptrdiff_t sl_remove_string(sl* list, const char* string);
233
234 // Removes "string" if it is found in the list, using strcasecmp().
235 // Returns the string or NULL if not found.
236 char* sl_remove_string_bycaseval(sl* list, const char* string);
237
238 // Removes "string" if it is found in the list, using strcmp().
239 // Returns the index where the string was found, or -1 if it wasn't found.
240 ptrdiff_t sl_remove_string_byval(sl* list, const char* string);
241
242 // remove all elements starting from "start" to the end of the list.
243 void sl_remove_from(sl* list, size_t start);
244
245 void sl_merge_lists(sl* list1, sl* list2);
246 void sl_print(sl* list);
247
248 /*
249 Removes duplicate entries, using strcmp().
250 */
251 void sl_remove_duplicates(sl* lst);
252
253 /*
254 Splits the given string 'str' into substrings separated by 'sepstring'.
255 (Some of the substrings may be empty, for example if the 'sepstring' appears
256 consecutively.)
257 Adds them to 'lst', if non-NULL.
258 Allocates and fills a new sl* if 'lst' is NULL.
259 The original string can be reconstructed by calling "sl_implode(lst, sepstring)"
260 */
261 sl* sl_split(sl* lst, const char* str, const char* sepstring);
262
263 // Like the PHP function implode(), joins each element in the list with the given
264 // "join" string. The result is a newly-allocate string containing:
265 // sl_get(list, 0) + join + sl_get(list, 1) + join + ... + join + sl_get(list, N-1)
266 // -AKA sl_join.
267 char* sl_implode(sl* list, const char* join);
268
269 // like Python's joinstring.join(list)
270 // -AKA sl_implode
271 char* sl_join(sl* list, const char* joinstring);
272
273 // same as sl_join(reverse(list), str)
274 char* sl_join_reverse(sl* list, const char* join);
275
276 // Appends the (newly-allocated) formatted string and returns it.
277 char*
278 #ifdef __GNUC__
279 ATTRIB_FORMAT(printf,2,3)
280 #endif
281 sl_appendf(sl* list, const char* format, ...);
282
283 // Appends the (newly-allocated) formatted string and returns it.
284 char* sl_appendvf(sl* list, const char* format, va_list va);
285
286 // Inserts the (newly-allocated) formatted string and returns it.
287 char*
288 #ifdef __GNUC__
289 ATTRIB_FORMAT(printf,3,4)
290 #endif
291 sl_insertf(sl* list, size_t index, const char* format, ...);
292
293
294 #define DEFINE_SORT 1
295 #define nl il
296 #define number int
297 #include "bl-nl.h"
298 #undef nl
299 #undef number
300
301 #define nl ll
302 #define number int64_t
303 #include "bl-nl.h"
304 #undef nl
305 #undef number
306
307 #define nl dl
308 #define number double
309 #include "bl-nl.h"
310 #undef nl
311 #undef number
312
313 #define nl fl
314 #define number float
315 #include "bl-nl.h"
316 #undef nl
317 #undef number
318
319 #undef DEFINE_SORT
320 #define DEFINE_SORT 0
321 #define nl pl
322 #define number void*
323 #include "bl-nl.h"
324 #undef nl
325 #undef number
326
327 #undef DEFINE_SORT
328
329 //////// Special functions ////////
330 void pl_free_elements(pl* list);
331 size_t pl_insert_sorted(pl* list, const void* data, int (*compare)(const void* v1, const void* v2));
332
333 #ifdef INCLUDE_INLINE_SOURCE
334 #define InlineDefine InlineDefineH
335
336 #include "bl.inc"
337
338 #define nl il
339 #define number int
340 #include "bl-nl.inc"
341 #undef nl
342 #undef number
343
344 #define nl ll
345 #define number int64_t
346 #include "bl-nl.inc"
347 #undef nl
348 #undef number
349
350 #define nl pl
351 #define number void*
352 #include "bl-nl.inc"
353 #undef nl
354 #undef number
355
356 #define nl dl
357 #define number double
358 #include "bl-nl.inc"
359 #undef nl
360 #undef number
361
362 #define nl fl
363 #define number float
364 #include "bl-nl.inc"
365 #undef nl
366 #undef number
367
368 #undef InlineDefine
369 #endif
370
371
372 #endif