jshn: drop json_select warnings when called from json_get_values()
[project/libubox.git] / avl.h
diff --git a/avl.h b/avl.h
index 1c57604d2a1a853e7012a3de694bcdac3811c57c..3a79e3a73e6beb1160c4b3eff04663da426936da 100644 (file)
--- a/avl.h
+++ b/avl.h
@@ -82,7 +82,7 @@ struct avl_node {
   /**
    * pointer to key of node
    */
-  void *key;
+  const void *key;
 
   /**
    * balance state of AVL tree (0,-1,+1)
@@ -155,13 +155,11 @@ enum avl_find_mode {
 };
 
 void EXPORT(avl_init)(struct avl_tree *, avl_tree_comp, bool, void *);
-struct avl_node *EXPORT(avl_find)(struct avl_tree *, const void *);
-struct avl_node *EXPORT(avl_find_greaterequal)(struct avl_tree *tree, const void *key);
-struct avl_node *EXPORT(avl_find_lessequal)(struct avl_tree *tree, const void *key);
+struct avl_node *EXPORT(avl_find)(const struct avl_tree *, const void *);
+struct avl_node *EXPORT(avl_find_greaterequal)(const struct avl_tree *tree, const void *key);
+struct avl_node *EXPORT(avl_find_lessequal)(const struct avl_tree *tree, const void *key);
 int EXPORT(avl_insert)(struct avl_tree *, struct avl_node *);
 void EXPORT(avl_delete)(struct avl_tree *, struct avl_node *);
-void *EXPORT(__avl_find_element)(struct avl_tree *tree, const void *key,
-    size_t offset, enum avl_find_mode mode);
 
 /**
  * @param tree pointer to avl-tree
@@ -192,6 +190,32 @@ avl_is_empty(struct avl_tree *tree) {
   return tree->count == 0;
 }
 
+/**
+ * Internal function to support returning the element from a avl tree query
+ * @param tree pointer to avl tree
+ * @param key pointer to key
+ * @param offset offset of node inside the embedded struct
+ * @param mode mode of lookup operation (less equal, equal or greater equal)
+ * @param pointer to elemen, NULL if no fitting one was found
+ */
+static inline void *
+__avl_find_element(const struct avl_tree *tree, const void *key, size_t offset, enum avl_find_mode mode) {
+  void *node = NULL;
+
+  switch (mode) {
+    case AVL_FIND_EQUAL:
+      node = avl_find(tree, key);
+      break;
+    case AVL_FIND_LESSEQUAL:
+      node = avl_find_lessequal(tree, key);
+      break;
+    case AVL_FIND_GREATEREQUAL:
+      node = avl_find_greaterequal(tree, key);
+      break;
+  }
+  return node == NULL ? NULL : (((char *)node) - offset);
+}
+
 /**
  * @param tree pointer to avl-tree
  * @param key pointer to key
@@ -243,7 +267,7 @@ avl_is_empty(struct avl_tree *tree) {
  *    (automatically converted to type 'element')
  */
 #define avl_first_element(tree, element, node_member) \
-  container_of((tree)->list_head.next, typeof(*(element)), node_member)
+  container_of((tree)->list_head.next, typeof(*(element)), node_member.list)
 
 /**
  * @param tree pointer to tree
@@ -255,7 +279,7 @@ avl_is_empty(struct avl_tree *tree) {
  *    (automatically converted to type 'element')
  */
 #define avl_last_element(tree, element, node_member) \
-  container_of((tree)->list_head.prev, typeof(*(element)), node_member)
+  container_of((tree)->list_head.prev, typeof(*(element)), node_member.list)
 
 /**
  * This function must not be called for the last element of
@@ -268,7 +292,7 @@ avl_is_empty(struct avl_tree *tree) {
  *    (automatically converted to type 'element')
  */
 #define avl_next_element(element, node_member) \
-  container_of((&(element)->node_member.list)->next, typeof(*(element)), node_member)
+  container_of((&(element)->node_member.list)->next, typeof(*(element)), node_member.list)
 
 /**
  * This function must not be called for the first element of
@@ -281,7 +305,7 @@ avl_is_empty(struct avl_tree *tree) {
  *    (automatically converted to type 'element')
  */
 #define avl_prev_element(element, node_member) \
-  container_of((&(element)->node_member.list)->prev, typeof(*(element)), node_member)
+  container_of((&(element)->node_member.list)->prev, typeof(*(element)), node_member.list)
 
 /**
  * Loop over a block of elements of a tree, used similar to a for() command.