gnode.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 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. /**
  54. * GCopyFunc:
  55. * @src: A pointer to the data which should be copied
  56. * @data: Additional data
  57. *
  58. * A function of this signature is used to copy the node data
  59. * when doing a deep-copy of a tree.
  60. *
  61. * Returns: A pointer to the copy
  62. *
  63. * Since: 2.4
  64. */
  65. typedef gpointer (*GCopyFunc) (gconstpointer src,
  66. gpointer data);
  67. /* N-way tree implementation
  68. */
  69. struct _GNode
  70. {
  71. gpointer data;
  72. GNode *next;
  73. GNode *prev;
  74. GNode *parent;
  75. GNode *children;
  76. };
  77. /**
  78. * G_NODE_IS_ROOT:
  79. * @node: a #GNode
  80. *
  81. * Returns %TRUE if a #GNode is the root of a tree.
  82. *
  83. * Returns: %TRUE if the #GNode is the root of a tree
  84. * (i.e. it has no parent or siblings)
  85. */
  86. #define G_NODE_IS_ROOT(node) (((GNode*) (node))->parent == NULL && \
  87. ((GNode*) (node))->prev == NULL && \
  88. ((GNode*) (node))->next == NULL)
  89. /**
  90. * G_NODE_IS_LEAF:
  91. * @node: a #GNode
  92. *
  93. * Returns %TRUE if a #GNode is a leaf node.
  94. *
  95. * Returns: %TRUE if the #GNode is a leaf node
  96. * (i.e. it has no children)
  97. */
  98. #define G_NODE_IS_LEAF(node) (((GNode*) (node))->children == NULL)
  99. GLIB_AVAILABLE_IN_ALL
  100. GNode* g_node_new (gpointer data);
  101. GLIB_AVAILABLE_IN_ALL
  102. void g_node_destroy (GNode *root);
  103. GLIB_AVAILABLE_IN_ALL
  104. void g_node_unlink (GNode *node);
  105. GLIB_AVAILABLE_IN_ALL
  106. GNode* g_node_copy_deep (GNode *node,
  107. GCopyFunc copy_func,
  108. gpointer data);
  109. GLIB_AVAILABLE_IN_ALL
  110. GNode* g_node_copy (GNode *node);
  111. GLIB_AVAILABLE_IN_ALL
  112. GNode* g_node_insert (GNode *parent,
  113. gint position,
  114. GNode *node);
  115. GLIB_AVAILABLE_IN_ALL
  116. GNode* g_node_insert_before (GNode *parent,
  117. GNode *sibling,
  118. GNode *node);
  119. GLIB_AVAILABLE_IN_ALL
  120. GNode* g_node_insert_after (GNode *parent,
  121. GNode *sibling,
  122. GNode *node);
  123. GLIB_AVAILABLE_IN_ALL
  124. GNode* g_node_prepend (GNode *parent,
  125. GNode *node);
  126. GLIB_AVAILABLE_IN_ALL
  127. guint g_node_n_nodes (GNode *root,
  128. GTraverseFlags flags);
  129. GLIB_AVAILABLE_IN_ALL
  130. GNode* g_node_get_root (GNode *node);
  131. GLIB_AVAILABLE_IN_ALL
  132. gboolean g_node_is_ancestor (GNode *node,
  133. GNode *descendant);
  134. GLIB_AVAILABLE_IN_ALL
  135. guint g_node_depth (GNode *node);
  136. GLIB_AVAILABLE_IN_ALL
  137. GNode* g_node_find (GNode *root,
  138. GTraverseType order,
  139. GTraverseFlags flags,
  140. gpointer data);
  141. /* convenience macros */
  142. /**
  143. * g_node_append:
  144. * @parent: the #GNode to place the new #GNode under
  145. * @node: the #GNode to insert
  146. *
  147. * Inserts a #GNode as the last child of the given parent.
  148. *
  149. * Returns: the inserted #GNode
  150. */
  151. #define g_node_append(parent, node) \
  152. g_node_insert_before ((parent), NULL, (node))
  153. /**
  154. * g_node_insert_data:
  155. * @parent: the #GNode to place the new #GNode under
  156. * @position: the position to place the new #GNode at. If position is -1,
  157. * the new #GNode is inserted as the last child of @parent
  158. * @data: the data for the new #GNode
  159. *
  160. * Inserts a new #GNode at the given position.
  161. *
  162. * Returns: the new #GNode
  163. */
  164. #define g_node_insert_data(parent, position, data) \
  165. g_node_insert ((parent), (position), g_node_new (data))
  166. /**
  167. * g_node_insert_data_after:
  168. * @parent: the #GNode to place the new #GNode under
  169. * @sibling: the sibling #GNode to place the new #GNode after
  170. * @data: the data for the new #GNode
  171. *
  172. * Inserts a new #GNode after the given sibling.
  173. *
  174. * Returns: the new #GNode
  175. */
  176. #define g_node_insert_data_after(parent, sibling, data) \
  177. g_node_insert_after ((parent), (sibling), g_node_new (data))
  178. /**
  179. * g_node_insert_data_before:
  180. * @parent: the #GNode to place the new #GNode under
  181. * @sibling: the sibling #GNode to place the new #GNode before
  182. * @data: the data for the new #GNode
  183. *
  184. * Inserts a new #GNode before the given sibling.
  185. *
  186. * Returns: the new #GNode
  187. */
  188. #define g_node_insert_data_before(parent, sibling, data) \
  189. g_node_insert_before ((parent), (sibling), g_node_new (data))
  190. /**
  191. * g_node_prepend_data:
  192. * @parent: the #GNode to place the new #GNode under
  193. * @data: the data for the new #GNode
  194. *
  195. * Inserts a new #GNode as the first child of the given parent.
  196. *
  197. * Returns: the new #GNode
  198. */
  199. #define g_node_prepend_data(parent, data) \
  200. g_node_prepend ((parent), g_node_new (data))
  201. /**
  202. * g_node_append_data:
  203. * @parent: the #GNode to place the new #GNode under
  204. * @data: the data for the new #GNode
  205. *
  206. * Inserts a new #GNode as the last child of the given parent.
  207. *
  208. * Returns: the new #GNode
  209. */
  210. #define g_node_append_data(parent, data) \
  211. g_node_insert_before ((parent), NULL, g_node_new (data))
  212. /* traversal function, assumes that 'node' is root
  213. * (only traverses 'node' and its subtree).
  214. * this function is just a high level interface to
  215. * low level traversal functions, optimized for speed.
  216. */
  217. GLIB_AVAILABLE_IN_ALL
  218. void g_node_traverse (GNode *root,
  219. GTraverseType order,
  220. GTraverseFlags flags,
  221. gint max_depth,
  222. GNodeTraverseFunc func,
  223. gpointer data);
  224. /* return the maximum tree height starting with 'node', this is an expensive
  225. * operation, since we need to visit all nodes. this could be shortened by
  226. * adding 'guint height' to struct _GNode, but then again, this is not very
  227. * often needed, and would make g_node_insert() more time consuming.
  228. */
  229. GLIB_AVAILABLE_IN_ALL
  230. guint g_node_max_height (GNode *root);
  231. GLIB_AVAILABLE_IN_ALL
  232. void g_node_children_foreach (GNode *node,
  233. GTraverseFlags flags,
  234. GNodeForeachFunc func,
  235. gpointer data);
  236. GLIB_AVAILABLE_IN_ALL
  237. void g_node_reverse_children (GNode *node);
  238. GLIB_AVAILABLE_IN_ALL
  239. guint g_node_n_children (GNode *node);
  240. GLIB_AVAILABLE_IN_ALL
  241. GNode* g_node_nth_child (GNode *node,
  242. guint n);
  243. GLIB_AVAILABLE_IN_ALL
  244. GNode* g_node_last_child (GNode *node);
  245. GLIB_AVAILABLE_IN_ALL
  246. GNode* g_node_find_child (GNode *node,
  247. GTraverseFlags flags,
  248. gpointer data);
  249. GLIB_AVAILABLE_IN_ALL
  250. gint g_node_child_position (GNode *node,
  251. GNode *child);
  252. GLIB_AVAILABLE_IN_ALL
  253. gint g_node_child_index (GNode *node,
  254. gpointer data);
  255. GLIB_AVAILABLE_IN_ALL
  256. GNode* g_node_first_sibling (GNode *node);
  257. GLIB_AVAILABLE_IN_ALL
  258. GNode* g_node_last_sibling (GNode *node);
  259. /**
  260. * g_node_prev_sibling:
  261. * @node: a #GNode
  262. *
  263. * Gets the previous sibling of a #GNode.
  264. *
  265. * Returns: the previous sibling of @node, or %NULL if @node is the first
  266. * node or %NULL
  267. */
  268. #define g_node_prev_sibling(node) ((node) ? \
  269. ((GNode*) (node))->prev : NULL)
  270. /**
  271. * g_node_next_sibling:
  272. * @node: a #GNode
  273. *
  274. * Gets the next sibling of a #GNode.
  275. *
  276. * Returns: the next sibling of @node, or %NULL if @node is the last node
  277. * or %NULL
  278. */
  279. #define g_node_next_sibling(node) ((node) ? \
  280. ((GNode*) (node))->next : NULL)
  281. /**
  282. * g_node_first_child:
  283. * @node: a #GNode
  284. *
  285. * Gets the first child of a #GNode.
  286. *
  287. * Returns: the first child of @node, or %NULL if @node is %NULL
  288. * or has no children
  289. */
  290. #define g_node_first_child(node) ((node) ? \
  291. ((GNode*) (node))->children : NULL)
  292. G_END_DECLS
  293. #endif /* __G_NODE_H__ */