2 * charybdis: an advanced ircd.
3 * irc_radixtree.c: Dictionary-based information storage.
5 * Copyright (c) 2007-2016 William Pitcock <nenolod -at- dereferenced.org>
6 * Copyright (c) 2007-2016 Jilles Tjoelker <jilles -at- stack.nl>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * 1. Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
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.
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.
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.
38 #include "irc_radixtree.h"
40 rb_dlink_list radixtree_list
= {NULL
, NULL
, 0};
45 * A radix trie that avoids one-way branching and redundant nodes.
47 * To find a node, the tree is traversed starting from the root. The
48 * nibnum in each node indicates which nibble of the key needs to be
49 * tested, and the appropriate branch is taken.
51 * The nibnum values are strictly increasing while going down the tree.
56 union irc_radixtree_elem
;
60 void (*canonize_cb
)(char *key
);
61 union irc_radixtree_elem
*root
;
69 #define POINTERS_PER_NODE 16
70 #define NIBBLE_VAL(key, nibnum) (((key)[(nibnum) / 2] >> ((nibnum) & 1 ? 0 : 4)) & 0xF)
72 struct irc_radixtree_node
74 /* nibble to test (nibble NUM%2 of byte NUM/2) */
77 /* branches of the tree */
78 union irc_radixtree_elem
*down
[POINTERS_PER_NODE
];
79 union irc_radixtree_elem
*parent
;
84 struct irc_radixtree_leaf
86 /* -1 to indicate this is a leaf, not a node */
89 /* data associated with the key */
92 /* key (canonized copy) */
94 union irc_radixtree_elem
*parent
;
99 union irc_radixtree_elem
102 struct irc_radixtree_node node
;
104 struct irc_radixtree_leaf leaf
;
107 #define IS_LEAF(elem) ((elem)->nibnum == -1)
109 /* Preserve compatibility with the old mowgli_patricia.h */
110 #define STATE_CUR(state) ((state)->pspare[0])
111 #define STATE_NEXT(state) ((state)->pspare[1])
116 * Find the smallest leaf hanging off a subtree.
119 * - element (may be leaf or node) heading subtree
122 * - lowest leaf in subtree
127 static union irc_radixtree_elem
*
128 first_leaf(union irc_radixtree_elem
*delem
)
132 while (!IS_LEAF(delem
))
134 for (val
= 0; val
< POINTERS_PER_NODE
; val
++)
135 if (delem
->node
.down
[val
] != NULL
)
137 delem
= delem
->node
.down
[val
];
146 * irc_radixtree_create_named(const char *name,
147 * void (*canonize_cb)(char *key))
149 * Dictionary object factory.
153 * - function to use for canonizing keys (for example, use
154 * a function that makes the string upper case to create
155 * a patricia with case-insensitive matching)
158 * - on success, a new patricia object.
161 * - if services runs out of memory and cannot allocate the object,
162 * the program will abort.
164 struct irc_radixtree
*
165 irc_radixtree_create(const char *name
, void (*canonize_cb
)(char *key
))
167 struct irc_radixtree
*dtree
= (struct irc_radixtree
*) rb_malloc(sizeof(struct irc_radixtree
));
169 dtree
->canonize_cb
= canonize_cb
;
170 dtree
->id
= rb_strdup(name
);
173 rb_dlinkAdd(dtree
, &dtree
->node
, &radixtree_list
);
179 * irc_radixtree_destroy(struct irc_radixtree *dtree,
180 * void (*destroy_cb)(const char *key, void *data, void *privdata),
183 * Recursively destroys all nodes in a patricia tree.
186 * - patricia tree object
187 * - optional iteration callback
188 * - optional opaque/private data to pass to callback
194 * - on success, a dtree and optionally it's children are destroyed.
197 * - if this is called without a callback, the objects bound to the
198 * DTree will not be destroyed.
201 irc_radixtree_destroy(struct irc_radixtree
*dtree
, void (*destroy_cb
)(const char *key
, void *data
, void *privdata
), void *privdata
)
203 struct irc_radixtree_iteration_state state
;
204 union irc_radixtree_elem
*delem
;
208 s_assert(dtree
!= NULL
);
210 IRC_RADIXTREE_FOREACH(entry
, &state
, dtree
)
212 delem
= STATE_CUR(&state
);
214 if (destroy_cb
!= NULL
)
215 (*destroy_cb
)(delem
->leaf
.key
, delem
->leaf
.data
,
218 irc_radixtree_delete(dtree
, delem
->leaf
.key
);
225 * irc_radixtree_foreach(struct irc_radixtree *dtree,
226 * int (*foreach_cb)(const char *key, void *data, void *privdata),
229 * Iterates over all entries in a DTree.
232 * - patricia tree object
233 * - optional iteration callback
234 * - optional opaque/private data to pass to callback
240 * - on success, a dtree is iterated
243 irc_radixtree_foreach(struct irc_radixtree
*dtree
, int (*foreach_cb
)(const char *key
, void *data
, void *privdata
), void *privdata
)
245 union irc_radixtree_elem
*delem
, *next
;
249 s_assert(dtree
!= NULL
);
256 /* Only one element in the tree */
259 if (foreach_cb
!= NULL
)
260 (*foreach_cb
)(delem
->leaf
.key
, delem
->leaf
.data
, privdata
);
270 next
= delem
->node
.down
[val
++];
271 while (next
== NULL
&& val
< POINTERS_PER_NODE
);
277 if (foreach_cb
!= NULL
)
278 (*foreach_cb
)(next
->leaf
.key
, next
->leaf
.data
, privdata
);
287 while (val
>= POINTERS_PER_NODE
)
289 val
= delem
->node
.parent_val
;
290 delem
= delem
->node
.parent
;
297 } while (delem
!= NULL
);
301 * irc_radixtree_search(struct irc_radixtree *dtree,
302 * void *(*foreach_cb)(const char *key, void *data, void *privdata),
305 * Searches all entries in a DTree using a custom callback.
308 * - patricia tree object
309 * - optional iteration callback
310 * - optional opaque/private data to pass to callback
313 * - on success, the requested object
314 * - on failure, NULL.
317 * - a dtree is iterated until the requested conditions are met
320 irc_radixtree_search(struct irc_radixtree
*dtree
, void *(*foreach_cb
)(const char *key
, void *data
, void *privdata
), void *privdata
)
322 union irc_radixtree_elem
*delem
, *next
;
327 s_assert(dtree
!= NULL
);
334 /* Only one element in the tree */
337 if (foreach_cb
!= NULL
)
338 return (*foreach_cb
)(delem
->leaf
.key
, delem
->leaf
.data
, privdata
);
348 next
= delem
->node
.down
[val
++];
349 while (next
== NULL
&& val
< POINTERS_PER_NODE
);
355 if (foreach_cb
!= NULL
)
356 ret
= (*foreach_cb
)(next
->leaf
.key
, next
->leaf
.data
, privdata
);
368 while (val
>= POINTERS_PER_NODE
)
370 val
= delem
->node
.parent_val
;
371 delem
= delem
->node
.parent
;
384 * irc_radixtree_foreach_start(struct irc_radixtree *dtree,
385 * struct irc_radixtree_iteration_state *state);
387 * Initializes a static DTree iterator.
390 * - patricia tree object
391 * - static DTree iterator
397 * - the static iterator, &state, is initialized.
400 irc_radixtree_foreach_start(struct irc_radixtree
*dtree
, struct irc_radixtree_iteration_state
*state
)
405 s_assert(state
!= NULL
);
407 if (dtree
->root
!= NULL
)
408 STATE_NEXT(state
) = first_leaf(dtree
->root
);
410 STATE_NEXT(state
) = NULL
;
412 STATE_CUR(state
) = STATE_NEXT(state
);
414 if (STATE_NEXT(state
) == NULL
)
417 /* make STATE_CUR point to first item and STATE_NEXT point to
419 irc_radixtree_foreach_next(dtree
, state
);
423 * irc_radixtree_foreach_cur(struct irc_radixtree *dtree,
424 * struct irc_radixtree_iteration_state *state);
426 * Returns the data from the current node being iterated by the
430 * - patricia tree object
431 * - static DTree iterator
434 * - reference to data in the current dtree node being iterated
440 irc_radixtree_foreach_cur(struct irc_radixtree
*dtree
, struct irc_radixtree_iteration_state
*state
)
445 s_assert(state
!= NULL
);
447 return STATE_CUR(state
) != NULL
?
448 ((struct irc_radixtree_leaf
*) STATE_CUR(state
))->data
: NULL
;
452 * irc_radixtree_foreach_next(struct irc_radixtree *dtree,
453 * struct irc_radixtree_iteration_state *state);
455 * Advances a static DTree iterator.
458 * - patricia tree object
459 * - static DTree iterator
465 * - the static iterator, &state, is advanced to a new DTree node.
468 irc_radixtree_foreach_next(struct irc_radixtree
*dtree
, struct irc_radixtree_iteration_state
*state
)
470 struct irc_radixtree_leaf
*leaf
;
472 union irc_radixtree_elem
*delem
, *next
;
479 s_assert(state
!= NULL
);
481 if (STATE_CUR(state
) == NULL
)
484 STATE_CUR(state
) = STATE_NEXT(state
);
486 if (STATE_NEXT(state
) == NULL
)
489 leaf
= STATE_NEXT(state
);
490 delem
= leaf
->parent
;
491 val
= leaf
->parent_val
;
493 while (delem
!= NULL
)
496 next
= delem
->node
.down
[val
++];
497 while (next
== NULL
&& val
< POINTERS_PER_NODE
);
503 /* We will find the original leaf first. */
504 if (&next
->leaf
!= leaf
)
506 if (strcmp(next
->leaf
.key
, leaf
->key
) < 0)
508 STATE_NEXT(state
) = NULL
;
512 STATE_NEXT(state
) = next
;
523 while (val
>= POINTERS_PER_NODE
)
525 val
= delem
->node
.parent_val
;
526 delem
= delem
->node
.parent
;
535 STATE_NEXT(state
) = NULL
;
539 * mowgli_radix_elem_find(struct irc_radixtree *dtree, const char *key)
541 * Looks up a DTree node by name.
544 * - patricia tree object
545 * - name of node to lookup
548 * - on success, the dtree node requested
554 struct irc_radixtree_leaf
*
555 mowgli_radix_elem_find(struct irc_radixtree
*dict
, const char *key
)
557 char ckey_store
[256];
559 char *ckey_buf
= NULL
;
561 union irc_radixtree_elem
*delem
;
565 s_assert(dict
!= NULL
);
566 s_assert(key
!= NULL
);
568 keylen
= strlen(key
);
570 if (dict
->canonize_cb
== NULL
)
576 if (keylen
>= (int) sizeof(ckey_store
))
578 ckey_buf
= rb_strdup(key
);
579 dict
->canonize_cb(ckey_buf
);
584 rb_strlcpy(ckey_store
, key
, sizeof ckey_store
);
585 dict
->canonize_cb(ckey_store
);
592 while (delem
!= NULL
&& !IS_LEAF(delem
))
594 if (delem
->nibnum
/ 2 < keylen
)
595 val
= NIBBLE_VAL(ckey
, delem
->nibnum
);
599 delem
= delem
->node
.down
[val
];
602 /* Now, if the key is in the tree, delem contains it. */
603 if ((delem
!= NULL
) && strcmp(delem
->leaf
.key
, ckey
))
606 if (ckey_buf
!= NULL
)
613 * irc_radixtree_add(struct irc_radixtree *dtree, const char *key, void *data)
615 * Creates a new DTree node and binds data to it.
618 * - patricia tree object
619 * - name for new DTree node
620 * - data to bind to the new DTree node
624 * - on failure, FALSE
627 * - data is inserted into the DTree.
629 struct irc_radixtree_leaf
*
630 mowgli_radix_elem_add(struct irc_radixtree
*dict
, const char *key
, void *data
)
634 union irc_radixtree_elem
*delem
, *prev
, *newnode
;
636 union irc_radixtree_elem
**place1
;
641 s_assert(dict
!= NULL
);
642 s_assert(key
!= NULL
);
643 s_assert(data
!= NULL
);
645 keylen
= strlen(key
);
646 ckey
= rb_strdup(key
);
651 if (dict
->canonize_cb
!= NULL
)
652 dict
->canonize_cb(ckey
);
655 val
= POINTERS_PER_NODE
+ 2; /* trap value */
658 while (delem
!= NULL
&& !IS_LEAF(delem
))
662 if (delem
->nibnum
/ 2 < keylen
)
663 val
= NIBBLE_VAL(ckey
, delem
->nibnum
);
667 delem
= delem
->node
.down
[val
];
670 /* Now, if the key is in the tree, delem contains it. */
671 if ((delem
!= NULL
) && !strcmp(delem
->leaf
.key
, ckey
))
677 if ((delem
== NULL
) && (prev
!= NULL
))
678 /* Get a leaf to compare with. */
679 delem
= first_leaf(prev
);
683 s_assert(prev
== NULL
);
684 s_assert(dict
->count
== 0);
685 place1
= &dict
->root
;
686 *place1
= rb_malloc(sizeof(struct irc_radixtree_leaf
));
687 s_assert(*place1
!= NULL
);
688 (*place1
)->nibnum
= -1;
689 (*place1
)->leaf
.data
= data
;
690 (*place1
)->leaf
.key
= ckey
;
691 (*place1
)->leaf
.parent
= prev
;
692 (*place1
)->leaf
.parent_val
= val
;
694 return &(*place1
)->leaf
;
697 /* Find the first nibble where they differ. */
698 for (i
= 0; NIBBLE_VAL(ckey
, i
) == NIBBLE_VAL(delem
->leaf
.key
, i
); i
++)
701 /* Find where to insert the new node. */
702 while (prev
!= NULL
&& prev
->nibnum
> i
)
704 val
= prev
->node
.parent_val
;
705 prev
= prev
->node
.parent
;
708 if ((prev
== NULL
) || (prev
->nibnum
< i
))
710 /* Insert new node below prev */
711 newnode
= rb_malloc(sizeof(struct irc_radixtree_node
));
712 s_assert(newnode
!= NULL
);
714 newnode
->node
.parent
= prev
;
715 newnode
->node
.parent_val
= val
;
717 for (j
= 0; j
< POINTERS_PER_NODE
; j
++)
718 newnode
->node
.down
[j
] = NULL
;
722 newnode
->node
.down
[NIBBLE_VAL(delem
->leaf
.key
, i
)] = dict
->root
;
724 if (IS_LEAF(dict
->root
))
726 dict
->root
->leaf
.parent
= newnode
;
727 dict
->root
->leaf
.parent_val
= NIBBLE_VAL(delem
->leaf
.key
, i
);
731 s_assert(dict
->root
->nibnum
> i
);
732 dict
->root
->node
.parent
= newnode
;
733 dict
->root
->node
.parent_val
= NIBBLE_VAL(delem
->leaf
.key
, i
);
736 dict
->root
= newnode
;
740 newnode
->node
.down
[NIBBLE_VAL(delem
->leaf
.key
, i
)] = prev
->node
.down
[val
];
742 if (IS_LEAF(prev
->node
.down
[val
]))
744 prev
->node
.down
[val
]->leaf
.parent
= newnode
;
745 prev
->node
.down
[val
]->leaf
.parent_val
= NIBBLE_VAL(delem
->leaf
.key
, i
);
749 prev
->node
.down
[val
]->node
.parent
= newnode
;
750 prev
->node
.down
[val
]->node
.parent_val
= NIBBLE_VAL(delem
->leaf
.key
, i
);
753 prev
->node
.down
[val
] = newnode
;
758 /* This nibble is already checked. */
759 s_assert(prev
->nibnum
== i
);
763 val
= NIBBLE_VAL(ckey
, i
);
764 place1
= &newnode
->node
.down
[val
];
765 s_assert(*place1
== NULL
);
766 *place1
= rb_malloc(sizeof(struct irc_radixtree_leaf
));
767 s_assert(*place1
!= NULL
);
768 (*place1
)->nibnum
= -1;
769 (*place1
)->leaf
.data
= data
;
770 (*place1
)->leaf
.key
= ckey
;
771 (*place1
)->leaf
.parent
= newnode
;
772 (*place1
)->leaf
.parent_val
= val
;
774 return &(*place1
)->leaf
;
778 irc_radixtree_add(struct irc_radixtree
*dict
, const char *key
, void *data
)
780 return (mowgli_radix_elem_add(dict
, key
, data
) != NULL
);
784 * irc_radixtree_delete(struct irc_radixtree *dtree, const char *key)
786 * Deletes data from a patricia tree.
789 * - patricia tree object
790 * - name of DTree node to delete
793 * - on success, the remaining data that needs to be rb_freed
797 * - data is removed from the DTree.
800 * - the returned data needs to be rb_freed/released manually!
803 irc_radixtree_delete(struct irc_radixtree
*dict
, const char *key
)
806 struct irc_radixtree_leaf
*leaf
;
808 leaf
= mowgli_radix_elem_find(dict
, key
);
814 irc_radixtree_elem_delete(dict
, leaf
);
819 irc_radixtree_elem_delete(struct irc_radixtree
*dict
, struct irc_radixtree_leaf
*leaf
)
821 union irc_radixtree_elem
*delem
, *prev
, *next
;
825 s_assert(dict
!= NULL
);
826 s_assert(leaf
!= NULL
);
828 delem
= (union irc_radixtree_elem
*) leaf
;
830 val
= delem
->leaf
.parent_val
;
831 prev
= delem
->leaf
.parent
;
833 rb_free(delem
->leaf
.key
);
838 prev
->node
.down
[val
] = NULL
;
840 /* Leaf is gone, now consider the node it was in. */
845 for (i
= 0; i
< POINTERS_PER_NODE
; i
++)
846 if (delem
->node
.down
[i
] != NULL
)
847 used
= used
== -1 ? i
: -2;
849 s_assert(used
== -2 || used
>= 0);
853 /* Only one pointer in this node, remove it.
854 * Replace the pointer that pointed to it by
855 * the sole pointer in it.
857 next
= delem
->node
.down
[used
];
858 val
= delem
->node
.parent_val
;
859 prev
= delem
->node
.parent
;
862 prev
->node
.down
[val
] = next
;
867 next
->leaf
.parent
= prev
, next
->leaf
.parent_val
= val
;
869 next
->node
.parent
= prev
, next
->node
.parent_val
= val
;
876 /* This was the last leaf. */
882 if (dict
->count
== 0)
884 s_assert(dict
->root
== NULL
);
890 * irc_radixtree_retrieve(struct irc_radixtree *dtree, const char *key)
892 * Retrieves data from a patricia.
895 * - patricia tree object
896 * - name of node to lookup
899 * - on success, the data bound to the DTree node.
906 irc_radixtree_retrieve(struct irc_radixtree
*dtree
, const char *key
)
908 struct irc_radixtree_leaf
*delem
= mowgli_radix_elem_find(dtree
, key
);
917 mowgli_radix_elem_get_key(struct irc_radixtree_leaf
*leaf
)
919 s_assert(leaf
!= NULL
);
925 mowgli_radix_elem_set_data(struct irc_radixtree_leaf
*leaf
, void *data
)
927 s_assert(leaf
!= NULL
);
933 mowgli_radix_elem_get_data(struct irc_radixtree_leaf
*leaf
)
935 s_assert(leaf
!= NULL
);
941 * irc_radixtree_size(struct irc_radixtree *dict)
943 * Returns the size of a patricia.
946 * - patricia tree object
955 irc_radixtree_size(struct irc_radixtree
*dict
)
957 s_assert(dict
!= NULL
);
962 /* returns the sum of the depths of the subtree rooted in delem at depth depth */
963 /* there is no need for this to be recursive, but it is easier... */
965 stats_recurse(union irc_radixtree_elem
*delem
, int depth
, int *pmaxdepth
)
969 union irc_radixtree_elem
*next
;
971 if (depth
> *pmaxdepth
)
977 s_assert(delem
->leaf
.parent
== NULL
);
980 s_assert(delem
->node
.parent
== NULL
);
986 for (val
= 0; val
< POINTERS_PER_NODE
; val
++)
988 next
= delem
->node
.down
[val
];
993 result
+= stats_recurse(next
, depth
+ 1, pmaxdepth
);
997 s_assert(next
->leaf
.parent
== delem
);
998 s_assert(next
->leaf
.parent_val
== val
);
1002 s_assert(next
->node
.parent
== delem
);
1003 s_assert(next
->node
.parent_val
== val
);
1004 s_assert(next
->node
.nibnum
> delem
->node
.nibnum
);
1012 * irc_radixtree_stats(struct irc_radixtree *dict, void (*cb)(const char *line, void *privdata), void *privdata)
1014 * Returns the size of a patricia.
1017 * - patricia tree object
1019 * - data for callback
1025 * - callback called with stats text
1028 irc_radixtree_stats(struct irc_radixtree
*dict
, void (*cb
)(const char *line
, void *privdata
), void *privdata
)
1033 s_assert(dict
!= NULL
);
1036 if (dict
->count
> 0)
1038 sum
= stats_recurse(dict
->root
, 0, &maxdepth
);
1039 snprintf(str
, sizeof str
, "%s: Objects: %d, Depth sum: %d, Avg depth: %d, Max depth: %d.", dict
->id
, dict
->count
, sum
, sum
/ dict
->count
, maxdepth
);
1043 snprintf(str
, sizeof str
, "%s: Objects: 0, Depth sum: 0, Avg depth: 0, Max depth: 0.", dict
->id
);
1051 irc_radixtree_stats_walk(void (*cb
)(const char *line
, void *privdata
), void *privdata
)
1055 RB_DLINK_FOREACH(ptr
, radixtree_list
.head
)
1057 irc_radixtree_stats(ptr
->data
, cb
, privdata
);
1061 void irc_radixtree_irccasecanon(char *str
)
1065 *str
= ToUpper(*str
);
1071 void irc_radixtree_strcasecanon(char *str
)
1075 *str
= toupper((unsigned char)*str
);