1 | /** @file
|
---|
2 | * innotek Portable Runtime - Abstract Table/Trees.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License as published by the Free Software Foundation,
|
---|
12 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
13 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
14 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | */
|
---|
16 |
|
---|
17 | #ifndef ___iprt_table_h
|
---|
18 | #define ___iprt_table_h
|
---|
19 |
|
---|
20 | #include <iprt/types.h>
|
---|
21 |
|
---|
22 | /** @defgroup grp_rt_tab RTTab - Generic Tree and Table Interface.
|
---|
23 | * @ingroup grp_rt
|
---|
24 | * @{
|
---|
25 | */
|
---|
26 |
|
---|
27 | __BEGIN_DECLS
|
---|
28 |
|
---|
29 | /** Pointer to an allocator. */
|
---|
30 | typedef struct RTTABALLOCATOR *PRTTABALLOCATOR;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Allocates memory.
|
---|
34 | *
|
---|
35 | * @returns Pointer to the allocated memory.
|
---|
36 | * @returns NULL on failure. (don't throw!)
|
---|
37 | * @param pAllocator The allocator structure.
|
---|
38 | * @param cb The number of bytes to allocate. (Never 0.)
|
---|
39 | */
|
---|
40 | typedef DECLCALLBACK(void *) FNRTTABALLOC(PRTTABALLOCATOR pAllocator, size_t cb);
|
---|
41 | /** Pointer to a FNRTTABALLOC() function. */
|
---|
42 | typedef FNRTTABALLOC *PFNRTTABALLOC;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Frees memory.
|
---|
46 | *
|
---|
47 | * @param pAllocator The allocator structure.
|
---|
48 | * @param pv The memory to free. (can be NULL)
|
---|
49 | */
|
---|
50 | typedef DECLCALLBACK(void *) FNRTTABFREE(PRTTABALLOCATOR pAllocator, void *pv);
|
---|
51 | /** Pointer to a FNRTTABFREE() function. */
|
---|
52 | typedef FNRTTABFREE *PFNRTTABFREE;
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * The allocator structure.
|
---|
56 | * (Hint: use this as like 'base class' for your custom allocators.)
|
---|
57 | */
|
---|
58 | typedef struct RTTABALLOCATOR
|
---|
59 | {
|
---|
60 | /** The allocation function. */
|
---|
61 | PFNRTTABALLOC pfnAlloc;
|
---|
62 | /** The free function. */
|
---|
63 | PFNRTTABFREE pfnFree;
|
---|
64 | } RTTABALLOCATOR;
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Gets the default allocator.
|
---|
68 | *
|
---|
69 | * @returns Pointer to the default allocator.
|
---|
70 | */
|
---|
71 | RTDECL(RTTABALLOCATOR) RTTabDefaultAllocator(void);
|
---|
72 |
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Compares two table items.
|
---|
76 | *
|
---|
77 | * @returns 0 if equal
|
---|
78 | * @returns <0 if pvItem1 is less than pvItem2 (pvItem2 is then greater than pvItem1).
|
---|
79 | * @returns >0 if pvItem1 is less than pvItem2 (pvItem1 is then greater than pvItem2).
|
---|
80 | *
|
---|
81 | * @param pvItem1 The first item.
|
---|
82 | * @param pvItem2 The second item.
|
---|
83 | * @param pvUser The user argument.
|
---|
84 | */
|
---|
85 | typedef DECLCALLBACK(int) FNRTTABCOMP(const void *pvItem1, const void *pvItem2, void *pvUser);
|
---|
86 | /** Pointer to a FNRTTABCOMP() function. */
|
---|
87 | typedef FNRTTABCOMP *PFNRTTABCOMP;
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Duplicates a table item.
|
---|
91 | * This is used when duplicating or copying a table.
|
---|
92 | *
|
---|
93 | * @returns Pointer to the copy.
|
---|
94 | * @returns NULL on failure.
|
---|
95 | *
|
---|
96 | * @param pvItem The item to copy.
|
---|
97 | * @param pvUser The user argument.
|
---|
98 | */
|
---|
99 | typedef DECLCALLBACK(void *) FNRTTABDUPLICATE(const void *pvItem, void *pvUser);
|
---|
100 | /** Pointer to a FNRTTABDUPLICATE() function. */
|
---|
101 | typedef FNRTTABDUPLICATE *PFNRTTABDUPLICATE;
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Callback function for doing something with an item.
|
---|
105 | *
|
---|
106 | * What exactly we're doing is specific to the context of the call.
|
---|
107 | *
|
---|
108 | * @param pvItem The item.
|
---|
109 | * @param pvUser The user argument.
|
---|
110 | */
|
---|
111 | typedef DECLCALLBACK(void) FNRTTABCALLBACK(const void *pvItem, void *pvUser);
|
---|
112 | /** Pointer to a FNRTTABCALLBACK() function. */
|
---|
113 | typedef FNRTTABCALLBACK *PFNRTTABCALLBACK;
|
---|
114 |
|
---|
115 |
|
---|
116 | /** Pointer to const table operations. */
|
---|
117 | typedef const struct RTTABOPS *PCRTTABOPS;
|
---|
118 | /** Pointer to a table. */
|
---|
119 | typedef struct RTTAB *PRTTAB;
|
---|
120 | /** Pointer to a const table. */
|
---|
121 | typedef const struct RTTAB *PCRTTAB;
|
---|
122 | /** Pointer to a traverser. */
|
---|
123 | typedef struct RTTABTRAVERSER *PRTTABTRAVERSER;
|
---|
124 | /** Pointer to a const traverser. */
|
---|
125 | typedef const struct RTTABTRAVERSER *PCRTTABTRAVERSER;
|
---|
126 | /** Pointer to a traverser core. */
|
---|
127 | typedef struct RTTABTRAVERSERCORE *PRTTABTRAVERSERCORE;
|
---|
128 | /** Pointer to a const traverser core. */
|
---|
129 | typedef const struct RTTABTRAVERSERCORE *PCRTTABTRAVERSERCORE;
|
---|
130 |
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Table operations.
|
---|
134 | */
|
---|
135 | typedef struct RTTABOPS
|
---|
136 | {
|
---|
137 | /**
|
---|
138 | * Create a table.
|
---|
139 | *
|
---|
140 | * @returns Pointer to the new table.
|
---|
141 | * @returns NULL if we're out of memory or some other resource.
|
---|
142 | * @param pOps The table operations.
|
---|
143 | * @param fCreateFlags The table type specific creation flags.
|
---|
144 | * @param pAllocator Custom allocator. Pass NULL for the default allocator.
|
---|
145 | * @param pfnComp The comparision function.
|
---|
146 | */
|
---|
147 | DECLCALLBACKMEMBER(PRTTAB, pfnCreate)(PCRTTABOPS pOps, unsigned fCreateFlags, PRTTABALLOCATOR pAllocator, PFNRTTABCOMP pfnComp);
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Duplicates a table to a table of the same type.
|
---|
151 | *
|
---|
152 | * @returns Pointer to the new table.
|
---|
153 | * @returns NULL if we're out of memory or some other resource.
|
---|
154 | * @param pTab The table to duplicate.
|
---|
155 | * @param pfnDuplicate Pointer to the item duplication function. If NULL the new table will
|
---|
156 | * be referencing the same data as the old one.
|
---|
157 | * @param pfnNewCB Callback which is called for all the items in the new table. Optional.
|
---|
158 | * @param pAllocator Custom allocator. Pass NULL to use the same allocator as pTab.
|
---|
159 | */
|
---|
160 | DECLCALLBACKMEMBER(PRTTAB, pfnDuplicate)(PCRTTAB pTab, PFNRTTABDUPLICATE pfnDuplicate, PFNRTTABCALLBACK pfnNewCB, PRTTABALLOCATOR pAllocator);
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Destroys a table.
|
---|
164 | *
|
---|
165 | * @param pTab The table to destroy.
|
---|
166 | */
|
---|
167 | DECLCALLBACKMEMBER(void, pfnDestroy)(PRTTAB pTab);
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * Inserts an item into the table, if a matching item is encountered
|
---|
171 | * the pointer to the pointer to it will be returned.
|
---|
172 | *
|
---|
173 | * @returns Pointer to the item pointer in the table.
|
---|
174 | * This can be used to replace existing items (don't break anything, dude).
|
---|
175 | * @returns NULL if we failed to allocate memory for the new node.
|
---|
176 | * @param pTab The table.
|
---|
177 | * @param pvItem The item which will be inserted if an matching item was not found in the table.
|
---|
178 | */
|
---|
179 | DECLCALLBACKMEMBER(void **, pfnProbe)(PRTTAB pTab, void *pvItem);
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Inserts an item into the table, fail if a matching item exists.
|
---|
183 | *
|
---|
184 | * @returns NULL on success and allocation failure.
|
---|
185 | * @returns Pointer to the matching item.
|
---|
186 | * @param pTab The table.
|
---|
187 | * @param pvItem The item which is to be inserted.
|
---|
188 | */
|
---|
189 | DECLCALLBACKMEMBER(void *, pfnInsert)(PRTTAB pTab, void *pvItem);
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Inserts an item into the table, if a matching item is encountered
|
---|
193 | * it will be replaced and returned.
|
---|
194 | *
|
---|
195 | * @returns NULL if inserted and allocation failure.
|
---|
196 | * @returns Pointer to the replaced item.
|
---|
197 | * @param pTab The table.
|
---|
198 | * @param pvItem The item which is to be inserted.
|
---|
199 | */
|
---|
200 | DECLCALLBACKMEMBER(void *, pfnReplace)(PRTTAB pTab, void *pvItem);
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * Removes an item from the table if found.
|
---|
204 | *
|
---|
205 | * @returns Pointer to the removed item.
|
---|
206 | * @returns NULL if no item matched pvItem.
|
---|
207 | * @param pTab The table.
|
---|
208 | * @param pvItem The item which is to be inserted.
|
---|
209 | */
|
---|
210 | DECLCALLBACKMEMBER(void *, pfnRemove)(PRTTAB pTab, const void *pvItem);
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * Finds an item in the table.
|
---|
214 | *
|
---|
215 | * @returns Pointer to the item it found.
|
---|
216 | * @returns NULL if no item matched pvItem.
|
---|
217 | * @param pTab The table.
|
---|
218 | * @param pvItem The item which is to be inserted.
|
---|
219 | */
|
---|
220 | DECLCALLBACKMEMBER(void *, pfnFind)(PRTTAB pTab, const void *pvItem);
|
---|
221 |
|
---|
222 | /**
|
---|
223 | * Initializes a traverser to the NULL item.
|
---|
224 | *
|
---|
225 | * The NULL item is an imaginary table item before the first and after
|
---|
226 | * the last items in the table.
|
---|
227 | *
|
---|
228 | * @returns Pointer to the traverser positioned at the NULL item.
|
---|
229 | * @returns NULL on failure to allocate the traverser.
|
---|
230 | *
|
---|
231 | * @param pTab The table.
|
---|
232 | * @param pTravNew Pointer to a preallocated structure. Optional.
|
---|
233 | */
|
---|
234 | DECLCALLBACKMEMBER(PRTTABTRAVERSERCORE, pfnTravInit)(PRTTAB pTab, PRTTABTRAVERSER pTravNew);
|
---|
235 |
|
---|
236 | /**
|
---|
237 | * Initializes a traverser to the first item in the table.
|
---|
238 | *
|
---|
239 | * If the table is empty, the traverser will be positioned at the NULL item
|
---|
240 | * like with RTTabTravInit().
|
---|
241 | *
|
---|
242 | * @returns Pointer to the traverser positioned at the first item or NULL item.
|
---|
243 | * @returns NULL on failure to allocate the traverser.
|
---|
244 | *
|
---|
245 | * @param pTab The table.
|
---|
246 | * @param pTravNew Pointer to a preallocated structure. Optional.
|
---|
247 | */
|
---|
248 | DECLCALLBACKMEMBER(PRTTABTRAVERSERCORE, pfnTravFirst)(PRTTAB pTab, PRTTABTRAVERSER pTravNew);
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * Initializes a traverser to the last item in the table.
|
---|
252 | *
|
---|
253 | * If the table is empty, the traverser will be positioned at the NULL item
|
---|
254 | * like with RTTabTravInit().
|
---|
255 | *
|
---|
256 | * @returns Pointer to the traverser positioned at the last item or NULL item.
|
---|
257 | * @returns NULL on failure to allocate the traverser.
|
---|
258 | *
|
---|
259 | * @param pTab The table.
|
---|
260 | * @param pTravNew Pointer to a preallocated structure. Optional.
|
---|
261 | */
|
---|
262 | DECLCALLBACKMEMBER(PRTTABTRAVERSERCORE, pfnTravLast)(PRTTAB pTab, PRTTABTRAVERSER pTravNew);
|
---|
263 |
|
---|
264 | /**
|
---|
265 | * Initializes a traverser to an item matching the given one.
|
---|
266 | *
|
---|
267 | * If the item isn't found, the traverser will be positioned at the NULL item
|
---|
268 | * like with RTTabTravInit().
|
---|
269 | *
|
---|
270 | * @returns Pointer to the traverser positioned at the matching item or NULL item.
|
---|
271 | * @returns NULL on failure to allocate the traverser.
|
---|
272 | *
|
---|
273 | * @param pTab The table.
|
---|
274 | * @param pTravNew Pointer to a preallocated structure. Optional.
|
---|
275 | * @param pvItem The item to find the match to.
|
---|
276 | */
|
---|
277 | DECLCALLBACKMEMBER(PRTTABTRAVERSERCORE, pfnTravFind)(PRTTAB pTab, PRTTABTRAVERSER pTravNew, const void *pvItem);
|
---|
278 |
|
---|
279 | /**
|
---|
280 | * Initializes a traverser to the inserted item.
|
---|
281 | *
|
---|
282 | * If there already exists an item in the tree matching pvItem, the traverser
|
---|
283 | * is positioned at that item like with RTTabTravFind().
|
---|
284 | *
|
---|
285 | * If the insert operation failes because of an out of memory condition, the
|
---|
286 | * traverser will be positioned at the NULL item like with RTTabTravInit().
|
---|
287 | *
|
---|
288 | * @returns Pointer to the traverser positioned at the inserted, existing or NULL item.
|
---|
289 | * @returns NULL on failure to allocate the traverser.
|
---|
290 | *
|
---|
291 | * @param pTab The table.
|
---|
292 | * @param pTravNew Pointer to a preallocated structure. Optional.
|
---|
293 | * @param pvItem The item to be inserted.
|
---|
294 | */
|
---|
295 | DECLCALLBACKMEMBER(PRTTABTRAVERSERCORE, pfnTravInsert)(PRTTAB pTab, PRTTABTRAVERSER pTravNew, void *pvItem);
|
---|
296 |
|
---|
297 | /**
|
---|
298 | * Duplicates a traverser.
|
---|
299 | *
|
---|
300 | * @returns The pointer to the duplicate.
|
---|
301 | * @returns NULL on allocation failure.
|
---|
302 | *
|
---|
303 | * @param pTrav The traverser to duplicate.
|
---|
304 | * @param pTravNew Pointer to a preallocated structure. Optional.
|
---|
305 | */
|
---|
306 | DECLCALLBACKMEMBER(PRTTABTRAVERSERCORE, pfnTravDuplicate)(PRTTABTRAVERSERCORE pTrav, PCRTTABTRAVERSER pTravNew);
|
---|
307 |
|
---|
308 | /**
|
---|
309 | * Frees a traverser.
|
---|
310 | *
|
---|
311 | * This can safely be called even if the traverser structure
|
---|
312 | * wasn't dynamically allocated or the constructor failed.
|
---|
313 | *
|
---|
314 | * @param pTrav The traverser which is to be free.
|
---|
315 | */
|
---|
316 | DECLCALLBACKMEMBER(void, pfnTravFree)(PRTTABTRAVERSERCORE pTrav);
|
---|
317 |
|
---|
318 | /**
|
---|
319 | * Gets the current item.
|
---|
320 | *
|
---|
321 | * @returns The current item. (NULL indicates the imaginary NULL item.)
|
---|
322 | * @param pTrav The traverser.
|
---|
323 | */
|
---|
324 | DECLCALLBACKMEMBER(void *, pfnTravCur)(PCRTTABTRAVERSERCORE pTrav);
|
---|
325 |
|
---|
326 | /**
|
---|
327 | * Advances to the next item.
|
---|
328 | *
|
---|
329 | * @returns The new current item. (NULL indicates the imaginary NULL item.)
|
---|
330 | * @param pTrav The traverser.
|
---|
331 | */
|
---|
332 | DECLCALLBACKMEMBER(void *, pfnTravNext)(PRTTABTRAVERSERCORE pTrav);
|
---|
333 |
|
---|
334 | /**
|
---|
335 | * Advances to the previous item.
|
---|
336 | *
|
---|
337 | * @returns The new current item. (NULL indicates the imaginary NULL item.)
|
---|
338 | * @param pTrav The traverser.
|
---|
339 | */
|
---|
340 | DECLCALLBACKMEMBER(void *, pfnTravPrev)(PRTTABTRAVERSERCORE pTrav);
|
---|
341 |
|
---|
342 | /**
|
---|
343 | * Replaces the current item.
|
---|
344 | *
|
---|
345 | * This has the same restrictions as RTTabProbe(), e.g. it's not permitted to
|
---|
346 | * break the order of the table.
|
---|
347 | *
|
---|
348 | * @returns The replaced item.
|
---|
349 | * @returns NULL if the current item is the NULL item. The traverser
|
---|
350 | * and table remains unchanged.
|
---|
351 | * @param pTrav The traverser.
|
---|
352 | * @param pvItem The item to be inserted.
|
---|
353 | */
|
---|
354 | DECLCALLBACKMEMBER(void *, pfnTravReplace)(PRTTABTRAVERSERCORE pTrav, void *pvItem);
|
---|
355 |
|
---|
356 | /** The type of table type. */
|
---|
357 | const char *pszType;
|
---|
358 | } RTTABOPS;
|
---|
359 |
|
---|
360 | /**
|
---|
361 | * A table.
|
---|
362 | */
|
---|
363 | typedef struct RTTAB
|
---|
364 | {
|
---|
365 | /** The table operations. */
|
---|
366 | PCRTTABOPS pOps;
|
---|
367 | /** The function for comparing table items. */
|
---|
368 | PFNRTTABCOMP pfnComp;
|
---|
369 | /** The number of items in the table. */
|
---|
370 | RTUINT cItems;
|
---|
371 | /** The table generation number.
|
---|
372 | * This must be updated whenever the table changes. */
|
---|
373 | RTUINT idGeneration;
|
---|
374 | } RTTAB;
|
---|
375 |
|
---|
376 |
|
---|
377 | /**
|
---|
378 | * Create a table.
|
---|
379 | *
|
---|
380 | * @returns Pointer to the new table.
|
---|
381 | * @returns NULL if we're out of memory or some other resource.
|
---|
382 | * @param pOps The table operations.
|
---|
383 | * @param fCreateFlags The table type specific creation flags.
|
---|
384 | * @param pAllocator Custom allocator. Pass NULL for the default allocator.
|
---|
385 | * @param pfnComp The comparision function.
|
---|
386 | */
|
---|
387 | DECLINLINE(PRTTAB) RTTabCreate(PCRTTABOPS pOps, unsigned fCreateFlags, PRTTABALLOCATOR pAllocator, PFNRTTABCOMP pfnComp)
|
---|
388 | {
|
---|
389 | return pOps->pfnCreate(pOps, fCreateFlags, pAllocator, pfnComp);
|
---|
390 | }
|
---|
391 |
|
---|
392 | /**
|
---|
393 | * Duplicates a table to a table of the same type.
|
---|
394 | *
|
---|
395 | * @returns Pointer to the new table.
|
---|
396 | * @returns NULL if we're out of memory or some other resource.
|
---|
397 | * @param pTab The table to duplicate.
|
---|
398 | * @param pfnDuplicate Pointer to the item duplication function. If NULL the new table will
|
---|
399 | * be referencing the same data as the old one.
|
---|
400 | * @param pfnNewCB Callback which is called for all the items in the new table. Optional.
|
---|
401 | * @param pAllocator Custom allocator. Pass NULL to use the same allocator as pTab.
|
---|
402 | */
|
---|
403 | DECLINLINE(PRTTAB) RTTabDuplicate(PCRTTAB pTab, PFNRTTABDUPLICATE pfnDuplicate, PFNRTTABCALLBACK pfnNewCB, PRTTABALLOCATOR pAllocator)
|
---|
404 | {
|
---|
405 | return pTab->pOps->pfnDuplicate(pTab, pfnDuplicate, pfnNewCB, pAllocator);
|
---|
406 | }
|
---|
407 |
|
---|
408 | /**
|
---|
409 | * Destroys a table.
|
---|
410 | *
|
---|
411 | * @param pTab The table to destroy.
|
---|
412 | */
|
---|
413 | DECLINLINE(void) RTTabDestroy(PRTTAB pTab)
|
---|
414 | {
|
---|
415 | return pTab->pOps->pfnDestroy(pTab);
|
---|
416 | }
|
---|
417 |
|
---|
418 | /**
|
---|
419 | * Count the item in the table.
|
---|
420 | *
|
---|
421 | * @returns Number of items in the table.
|
---|
422 | * @param pTab The table to count.
|
---|
423 | */
|
---|
424 | DECLINLINE(RTUINT) RTTabCount(PRTTAB pTab)
|
---|
425 | {
|
---|
426 | return pTab->cItems;
|
---|
427 | }
|
---|
428 |
|
---|
429 | /**
|
---|
430 | * Inserts an item into the table, if a matching item is encountered
|
---|
431 | * the pointer to the pointer to it will be returned.
|
---|
432 | *
|
---|
433 | * @returns Pointer to the item pointer in the table.
|
---|
434 | * This can be used to replace existing items (don't break anything, dude).
|
---|
435 | * @returns NULL if we failed to allocate memory for the new node.
|
---|
436 | * @param pTab The table.
|
---|
437 | * @param pvItem The item which will be inserted if an matching item was not found in the table.
|
---|
438 | */
|
---|
439 | DECLINLINE(void **) RTTabProbe(PRTTAB pTab, void *pvItem)
|
---|
440 | {
|
---|
441 | return pTab->pOps->pfnProbe(pTab, pvItem);
|
---|
442 | }
|
---|
443 |
|
---|
444 | /**
|
---|
445 | * Inserts an item into the table, fail if a matching item exists.
|
---|
446 | *
|
---|
447 | * @returns NULL on success and allocation failure.
|
---|
448 | * @returns Pointer to the matching item.
|
---|
449 | * @param pTab The table.
|
---|
450 | * @param pvItem The item which is to be inserted.
|
---|
451 | */
|
---|
452 | DECLINLINE(void *) RTTabInsert(PRTTAB pTab, void *pvItem)
|
---|
453 | {
|
---|
454 | return pTab->pOps->pfnInsert(pTab, pvItem);
|
---|
455 | }
|
---|
456 |
|
---|
457 | /**
|
---|
458 | * Inserts an item into the table, if a matching item is encountered
|
---|
459 | * it will be replaced and returned.
|
---|
460 | *
|
---|
461 | * @returns NULL if inserted and allocation failure.
|
---|
462 | * @returns Pointer to the replaced item.
|
---|
463 | * @param pTab The table.
|
---|
464 | * @param pvItem The item which is to be inserted.
|
---|
465 | */
|
---|
466 | DECLINLINE(void *) RTTabReplace(PRTTAB pTab, void *pvItem)
|
---|
467 | {
|
---|
468 | return pTab->pOps->pfnReplace(pTab, pvItem);
|
---|
469 | }
|
---|
470 |
|
---|
471 | /**
|
---|
472 | * Removes an item from the table if found.
|
---|
473 | *
|
---|
474 | * @returns Pointer to the removed item.
|
---|
475 | * @returns NULL if no item matched pvItem.
|
---|
476 | * @param pTab The table.
|
---|
477 | * @param pvItem The item which is to be inserted.
|
---|
478 | */
|
---|
479 | DECLINLINE(void *) RTTabRemove(PRTTAB pTab, const void *pvItem)
|
---|
480 | {
|
---|
481 | return pTab->pOps->pfnRemove(pTab, pvItem);
|
---|
482 | }
|
---|
483 |
|
---|
484 | /**
|
---|
485 | * Finds an item in the table.
|
---|
486 | *
|
---|
487 | * @returns Pointer to the item it found.
|
---|
488 | * @returns NULL if no item matched pvItem.
|
---|
489 | * @param pTab The table.
|
---|
490 | * @param pvItem The item to find the match to.
|
---|
491 | */
|
---|
492 | DECLINLINE(void *) RTTabFind(PRTTAB pTab, const void *pvItem)
|
---|
493 | {
|
---|
494 | return pTab->pOps->pfnFind(pTab, pvItem);
|
---|
495 | }
|
---|
496 |
|
---|
497 |
|
---|
498 | /**
|
---|
499 | * Common traverser core.
|
---|
500 | */
|
---|
501 | typedef struct RTTABTRAVERSERCORE
|
---|
502 | {
|
---|
503 | /** The table being traversed. */
|
---|
504 | PRTTAB pTab;
|
---|
505 | /** Indicates that this traverser was allocated. */
|
---|
506 | bool fAllocated;
|
---|
507 | /** The table generation id this traverser was last updated for.
|
---|
508 | * This is used to catch up with table changes. */
|
---|
509 | RTUINT idGeneration;
|
---|
510 | } RTTABTRAVERSERCORE;
|
---|
511 |
|
---|
512 | /**
|
---|
513 | * Generic traverser structure.
|
---|
514 | *
|
---|
515 | * Tree implementations will use the tree specific part by mapping
|
---|
516 | * this structure onto their own internal traverser structure.
|
---|
517 | *
|
---|
518 | * @remark It would be better to use alloca() for allocating the structure,
|
---|
519 | * OTOH this is simpler for the user.
|
---|
520 | */
|
---|
521 | typedef struct RTTABTRAVERSER
|
---|
522 | {
|
---|
523 | /** The common core of the traverser data. */
|
---|
524 | RTTABTRAVERSERCORE Core;
|
---|
525 | /** The tree specific data. */
|
---|
526 | void *apvTreeSpecific[32];
|
---|
527 | } RTTABTRAVERSER;
|
---|
528 |
|
---|
529 |
|
---|
530 | /**
|
---|
531 | * Initializes a traverser to the NULL item.
|
---|
532 | *
|
---|
533 | * The NULL item is an imaginary table item before the first and after
|
---|
534 | * the last items in the table.
|
---|
535 | *
|
---|
536 | * @returns Pointer to the traverser positioned at the NULL item.
|
---|
537 | * @returns NULL on failure to allocate the traverser.
|
---|
538 | *
|
---|
539 | * @param pTab The table.
|
---|
540 | * @param pTravNew Pointer to a preallocated structure. Optional.
|
---|
541 | */
|
---|
542 | DECLINLINE(PRTTABTRAVERSERCORE) RTTabTravInit(PRTTAB pTab, PRTTABTRAVERSER pTravNew)
|
---|
543 | {
|
---|
544 | return pTab->pOps->pfnTravInit(pTab, pTravNew);
|
---|
545 | }
|
---|
546 |
|
---|
547 | /**
|
---|
548 | * Initializes a traverser to the first item in the table.
|
---|
549 | *
|
---|
550 | * If the table is empty, the traverser will be positioned at the NULL item
|
---|
551 | * like with RTTabTravInit().
|
---|
552 | *
|
---|
553 | * @returns Pointer to the traverser positioned at the first item or NULL item.
|
---|
554 | * @returns NULL on failure to allocate the traverser.
|
---|
555 | *
|
---|
556 | * @param pTab The table.
|
---|
557 | * @param pTravNew Pointer to a preallocated structure. Optional.
|
---|
558 | */
|
---|
559 | DECLINLINE(PRTTABTRAVERSERCORE) RTTabTravFirst(PRTTAB pTab, PRTTABTRAVERSER pTravNew)
|
---|
560 | {
|
---|
561 | return pTab->pOps->pfnTravFirst(pTab, pTravNew);
|
---|
562 | }
|
---|
563 |
|
---|
564 | /**
|
---|
565 | * Initializes a traverser to the last item in the table.
|
---|
566 | *
|
---|
567 | * If the table is empty, the traverser will be positioned at the NULL item
|
---|
568 | * like with RTTabTravInit().
|
---|
569 | *
|
---|
570 | * @returns Pointer to the traverser positioned at the last item or NULL item.
|
---|
571 | * @returns NULL on failure to allocate the traverser.
|
---|
572 | *
|
---|
573 | * @param pTab The table.
|
---|
574 | * @param pTravNew Pointer to a preallocated structure. Optional.
|
---|
575 | */
|
---|
576 | DECLINLINE(PRTTABTRAVERSERCORE) RTTabTravLast(PRTTAB pTab, PRTTABTRAVERSER pTravNew)
|
---|
577 | {
|
---|
578 | return pTab->pOps->pfnTravLast(pTab, pTravNew);
|
---|
579 | }
|
---|
580 |
|
---|
581 | /**
|
---|
582 | * Initializes a traverser to an item matching the given one.
|
---|
583 | *
|
---|
584 | * If the item isn't found, the traverser will be positioned at the NULL item
|
---|
585 | * like with RTTabTravInit().
|
---|
586 | *
|
---|
587 | * @returns Pointer to the traverser positioned at the matching item or NULL item.
|
---|
588 | * @returns NULL on failure to allocate the traverser.
|
---|
589 | *
|
---|
590 | * @param pTab The table.
|
---|
591 | * @param pTravNew Pointer to a preallocated structure. Optional.
|
---|
592 | * @param pvItem The item to find the match to.
|
---|
593 | */
|
---|
594 | DECLINLINE(PRTTABTRAVERSERCORE) RTTabTravFind(PRTTAB pTab, PRTTABTRAVERSER pTravNew, const void *pvItem)
|
---|
595 | {
|
---|
596 | return pTab->pOps->pfnTravFind(pTab, pTravNew, pvItem);
|
---|
597 | }
|
---|
598 |
|
---|
599 | /**
|
---|
600 | * Initializes a traverser to the inserted item.
|
---|
601 | *
|
---|
602 | * If there already exists an item in the tree matching pvItem, the traverser
|
---|
603 | * is positioned at that item like with RTTabTravFind().
|
---|
604 | *
|
---|
605 | * If the insert operation failes because of an out of memory condition, the
|
---|
606 | * traverser will be positioned at the NULL item like with RTTabTravInit().
|
---|
607 | *
|
---|
608 | * @returns Pointer to the traverser positioned at the inserted, existing or NULL item.
|
---|
609 | * @returns NULL on failure to allocate the traverser.
|
---|
610 | *
|
---|
611 | * @param pTab The table.
|
---|
612 | * @param pTravNew Pointer to a preallocated structure. Optional.
|
---|
613 | * @param pvItem The item to be inserted.
|
---|
614 | */
|
---|
615 | DECLINLINE(PRTTABTRAVERSERCORE) RTTabTravInsert(PRTTAB pTab, PRTTABTRAVERSER pTravNew, void *pvItem)
|
---|
616 | {
|
---|
617 | return pTab->pOps->pfnTravInsert(pTab, pTravNew, pvItem);
|
---|
618 | }
|
---|
619 |
|
---|
620 | /**
|
---|
621 | * Duplicates a traverser.
|
---|
622 | *
|
---|
623 | * @returns The pointer to the duplicate.
|
---|
624 | * @returns NULL on allocation failure.
|
---|
625 | *
|
---|
626 | * @param pTrav The traverser to duplicate.
|
---|
627 | * @param pTravNew Pointer to a preallocated structure. Optional.
|
---|
628 | */
|
---|
629 | DECLINLINE(PRTTABTRAVERSERCORE) RTTabTravDuplicate(PRTTABTRAVERSERCORE pTrav, PCRTTABTRAVERSER pTravNew)
|
---|
630 | {
|
---|
631 | if (pTrav)
|
---|
632 | return pTrav->pTab->pOps->pfnTravDuplicate(pTrav, pTravNew);
|
---|
633 | return NULL;
|
---|
634 | }
|
---|
635 |
|
---|
636 | /**
|
---|
637 | * Frees a traverser.
|
---|
638 | *
|
---|
639 | * This can safely be called even if the traverser structure
|
---|
640 | * wasn't dynamically allocated or the constructor failed.
|
---|
641 | *
|
---|
642 | * @param pTrav The traverser which is to be free.
|
---|
643 | */
|
---|
644 | DECLINLINE(void) RTTabTravFree(PRTTABTRAVERSERCORE pTrav)
|
---|
645 | {
|
---|
646 | if (pTrav && pTrav->fAllocated)
|
---|
647 | pTrav->pTab->pOps->pfnTravFree(pTrav);
|
---|
648 | }
|
---|
649 |
|
---|
650 | /**
|
---|
651 | * Gets the current item.
|
---|
652 | *
|
---|
653 | * @returns The current item. (NULL indicates the imaginary NULL item.)
|
---|
654 | * @param pTrav The traverser.
|
---|
655 | */
|
---|
656 | DECLINLINE(void *) RTTabTravCur(PCRTTABTRAVERSERCORE pTrav)
|
---|
657 | {
|
---|
658 | return pTrav->pTab->pOps->pfnTravCur(pTrav);
|
---|
659 | }
|
---|
660 |
|
---|
661 | /**
|
---|
662 | * Advances to the next item.
|
---|
663 | *
|
---|
664 | * @returns The new current item. (NULL indicates the imaginary NULL item.)
|
---|
665 | * @param pTrav The traverser.
|
---|
666 | */
|
---|
667 | DECLINLINE(void *) RTTabTravNext(PRTTABTRAVERSERCORE pTrav)
|
---|
668 | {
|
---|
669 | return pTrav->pTab->pOps->pfnTravNext(pTrav);
|
---|
670 | }
|
---|
671 |
|
---|
672 | /**
|
---|
673 | * Advances to the previous item.
|
---|
674 | *
|
---|
675 | * @returns The new current item. (NULL indicates the imaginary NULL item.)
|
---|
676 | * @param pTrav The traverser.
|
---|
677 | */
|
---|
678 | DECLINLINE(void *) RTTabTravPrev(PRTTABTRAVERSERCORE pTrav)
|
---|
679 | {
|
---|
680 | return pTrav->pTab->pOps->pfnTravPrev(pTrav);
|
---|
681 | }
|
---|
682 |
|
---|
683 | /**
|
---|
684 | * Replaces the current item.
|
---|
685 | *
|
---|
686 | * This has the same restrictions as RTTabProbe(), e.g. it's not permitted to
|
---|
687 | * break the order of the table.
|
---|
688 | *
|
---|
689 | * @returns The replaced item.
|
---|
690 | * @returns NULL if the current item is the NULL item. The traverser
|
---|
691 | * and table remains unchanged.
|
---|
692 | * @param pTrav The traverser.
|
---|
693 | * @param pvItem The item to be inserted.
|
---|
694 | */
|
---|
695 | DECLINLINE(void *) RTTabTravReplace(PRTTABTRAVERSERCORE pTrav, void *pvItem)
|
---|
696 | {
|
---|
697 | return pTrav->pTab->pOps->pfnTravReplace(pTrav, pvItem);
|
---|
698 | }
|
---|
699 |
|
---|
700 | __END_DECLS
|
---|
701 |
|
---|
702 | /** @} */
|
---|
703 |
|
---|
704 | #endif
|
---|