| 1 |
/* |
| 2 |
* libmowgli: A collection of useful routines for programming. |
| 3 |
* patricia.c: Dictionary-based information storage. |
| 4 |
* |
| 5 |
* Copyright (c) 2007 William Pitcock <nenolod -at- sacredspiral.co.uk> |
| 6 |
* Copyright (c) 2007-2010 Jilles Tjoelker <jilles -at- stack.nl> |
| 7 |
* |
| 8 |
* Redistribution and use in source and binary forms, with or without |
| 9 |
* modification, are permitted provided that the following conditions are |
| 10 |
* met: |
| 11 |
* |
| 12 |
* 1. Redistributions of source code must retain the above copyright notice, |
| 13 |
* this list of conditions and the following disclaimer. |
| 14 |
* |
| 15 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 16 |
* notice, this list of conditions and the following disclaimer in the |
| 17 |
* documentation and/or other materials provided with the distribution. |
| 18 |
* |
| 19 |
* 3. The name of the author may not be used to endorse or promote products |
| 20 |
* derived from this software without specific prior written permission. |
| 21 |
* |
| 22 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 23 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 24 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 25 |
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
| 26 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 27 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 28 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 29 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 30 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 31 |
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 32 |
* POSSIBILITY OF SUCH DAMAGE. |
| 33 |
*/ |
| 34 |
|
| 35 |
#include "mowgli.h" |
| 36 |
|
| 37 |
static mowgli_heap_t *leaf_heap = NULL; |
| 38 |
static mowgli_heap_t *node_heap = NULL; |
| 39 |
|
| 40 |
/* |
| 41 |
* Patricia tree. |
| 42 |
* |
| 43 |
* A radix trie that avoids one-way branching and redundant nodes. |
| 44 |
* |
| 45 |
* To find a node, the tree is traversed starting from the root. The |
| 46 |
* nibnum in each node indicates which nibble of the key needs to be |
| 47 |
* tested, and the appropriate branch is taken. |
| 48 |
* |
| 49 |
* The nibnum values are strictly increasing while going down the tree. |
| 50 |
* |
| 51 |
* -- jilles |
| 52 |
*/ |
| 53 |
|
| 54 |
union patricia_elem; |
| 55 |
|
| 56 |
struct mowgli_patricia_ |
| 57 |
{ |
| 58 |
void (*canonize_cb)(char *key); |
| 59 |
union patricia_elem *root; |
| 60 |
|
| 61 |
unsigned int count; |
| 62 |
char *id; |
| 63 |
}; |
| 64 |
|
| 65 |
#define POINTERS_PER_NODE 16 |
| 66 |
#define NIBBLE_VAL(key, nibnum) (((key)[(nibnum) / 2] >> ((nibnum) & 1 ? 0 : 4)) & 0xF) |
| 67 |
|
| 68 |
struct patricia_node |
| 69 |
{ |
| 70 |
/* nibble to test (nibble NUM%2 of byte NUM/2) */ |
| 71 |
int nibnum; |
| 72 |
|
| 73 |
/* branches of the tree */ |
| 74 |
union patricia_elem *down[POINTERS_PER_NODE]; |
| 75 |
|
| 76 |
union patricia_elem *parent; |
| 77 |
|
| 78 |
char parent_val; |
| 79 |
}; |
| 80 |
|
| 81 |
/* mowgli_patricia_elem_ is the name used in mowgli_patricia.h. |
| 82 |
* patricia_leaf is the name historically used here. */ |
| 83 |
#define patricia_leaf mowgli_patricia_elem_ |
| 84 |
|
| 85 |
struct patricia_leaf |
| 86 |
{ |
| 87 |
/* -1 to indicate this is a leaf, not a node */ |
| 88 |
int nibnum; |
| 89 |
|
| 90 |
/* data associated with the key */ |
| 91 |
void *data; |
| 92 |
|
| 93 |
/* key (canonized copy) */ |
| 94 |
char *key; |
| 95 |
union patricia_elem *parent; |
| 96 |
|
| 97 |
char parent_val; |
| 98 |
}; |
| 99 |
|
| 100 |
union patricia_elem |
| 101 |
{ |
| 102 |
int nibnum; |
| 103 |
struct patricia_node node; |
| 104 |
|
| 105 |
struct patricia_leaf leaf; |
| 106 |
}; |
| 107 |
|
| 108 |
#define IS_LEAF(elem) ((elem)->nibnum == -1) |
| 109 |
|
| 110 |
/* Preserve compatibility with the old mowgli_patricia.h */ |
| 111 |
#define STATE_CUR(state) ((state)->pspare[0]) |
| 112 |
#define STATE_NEXT(state) ((state)->pspare[1]) |
| 113 |
|
| 114 |
/* |
| 115 |
* first_leaf() |
| 116 |
* |
| 117 |
* Find the smallest leaf hanging off a subtree. |
| 118 |
* |
| 119 |
* Inputs: |
| 120 |
* - element (may be leaf or node) heading subtree |
| 121 |
* |
| 122 |
* Outputs: |
| 123 |
* - lowest leaf in subtree |
| 124 |
* |
| 125 |
* Side Effects: |
| 126 |
* - none |
| 127 |
*/ |
| 128 |
static union patricia_elem * |
| 129 |
first_leaf(union patricia_elem *delem) |
| 130 |
{ |
| 131 |
int val; |
| 132 |
|
| 133 |
while (!IS_LEAF(delem)) |
| 134 |
{ |
| 135 |
for (val = 0; val < POINTERS_PER_NODE; val++) |
| 136 |
if (delem->node.down[val] != NULL) |
| 137 |
{ |
| 138 |
delem = delem->node.down[val]; |
| 139 |
break; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
return delem; |
| 144 |
} |
| 145 |
|
| 146 |
/* |
| 147 |
* mowgli_patricia_create(void (*canonize_cb)(char *key)) |
| 148 |
* |
| 149 |
* Dictionary object factory. |
| 150 |
* |
| 151 |
* Inputs: |
| 152 |
* - function to use for canonizing keys (for example, use |
| 153 |
* a function that makes the string upper case to create |
| 154 |
* a patricia with case-insensitive matching) |
| 155 |
* |
| 156 |
* Outputs: |
| 157 |
* - on success, a new patricia object. |
| 158 |
* |
| 159 |
* Side Effects: |
| 160 |
* - if services runs out of memory and cannot allocate the object, |
| 161 |
* the program will abort. |
| 162 |
*/ |
| 163 |
mowgli_patricia_t * |
| 164 |
mowgli_patricia_create(void (*canonize_cb)(char *key)) |
| 165 |
{ |
| 166 |
return mowgli_patricia_create_named(NULL, canonize_cb); |
| 167 |
} |
| 168 |
|
| 169 |
/* |
| 170 |
* mowgli_patricia_create_named(const char *name, |
| 171 |
* void (*canonize_cb)(char *key)) |
| 172 |
* |
| 173 |
* Dictionary object factory. |
| 174 |
* |
| 175 |
* Inputs: |
| 176 |
* - patricia name |
| 177 |
* - function to use for canonizing keys (for example, use |
| 178 |
* a function that makes the string upper case to create |
| 179 |
* a patricia with case-insensitive matching) |
| 180 |
* |
| 181 |
* Outputs: |
| 182 |
* - on success, a new patricia object. |
| 183 |
* |
| 184 |
* Side Effects: |
| 185 |
* - if services runs out of memory and cannot allocate the object, |
| 186 |
* the program will abort. |
| 187 |
*/ |
| 188 |
mowgli_patricia_t * |
| 189 |
mowgli_patricia_create_named(const char *name, void (*canonize_cb)(char *key)) |
| 190 |
{ |
| 191 |
mowgli_patricia_t *dtree = mowgli_alloc(sizeof *dtree); |
| 192 |
|
| 193 |
dtree->canonize_cb = canonize_cb; |
| 194 |
|
| 195 |
if (name && *name) |
| 196 |
dtree->id = mowgli_strdup(name); |
| 197 |
|
| 198 |
if (!leaf_heap) |
| 199 |
leaf_heap = mowgli_heap_create(sizeof(struct patricia_leaf), 1024, BH_NOW); |
| 200 |
|
| 201 |
if (!node_heap) |
| 202 |
node_heap = mowgli_heap_create(sizeof(struct patricia_node), 128, BH_NOW); |
| 203 |
|
| 204 |
dtree->root = NULL; |
| 205 |
|
| 206 |
return dtree; |
| 207 |
} |
| 208 |
|
| 209 |
/* |
| 210 |
* mowgli_patricia_shutdown(void) |
| 211 |
* |
| 212 |
* Clean up after patricia to ensure all memory is released as soon as |
| 213 |
* possible (destroys both heaps). |
| 214 |
* |
| 215 |
* Inputs: |
| 216 |
* - nothing |
| 217 |
* |
| 218 |
* Outputs: |
| 219 |
* - nothing |
| 220 |
* |
| 221 |
* Side Effects: |
| 222 |
* - patricia's internal heaps are destroyed and deallocated |
| 223 |
*/ |
| 224 |
void |
| 225 |
mowgli_patricia_shutdown(void) |
| 226 |
{ |
| 227 |
if (leaf_heap) |
| 228 |
mowgli_heap_destroy(leaf_heap); |
| 229 |
|
| 230 |
if (node_heap) |
| 231 |
mowgli_heap_destroy(node_heap); |
| 232 |
|
| 233 |
return; |
| 234 |
} |
| 235 |
|
| 236 |
/* |
| 237 |
* mowgli_patricia_destroy(mowgli_patricia_t *dtree, |
| 238 |
* void (*destroy_cb)(const char *key, void *data, void *privdata), |
| 239 |
* void *privdata); |
| 240 |
* |
| 241 |
* Recursively destroys all nodes in a patricia tree. |
| 242 |
* |
| 243 |
* Inputs: |
| 244 |
* - patricia tree object |
| 245 |
* - optional iteration callback |
| 246 |
* - optional opaque/private data to pass to callback |
| 247 |
* |
| 248 |
* Outputs: |
| 249 |
* - nothing |
| 250 |
* |
| 251 |
* Side Effects: |
| 252 |
* - on success, a dtree and optionally it's children are destroyed. |
| 253 |
* |
| 254 |
* Notes: |
| 255 |
* - if this is called without a callback, the objects bound to the |
| 256 |
* DTree will not be destroyed. |
| 257 |
*/ |
| 258 |
void |
| 259 |
mowgli_patricia_destroy(mowgli_patricia_t *dtree, void (*destroy_cb)(const char *key, void *data, void *privdata), void *privdata) |
| 260 |
{ |
| 261 |
mowgli_patricia_iteration_state_t state; |
| 262 |
union patricia_elem *delem; |
| 263 |
|
| 264 |
void *entry; |
| 265 |
|
| 266 |
return_if_fail(dtree != NULL); |
| 267 |
|
| 268 |
MOWGLI_PATRICIA_FOREACH(entry, &state, dtree) |
| 269 |
{ |
| 270 |
delem = STATE_CUR(&state); |
| 271 |
|
| 272 |
if (destroy_cb != NULL) |
| 273 |
(*destroy_cb)(delem->leaf.key, delem->leaf.data, |
| 274 |
privdata); |
| 275 |
|
| 276 |
mowgli_patricia_delete(dtree, delem->leaf.key); |
| 277 |
} |
| 278 |
|
| 279 |
mowgli_free(dtree); |
| 280 |
} |
| 281 |
|
| 282 |
/* |
| 283 |
* mowgli_patricia_foreach(mowgli_patricia_t *dtree, |
| 284 |
* int (*foreach_cb)(const char *key, void *data, void *privdata), |
| 285 |
* void *privdata); |
| 286 |
* |
| 287 |
* Iterates over all entries in a DTree. |
| 288 |
* |
| 289 |
* Inputs: |
| 290 |
* - patricia tree object |
| 291 |
* - optional iteration callback |
| 292 |
* - optional opaque/private data to pass to callback |
| 293 |
* |
| 294 |
* Outputs: |
| 295 |
* - nothing |
| 296 |
* |
| 297 |
* Side Effects: |
| 298 |
* - on success, a dtree is iterated |
| 299 |
*/ |
| 300 |
void |
| 301 |
mowgli_patricia_foreach(mowgli_patricia_t *dtree, int (*foreach_cb)(const char *key, void *data, void *privdata), void *privdata) |
| 302 |
{ |
| 303 |
union patricia_elem *delem, *next; |
| 304 |
|
| 305 |
int val; |
| 306 |
|
| 307 |
return_if_fail(dtree != NULL); |
| 308 |
|
| 309 |
delem = dtree->root; |
| 310 |
|
| 311 |
if (delem == NULL) |
| 312 |
return; |
| 313 |
|
| 314 |
/* Only one element in the tree */ |
| 315 |
if (IS_LEAF(delem)) |
| 316 |
{ |
| 317 |
if (foreach_cb != NULL) |
| 318 |
(*foreach_cb)(delem->leaf.key, delem->leaf.data, privdata); |
| 319 |
|
| 320 |
return; |
| 321 |
} |
| 322 |
|
| 323 |
val = 0; |
| 324 |
|
| 325 |
do |
| 326 |
{ |
| 327 |
do |
| 328 |
next = delem->node.down[val++]; |
| 329 |
while (next == NULL && val < POINTERS_PER_NODE); |
| 330 |
|
| 331 |
if (next != NULL) |
| 332 |
{ |
| 333 |
if (IS_LEAF(next)) |
| 334 |
{ |
| 335 |
if (foreach_cb != NULL) |
| 336 |
(*foreach_cb)(next->leaf.key, next->leaf.data, privdata); |
| 337 |
} |
| 338 |
else |
| 339 |
{ |
| 340 |
delem = next; |
| 341 |
val = 0; |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
while (val >= POINTERS_PER_NODE) |
| 346 |
{ |
| 347 |
val = delem->node.parent_val; |
| 348 |
delem = delem->node.parent; |
| 349 |
|
| 350 |
if (delem == NULL) |
| 351 |
break; |
| 352 |
|
| 353 |
val++; |
| 354 |
} |
| 355 |
} while (delem != NULL); |
| 356 |
} |
| 357 |
|
| 358 |
/* |
| 359 |
* mowgli_patricia_search(mowgli_patricia_t *dtree, |
| 360 |
* void *(*foreach_cb)(const char *key, void *data, void *privdata), |
| 361 |
* void *privdata); |
| 362 |
* |
| 363 |
* Searches all entries in a DTree using a custom callback. |
| 364 |
* |
| 365 |
* Inputs: |
| 366 |
* - patricia tree object |
| 367 |
* - optional iteration callback |
| 368 |
* - optional opaque/private data to pass to callback |
| 369 |
* |
| 370 |
* Outputs: |
| 371 |
* - on success, the requested object |
| 372 |
* - on failure, NULL. |
| 373 |
* |
| 374 |
* Side Effects: |
| 375 |
* - a dtree is iterated until the requested conditions are met |
| 376 |
*/ |
| 377 |
void * |
| 378 |
mowgli_patricia_search(mowgli_patricia_t *dtree, void *(*foreach_cb)(const char *key, void *data, void *privdata), void *privdata) |
| 379 |
{ |
| 380 |
union patricia_elem *delem, *next; |
| 381 |
|
| 382 |
int val; |
| 383 |
void *ret = NULL; |
| 384 |
|
| 385 |
return_val_if_fail(dtree != NULL, NULL); |
| 386 |
|
| 387 |
delem = dtree->root; |
| 388 |
|
| 389 |
if (delem == NULL) |
| 390 |
return NULL; |
| 391 |
|
| 392 |
/* Only one element in the tree */ |
| 393 |
if (IS_LEAF(delem)) |
| 394 |
{ |
| 395 |
if (foreach_cb != NULL) |
| 396 |
return (*foreach_cb)(delem->leaf.key, delem->leaf.data, privdata); |
| 397 |
|
| 398 |
return NULL; |
| 399 |
} |
| 400 |
|
| 401 |
val = 0; |
| 402 |
|
| 403 |
for (;;) |
| 404 |
{ |
| 405 |
do |
| 406 |
next = delem->node.down[val++]; |
| 407 |
while (next == NULL && val < POINTERS_PER_NODE); |
| 408 |
|
| 409 |
if (next != NULL) |
| 410 |
{ |
| 411 |
if (IS_LEAF(next)) |
| 412 |
{ |
| 413 |
if (foreach_cb != NULL) |
| 414 |
ret = (*foreach_cb)(next->leaf.key, next->leaf.data, privdata); |
| 415 |
|
| 416 |
if (ret != NULL) |
| 417 |
break; |
| 418 |
} |
| 419 |
else |
| 420 |
{ |
| 421 |
delem = next; |
| 422 |
val = 0; |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
while (val >= POINTERS_PER_NODE) |
| 427 |
{ |
| 428 |
val = delem->node.parent_val; |
| 429 |
delem = delem->node.parent; |
| 430 |
|
| 431 |
if (delem == NULL) |
| 432 |
break; |
| 433 |
|
| 434 |
val++; |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
return ret; |
| 439 |
} |
| 440 |
|
| 441 |
/* |
| 442 |
* mowgli_patricia_foreach_start(mowgli_patricia_t *dtree, |
| 443 |
* mowgli_patricia_iteration_state_t *state); |
| 444 |
* |
| 445 |
* Initializes a static DTree iterator. |
| 446 |
* |
| 447 |
* Inputs: |
| 448 |
* - patricia tree object |
| 449 |
* - static DTree iterator |
| 450 |
* |
| 451 |
* Outputs: |
| 452 |
* - nothing |
| 453 |
* |
| 454 |
* Side Effects: |
| 455 |
* - the static iterator, &state, is initialized. |
| 456 |
*/ |
| 457 |
void |
| 458 |
mowgli_patricia_foreach_start(mowgli_patricia_t *dtree, mowgli_patricia_iteration_state_t *state) |
| 459 |
{ |
| 460 |
if (dtree == NULL) |
| 461 |
return; |
| 462 |
|
| 463 |
return_if_fail(state != NULL); |
| 464 |
|
| 465 |
if (dtree->root != NULL) |
| 466 |
STATE_NEXT(state) = first_leaf(dtree->root); |
| 467 |
else |
| 468 |
STATE_NEXT(state) = NULL; |
| 469 |
|
| 470 |
STATE_CUR(state) = STATE_NEXT(state); |
| 471 |
|
| 472 |
if (STATE_NEXT(state) == NULL) |
| 473 |
return; |
| 474 |
|
| 475 |
/* make STATE_CUR point to first item and STATE_NEXT point to |
| 476 |
* second item */ |
| 477 |
mowgli_patricia_foreach_next(dtree, state); |
| 478 |
} |
| 479 |
|
| 480 |
/* |
| 481 |
* mowgli_patricia_foreach_cur(mowgli_patricia_t *dtree, |
| 482 |
* mowgli_patricia_iteration_state_t *state); |
| 483 |
* |
| 484 |
* Returns the data from the current node being iterated by the |
| 485 |
* static iterator. |
| 486 |
* |
| 487 |
* Inputs: |
| 488 |
* - patricia tree object |
| 489 |
* - static DTree iterator |
| 490 |
* |
| 491 |
* Outputs: |
| 492 |
* - reference to data in the current dtree node being iterated |
| 493 |
* |
| 494 |
* Side Effects: |
| 495 |
* - none |
| 496 |
*/ |
| 497 |
void * |
| 498 |
mowgli_patricia_foreach_cur(mowgli_patricia_t *dtree, mowgli_patricia_iteration_state_t *state) |
| 499 |
{ |
| 500 |
if (dtree == NULL) |
| 501 |
return NULL; |
| 502 |
|
| 503 |
return_val_if_fail(state != NULL, NULL); |
| 504 |
|
| 505 |
return STATE_CUR(state) != NULL ? |
| 506 |
((struct patricia_leaf *) STATE_CUR(state))->data : NULL; |
| 507 |
} |
| 508 |
|
| 509 |
/* |
| 510 |
* mowgli_patricia_foreach_next(mowgli_patricia_t *dtree, |
| 511 |
* mowgli_patricia_iteration_state_t *state); |
| 512 |
* |
| 513 |
* Advances a static DTree iterator. |
| 514 |
* |
| 515 |
* Inputs: |
| 516 |
* - patricia tree object |
| 517 |
* - static DTree iterator |
| 518 |
* |
| 519 |
* Outputs: |
| 520 |
* - nothing |
| 521 |
* |
| 522 |
* Side Effects: |
| 523 |
* - the static iterator, &state, is advanced to a new DTree node. |
| 524 |
*/ |
| 525 |
void |
| 526 |
mowgli_patricia_foreach_next(mowgli_patricia_t *dtree, mowgli_patricia_iteration_state_t *state) |
| 527 |
{ |
| 528 |
struct patricia_leaf *leaf; |
| 529 |
|
| 530 |
union patricia_elem *delem, *next; |
| 531 |
|
| 532 |
int val; |
| 533 |
|
| 534 |
if (dtree == NULL) |
| 535 |
return; |
| 536 |
|
| 537 |
return_if_fail(state != NULL); |
| 538 |
|
| 539 |
if (STATE_CUR(state) == NULL) |
| 540 |
{ |
| 541 |
mowgli_log("mowgli_patricia_foreach_next(): called again after iteration finished on dtree<%p>", (void *) dtree); |
| 542 |
return; |
| 543 |
} |
| 544 |
|
| 545 |
STATE_CUR(state) = STATE_NEXT(state); |
| 546 |
|
| 547 |
if (STATE_NEXT(state) == NULL) |
| 548 |
return; |
| 549 |
|
| 550 |
leaf = STATE_NEXT(state); |
| 551 |
delem = leaf->parent; |
| 552 |
val = leaf->parent_val; |
| 553 |
|
| 554 |
while (delem != NULL) |
| 555 |
{ |
| 556 |
do |
| 557 |
next = delem->node.down[val++]; |
| 558 |
while (next == NULL && val < POINTERS_PER_NODE); |
| 559 |
|
| 560 |
if (next != NULL) |
| 561 |
{ |
| 562 |
if (IS_LEAF(next)) |
| 563 |
{ |
| 564 |
/* We will find the original leaf first. */ |
| 565 |
if (&next->leaf != leaf) |
| 566 |
{ |
| 567 |
if (strcmp(next->leaf.key, leaf->key) < 0) |
| 568 |
{ |
| 569 |
mowgli_log("mowgli_patricia_foreach_next(): iteration went backwards (libmowgli bug) on dtree<%p>", (void *) dtree); |
| 570 |
STATE_NEXT(state) = NULL; |
| 571 |
return; |
| 572 |
} |
| 573 |
|
| 574 |
STATE_NEXT(state) = next; |
| 575 |
return; |
| 576 |
} |
| 577 |
} |
| 578 |
else |
| 579 |
{ |
| 580 |
delem = next; |
| 581 |
val = 0; |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
while (val >= POINTERS_PER_NODE) |
| 586 |
{ |
| 587 |
val = delem->node.parent_val; |
| 588 |
delem = delem->node.parent; |
| 589 |
|
| 590 |
if (delem == NULL) |
| 591 |
break; |
| 592 |
|
| 593 |
val++; |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
STATE_NEXT(state) = NULL; |
| 598 |
} |
| 599 |
|
| 600 |
/* |
| 601 |
* mowgli_patricia_elem_find(mowgli_patricia_t *dtree, const char *key) |
| 602 |
* |
| 603 |
* Looks up a DTree node by name. |
| 604 |
* |
| 605 |
* Inputs: |
| 606 |
* - patricia tree object |
| 607 |
* - name of node to lookup |
| 608 |
* |
| 609 |
* Outputs: |
| 610 |
* - on success, the dtree node requested |
| 611 |
* - on failure, NULL |
| 612 |
* |
| 613 |
* Side Effects: |
| 614 |
* - none |
| 615 |
*/ |
| 616 |
struct patricia_leaf * |
| 617 |
mowgli_patricia_elem_find(mowgli_patricia_t *dict, const char *key) |
| 618 |
{ |
| 619 |
char ckey_store[256]; |
| 620 |
|
| 621 |
char *ckey_buf = NULL; |
| 622 |
const char *ckey; |
| 623 |
union patricia_elem *delem; |
| 624 |
|
| 625 |
int val, keylen; |
| 626 |
|
| 627 |
return_val_if_fail(dict != NULL, NULL); |
| 628 |
return_val_if_fail(key != NULL, NULL); |
| 629 |
|
| 630 |
keylen = strlen(key); |
| 631 |
|
| 632 |
if (dict->canonize_cb == NULL) |
| 633 |
{ |
| 634 |
ckey = key; |
| 635 |
} |
| 636 |
else |
| 637 |
{ |
| 638 |
if (keylen >= (int) sizeof(ckey_store)) |
| 639 |
{ |
| 640 |
ckey_buf = mowgli_strdup(key); |
| 641 |
dict->canonize_cb(ckey_buf); |
| 642 |
ckey = ckey_buf; |
| 643 |
} |
| 644 |
else |
| 645 |
{ |
| 646 |
mowgli_strlcpy(ckey_store, key, sizeof ckey_store); |
| 647 |
dict->canonize_cb(ckey_store); |
| 648 |
ckey = ckey_store; |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
delem = dict->root; |
| 653 |
|
| 654 |
while (delem != NULL && !IS_LEAF(delem)) |
| 655 |
{ |
| 656 |
if (delem->nibnum / 2 < keylen) |
| 657 |
val = NIBBLE_VAL(ckey, delem->nibnum); |
| 658 |
else |
| 659 |
val = 0; |
| 660 |
|
| 661 |
delem = delem->node.down[val]; |
| 662 |
} |
| 663 |
|
| 664 |
/* Now, if the key is in the tree, delem contains it. */ |
| 665 |
if ((delem != NULL) && strcmp(delem->leaf.key, ckey)) |
| 666 |
delem = NULL; |
| 667 |
|
| 668 |
if (ckey_buf != NULL) |
| 669 |
mowgli_free(ckey_buf); |
| 670 |
|
| 671 |
if (delem == NULL) |
| 672 |
return NULL; |
| 673 |
|
| 674 |
return &delem->leaf; |
| 675 |
} |
| 676 |
|
| 677 |
/* |
| 678 |
* mowgli_patricia_add(mowgli_patricia_t *dtree, const char *key, void *data) |
| 679 |
* |
| 680 |
* Creates a new DTree node and binds data to it. |
| 681 |
* |
| 682 |
* Inputs: |
| 683 |
* - patricia tree object |
| 684 |
* - name for new DTree node |
| 685 |
* - data to bind to the new DTree node |
| 686 |
* |
| 687 |
* Outputs: |
| 688 |
* - on success, TRUE |
| 689 |
* - on failure, FALSE |
| 690 |
* |
| 691 |
* Side Effects: |
| 692 |
* - data is inserted into the DTree. |
| 693 |
*/ |
| 694 |
struct patricia_leaf * |
| 695 |
mowgli_patricia_elem_add(mowgli_patricia_t *dict, const char *key, void *data) |
| 696 |
{ |
| 697 |
char *ckey; |
| 698 |
|
| 699 |
union patricia_elem *delem, *prev, *newnode; |
| 700 |
|
| 701 |
union patricia_elem **place1; |
| 702 |
|
| 703 |
int val, keylen; |
| 704 |
int i, j; |
| 705 |
|
| 706 |
return_val_if_fail(dict != NULL, NULL); |
| 707 |
return_val_if_fail(key != NULL, NULL); |
| 708 |
return_val_if_fail(data != NULL, NULL); |
| 709 |
|
| 710 |
keylen = strlen(key); |
| 711 |
ckey = mowgli_strdup(key); |
| 712 |
|
| 713 |
if (ckey == NULL) |
| 714 |
{ |
| 715 |
mowgli_log("major WTF: ckey is NULL, not adding node."); |
| 716 |
return NULL; |
| 717 |
} |
| 718 |
|
| 719 |
if (dict->canonize_cb != NULL) |
| 720 |
dict->canonize_cb(ckey); |
| 721 |
|
| 722 |
prev = NULL; |
| 723 |
val = POINTERS_PER_NODE + 2; /* trap value */ |
| 724 |
delem = dict->root; |
| 725 |
|
| 726 |
while (delem != NULL && !IS_LEAF(delem)) |
| 727 |
{ |
| 728 |
prev = delem; |
| 729 |
|
| 730 |
if (delem->nibnum / 2 < keylen) |
| 731 |
val = NIBBLE_VAL(ckey, delem->nibnum); |
| 732 |
else |
| 733 |
val = 0; |
| 734 |
|
| 735 |
delem = delem->node.down[val]; |
| 736 |
} |
| 737 |
|
| 738 |
/* Now, if the key is in the tree, delem contains it. */ |
| 739 |
if ((delem != NULL) && !strcmp(delem->leaf.key, ckey)) |
| 740 |
{ |
| 741 |
mowgli_log("Key is already in dict, ignoring duplicate"); |
| 742 |
mowgli_free(ckey); |
| 743 |
return NULL; |
| 744 |
} |
| 745 |
|
| 746 |
if ((delem == NULL) && (prev != NULL)) |
| 747 |
/* Get a leaf to compare with. */ |
| 748 |
delem = first_leaf(prev); |
| 749 |
|
| 750 |
if (delem == NULL) |
| 751 |
{ |
| 752 |
soft_assert(prev == NULL); |
| 753 |
soft_assert(dict->count == 0); |
| 754 |
place1 = &dict->root; |
| 755 |
*place1 = mowgli_heap_alloc(leaf_heap); |
| 756 |
return_val_if_fail(*place1 != NULL, NULL); |
| 757 |
(*place1)->nibnum = -1; |
| 758 |
(*place1)->leaf.data = data; |
| 759 |
(*place1)->leaf.key = ckey; |
| 760 |
(*place1)->leaf.parent = prev; |
| 761 |
(*place1)->leaf.parent_val = val; |
| 762 |
dict->count++; |
| 763 |
return &(*place1)->leaf; |
| 764 |
} |
| 765 |
|
| 766 |
/* Find the first nibble where they differ. */ |
| 767 |
for (i = 0; NIBBLE_VAL(ckey, i) == NIBBLE_VAL(delem->leaf.key, i); i++) |
| 768 |
; |
| 769 |
|
| 770 |
/* Find where to insert the new node. */ |
| 771 |
while (prev != NULL && prev->nibnum > i) |
| 772 |
{ |
| 773 |
val = prev->node.parent_val; |
| 774 |
prev = prev->node.parent; |
| 775 |
} |
| 776 |
|
| 777 |
if ((prev == NULL) || (prev->nibnum < i)) |
| 778 |
{ |
| 779 |
/* Insert new node below prev */ |
| 780 |
newnode = mowgli_heap_alloc(node_heap); |
| 781 |
return_val_if_fail(newnode != NULL, NULL); |
| 782 |
newnode->nibnum = i; |
| 783 |
newnode->node.parent = prev; |
| 784 |
newnode->node.parent_val = val; |
| 785 |
|
| 786 |
for (j = 0; j < POINTERS_PER_NODE; j++) |
| 787 |
newnode->node.down[j] = NULL; |
| 788 |
|
| 789 |
if (prev == NULL) |
| 790 |
{ |
| 791 |
newnode->node.down[NIBBLE_VAL(delem->leaf.key, i)] = dict->root; |
| 792 |
|
| 793 |
if (IS_LEAF(dict->root)) |
| 794 |
{ |
| 795 |
dict->root->leaf.parent = newnode; |
| 796 |
dict->root->leaf.parent_val = NIBBLE_VAL(delem->leaf.key, i); |
| 797 |
} |
| 798 |
else |
| 799 |
{ |
| 800 |
soft_assert(dict->root->nibnum > i); |
| 801 |
dict->root->node.parent = newnode; |
| 802 |
dict->root->node.parent_val = NIBBLE_VAL(delem->leaf.key, i); |
| 803 |
} |
| 804 |
|
| 805 |
dict->root = newnode; |
| 806 |
} |
| 807 |
else |
| 808 |
{ |
| 809 |
newnode->node.down[NIBBLE_VAL(delem->leaf.key, i)] = prev->node.down[val]; |
| 810 |
|
| 811 |
if (IS_LEAF(prev->node.down[val])) |
| 812 |
{ |
| 813 |
prev->node.down[val]->leaf.parent = newnode; |
| 814 |
prev->node.down[val]->leaf.parent_val = NIBBLE_VAL(delem->leaf.key, i); |
| 815 |
} |
| 816 |
else |
| 817 |
{ |
| 818 |
prev->node.down[val]->node.parent = newnode; |
| 819 |
prev->node.down[val]->node.parent_val = NIBBLE_VAL(delem->leaf.key, i); |
| 820 |
} |
| 821 |
|
| 822 |
prev->node.down[val] = newnode; |
| 823 |
} |
| 824 |
} |
| 825 |
else |
| 826 |
{ |
| 827 |
/* This nibble is already checked. */ |
| 828 |
soft_assert(prev->nibnum == i); |
| 829 |
newnode = prev; |
| 830 |
} |
| 831 |
|
| 832 |
val = NIBBLE_VAL(ckey, i); |
| 833 |
place1 = &newnode->node.down[val]; |
| 834 |
soft_assert(*place1 == NULL); |
| 835 |
*place1 = mowgli_heap_alloc(leaf_heap); |
| 836 |
return_val_if_fail(*place1 != NULL, NULL); |
| 837 |
(*place1)->nibnum = -1; |
| 838 |
(*place1)->leaf.data = data; |
| 839 |
(*place1)->leaf.key = ckey; |
| 840 |
(*place1)->leaf.parent = newnode; |
| 841 |
(*place1)->leaf.parent_val = val; |
| 842 |
dict->count++; |
| 843 |
return &(*place1)->leaf; |
| 844 |
} |
| 845 |
|
| 846 |
mowgli_boolean_t |
| 847 |
mowgli_patricia_add(mowgli_patricia_t *dict, const char *key, void *data) |
| 848 |
{ |
| 849 |
return (mowgli_patricia_elem_add(dict, key, data) != NULL) ? TRUE : FALSE; |
| 850 |
} |
| 851 |
|
| 852 |
/* |
| 853 |
* mowgli_patricia_delete(mowgli_patricia_t *dtree, const char *key) |
| 854 |
* |
| 855 |
* Deletes data from a patricia tree. |
| 856 |
* |
| 857 |
* Inputs: |
| 858 |
* - patricia tree object |
| 859 |
* - name of DTree node to delete |
| 860 |
* |
| 861 |
* Outputs: |
| 862 |
* - on success, the remaining data that needs to be mowgli_freed |
| 863 |
* - on failure, NULL |
| 864 |
* |
| 865 |
* Side Effects: |
| 866 |
* - data is removed from the DTree. |
| 867 |
* |
| 868 |
* Notes: |
| 869 |
* - the returned data needs to be mowgli_freed/released manually! |
| 870 |
*/ |
| 871 |
void * |
| 872 |
mowgli_patricia_delete(mowgli_patricia_t *dict, const char *key) |
| 873 |
{ |
| 874 |
void *data; |
| 875 |
struct patricia_leaf *leaf; |
| 876 |
|
| 877 |
leaf = mowgli_patricia_elem_find(dict, key); |
| 878 |
|
| 879 |
if (leaf == NULL) |
| 880 |
return NULL; |
| 881 |
|
| 882 |
data = leaf->data; |
| 883 |
mowgli_patricia_elem_delete(dict, leaf); |
| 884 |
return data; |
| 885 |
} |
| 886 |
|
| 887 |
void |
| 888 |
mowgli_patricia_elem_delete(mowgli_patricia_t *dict, struct patricia_leaf *leaf) |
| 889 |
{ |
| 890 |
union patricia_elem *delem, *prev, *next; |
| 891 |
|
| 892 |
int val, i, used; |
| 893 |
|
| 894 |
return_if_fail(dict != NULL); |
| 895 |
return_if_fail(leaf != NULL); |
| 896 |
|
| 897 |
delem = (union patricia_elem *) leaf; |
| 898 |
|
| 899 |
val = delem->leaf.parent_val; |
| 900 |
prev = delem->leaf.parent; |
| 901 |
|
| 902 |
mowgli_free(delem->leaf.key); |
| 903 |
mowgli_heap_free(leaf_heap, delem); |
| 904 |
|
| 905 |
if (prev != NULL) |
| 906 |
{ |
| 907 |
prev->node.down[val] = NULL; |
| 908 |
|
| 909 |
/* Leaf is gone, now consider the node it was in. */ |
| 910 |
delem = prev; |
| 911 |
|
| 912 |
used = -1; |
| 913 |
|
| 914 |
for (i = 0; i < POINTERS_PER_NODE; i++) |
| 915 |
if (delem->node.down[i] != NULL) |
| 916 |
used = used == -1 ? i : -2; |
| 917 |
|
| 918 |
soft_assert(used == -2 || used >= 0); |
| 919 |
|
| 920 |
if (used >= 0) |
| 921 |
{ |
| 922 |
/* Only one pointer in this node, remove it. |
| 923 |
* Replace the pointer that pointed to it by |
| 924 |
* the sole pointer in it. |
| 925 |
*/ |
| 926 |
next = delem->node.down[used]; |
| 927 |
val = delem->node.parent_val; |
| 928 |
prev = delem->node.parent; |
| 929 |
|
| 930 |
if (prev != NULL) |
| 931 |
prev->node.down[val] = next; |
| 932 |
else |
| 933 |
dict->root = next; |
| 934 |
|
| 935 |
if (IS_LEAF(next)) |
| 936 |
{ |
| 937 |
next->leaf.parent = prev; |
| 938 |
next->leaf.parent_val = val; |
| 939 |
} |
| 940 |
else |
| 941 |
{ |
| 942 |
next->node.parent = prev; |
| 943 |
next->node.parent_val = val; |
| 944 |
} |
| 945 |
|
| 946 |
mowgli_heap_free(node_heap, delem); |
| 947 |
} |
| 948 |
} |
| 949 |
else |
| 950 |
{ |
| 951 |
/* This was the last leaf. */ |
| 952 |
dict->root = NULL; |
| 953 |
} |
| 954 |
|
| 955 |
dict->count--; |
| 956 |
|
| 957 |
if (dict->count == 0) |
| 958 |
{ |
| 959 |
soft_assert(dict->root == NULL); |
| 960 |
dict->root = NULL; |
| 961 |
} |
| 962 |
} |
| 963 |
|
| 964 |
/* |
| 965 |
* mowgli_patricia_retrieve(mowgli_patricia_t *dtree, const char *key) |
| 966 |
* |
| 967 |
* Retrieves data from a patricia. |
| 968 |
* |
| 969 |
* Inputs: |
| 970 |
* - patricia tree object |
| 971 |
* - name of node to lookup |
| 972 |
* |
| 973 |
* Outputs: |
| 974 |
* - on success, the data bound to the DTree node. |
| 975 |
* - on failure, NULL |
| 976 |
* |
| 977 |
* Side Effects: |
| 978 |
* - none |
| 979 |
*/ |
| 980 |
void * |
| 981 |
mowgli_patricia_retrieve(mowgli_patricia_t *dtree, const char *key) |
| 982 |
{ |
| 983 |
struct patricia_leaf *delem = mowgli_patricia_elem_find(dtree, key); |
| 984 |
|
| 985 |
if (delem != NULL) |
| 986 |
return delem->data; |
| 987 |
|
| 988 |
return NULL; |
| 989 |
} |
| 990 |
|
| 991 |
const char * |
| 992 |
mowgli_patricia_elem_get_key(struct patricia_leaf *leaf) |
| 993 |
{ |
| 994 |
return_val_if_fail(leaf != NULL, NULL); |
| 995 |
|
| 996 |
return leaf->key; |
| 997 |
} |
| 998 |
|
| 999 |
void |
| 1000 |
mowgli_patricia_elem_set_data(struct patricia_leaf *leaf, void *data) |
| 1001 |
{ |
| 1002 |
return_if_fail(leaf != NULL); |
| 1003 |
|
| 1004 |
leaf->data = data; |
| 1005 |
} |
| 1006 |
|
| 1007 |
void * |
| 1008 |
mowgli_patricia_elem_get_data(struct patricia_leaf *leaf) |
| 1009 |
{ |
| 1010 |
return_val_if_fail(leaf != NULL, NULL); |
| 1011 |
|
| 1012 |
return leaf->data; |
| 1013 |
} |
| 1014 |
|
| 1015 |
/* |
| 1016 |
* mowgli_patricia_size(mowgli_patricia_t *dict) |
| 1017 |
* |
| 1018 |
* Returns the size of a patricia. |
| 1019 |
* |
| 1020 |
* Inputs: |
| 1021 |
* - patricia tree object |
| 1022 |
* |
| 1023 |
* Outputs: |
| 1024 |
* - size of patricia |
| 1025 |
* |
| 1026 |
* Side Effects: |
| 1027 |
* - none |
| 1028 |
*/ |
| 1029 |
unsigned int |
| 1030 |
mowgli_patricia_size(mowgli_patricia_t *dict) |
| 1031 |
{ |
| 1032 |
return_val_if_fail(dict != NULL, 0); |
| 1033 |
|
| 1034 |
return dict->count; |
| 1035 |
} |
| 1036 |
|
| 1037 |
/* returns the sum of the depths of the subtree rooted in delem at depth depth */ |
| 1038 |
/* there is no need for this to be recursive, but it is easier... */ |
| 1039 |
static int |
| 1040 |
stats_recurse(union patricia_elem *delem, unsigned int depth, unsigned int *pmaxdepth) |
| 1041 |
{ |
| 1042 |
unsigned int result = 0; |
| 1043 |
int val; |
| 1044 |
union patricia_elem *next; |
| 1045 |
|
| 1046 |
if (depth > *pmaxdepth) |
| 1047 |
*pmaxdepth = depth; |
| 1048 |
|
| 1049 |
if (depth == 0) |
| 1050 |
{ |
| 1051 |
if (IS_LEAF(delem)) |
| 1052 |
soft_assert(delem->leaf.parent == NULL); |
| 1053 |
|
| 1054 |
else |
| 1055 |
soft_assert(delem->node.parent == NULL); |
| 1056 |
} |
| 1057 |
|
| 1058 |
if (IS_LEAF(delem)) |
| 1059 |
return depth; |
| 1060 |
|
| 1061 |
for (val = 0; val < POINTERS_PER_NODE; val++) |
| 1062 |
{ |
| 1063 |
next = delem->node.down[val]; |
| 1064 |
|
| 1065 |
if (next == NULL) |
| 1066 |
continue; |
| 1067 |
|
| 1068 |
result += stats_recurse(next, depth + 1, pmaxdepth); |
| 1069 |
|
| 1070 |
if (IS_LEAF(next)) |
| 1071 |
{ |
| 1072 |
soft_assert(next->leaf.parent == delem); |
| 1073 |
soft_assert(next->leaf.parent_val == val); |
| 1074 |
} |
| 1075 |
else |
| 1076 |
{ |
| 1077 |
soft_assert(next->node.parent == delem); |
| 1078 |
soft_assert(next->node.parent_val == val); |
| 1079 |
soft_assert(next->node.nibnum > delem->node.nibnum); |
| 1080 |
} |
| 1081 |
} |
| 1082 |
|
| 1083 |
return result; |
| 1084 |
} |
| 1085 |
|
| 1086 |
/* |
| 1087 |
* mowgli_patricia_stats(mowgli_patricia_t *dict, void (*cb)(const char *line, void *privdata), void *privdata) |
| 1088 |
* |
| 1089 |
* Returns the size of a patricia. |
| 1090 |
* |
| 1091 |
* Inputs: |
| 1092 |
* - patricia tree object |
| 1093 |
* - callback |
| 1094 |
* - data for callback |
| 1095 |
* |
| 1096 |
* Outputs: |
| 1097 |
* - none |
| 1098 |
* |
| 1099 |
* Side Effects: |
| 1100 |
* - callback called with stats text |
| 1101 |
*/ |
| 1102 |
void |
| 1103 |
mowgli_patricia_stats(mowgli_patricia_t *dict, void (*cb)(const char *line, void *privdata), void *privdata) |
| 1104 |
{ |
| 1105 |
char str[256]; |
| 1106 |
unsigned int sum, maxdepth; |
| 1107 |
|
| 1108 |
return_if_fail(dict != NULL); |
| 1109 |
|
| 1110 |
if (dict->id != NULL) |
| 1111 |
snprintf(str, sizeof str, "Dictionary stats for %s (%u)", |
| 1112 |
dict->id, dict->count); |
| 1113 |
else |
| 1114 |
snprintf(str, sizeof str, "Dictionary stats for <%p> (%u)", |
| 1115 |
(void *) dict, dict->count); |
| 1116 |
|
| 1117 |
cb(str, privdata); |
| 1118 |
maxdepth = 0; |
| 1119 |
|
| 1120 |
if (dict->count > 0) |
| 1121 |
{ |
| 1122 |
sum = stats_recurse(dict->root, 0, &maxdepth); |
| 1123 |
snprintf(str, sizeof str, "Depth sum %u Avg depth %u Max depth %u", sum, sum / dict->count, maxdepth); |
| 1124 |
} |
| 1125 |
else |
| 1126 |
{ |
| 1127 |
snprintf(str, sizeof str, "Depth sum 0 Avg depth 0 Max depth 0"); |
| 1128 |
} |
| 1129 |
|
| 1130 |
cb(str, privdata); |
| 1131 |
return; |
| 1132 |
} |