A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

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