A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

366 lines
11 KiB

  1. /*
  2. * avl_tree.h - intrusive, nonrecursive AVL tree data structure (self-balancing
  3. * binary search tree), header file
  4. *
  5. * Written in 2014-2016 by Eric Biggers <ebiggers3@gmail.com>
  6. * Slight changes for compatibility by Ben Kurtovic <ben.kurtovic@gmail.com>
  7. *
  8. * To the extent possible under law, the author(s) have dedicated all copyright
  9. * and related and neighboring rights to this software to the public domain
  10. * worldwide via the Creative Commons Zero 1.0 Universal Public Domain
  11. * Dedication (the "CC0").
  12. *
  13. * This software is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  15. * FOR A PARTICULAR PURPOSE. See the CC0 for more details.
  16. *
  17. * You should have received a copy of the CC0 along with this software; if not
  18. * see <http://creativecommons.org/publicdomain/zero/1.0/>.
  19. */
  20. #ifndef _AVL_TREE_H_
  21. #define _AVL_TREE_H_
  22. #include <stddef.h>
  23. #if defined(_MSC_VER) && (_MSC_VER < 1600)
  24. typedef unsigned long uintptr_t;
  25. #else
  26. #include <stdint.h>
  27. #endif
  28. #ifdef __GNUC__
  29. # define AVL_INLINE inline __attribute__((always_inline))
  30. #elif defined(_MSC_VER) && (_MSC_VER < 1900)
  31. # define AVL_INLINE __inline
  32. #else
  33. # define AVL_INLINE inline
  34. #endif
  35. /* Node in an AVL tree. Embed this in some other data structure. */
  36. struct avl_tree_node {
  37. /* Pointer to left child or NULL */
  38. struct avl_tree_node *left;
  39. /* Pointer to right child or NULL */
  40. struct avl_tree_node *right;
  41. /* Pointer to parent combined with the balance factor. This saves 4 or
  42. * 8 bytes of memory depending on the CPU architecture.
  43. *
  44. * Low 2 bits: One greater than the balance factor of this subtree,
  45. * which is equal to height(right) - height(left). The mapping is:
  46. *
  47. * 00 => -1
  48. * 01 => 0
  49. * 10 => +1
  50. * 11 => undefined
  51. *
  52. * The rest of the bits are the pointer to the parent node. It must be
  53. * 4-byte aligned, and it will be NULL if this is the root node and
  54. * therefore has no parent. */
  55. uintptr_t parent_balance;
  56. };
  57. /* Cast an AVL tree node to the containing data structure. */
  58. #define avl_tree_entry(entry, type, member) \
  59. ((type*) ((char *)(entry) - offsetof(type, member)))
  60. /* Returns a pointer to the parent of the specified AVL tree node, or NULL if it
  61. * is already the root of the tree. */
  62. static AVL_INLINE struct avl_tree_node *
  63. avl_get_parent(const struct avl_tree_node *node)
  64. {
  65. return (struct avl_tree_node *)(node->parent_balance & ~3);
  66. }
  67. /* Marks the specified AVL tree node as unlinked from any tree. */
  68. static AVL_INLINE void
  69. avl_tree_node_set_unlinked(struct avl_tree_node *node)
  70. {
  71. node->parent_balance = (uintptr_t)node;
  72. }
  73. /* Returns true iff the specified AVL tree node has been marked with
  74. * avl_tree_node_set_unlinked() and has not subsequently been inserted into a
  75. * tree. */
  76. static AVL_INLINE int
  77. avl_tree_node_is_unlinked(const struct avl_tree_node *node)
  78. {
  79. return node->parent_balance == (uintptr_t)node;
  80. }
  81. /* (Internal use only) */
  82. extern void
  83. avl_tree_rebalance_after_insert(struct avl_tree_node **root_ptr,
  84. struct avl_tree_node *inserted);
  85. /*
  86. * Looks up an item in the specified AVL tree.
  87. *
  88. * @root
  89. * Pointer to the root of the AVL tree. (This can be NULL --- that just
  90. * means the tree is empty.)
  91. *
  92. * @cmp_ctx
  93. * First argument to pass to the comparison callback. This generally
  94. * should be a pointer to an object equal to the one being searched for.
  95. *
  96. * @cmp
  97. * Comparison callback. Must return < 0, 0, or > 0 if the first argument
  98. * is less than, equal to, or greater than the second argument,
  99. * respectively. The first argument will be @cmp_ctx and the second
  100. * argument will be a pointer to the AVL tree node of an item in the tree.
  101. *
  102. * Returns a pointer to the AVL tree node of the resulting item, or NULL if the
  103. * item was not found.
  104. *
  105. * Example:
  106. *
  107. * struct int_wrapper {
  108. * int data;
  109. * struct avl_tree_node index_node;
  110. * };
  111. *
  112. * static int _avl_cmp_int_to_node(const void *intptr,
  113. * const struct avl_tree_node *nodeptr)
  114. * {
  115. * int n1 = *(const int *)intptr;
  116. * int n2 = avl_tree_entry(nodeptr, struct int_wrapper, index_node)->data;
  117. * if (n1 < n2)
  118. * return -1;
  119. * else if (n1 > n2)
  120. * return 1;
  121. * else
  122. * return 0;
  123. * }
  124. *
  125. * bool contains_int(struct avl_tree_node *root, int n)
  126. * {
  127. * struct avl_tree_node *result;
  128. *
  129. * result = avl_tree_lookup(root, &n, _avl_cmp_int_to_node);
  130. * return result ? true : false;
  131. * }
  132. */
  133. static AVL_INLINE struct avl_tree_node *
  134. avl_tree_lookup(const struct avl_tree_node *root,
  135. const void *cmp_ctx,
  136. int (*cmp)(const void *, const struct avl_tree_node *))
  137. {
  138. const struct avl_tree_node *cur = root;
  139. while (cur) {
  140. int res = (*cmp)(cmp_ctx, cur);
  141. if (res < 0)
  142. cur = cur->left;
  143. else if (res > 0)
  144. cur = cur->right;
  145. else
  146. break;
  147. }
  148. return (struct avl_tree_node*)cur;
  149. }
  150. /* Same as avl_tree_lookup(), but uses a more specific type for the comparison
  151. * function. Specifically, with this function the item being searched for is
  152. * expected to be in the same format as those already in the tree, with an
  153. * embedded 'struct avl_tree_node'. */
  154. static AVL_INLINE struct avl_tree_node *
  155. avl_tree_lookup_node(const struct avl_tree_node *root,
  156. const struct avl_tree_node *node,
  157. int (*cmp)(const struct avl_tree_node *,
  158. const struct avl_tree_node *))
  159. {
  160. const struct avl_tree_node *cur = root;
  161. while (cur) {
  162. int res = (*cmp)(node, cur);
  163. if (res < 0)
  164. cur = cur->left;
  165. else if (res > 0)
  166. cur = cur->right;
  167. else
  168. break;
  169. }
  170. return (struct avl_tree_node*)cur;
  171. }
  172. /*
  173. * Inserts an item into the specified AVL tree.
  174. *
  175. * @root_ptr
  176. * Location of the AVL tree's root pointer. Indirection is needed because
  177. * the root node may change as a result of rotations caused by the
  178. * insertion. Initialize *root_ptr to NULL for an empty tree.
  179. *
  180. * @item
  181. * Pointer to the `struct avl_tree_node' embedded in the item to insert.
  182. * No members in it need be pre-initialized, although members in the
  183. * containing structure should be pre-initialized so that @cmp can use them
  184. * in comparisons.
  185. *
  186. * @cmp
  187. * Comparison callback. Must return < 0, 0, or > 0 if the first argument
  188. * is less than, equal to, or greater than the second argument,
  189. * respectively. The first argument will be @item and the second
  190. * argument will be a pointer to an AVL tree node embedded in some
  191. * previously-inserted item to which @item is being compared.
  192. *
  193. * If no item in the tree is comparatively equal (via @cmp) to @item, inserts
  194. * @item and returns NULL. Otherwise does nothing and returns a pointer to the
  195. * AVL tree node embedded in the previously-inserted item which compared equal
  196. * to @item.
  197. *
  198. * Example:
  199. *
  200. * struct int_wrapper {
  201. * int data;
  202. * struct avl_tree_node index_node;
  203. * };
  204. *
  205. * #define GET_DATA(i) avl_tree_entry((i), struct int_wrapper, index_node)->data
  206. *
  207. * static int _avl_cmp_ints(const struct avl_tree_node *node1,
  208. * const struct avl_tree_node *node2)
  209. * {
  210. * int n1 = GET_DATA(node1);
  211. * int n2 = GET_DATA(node2);
  212. * if (n1 < n2)
  213. * return -1;
  214. * else if (n1 > n2)
  215. * return 1;
  216. * else
  217. * return 0;
  218. * }
  219. *
  220. * bool insert_int(struct avl_tree_node **root_ptr, int data)
  221. * {
  222. * struct int_wrapper *i = malloc(sizeof(struct int_wrapper));
  223. * i->data = data;
  224. * if (avl_tree_insert(root_ptr, &i->index_node, _avl_cmp_ints)) {
  225. * // Duplicate.
  226. * free(i);
  227. * return false;
  228. * }
  229. * return true;
  230. * }
  231. */
  232. static AVL_INLINE struct avl_tree_node *
  233. avl_tree_insert(struct avl_tree_node **root_ptr,
  234. struct avl_tree_node *item,
  235. int (*cmp)(const struct avl_tree_node *,
  236. const struct avl_tree_node *))
  237. {
  238. struct avl_tree_node **cur_ptr = root_ptr, *cur = NULL;
  239. int res;
  240. while (*cur_ptr) {
  241. cur = *cur_ptr;
  242. res = (*cmp)(item, cur);
  243. if (res < 0)
  244. cur_ptr = &cur->left;
  245. else if (res > 0)
  246. cur_ptr = &cur->right;
  247. else
  248. return cur;
  249. }
  250. *cur_ptr = item;
  251. item->parent_balance = (uintptr_t)cur | 1;
  252. avl_tree_rebalance_after_insert(root_ptr, item);
  253. return NULL;
  254. }
  255. /* Removes an item from the specified AVL tree.
  256. * See implementation for details. */
  257. extern void
  258. avl_tree_remove(struct avl_tree_node **root_ptr, struct avl_tree_node *node);
  259. /* Nonrecursive AVL tree traversal functions */
  260. extern struct avl_tree_node *
  261. avl_tree_first_in_order(const struct avl_tree_node *root);
  262. extern struct avl_tree_node *
  263. avl_tree_last_in_order(const struct avl_tree_node *root);
  264. extern struct avl_tree_node *
  265. avl_tree_next_in_order(const struct avl_tree_node *node);
  266. extern struct avl_tree_node *
  267. avl_tree_prev_in_order(const struct avl_tree_node *node);
  268. extern struct avl_tree_node *
  269. avl_tree_first_in_postorder(const struct avl_tree_node *root);
  270. extern struct avl_tree_node *
  271. avl_tree_next_in_postorder(const struct avl_tree_node *prev,
  272. const struct avl_tree_node *prev_parent);
  273. /*
  274. * Iterate through the nodes in an AVL tree in sorted order.
  275. * You may not modify the tree during the iteration.
  276. *
  277. * @child_struct
  278. * Variable that will receive a pointer to each struct inserted into the
  279. * tree.
  280. * @root
  281. * Root of the AVL tree.
  282. * @struct_name
  283. * Type of *child_struct.
  284. * @struct_member
  285. * Member of @struct_name type that is the AVL tree node.
  286. *
  287. * Example:
  288. *
  289. * struct int_wrapper {
  290. * int data;
  291. * struct avl_tree_node index_node;
  292. * };
  293. *
  294. * void print_ints(struct avl_tree_node *root)
  295. * {
  296. * struct int_wrapper *i;
  297. *
  298. * avl_tree_for_each_in_order(i, root, struct int_wrapper, index_node)
  299. * printf("%d\n", i->data);
  300. * }
  301. */
  302. #define avl_tree_for_each_in_order(child_struct, root, \
  303. struct_name, struct_member) \
  304. for (struct avl_tree_node *_cur = \
  305. avl_tree_first_in_order(root); \
  306. _cur && ((child_struct) = \
  307. avl_tree_entry(_cur, struct_name, \
  308. struct_member), 1); \
  309. _cur = avl_tree_next_in_order(_cur))
  310. /*
  311. * Like avl_tree_for_each_in_order(), but uses the reverse order.
  312. */
  313. #define avl_tree_for_each_in_reverse_order(child_struct, root, \
  314. struct_name, struct_member) \
  315. for (struct avl_tree_node *_cur = \
  316. avl_tree_last_in_order(root); \
  317. _cur && ((child_struct) = \
  318. avl_tree_entry(_cur, struct_name, \
  319. struct_member), 1); \
  320. _cur = avl_tree_prev_in_order(_cur))
  321. /*
  322. * Like avl_tree_for_each_in_order(), but iterates through the nodes in
  323. * postorder, so the current node may be deleted or freed.
  324. */
  325. #define avl_tree_for_each_in_postorder(child_struct, root, \
  326. struct_name, struct_member) \
  327. for (struct avl_tree_node *_cur = \
  328. avl_tree_first_in_postorder(root), *_parent; \
  329. _cur && ((child_struct) = \
  330. avl_tree_entry(_cur, struct_name, \
  331. struct_member), 1) \
  332. && (_parent = avl_get_parent(_cur), 1); \
  333. _cur = avl_tree_next_in_postorder(_cur, _parent))
  334. #endif /* _AVL_TREE_H_ */