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