1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
|
/*
WARNING: This file was generated by dkct.
Changes you make here will be lost if dkct is run again!
You should modify the original source and run dkct on it.
Original source: dk4sto.ctr
*/
/*
Copyright (C) 2011-2017, Dirk Krause
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above opyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the author nor the names of contributors may be used
to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef DK4STO_H_INCLUDED
/** Avoid multiple inclusions. */
#define DK4STO_H_INCLUDED 1
#line 10 "dk4sto.ctr"
/** @file
Sorted and unsorted data storage in
AVL trees or double linked lists.
CRT on Windows: Optional.
*/
#ifndef DK4CONF_H_INCLUDED
#include "dk4conf.h"
#endif
#ifndef DK4TYPES_H_INCLUDED
#include "dk4types.h"
#endif
#ifndef DK4ERROR_H_INCLUDED
#include "dk4error.h"
#endif
/** Evaluation result for an object.
*/
typedef union {
double d; /**< Evaluation to double. */
float f; /**< Evaluation to float. */
long l; /**< Evaluation to long. */
unsigned long ul; /**< Evaluation to unsigned long. */
int i; /**< Evaluation to int. */
unsigned int ui; /**< Evaluation to unsigned int. */
short s; /**< Evaluation to short. */
unsigned short us; /**< Evaluation to unsigned short. */
char c; /**< Evaluation to char. */
unsigned char uc; /**< Evaluation to unsigned char. */
} dk4_object_eval_res_t;
/** Internal node for dk4_sto_t.
This structure is used internally by the dksto module.
*/
struct dk4__sto__node {
dk4_object_eval_res_t v; /**< Object evaluation of @a o. */
struct dk4__sto__node *p; /**< Parent node in tree. */
struct dk4__sto__node *l; /**< Left child (tree) or neighbour (list). */
struct dk4__sto__node *r; /**< Right child (tree) or neighbour (list). */
void *o; /**< Object to store. */
short b; /**< Balance flag (used in tree only). */
short w; /**< Direction to walk. */
};
/** Internal node for dk4_sto_t.
*/
typedef struct dk4__sto__node dk4_sto_node_t;
/** Pointer to internal node for dk4_sto_t.
*/
typedef dk4_sto_node_t *dk4_sto_node_p;
/** Evaluation functions.
These function type are used to evaluate and compare objects
stored in a sorted dk4_sto_t.
*/
/** Object evaluation function for sorted storage.
*/
typedef char dk4_fct_eval_c_t(void const *obj, int crit);
/** Object evaluation function for sorted storage.
*/
typedef unsigned char dk4_fct_eval_uc_t(void const *obj, int crit);
/** Object evaluation function for sorted storage.
*/
typedef short dk4_fct_eval_s_t(void const *obj, int crit);
/** Object evaluation function for sorted storage.
*/
typedef unsigned short dk4_fct_eval_us_t(void const *obj, int crit);
/** Object evaluation function for sorted storage.
*/
typedef int dk4_fct_eval_i_t(void const *obj, int crit);
/** Object evaluation function for sorted storage.
*/
typedef unsigned int dk4_fct_eval_ui_t(void const *obj, int crit);
/** Object evaluation function for sorted storage.
*/
typedef long dk4_fct_eval_l_t(void const *obj, int crit);
/** Object evaluation function for sorted storage.
*/
typedef unsigned long dk4_fct_eval_ul_t(void const *obj, int crit);
/** Object evaluation function for sorted storage.
*/
typedef float dk4_fct_eval_f_t(void const *obj, int crit);
/** Object evaluation function for sorted storage.
*/
typedef double dk4_fct_eval_d_t(void const *obj, int crit);
/** Object comparison function for sorted storage.
When comparing to objects while inserting a new object into
a storage both @a o1 and @a o2 are object pointers.
When this function is called from dk4sto_it_find_like() the
@a o1 argument is an object pointer from the storage,
@a o2 is the pointer passed to dk4sto_it_find_like().
*/
typedef int dk4_fct_comp_t(void const *o1, void const *o2, int crit);
/** Choice for object evaluation function.
*/
typedef union {
dk4_fct_eval_c_t *c; /**< Evaluate to char. */
dk4_fct_eval_uc_t *uc; /**< Evaluate to unsigned char. */
dk4_fct_eval_s_t *s; /**< Evaluate to short. */
dk4_fct_eval_us_t *us; /**< Evaluate to unsigned short. */
dk4_fct_eval_i_t *i; /**< Evaluate to int. */
dk4_fct_eval_ui_t *ui; /**< Evaluate to unsigned int. */
dk4_fct_eval_l_t *l; /**< Evaluate to long. */
dk4_fct_eval_ul_t*ul; /**< Evaluate to unsigned long. */
dk4_fct_eval_f_t *f; /**< Evaluate to float. */
dk4_fct_eval_d_t *d; /**< Evaluate to double. */
dk4_fct_comp_t *comp; /**< Compare two objects. */
} dk4_object_eval_fct_t;
/** Object storage.
A storage can be used to store pointers to objects.
*/
typedef struct dk4__sto__t {
dk4_object_eval_fct_t e; /**< Comparison or evaluation function. */
dk4_sto_node_p *d; /**< Critical path for delete operations. */
dk4_sto_node_t *r; /**< Tree root or start of list. */
void *i; /**< Double-linked list of iterators. */
int h; /**< Selection for comparison or evaluation. */
int c; /**< Comparison or evaluation criteria. */
int t; /**< Flag: Use tree, 1=tree, 0=list. */
short l; /**< Path length of critical path. */
} dk4_sto_t;
/** Storage iterator.
This structure can be used to iterate through a dk4_sto_t.
*/
struct dk4__sto__iterator {
struct dk4__sto__iterator *l; /**< Left neighbour (preceeding iterator). */
struct dk4__sto__iterator *r; /**< Right neighbour (following iterator). */
dk4_sto_t *s; /**< Owner storage. */
dk4_sto_node_t *c; /**< Current node, current position. */
};
/** Storage iterator. Can be used to iterate through a dk4_sto_t.
*/
typedef struct dk4__sto__iterator dk4_sto_it_t;
#ifdef __cplusplus
extern "C" {
#endif
/** Create new storage.
@param erp Error report, may be NULL.
@return Pointer to new storage on succcess, NULL on error.
Use dk4sto_close() to destroy the storage when done with it.
Error codes:
- DK4_E_INVALID_ARGUMENTS<br>
if elsize or nelem is 0,
- DK4_E_MATH_OVERFLOW<br>
on numeric overflow when calculating the
product of elsize and nelem,
- DK4_E_MEMORY_ALLOCATION_FAILED<br>
with mem.elsize and mem.nelem
set if there is not enough memory available.
*/
dk4_sto_t *
dk4sto_open(dk4_er_t *erp);
/** Destroy storage, release memory.
@param st Storage to destroy.
*/
void
dk4sto_close(dk4_sto_t *st);
/** Remove all pointers from a storage.
@param st Storage.
*/
void
dk4sto_remove_all(dk4_sto_t *st);
/** Remove one pointer from storage.
@param st Storage.
@param o Object pointer to remove.
@param erp Error report, may be NULL.
@return 1 on success, 0 on error (not found).
Error codes:
- DK4_E_INVALID_ARGUMENTS<br>
if st or o is NULL,
- DK4_E_NOT_FOUND
if o was not found in st.
*/
int
dk4sto_remove(dk4_sto_t *st, void *o, dk4_er_t *erp);
/** Add object pointer to storage.
@param st Storage.
@param o Object pointer to add.
@param erp Error report, may be NULL.
@return 1 on success, 0 on error (not enough memory).
Error codes:
- DK4_E_INVALID_ARGUMENTS<br>
if st or os is NULL,
- DK4_E_MEMORY_ALLOCATION_FAILED<br>
with mem.elsize and mem.nelem
set if there is not enough memory available.
*/
int
dk4sto_add(dk4_sto_t *st, void *o, dk4_er_t *erp);
/** Create iterator for storage.
@param st Storage.
@param erp Error report, may be NULL.
@return Pointer to new iterator on success, NULL on error.
Use dk4sto_it_close() to destroy the iterator when done with it.
When closing/destroying a storage all iterators for that
storage are destroyed automatically.
Error codes:
- DK4_E_INVALID_ARGUMENTS<br>
if st is NULL,
- DK4_E_MEMORY_ALLOCATION_FAILED<br>
with mem.elsize and mem.nelem
set if there is not enough memory available.
*/
dk4_sto_it_t *
dk4sto_it_open(dk4_sto_t *st, dk4_er_t *erp);
/** Destroy iterator.
@param it Iterator to destroy.
*/
void
dk4sto_it_close(dk4_sto_it_t *it);
/** Reset iterator (next call to dk4sto_it_next() will return the
first object pointer.
@param it Iterator.
*/
void
dk4sto_it_reset(dk4_sto_it_t *it);
/** Return next object pointer.
@param it Iterator.
@return Pointer to next object on success, NULL on error
(no more pointers available).
*/
void *
dk4sto_it_next(dk4_sto_it_t *it);
/** Find a pointer exactly.
@param i Iterator.
@param o Object pointer.
@return Object pointer on success, NULL on error (object not found).
The next calls to dk4sto_it_next() will return pointers to objects
after the specified object \a o.
*/
void *
dk4sto_it_find_exact(dk4_sto_it_t *i, void const *o);
/** Find object pointer for object evaluating equally to \a o.
@param i Iterator.
@param o Object pointer.
@param cr Comparison criteria.
@return Object pointer on success, NULL on error (no such object).
*/
void *
dk4sto_it_find_like(dk4_sto_it_t *i, void const *o, int cr);
/** Set evaluation function.
@param st Storage.
@param f Function evaluating a pointer to a char.
@param cr Evaluation criteria.
@return 1 on success, 0 on error.
*/
int
dk4sto_set_eval_c(dk4_sto_t *st, dk4_fct_eval_c_t *f, int cr);
/** Set evaluation function.
@param st Storage.
@param f Function evaluating a pointer to an unsigned char.
@param cr Evaluation criteria.
@return 1 on success, 0 on error.
*/
int
dk4sto_set_eval_uc(dk4_sto_t *st, dk4_fct_eval_uc_t *f, int cr);
/** Set evaluation function.
@param st Storage.
@param f Function evaluating a pointer to a short.
@param cr Evaluation criteria.
@return 1 on success, 0 on error.
*/
int
dk4sto_set_eval_s(dk4_sto_t *st, dk4_fct_eval_s_t *f, int cr);
/** Set evaluation function.
@param st Storage.
@param f Function evaluating a pointer to an unsigned short.
@param cr Evaluation criteria.
@return 1 on success, 0 on error.
*/
int
dk4sto_set_eval_us(dk4_sto_t *st, dk4_fct_eval_us_t *f, int cr);
/** Set evaluation function.
@param st Storage.
@param f Function evaluating a pointer to an int.
@param cr Evaluation criteria.
@return 1 on success, 0 on error.
*/
int
dk4sto_set_eval_i(dk4_sto_t *st, dk4_fct_eval_i_t *f, int cr);
/** Set evaluation function.
@param st Storage.
@param f Function evaluating a pointer to an unsigned int.
@param cr Evaluation criteria.
@return 1 on success, 0 on error.
*/
int
dk4sto_set_eval_ui(dk4_sto_t *st, dk4_fct_eval_ui_t *f, int cr);
/** Set evaluation function.
@param st Storage.
@param f Function evaluating a pointer to a long.
@param cr Evaluation criteria.
@return 1 on success, 0 on error.
*/
int
dk4sto_set_eval_l(dk4_sto_t *st, dk4_fct_eval_l_t *f, int cr);
/** Set evaluation function.
@param st Storage.
@param f Function evaluating a pointer to an unsigned long.
@param cr Evaluation criteria.
@return 1 on success, 0 on error.
*/
int
dk4sto_set_eval_ul(dk4_sto_t *st, dk4_fct_eval_ul_t *f, int cr);
/** Set evaluation function.
@param st Storage.
@param f Function evaluating a pointer to a float.
@param cr Evaluation criteria.
@return 1 on success, 0 on error.
*/
int
dk4sto_set_eval_f(dk4_sto_t *st, dk4_fct_eval_f_t *f, int cr);
/** Set evaluation function.
@param st Storage.
@param f Function evaluating a pointer to a double.
@param cr Evaluation criteria.
@return 1 on success, 0 on error.
*/
int
dk4sto_set_eval_d(dk4_sto_t *st, dk4_fct_eval_d_t *f, int cr);
/** Set comparison function.
@param st Storage.
@param f Function comparing two object pointers.
@param cr Comparison criteria.
@return 1 on success, 0 on error.
*/
int
dk4sto_set_comp(dk4_sto_t *st, dk4_fct_comp_t *f, int cr);
/** Allow use of tree structures.
This function must be called before adding any data.
@param st Storage.
@param ok Flag: Trees may be used.
@return 1 on success, 0 on error.
*/
int
dk4sto_use_trees(dk4_sto_t *st,int ok);
/** Find object pointer at the trees root.
@param s Storage.
*/
void *
dk4sto_find_root(dk4_sto_t *s);
/** Find object pointer for the parent node
of the last found object.
@param i Iterator.
@return Object pointer on success, NULL on error.
*/
void *
dk4sto_it_find_parent(dk4_sto_it_t *i);
/** Find object pointer for node on left child of the last found object.
@param i Iterator.
@return Object pointer on success, NULL on error.
*/
void *
dk4sto_it_find_left(dk4_sto_it_t *i);
/** Find object pointer for node on right child of the last found object.
@param i Iterator.
@return Object pointer on success, NULL on error.
*/
void *
dk4sto_it_find_right(dk4_sto_it_t *i);
/** Find object pointer at the trees root.
@param i Iterator.
*/
void *
dk4sto_it_find_root(dk4_sto_it_t *i);
#ifdef __cplusplus
}
#endif
#endif
|