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.
 
 
 
 
 
 

291 line
8.2 KiB

  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, write to the
  16. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. * Boston, MA 02111-1307, USA.
  18. */
  19. /*
  20. * Modified by the GLib Team and others 1997-2000. See the AUTHORS
  21. * file for a list of people on the GLib Team. See the ChangeLog
  22. * files for a list of changes. These files are distributed with
  23. * GLib at ftp://ftp.gtk.org/pub/gtk/.
  24. */
  25. #if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  26. #error "Only <glib.h> can be included directly."
  27. #endif
  28. #ifndef __G_NODE_H__
  29. #define __G_NODE_H__
  30. #include <glib/gmem.h>
  31. G_BEGIN_DECLS
  32. typedef struct _GNode GNode;
  33. /* Tree traverse flags */
  34. typedef enum
  35. {
  36. G_TRAVERSE_LEAVES = 1 << 0,
  37. G_TRAVERSE_NON_LEAVES = 1 << 1,
  38. G_TRAVERSE_ALL = G_TRAVERSE_LEAVES | G_TRAVERSE_NON_LEAVES,
  39. G_TRAVERSE_MASK = 0x03,
  40. G_TRAVERSE_LEAFS = G_TRAVERSE_LEAVES,
  41. G_TRAVERSE_NON_LEAFS = G_TRAVERSE_NON_LEAVES
  42. } GTraverseFlags;
  43. /* Tree traverse orders */
  44. typedef enum
  45. {
  46. G_IN_ORDER,
  47. G_PRE_ORDER,
  48. G_POST_ORDER,
  49. G_LEVEL_ORDER
  50. } GTraverseType;
  51. typedef gboolean (*GNodeTraverseFunc) (GNode *node,
  52. gpointer data);
  53. typedef void (*GNodeForeachFunc) (GNode *node,
  54. gpointer data);
  55. /**
  56. * GCopyFunc:
  57. * @src: A pointer to the data which should be copied
  58. * @data: Additional data
  59. *
  60. * A function of this signature is used to copy the node data
  61. * when doing a deep-copy of a tree.
  62. *
  63. * Returns: A pointer to the copy
  64. *
  65. * Since: 2.4
  66. */
  67. typedef gpointer (*GCopyFunc) (gconstpointer src,
  68. gpointer data);
  69. /* N-way tree implementation
  70. */
  71. struct _GNode
  72. {
  73. gpointer data;
  74. GNode *next;
  75. GNode *prev;
  76. GNode *parent;
  77. GNode *children;
  78. };
  79. /**
  80. * G_NODE_IS_ROOT:
  81. * @node: a #GNode
  82. *
  83. * Returns %TRUE if a #GNode is the root of a tree.
  84. *
  85. * Returns: %TRUE if the #GNode is the root of a tree
  86. * (i.e. it has no parent or siblings)
  87. */
  88. #define G_NODE_IS_ROOT(node) (((GNode*) (node))->parent == NULL && \
  89. ((GNode*) (node))->prev == NULL && \
  90. ((GNode*) (node))->next == NULL)
  91. /**
  92. * G_NODE_IS_LEAF:
  93. * @node: a #GNode
  94. *
  95. * Returns %TRUE if a #GNode is a leaf node.
  96. *
  97. * Returns: %TRUE if the #GNode is a leaf node
  98. * (i.e. it has no children)
  99. */
  100. #define G_NODE_IS_LEAF(node) (((GNode*) (node))->children == NULL)
  101. GNode* g_node_new (gpointer data);
  102. void g_node_destroy (GNode *root);
  103. void g_node_unlink (GNode *node);
  104. GNode* g_node_copy_deep (GNode *node,
  105. GCopyFunc copy_func,
  106. gpointer data);
  107. GNode* g_node_copy (GNode *node);
  108. GNode* g_node_insert (GNode *parent,
  109. gint position,
  110. GNode *node);
  111. GNode* g_node_insert_before (GNode *parent,
  112. GNode *sibling,
  113. GNode *node);
  114. GNode* g_node_insert_after (GNode *parent,
  115. GNode *sibling,
  116. GNode *node);
  117. GNode* g_node_prepend (GNode *parent,
  118. GNode *node);
  119. guint g_node_n_nodes (GNode *root,
  120. GTraverseFlags flags);
  121. GNode* g_node_get_root (GNode *node);
  122. gboolean g_node_is_ancestor (GNode *node,
  123. GNode *descendant);
  124. guint g_node_depth (GNode *node);
  125. GNode* g_node_find (GNode *root,
  126. GTraverseType order,
  127. GTraverseFlags flags,
  128. gpointer data);
  129. /* convenience macros */
  130. /**
  131. * g_node_append:
  132. * @parent: the #GNode to place the new #GNode under
  133. * @node: the #GNode to insert
  134. *
  135. * Inserts a #GNode as the last child of the given parent.
  136. *
  137. * Returns: the inserted #GNode
  138. */
  139. #define g_node_append(parent, node) \
  140. g_node_insert_before ((parent), NULL, (node))
  141. /**
  142. * g_node_insert_data:
  143. * @parent: the #GNode to place the new #GNode under
  144. * @position: the position to place the new #GNode at. If position is -1,
  145. * the new #GNode is inserted as the last child of @parent
  146. * @data: the data for the new #GNode
  147. *
  148. * Inserts a new #GNode at the given position.
  149. *
  150. * Returns: the new #GNode
  151. */
  152. #define g_node_insert_data(parent, position, data) \
  153. g_node_insert ((parent), (position), g_node_new (data))
  154. /**
  155. * g_node_insert_data_before:
  156. * @parent: the #GNode to place the new #GNode under
  157. * @sibling: the sibling #GNode to place the new #GNode before
  158. * @data: the data for the new #GNode
  159. *
  160. * Inserts a new #GNode before the given sibling.
  161. *
  162. * Returns: the new #GNode
  163. */
  164. #define g_node_insert_data_before(parent, sibling, data) \
  165. g_node_insert_before ((parent), (sibling), g_node_new (data))
  166. /**
  167. * g_node_prepend_data:
  168. * @parent: the #GNode to place the new #GNode under
  169. * @data: the data for the new #GNode
  170. *
  171. * Inserts a new #GNode as the first child of the given parent.
  172. *
  173. * Returns: the new #GNode
  174. */
  175. #define g_node_prepend_data(parent, data) \
  176. g_node_prepend ((parent), g_node_new (data))
  177. /**
  178. * g_node_append_data:
  179. * @parent: the #GNode to place the new #GNode under
  180. * @data: the data for the new #GNode
  181. *
  182. * Inserts a new #GNode as the last child of the given parent.
  183. *
  184. * Returns: the new #GNode
  185. */
  186. #define g_node_append_data(parent, data) \
  187. g_node_insert_before ((parent), NULL, g_node_new (data))
  188. /* traversal function, assumes that `node' is root
  189. * (only traverses `node' and its subtree).
  190. * this function is just a high level interface to
  191. * low level traversal functions, optimized for speed.
  192. */
  193. void g_node_traverse (GNode *root,
  194. GTraverseType order,
  195. GTraverseFlags flags,
  196. gint max_depth,
  197. GNodeTraverseFunc func,
  198. gpointer data);
  199. /* return the maximum tree height starting with `node', this is an expensive
  200. * operation, since we need to visit all nodes. this could be shortened by
  201. * adding `guint height' to struct _GNode, but then again, this is not very
  202. * often needed, and would make g_node_insert() more time consuming.
  203. */
  204. guint g_node_max_height (GNode *root);
  205. void g_node_children_foreach (GNode *node,
  206. GTraverseFlags flags,
  207. GNodeForeachFunc func,
  208. gpointer data);
  209. void g_node_reverse_children (GNode *node);
  210. guint g_node_n_children (GNode *node);
  211. GNode* g_node_nth_child (GNode *node,
  212. guint n);
  213. GNode* g_node_last_child (GNode *node);
  214. GNode* g_node_find_child (GNode *node,
  215. GTraverseFlags flags,
  216. gpointer data);
  217. gint g_node_child_position (GNode *node,
  218. GNode *child);
  219. gint g_node_child_index (GNode *node,
  220. gpointer data);
  221. GNode* g_node_first_sibling (GNode *node);
  222. GNode* g_node_last_sibling (GNode *node);
  223. /**
  224. * g_node_prev_sibling:
  225. * @node: a #GNode
  226. *
  227. * Gets the previous sibling of a #GNode.
  228. *
  229. * Returns: the previous sibling of @node, or %NULL if @node is the first
  230. * node or %NULL
  231. */
  232. #define g_node_prev_sibling(node) ((node) ? \
  233. ((GNode*) (node))->prev : NULL)
  234. /**
  235. * g_node_next_sibling:
  236. * @node: a #GNode
  237. *
  238. * Gets the next sibling of a #GNode.
  239. *
  240. * Returns: the next sibling of @node, or %NULL if @node is the last node
  241. * or %NULL
  242. */
  243. #define g_node_next_sibling(node) ((node) ? \
  244. ((GNode*) (node))->next : NULL)
  245. /**
  246. * g_node_first_child:
  247. * @node: a #GNode
  248. *
  249. * Gets the first child of a #GNode.
  250. *
  251. * Returns: the first child of @node, or %NULL if @node is %NULL
  252. * or has no children
  253. */
  254. #define g_node_first_child(node) ((node) ? \
  255. ((GNode*) (node))->children : NULL)
  256. #ifndef G_DISABLE_DEPRECATED
  257. void g_node_push_allocator (gpointer dummy);
  258. void g_node_pop_allocator (void);
  259. #endif
  260. G_END_DECLS
  261. #endif /* __G_NODE_H__ */