]> jfr.im git - solanum.git/commitdiff
ircd: radixtree: add irc_radixtree_foreach_start_from() which uses irc_radixtree_elem...
authorWilliam Pitcock <redacted>
Thu, 21 Jan 2016 02:02:03 +0000 (21:02 -0500)
committerWilliam Pitcock <redacted>
Thu, 21 Jan 2016 02:02:03 +0000 (21:02 -0500)
include/irc_radixtree.h
ircd/irc_radixtree.c

index a2b590783934e9b5a509f40bc1d0adc1638e3a70..3d504e0192c7527947ff86ac0916a7f6cd27deb8 100644 (file)
@@ -55,6 +55,9 @@ struct irc_radixtree_iteration_state
 #define IRC_RADIXTREE_FOREACH(element, state, dict) \
        for (irc_radixtree_foreach_start((dict), (state)); (element = irc_radixtree_foreach_cur((dict), (state))); irc_radixtree_foreach_next((dict), (state)))
 
+#define IRC_RADIXTREE_FOREACH_FROM(element, state, dict, key) \
+       for (irc_radixtree_foreach_start_from((dict), (state), (key)); (element = irc_radixtree_foreach_cur((dict), (state))); irc_radixtree_foreach_next((dict), (state)))
+
 /*
  * irc_radixtree_create() creates a new patricia tree of the defined resolution.
  * compare_cb is the canonizing function.
@@ -100,6 +103,15 @@ extern void *irc_radixtree_search(struct irc_radixtree *dtree, void *(*foreach_c
  */
 extern void irc_radixtree_foreach_start(struct irc_radixtree *dtree, struct irc_radixtree_iteration_state *state);
 
+/*
+ * irc_radixtree_foreach_start_from() begins an iteration over all items,
+ * starting with the item specified by `key`.  If there is only one iteration
+ * in progress at a time, it is permitted to remove the current element
+ * of the iteration (but not any other element).
+ * Use NULL as a key to have it start at the beginning.
+ */
+extern void irc_radixtree_foreach_start_from(struct irc_radixtree *dtree, struct irc_radixtree_iteration_state *state, const char *key);
+
 /*
  * irc_radixtree_foreach_cur() returns the current element of the iteration,
  * or NULL if there are no more elements.
index 9ede33f843936974ba7027ba9ef8529310a47b7f..008e233573e18c956ed0061b1236250b9c339baa 100644 (file)
@@ -609,6 +609,41 @@ irc_radixtree_elem_find(struct irc_radixtree *dict, const char *key)
        return &delem->leaf;
 }
 
+/*
+ * irc_radixtree_foreach_start_from(struct irc_radixtree *dtree, struct irc_radixtree_iteration_state *state, const char *key)
+ *
+ * Starts iteration from a specified key, by wrapping irc_radixtree_elem_find().
+ *
+ * Inputs:
+ *     - patricia tree object
+ *     - iterator
+ *     - key to start from
+ *
+ * Outputs:
+ *     - none
+ *
+ * Side Effects:
+ *     - the iterator's state is initialized at a specific point
+ */
+void
+irc_radixtree_foreach_start_from(struct irc_radixtree *dtree, struct irc_radixtree_iteration_state *state, const char *key)
+{
+       s_assert(dtree != NULL);
+       s_assert(state != NULL);
+
+       if (key != NULL)
+       {
+               STATE_CUR(state) = NULL;
+               STATE_NEXT(state) = irc_radixtree_elem_find(dtree, key);
+
+               /* make STATE_CUR point to selected item and STATE_NEXT point to
+                * next item in the tree */
+               irc_radixtree_foreach_next(dtree, state);
+       }
+       else
+               irc_radixtree_foreach_start(dtree, state);
+}
+
 /*
  * irc_radixtree_add(struct irc_radixtree *dtree, const char *key, void *data)
  *