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