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