avl.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /*
  2. * PacketBB handler library (see RFC 5444)
  3. * Copyright (c) 2010 Henning Rogge <hrogge@googlemail.com>
  4. * Original OLSRd implementation by Hannes Gredler <hannes@gredler.at>
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * * Neither the name of olsr.org, olsrd nor the names of its
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. * Visit http://www.olsr.org/git for more information.
  35. *
  36. * If you find this software useful feel free to make a donation
  37. * to the project. For more information see the website or contact
  38. * the copyright holders.
  39. */
  40. #ifndef _AVL_H
  41. #define _AVL_H
  42. #include <stddef.h>
  43. #include <stdbool.h>
  44. #include "list.h"
  45. /* Support for OLSR.org linker symbol export */
  46. #define EXPORT(sym) sym
  47. /**
  48. * This element is a member of a avl-tree. It must be contained in all
  49. * larger structs that should be put into a tree.
  50. */
  51. struct avl_node {
  52. /**
  53. * Linked list node for supporting easy iteration and multiple
  54. * elments with the same key.
  55. *
  56. * this must be the first element of an avl_node to
  57. * make casting for lists easier
  58. */
  59. struct list_head list;
  60. /**
  61. * Pointer to parent node in tree, NULL if root node
  62. */
  63. struct avl_node *parent;
  64. /**
  65. * Pointer to left child
  66. */
  67. struct avl_node *left;
  68. /**
  69. * Pointer to right child
  70. */
  71. struct avl_node *right;
  72. /**
  73. * pointer to key of node
  74. */
  75. const void *key;
  76. /**
  77. * balance state of AVL tree (0,-1,+1)
  78. */
  79. signed char balance;
  80. /**
  81. * true if first of a series of nodes with same key
  82. */
  83. bool leader;
  84. };
  85. /**
  86. * Prototype for avl comparators
  87. * @param k1 first key
  88. * @param k2 second key
  89. * @param ptr custom data for tree comparator
  90. * @return +1 if k1>k2, -1 if k1<k2, 0 if k1==k2
  91. */
  92. typedef int (*avl_tree_comp) (const void *k1, const void *k2, void *ptr);
  93. /**
  94. * This struct is the central management part of an avl tree.
  95. * One of them is necessary for each avl_tree.
  96. */
  97. struct avl_tree {
  98. /**
  99. * Head of linked list node for supporting easy iteration
  100. * and multiple elments with the same key.
  101. */
  102. struct list_head list_head;
  103. /**
  104. * pointer to the root node of the avl tree, NULL if tree is empty
  105. */
  106. struct avl_node *root;
  107. /**
  108. * number of nodes in the avl tree
  109. */
  110. unsigned int count;
  111. /**
  112. * true if multiple nodes with the same key are
  113. * allowed in the tree, false otherwise
  114. */
  115. bool allow_dups;
  116. /**
  117. * pointer to the tree comparator
  118. *
  119. * First two parameters are keys to compare,
  120. * third parameter is a copy of cmp_ptr
  121. */
  122. avl_tree_comp comp;
  123. /**
  124. * custom pointer delivered to the tree comparator
  125. */
  126. void *cmp_ptr;
  127. };
  128. /**
  129. * internal enum for avl_find_... macros
  130. */
  131. enum avl_find_mode {
  132. AVL_FIND_EQUAL,
  133. AVL_FIND_LESSEQUAL,
  134. AVL_FIND_GREATEREQUAL
  135. };
  136. #define AVL_TREE_INIT(_name, _comp, _allow_dups, _cmp_ptr) \
  137. { \
  138. .list_head = LIST_HEAD_INIT(_name.list_head), \
  139. .comp = _comp, \
  140. .allow_dups = _allow_dups, \
  141. .cmp_ptr = _cmp_ptr \
  142. }
  143. #define AVL_TREE(_name, _comp, _allow_dups, _cmp_ptr) \
  144. struct avl_tree _name = \
  145. AVL_TREE_INIT(_name, _comp, _allow_dups, _cmp_ptr)
  146. void EXPORT(avl_init)(struct avl_tree *, avl_tree_comp, bool, void *);
  147. struct avl_node *EXPORT(avl_find)(const struct avl_tree *, const void *);
  148. struct avl_node *EXPORT(avl_find_greaterequal)(const struct avl_tree *tree, const void *key);
  149. struct avl_node *EXPORT(avl_find_lessequal)(const struct avl_tree *tree, const void *key);
  150. int EXPORT(avl_insert)(struct avl_tree *, struct avl_node *);
  151. void EXPORT(avl_delete)(struct avl_tree *, struct avl_node *);
  152. /**
  153. * @param tree pointer to avl-tree
  154. * @param node pointer to node of the tree
  155. * @return true if node is the first one of the tree, false otherwise
  156. */
  157. static inline bool
  158. avl_is_first(struct avl_tree *tree, struct avl_node *node) {
  159. return tree->list_head.next == &node->list;
  160. }
  161. /**
  162. * @param tree pointer to avl-tree
  163. * @param node pointer to node of the tree
  164. * @return true if node is the last one of the tree, false otherwise
  165. */
  166. static inline bool
  167. avl_is_last(struct avl_tree *tree, struct avl_node *node) {
  168. return tree->list_head.prev == &node->list;
  169. }
  170. /**
  171. * @param tree pointer to avl-tree
  172. * @return true if the tree is empty, false otherwise
  173. */
  174. static inline bool
  175. avl_is_empty(struct avl_tree *tree) {
  176. return tree->count == 0;
  177. }
  178. /**
  179. * Internal function to support returning the element from a avl tree query
  180. * @param tree pointer to avl tree
  181. * @param key pointer to key
  182. * @param offset offset of node inside the embedded struct
  183. * @param mode mode of lookup operation (less equal, equal or greater equal)
  184. * @param pointer to elemen, NULL if no fitting one was found
  185. */
  186. static inline void *
  187. __avl_find_element(const struct avl_tree *tree, const void *key, size_t offset, enum avl_find_mode mode) {
  188. void *node = NULL;
  189. switch (mode) {
  190. case AVL_FIND_EQUAL:
  191. node = avl_find(tree, key);
  192. break;
  193. case AVL_FIND_LESSEQUAL:
  194. node = avl_find_lessequal(tree, key);
  195. break;
  196. case AVL_FIND_GREATEREQUAL:
  197. node = avl_find_greaterequal(tree, key);
  198. break;
  199. }
  200. return node == NULL ? NULL : (((char *)node) - offset);
  201. }
  202. /**
  203. * @param tree pointer to avl-tree
  204. * @param key pointer to key
  205. * @param element pointer to a node element
  206. * (don't need to be initialized)
  207. * @param node_element name of the avl_node element inside the
  208. * larger struct
  209. * @return pointer to tree element with the specified key,
  210. * NULL if no element was found
  211. */
  212. #define avl_find_element(tree, key, element, node_element) \
  213. ((__typeof__(*(element)) *)__avl_find_element(tree, key, offsetof(typeof(*(element)), node_element), AVL_FIND_EQUAL))
  214. /**
  215. * @param tree pointer to avl-tree
  216. * @param key pointer to specified key
  217. * @param element pointer to a node element
  218. * (don't need to be initialized)
  219. * @param node_element name of the avl_node element inside the
  220. * larger struct
  221. * return pointer to last tree element with less or equal key than specified key,
  222. * NULL if no element was found
  223. */
  224. #define avl_find_le_element(tree, key, element, node_element) \
  225. ((__typeof__(*(element)) *)__avl_find_element(tree, key, offsetof(typeof(*(element)), node_element), AVL_FIND_LESSEQUAL))
  226. /**
  227. * @param tree pointer to avl-tree
  228. * @param key pointer to specified key
  229. * @param element pointer to a node element
  230. * (don't need to be initialized)
  231. * @param node_element name of the avl_node element inside the
  232. * larger struct
  233. * return pointer to first tree element with greater or equal key than specified key,
  234. * NULL if no element was found
  235. */
  236. #define avl_find_ge_element(tree, key, element, node_element) \
  237. ((__typeof__(*(element)) *)__avl_find_element(tree, key, offsetof(typeof(*(element)), node_element), AVL_FIND_GREATEREQUAL))
  238. /**
  239. * This function must not be called for an empty tree
  240. *
  241. * @param tree pointer to avl-tree
  242. * @param element pointer to a node element
  243. * (don't need to be initialized)
  244. * @param node_member name of the avl_node element inside the
  245. * larger struct
  246. * @return pointer to the first element of the avl_tree
  247. * (automatically converted to type 'element')
  248. */
  249. #define avl_first_element(tree, element, node_member) \
  250. container_of((tree)->list_head.next, __typeof__(*(element)), node_member.list)
  251. /**
  252. * @param tree pointer to tree
  253. * @param element pointer to a node struct that contains the avl_node
  254. * (don't need to be initialized)
  255. * @param node_member name of the avl_node element inside the
  256. * larger struct
  257. * @return pointer to the last element of the avl_tree
  258. * (automatically converted to type 'element')
  259. */
  260. #define avl_last_element(tree, element, node_member) \
  261. container_of((tree)->list_head.prev, __typeof__(*(element)), node_member.list)
  262. /**
  263. * This function must not be called for the last element of
  264. * an avl tree
  265. *
  266. * @param element pointer to a node of the tree
  267. * @param node_member name of the avl_node element inside the
  268. * larger struct
  269. * @return pointer to the node after 'element'
  270. * (automatically converted to type 'element')
  271. */
  272. #define avl_next_element(element, node_member) \
  273. container_of((&(element)->node_member.list)->next, __typeof__(*(element)), node_member.list)
  274. /**
  275. * This function must not be called for the first element of
  276. * an avl tree
  277. *
  278. * @param element pointer to a node of the tree
  279. * @param node_member name of the avl_node element inside the
  280. * larger struct
  281. * @return pointer to the node before 'element'
  282. * (automatically converted to type 'element')
  283. */
  284. #define avl_prev_element(element, node_member) \
  285. container_of((&(element)->node_member.list)->prev, __typeof__(*(element)), node_member.list)
  286. /**
  287. * Loop over a block of elements of a tree, used similar to a for() command.
  288. * This loop should not be used if elements are removed from the tree during
  289. * the loop.
  290. *
  291. * @param first pointer to first element of loop
  292. * @param last pointer to last element of loop
  293. * @param element pointer to a node of the tree, this element will
  294. * contain the current node of the list during the loop
  295. * @param node_member name of the avl_node element inside the
  296. * larger struct
  297. */
  298. #define avl_for_element_range(first, last, element, node_member) \
  299. for (element = (first); \
  300. element->node_member.list.prev != &(last)->node_member.list; \
  301. element = avl_next_element(element, node_member))
  302. /**
  303. * Loop over a block of elements of a tree backwards, used similar to a for() command.
  304. * This loop should not be used if elements are removed from the tree during
  305. * the loop.
  306. *
  307. * @param first pointer to first element of loop
  308. * @param last pointer to last element of loop
  309. * @param element pointer to a node of the tree, this element will
  310. * contain the current node of the list during the loop
  311. * @param node_member name of the avl_node element inside the
  312. * larger struct
  313. */
  314. #define avl_for_element_range_reverse(first, last, element, node_member) \
  315. for (element = (last); \
  316. element->node_member.list.next != &(first)->node_member.list; \
  317. element = avl_prev_element(element, node_member))
  318. /**
  319. * Loop over all elements of an avl_tree, used similar to a for() command.
  320. * This loop should not be used if elements are removed from the tree during
  321. * the loop.
  322. *
  323. * @param tree pointer to avl-tree
  324. * @param element pointer to a node of the tree, this element will
  325. * contain the current node of the tree during the loop
  326. * @param node_member name of the avl_node element inside the
  327. * larger struct
  328. */
  329. #define avl_for_each_element(tree, element, node_member) \
  330. avl_for_element_range(avl_first_element(tree, element, node_member), \
  331. avl_last_element(tree, element, node_member), \
  332. element, node_member)
  333. /**
  334. * Loop over all elements of an avl_tree backwards, used similar to a for() command.
  335. * This loop should not be used if elements are removed from the tree during
  336. * the loop.
  337. *
  338. * @param tree pointer to avl-tree
  339. * @param element pointer to a node of the tree, this element will
  340. * contain the current node of the tree during the loop
  341. * @param node_member name of the avl_node element inside the
  342. * larger struct
  343. */
  344. #define avl_for_each_element_reverse(tree, element, node_member) \
  345. avl_for_element_range_reverse(avl_first_element(tree, element, node_member), \
  346. avl_last_element(tree, element, node_member), \
  347. element, node_member)
  348. /**
  349. * Loop over a block of elements of a tree, used similar to a for() command.
  350. * This loop should not be used if elements are removed from the tree during
  351. * the loop.
  352. * The loop runs from the element 'first' to the end of the tree.
  353. *
  354. * @param tree pointer to avl-tree
  355. * @param first pointer to first element of loop
  356. * @param element pointer to a node of the tree, this element will
  357. * contain the current node of the list during the loop
  358. * @param node_member name of the avl_node element inside the
  359. * larger struct
  360. */
  361. #define avl_for_element_to_last(tree, first, element, node_member) \
  362. avl_for_element_range(first, avl_last_element(tree, element, node_member), element, node_member)
  363. /**
  364. * Loop over a block of elements of a tree backwards, used similar to a for() command.
  365. * This loop should not be used if elements are removed from the tree during
  366. * the loop.
  367. * The loop runs from the element 'first' to the end of the tree.
  368. *
  369. * @param tree pointer to avl-tree
  370. * @param first pointer to first element of loop
  371. * @param element pointer to a node of the tree, this element will
  372. * contain the current node of the list during the loop
  373. * @param node_member name of the avl_node element inside the
  374. * larger struct
  375. */
  376. #define avl_for_element_to_last_reverse(tree, first, element, node_member) \
  377. avl_for_element_range_reverse(first, avl_last_element(tree, element, node_member), element, node_member)
  378. /**
  379. * Loop over a block of elements of a tree, used similar to a for() command.
  380. * This loop should not be used if elements are removed from the tree during
  381. * the loop.
  382. * The loop runs from the start of the tree to the element 'last'.
  383. *
  384. * @param tree pointer to avl-tree
  385. * @param last pointer to last element of loop
  386. * @param element pointer to a node of the tree, this element will
  387. * contain the current node of the list during the loop
  388. * @param node_member name of the avl_node element inside the
  389. * larger struct
  390. */
  391. #define avl_for_first_to_element(tree, last, element, node_member) \
  392. avl_for_element_range(avl_first_element(tree, element, node_member), last, element, node_member)
  393. /**
  394. * Loop over a block of elements of a tree backwards, used similar to a for() command.
  395. * This loop should not be used if elements are removed from the tree during
  396. * the loop.
  397. * The loop runs from the start of the tree to the element 'last'.
  398. *
  399. * @param tree pointer to avl-tree
  400. * @param last pointer to last element of loop
  401. * @param element pointer to a node of the tree, this element will
  402. * contain the current node of the list during the loop
  403. * @param node_member name of the avl_node element inside the
  404. * larger struct
  405. */
  406. #define avl_for_first_to_element_reverse(tree, last, element, node_member) \
  407. avl_for_element_range_reverse(avl_first_element(tree, element, node_member), last, element, node_member)
  408. /**
  409. * Loop over a block of nodes of a tree, used similar to a for() command.
  410. * This loop can be used if the current element might be removed from
  411. * the tree during the loop. Other elements should not be removed during
  412. * the loop.
  413. *
  414. * @param first_element first element of loop
  415. * @param last_element last element of loop
  416. * @param element iterator pointer to tree element struct
  417. * @param node_member name of avl_node within tree element struct
  418. * @param ptr pointer to tree element struct which is used to store
  419. * the next node during the loop
  420. */
  421. #define avl_for_element_range_safe(first_element, last_element, element, node_member, ptr) \
  422. for (element = (first_element), ptr = avl_next_element(first_element, node_member); \
  423. element->node_member.list.prev != &(last_element)->node_member.list; \
  424. element = ptr, ptr = avl_next_element(ptr, node_member))
  425. /**
  426. * Loop over a block of elements of a tree backwards, used similar to a for() command.
  427. * This loop can be used if the current element might be removed from
  428. * the tree during the loop. Other elements should not be removed during
  429. * the loop.
  430. *
  431. * @param first_element first element of range (will be last returned by the loop)
  432. * @param last_element last element of range (will be first returned by the loop)
  433. * @param element iterator pointer to node element struct
  434. * @param node_member name of avl_node within node element struct
  435. * @param ptr pointer to node element struct which is used to store
  436. * the previous node during the loop
  437. */
  438. #define avl_for_element_range_reverse_safe(first_element, last_element, element, node_member, ptr) \
  439. for (element = (last_element), ptr = avl_prev_element(last_element, node_member); \
  440. element->node_member.list.next != &(first_element)->node_member.list; \
  441. element = ptr, ptr = avl_prev_element(ptr, node_member))
  442. /**
  443. * Loop over all elements of an avl_tree, used similar to a for() command.
  444. * This loop can be used if the current element might be removed from
  445. * the tree during the loop. Other elements should not be removed during
  446. * the loop.
  447. *
  448. * @param tree pointer to avl-tree
  449. * @param element pointer to a node of the tree, this element will
  450. * contain the current node of the tree during the loop
  451. * @param node_member name of the avl_node element inside the
  452. * larger struct
  453. * @param ptr pointer to a tree element which is used to store
  454. * the next node during the loop
  455. */
  456. #define avl_for_each_element_safe(tree, element, node_member, ptr) \
  457. avl_for_element_range_safe(avl_first_element(tree, element, node_member), \
  458. avl_last_element(tree, element, node_member), \
  459. element, node_member, ptr)
  460. /**
  461. * Loop over all elements of an avl_tree backwards, used similar to a for() command.
  462. * This loop can be used if the current element might be removed from
  463. * the tree during the loop. Other elements should not be removed during
  464. * the loop.
  465. *
  466. * @param tree pointer to avl-tree
  467. * @param element pointer to a node of the tree, this element will
  468. * contain the current node of the tree during the loop
  469. * @param node_member name of the avl_node element inside the
  470. * larger struct
  471. * @param ptr pointer to a tree element which is used to store
  472. * the next node during the loop
  473. */
  474. #define avl_for_each_element_reverse_safe(tree, element, node_member, ptr) \
  475. avl_for_element_range_reverse_safe(avl_first_element(tree, element, node_member), \
  476. avl_last_element(tree, element, node_member), \
  477. element, node_member, ptr)
  478. /**
  479. * A special loop that removes all elements of the tree and cleans up the tree
  480. * root. The loop body is responsible to free the node elements of the tree.
  481. *
  482. * This loop is much faster than a normal one for clearing the tree because it
  483. * does not rebalance the tree after each removal. Do NOT use a break command
  484. * inside.
  485. * You can free the memory of the elements within the loop.
  486. * Do NOT call avl_delete() on the elements within the loop,
  487. *
  488. * @param tree pointer to avl-tree
  489. * @param element pointer to a node of the tree, this element will
  490. * contain the current node of the tree during the loop
  491. * @param node_member name of the avl_node element inside the
  492. * larger struct
  493. * @param ptr pointer to a tree element which is used to store
  494. * the next node during the loop
  495. */
  496. #define avl_remove_all_elements(tree, element, node_member, ptr) \
  497. for (element = avl_first_element(tree, element, node_member), \
  498. ptr = avl_next_element(element, node_member), \
  499. INIT_LIST_HEAD(&(tree)->list_head), \
  500. (tree)->root = NULL; \
  501. (tree)->count > 0; \
  502. element = ptr, ptr = avl_next_element(ptr, node_member), (tree)->count--)
  503. #endif /* _AVL_H */
  504. /*
  505. * Local Variables:
  506. * c-basic-offset: 2
  507. * indent-tabs-mode: nil
  508. * End:
  509. */