gnode.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /* GLIB - Library of useful routines for C programming
  2. * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /*
  18. * Modified by the GLib Team and others 1997-2000. See the AUTHORS
  19. * file for a list of people on the GLib Team. See the ChangeLog
  20. * files for a list of changes. These files are distributed with
  21. * GLib at ftp://ftp.gtk.org/pub/gtk/.
  22. */
  23. #ifndef __G_NODE_H__
  24. #define __G_NODE_H__
  25. #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  26. #error "Only <glib.h> can be included directly."
  27. #endif
  28. #include <glib/gmem.h>
  29. G_BEGIN_DECLS
  30. typedef struct _GNode GNode;
  31. /* Tree traverse flags */
  32. typedef enum
  33. {
  34. G_TRAVERSE_LEAVES = 1 << 0,
  35. G_TRAVERSE_NON_LEAVES = 1 << 1,
  36. G_TRAVERSE_ALL = G_TRAVERSE_LEAVES | G_TRAVERSE_NON_LEAVES,
  37. G_TRAVERSE_MASK = 0x03,
  38. G_TRAVERSE_LEAFS = G_TRAVERSE_LEAVES,
  39. G_TRAVERSE_NON_LEAFS = G_TRAVERSE_NON_LEAVES
  40. } GTraverseFlags;
  41. /* Tree traverse orders */
  42. typedef enum
  43. {
  44. G_IN_ORDER,
  45. G_PRE_ORDER,
  46. G_POST_ORDER,
  47. G_LEVEL_ORDER
  48. } GTraverseType;
  49. typedef gboolean (*GNodeTraverseFunc) (GNode *node,
  50. gpointer data);
  51. typedef void (*GNodeForeachFunc) (GNode *node,
  52. gpointer data);
  53. /* N-way tree implementation
  54. */
  55. struct _GNode
  56. {
  57. gpointer data;
  58. GNode *next;
  59. GNode *prev;
  60. GNode *parent;
  61. GNode *children;
  62. };
  63. /**
  64. * G_NODE_IS_ROOT:
  65. * @node: a #GNode
  66. *
  67. * Returns %TRUE if a #GNode is the root of a tree.
  68. *
  69. * Returns: %TRUE if the #GNode is the root of a tree
  70. * (i.e. it has no parent or siblings)
  71. */
  72. #define G_NODE_IS_ROOT(node) (((GNode*) (node))->parent == NULL && \
  73. ((GNode*) (node))->prev == NULL && \
  74. ((GNode*) (node))->next == NULL)
  75. /**
  76. * G_NODE_IS_LEAF:
  77. * @node: a #GNode
  78. *
  79. * Returns %TRUE if a #GNode is a leaf node.
  80. *
  81. * Returns: %TRUE if the #GNode is a leaf node
  82. * (i.e. it has no children)
  83. */
  84. #define G_NODE_IS_LEAF(node) (((GNode*) (node))->children == NULL)
  85. GLIB_AVAILABLE_IN_ALL
  86. GNode* g_node_new (gpointer data);
  87. GLIB_AVAILABLE_IN_ALL
  88. void g_node_destroy (GNode *root);
  89. GLIB_AVAILABLE_IN_ALL
  90. void g_node_unlink (GNode *node);
  91. GLIB_AVAILABLE_IN_ALL
  92. GNode* g_node_copy_deep (GNode *node,
  93. GCopyFunc copy_func,
  94. gpointer data);
  95. GLIB_AVAILABLE_IN_ALL
  96. GNode* g_node_copy (GNode *node);
  97. GLIB_AVAILABLE_IN_ALL
  98. GNode* g_node_insert (GNode *parent,
  99. gint position,
  100. GNode *node);
  101. GLIB_AVAILABLE_IN_ALL
  102. GNode* g_node_insert_before (GNode *parent,
  103. GNode *sibling,
  104. GNode *node);
  105. GLIB_AVAILABLE_IN_ALL
  106. GNode* g_node_insert_after (GNode *parent,
  107. GNode *sibling,
  108. GNode *node);
  109. GLIB_AVAILABLE_IN_ALL
  110. GNode* g_node_prepend (GNode *parent,
  111. GNode *node);
  112. GLIB_AVAILABLE_IN_ALL
  113. guint g_node_n_nodes (GNode *root,
  114. GTraverseFlags flags);
  115. GLIB_AVAILABLE_IN_ALL
  116. GNode* g_node_get_root (GNode *node);
  117. GLIB_AVAILABLE_IN_ALL
  118. gboolean g_node_is_ancestor (GNode *node,
  119. GNode *descendant);
  120. GLIB_AVAILABLE_IN_ALL
  121. guint g_node_depth (GNode *node);
  122. GLIB_AVAILABLE_IN_ALL
  123. GNode* g_node_find (GNode *root,
  124. GTraverseType order,
  125. GTraverseFlags flags,
  126. gpointer data);
  127. /* convenience macros */
  128. /**
  129. * g_node_append:
  130. * @parent: the #GNode to place the new #GNode under
  131. * @node: the #GNode to insert
  132. *
  133. * Inserts a #GNode as the last child of the given parent.
  134. *
  135. * Returns: the inserted #GNode
  136. */
  137. #define g_node_append(parent, node) \
  138. g_node_insert_before ((parent), NULL, (node))
  139. /**
  140. * g_node_insert_data:
  141. * @parent: the #GNode to place the new #GNode under
  142. * @position: the position to place the new #GNode at. If position is -1,
  143. * the new #GNode is inserted as the last child of @parent
  144. * @data: the data for the new #GNode
  145. *
  146. * Inserts a new #GNode at the given position.
  147. *
  148. * Returns: the new #GNode
  149. */
  150. #define g_node_insert_data(parent, position, data) \
  151. g_node_insert ((parent), (position), g_node_new (data))
  152. /**
  153. * g_node_insert_data_after:
  154. * @parent: the #GNode to place the new #GNode under
  155. * @sibling: the sibling #GNode to place the new #GNode after
  156. * @data: the data for the new #GNode
  157. *
  158. * Inserts a new #GNode after the given sibling.
  159. *
  160. * Returns: the new #GNode
  161. */
  162. #define g_node_insert_data_after(parent, sibling, data) \
  163. g_node_insert_after ((parent), (sibling), g_node_new (data))
  164. /**
  165. * g_node_insert_data_before:
  166. * @parent: the #GNode to place the new #GNode under
  167. * @sibling: the sibling #GNode to place the new #GNode before
  168. * @data: the data for the new #GNode
  169. *
  170. * Inserts a new #GNode before the given sibling.
  171. *
  172. * Returns: the new #GNode
  173. */
  174. #define g_node_insert_data_before(parent, sibling, data) \
  175. g_node_insert_before ((parent), (sibling), g_node_new (data))
  176. /**
  177. * g_node_prepend_data:
  178. * @parent: the #GNode to place the new #GNode under
  179. * @data: the data for the new #GNode
  180. *
  181. * Inserts a new #GNode as the first child of the given parent.
  182. *
  183. * Returns: the new #GNode
  184. */
  185. #define g_node_prepend_data(parent, data) \
  186. g_node_prepend ((parent), g_node_new (data))
  187. /**
  188. * g_node_append_data:
  189. * @parent: the #GNode to place the new #GNode under
  190. * @data: the data for the new #GNode
  191. *
  192. * Inserts a new #GNode as the last child of the given parent.
  193. *
  194. * Returns: the new #GNode
  195. */
  196. #define g_node_append_data(parent, data) \
  197. g_node_insert_before ((parent), NULL, g_node_new (data))
  198. /* traversal function, assumes that 'node' is root
  199. * (only traverses 'node' and its subtree).
  200. * this function is just a high level interface to
  201. * low level traversal functions, optimized for speed.
  202. */
  203. GLIB_AVAILABLE_IN_ALL
  204. void g_node_traverse (GNode *root,
  205. GTraverseType order,
  206. GTraverseFlags flags,
  207. gint max_depth,
  208. GNodeTraverseFunc func,
  209. gpointer data);
  210. /* return the maximum tree height starting with 'node', this is an expensive
  211. * operation, since we need to visit all nodes. this could be shortened by
  212. * adding 'guint height' to struct _GNode, but then again, this is not very
  213. * often needed, and would make g_node_insert() more time consuming.
  214. */
  215. GLIB_AVAILABLE_IN_ALL
  216. guint g_node_max_height (GNode *root);
  217. GLIB_AVAILABLE_IN_ALL
  218. void g_node_children_foreach (GNode *node,
  219. GTraverseFlags flags,
  220. GNodeForeachFunc func,
  221. gpointer data);
  222. GLIB_AVAILABLE_IN_ALL
  223. void g_node_reverse_children (GNode *node);
  224. GLIB_AVAILABLE_IN_ALL
  225. guint g_node_n_children (GNode *node);
  226. GLIB_AVAILABLE_IN_ALL
  227. GNode* g_node_nth_child (GNode *node,
  228. guint n);
  229. GLIB_AVAILABLE_IN_ALL
  230. GNode* g_node_last_child (GNode *node);
  231. GLIB_AVAILABLE_IN_ALL
  232. GNode* g_node_find_child (GNode *node,
  233. GTraverseFlags flags,
  234. gpointer data);
  235. GLIB_AVAILABLE_IN_ALL
  236. gint g_node_child_position (GNode *node,
  237. GNode *child);
  238. GLIB_AVAILABLE_IN_ALL
  239. gint g_node_child_index (GNode *node,
  240. gpointer data);
  241. GLIB_AVAILABLE_IN_ALL
  242. GNode* g_node_first_sibling (GNode *node);
  243. GLIB_AVAILABLE_IN_ALL
  244. GNode* g_node_last_sibling (GNode *node);
  245. /**
  246. * g_node_prev_sibling:
  247. * @node: a #GNode
  248. *
  249. * Gets the previous sibling of a #GNode.
  250. *
  251. * Returns: the previous sibling of @node, or %NULL if @node is the first
  252. * node or %NULL
  253. */
  254. #define g_node_prev_sibling(node) ((node) ? \
  255. ((GNode*) (node))->prev : NULL)
  256. /**
  257. * g_node_next_sibling:
  258. * @node: a #GNode
  259. *
  260. * Gets the next sibling of a #GNode.
  261. *
  262. * Returns: the next sibling of @node, or %NULL if @node is the last node
  263. * or %NULL
  264. */
  265. #define g_node_next_sibling(node) ((node) ? \
  266. ((GNode*) (node))->next : NULL)
  267. /**
  268. * g_node_first_child:
  269. * @node: a #GNode
  270. *
  271. * Gets the first child of a #GNode.
  272. *
  273. * Returns: the first child of @node, or %NULL if @node is %NULL
  274. * or has no children
  275. */
  276. #define g_node_first_child(node) ((node) ? \
  277. ((GNode*) (node))->children : NULL)
  278. G_END_DECLS
  279. #endif /* __G_NODE_H__ */