Commit 5272119c authored by Tim Janik's avatar Tim Janik Committed by Tim Janik

added a GNode test.

Fri Jul 31 22:17:05 1998  Tim Janik  <timj@gtk.org>

        * testglib.c (g_node_test): added a GNode test.

Fri Jul 31 09:08:16 1998  Tim Janik  <timj@gtk.org>

        * Makefile.am: compile gnode.c.

        * glib.h:
        * gnode.c: added implementation of n-way trees.

        * gtree.c (g_tree_traverse): added a warning to the switch() statement
        which says that G_LEVEL_ORDER is not implemented.
parent e502a234
Fri Jul 31 22:17:05 1998 Tim Janik <timj@gtk.org>
* testglib.c (g_node_test): added a GNode test.
Fri Jul 31 09:08:16 1998 Tim Janik <timj@gtk.org>
* Makefile.am: compile gnode.c.
* glib.h:
* gnode.c: added implementation of n-way trees.
* gtree.c (g_tree_traverse): added a warning to the switch() statement
which says that G_LEVEL_ORDER is not implemented.
Mon Jul 27 00:17:30 CDT 1998 Shawn T. Amundson <amundson@gtk.org>
* Released GLib 1.1.0
......
Fri Jul 31 22:17:05 1998 Tim Janik <timj@gtk.org>
* testglib.c (g_node_test): added a GNode test.
Fri Jul 31 09:08:16 1998 Tim Janik <timj@gtk.org>
* Makefile.am: compile gnode.c.
* glib.h:
* gnode.c: added implementation of n-way trees.
* gtree.c (g_tree_traverse): added a warning to the switch() statement
which says that G_LEVEL_ORDER is not implemented.
Mon Jul 27 00:17:30 CDT 1998 Shawn T. Amundson <amundson@gtk.org>
* Released GLib 1.1.0
......
Fri Jul 31 22:17:05 1998 Tim Janik <timj@gtk.org>
* testglib.c (g_node_test): added a GNode test.
Fri Jul 31 09:08:16 1998 Tim Janik <timj@gtk.org>
* Makefile.am: compile gnode.c.
* glib.h:
* gnode.c: added implementation of n-way trees.
* gtree.c (g_tree_traverse): added a warning to the switch() statement
which says that G_LEVEL_ORDER is not implemented.
Mon Jul 27 00:17:30 CDT 1998 Shawn T. Amundson <amundson@gtk.org>
* Released GLib 1.1.0
......
Fri Jul 31 22:17:05 1998 Tim Janik <timj@gtk.org>
* testglib.c (g_node_test): added a GNode test.
Fri Jul 31 09:08:16 1998 Tim Janik <timj@gtk.org>
* Makefile.am: compile gnode.c.
* glib.h:
* gnode.c: added implementation of n-way trees.
* gtree.c (g_tree_traverse): added a warning to the switch() statement
which says that G_LEVEL_ORDER is not implemented.
Mon Jul 27 00:17:30 CDT 1998 Shawn T. Amundson <amundson@gtk.org>
* Released GLib 1.1.0
......
Fri Jul 31 22:17:05 1998 Tim Janik <timj@gtk.org>
* testglib.c (g_node_test): added a GNode test.
Fri Jul 31 09:08:16 1998 Tim Janik <timj@gtk.org>
* Makefile.am: compile gnode.c.
* glib.h:
* gnode.c: added implementation of n-way trees.
* gtree.c (g_tree_traverse): added a warning to the switch() statement
which says that G_LEVEL_ORDER is not implemented.
Mon Jul 27 00:17:30 CDT 1998 Shawn T. Amundson <amundson@gtk.org>
* Released GLib 1.1.0
......
Fri Jul 31 22:17:05 1998 Tim Janik <timj@gtk.org>
* testglib.c (g_node_test): added a GNode test.
Fri Jul 31 09:08:16 1998 Tim Janik <timj@gtk.org>
* Makefile.am: compile gnode.c.
* glib.h:
* gnode.c: added implementation of n-way trees.
* gtree.c (g_tree_traverse): added a warning to the switch() statement
which says that G_LEVEL_ORDER is not implemented.
Mon Jul 27 00:17:30 CDT 1998 Shawn T. Amundson <amundson@gtk.org>
* Released GLib 1.1.0
......
Fri Jul 31 22:17:05 1998 Tim Janik <timj@gtk.org>
* testglib.c (g_node_test): added a GNode test.
Fri Jul 31 09:08:16 1998 Tim Janik <timj@gtk.org>
* Makefile.am: compile gnode.c.
* glib.h:
* gnode.c: added implementation of n-way trees.
* gtree.c (g_tree_traverse): added a warning to the switch() statement
which says that G_LEVEL_ORDER is not implemented.
Mon Jul 27 00:17:30 CDT 1998 Shawn T. Amundson <amundson@gtk.org>
* Released GLib 1.1.0
......
Fri Jul 31 22:17:05 1998 Tim Janik <timj@gtk.org>
* testglib.c (g_node_test): added a GNode test.
Fri Jul 31 09:08:16 1998 Tim Janik <timj@gtk.org>
* Makefile.am: compile gnode.c.
* glib.h:
* gnode.c: added implementation of n-way trees.
* gtree.c (g_tree_traverse): added a warning to the switch() statement
which says that G_LEVEL_ORDER is not implemented.
Mon Jul 27 00:17:30 CDT 1998 Shawn T. Amundson <amundson@gtk.org>
* Released GLib 1.1.0
......
......@@ -20,6 +20,7 @@ libglib_1_1_la_SOURCES = \
glist.c \
gmem.c \
gmessages.c \
gnode.c \
gprimes.c \
gslist.c \
gtimer.c \
......
......@@ -475,6 +475,7 @@ typedef union _GValue GValue;
typedef struct _GCompletion GCompletion;
typedef struct _GRelation GRelation;
typedef struct _GTuples GTuples;
typedef struct _GNode GNode;
typedef void (*GFunc) (gpointer data,
......@@ -488,6 +489,10 @@ typedef void (*GCacheDestroyFunc) (gpointer value);
typedef gint (*GTraverseFunc) (gpointer key,
gpointer value,
gpointer data);
typedef gboolean (*GNodeTraverseFunc) (GNode *node,
gpointer data);
typedef void (*GNodeForeachFunc) (GNode *node,
gpointer data);
typedef gint (*GSearchFunc) (gpointer key,
gpointer data);
typedef void (*GErrorFunc) (gchar *str);
......@@ -562,9 +567,18 @@ typedef enum
{
G_IN_ORDER,
G_PRE_ORDER,
G_POST_ORDER
G_POST_ORDER,
G_LEVEL_ORDER
} GTraverseType;
typedef enum
{
G_TRAVERSE_LEAFS = 1 << 0,
G_TRAVERSE_NON_LEAFS = 1 << 1,
G_TRAVERSE_ALL = G_TRAVERSE_LEAFS | G_TRAVERSE_NON_LEAFS,
G_TRAVERSE_MASK = 0x03
} GTraverseFlags;
/* Doubly linked lists
*/
GList* g_list_alloc (void);
......@@ -707,7 +721,7 @@ void g_cache_value_foreach (GCache *cache,
gpointer user_data);
/* Trees
/* Balanced binary trees
*/
GTree* g_tree_new (GCompareFunc key_compare_func);
void g_tree_destroy (GTree *tree);
......@@ -729,6 +743,99 @@ gint g_tree_height (GTree *tree);
gint g_tree_nnodes (GTree *tree);
/* N-way tree implementation
*/
struct _GNode
{
GNode *prev;
GNode *next;
GNode *parent;
GNode *children;
gpointer data;
};
#define G_NODE_IS_ROOT(node) (((GNode*) (node))->parent == NULL && \
((GNode*) (node))->prev == NULL && \
((GNode*) (node))->next == NULL)
#define G_NODE_IS_LEAF(node) (((GNode*) (node))->children == NULL)
GNode* g_node_new (gpointer data);
void g_node_destroy (GNode *root);
void g_node_unlink (GNode *node);
void g_node_insert (GNode *parent,
gint position,
GNode *node);
void g_node_insert_before (GNode *parent,
GNode *sibling,
GNode *node);
void g_node_prepend (GNode *parent,
GNode *node);
guint g_node_n_nodes (GNode *root,
GTraverseFlags flags);
GNode* g_node_get_root (GNode *node);
gboolean g_node_is_ancestor (GNode *node,
GNode *descendant);
guint g_node_depth (GNode *node);
GNode* g_node_find (GNode *root,
GTraverseType order,
GTraverseFlags flags,
gpointer data);
/* traversal function, assumes that `node' is root
* (only traverses `node' and its subtree).
* this function is just a high level interface to
* low level traversal functions, optimized for speed.
*/
void g_node_traverse (GNode *root,
GTraverseType order,
GTraverseFlags flags,
gint max_depth,
GNodeTraverseFunc func,
gpointer data);
/* return the maximum tree height starting with `node', this is an expensive
* operation, since we need to visit all nodes. this could be shortened by
* adding `guint height' to struct _GNode, but then again, this is not very
* often needed, and would make g_node_insert() more time consuming.
*/
guint g_node_max_height (GNode *root);
void g_node_children_foreach (GNode *node,
GTraverseFlags flags,
GNodeForeachFunc func,
gpointer data);
void g_node_reverse_children (GNode *node);
guint g_node_n_children (GNode *node);
GNode* g_node_nth_child (GNode *node,
guint n);
GNode* g_node_last_child (GNode *node);
GNode* g_node_find_child (GNode *node,
GTraverseFlags flags,
gpointer data);
gint g_node_child_position (GNode *node,
GNode *child);
gint g_node_child_index (GNode *node,
gpointer data);
GNode* g_node_first_sibling (GNode *node);
GNode* g_node_last_sibling (GNode *node);
#define g_node_prev_sibling(node) ((node) ? \
((GNode*) (node))->prev : NULL)
#define g_node_next_sibling(node) ((node) ? \
((GNode*) (node))->next : NULL)
#define g_node_first_child(node) ((node) ? \
((GNode*) (node))->children : NULL)
#define g_node_append(parent, node) G_STMT_START { \
g_node_insert_before ((parent), \
NULL, \
(node)); \
} G_STMT_END
/* Memory
*/
......
......@@ -20,6 +20,7 @@ libglib_1_1_la_SOURCES = \
glist.c \
gmem.c \
gmessages.c \
gnode.c \
gprimes.c \
gslist.c \
gtimer.c \
......
......@@ -475,6 +475,7 @@ typedef union _GValue GValue;
typedef struct _GCompletion GCompletion;
typedef struct _GRelation GRelation;
typedef struct _GTuples GTuples;
typedef struct _GNode GNode;
typedef void (*GFunc) (gpointer data,
......@@ -488,6 +489,10 @@ typedef void (*GCacheDestroyFunc) (gpointer value);
typedef gint (*GTraverseFunc) (gpointer key,
gpointer value,
gpointer data);
typedef gboolean (*GNodeTraverseFunc) (GNode *node,
gpointer data);
typedef void (*GNodeForeachFunc) (GNode *node,
gpointer data);
typedef gint (*GSearchFunc) (gpointer key,
gpointer data);
typedef void (*GErrorFunc) (gchar *str);
......@@ -562,9 +567,18 @@ typedef enum
{
G_IN_ORDER,
G_PRE_ORDER,
G_POST_ORDER
G_POST_ORDER,
G_LEVEL_ORDER
} GTraverseType;
typedef enum
{
G_TRAVERSE_LEAFS = 1 << 0,
G_TRAVERSE_NON_LEAFS = 1 << 1,
G_TRAVERSE_ALL = G_TRAVERSE_LEAFS | G_TRAVERSE_NON_LEAFS,
G_TRAVERSE_MASK = 0x03
} GTraverseFlags;
/* Doubly linked lists
*/
GList* g_list_alloc (void);
......@@ -707,7 +721,7 @@ void g_cache_value_foreach (GCache *cache,
gpointer user_data);
/* Trees
/* Balanced binary trees
*/
GTree* g_tree_new (GCompareFunc key_compare_func);
void g_tree_destroy (GTree *tree);
......@@ -729,6 +743,99 @@ gint g_tree_height (GTree *tree);
gint g_tree_nnodes (GTree *tree);
/* N-way tree implementation
*/
struct _GNode
{
GNode *prev;
GNode *next;
GNode *parent;
GNode *children;
gpointer data;
};
#define G_NODE_IS_ROOT(node) (((GNode*) (node))->parent == NULL && \
((GNode*) (node))->prev == NULL && \
((GNode*) (node))->next == NULL)
#define G_NODE_IS_LEAF(node) (((GNode*) (node))->children == NULL)
GNode* g_node_new (gpointer data);
void g_node_destroy (GNode *root);
void g_node_unlink (GNode *node);
void g_node_insert (GNode *parent,
gint position,
GNode *node);
void g_node_insert_before (GNode *parent,
GNode *sibling,
GNode *node);
void g_node_prepend (GNode *parent,
GNode *node);
guint g_node_n_nodes (GNode *root,
GTraverseFlags flags);
GNode* g_node_get_root (GNode *node);
gboolean g_node_is_ancestor (GNode *node,
GNode *descendant);
guint g_node_depth (GNode *node);
GNode* g_node_find (GNode *root,
GTraverseType order,
GTraverseFlags flags,
gpointer data);
/* traversal function, assumes that `node' is root
* (only traverses `node' and its subtree).
* this function is just a high level interface to
* low level traversal functions, optimized for speed.
*/
void g_node_traverse (GNode *root,
GTraverseType order,
GTraverseFlags flags,
gint max_depth,
GNodeTraverseFunc func,
gpointer data);
/* return the maximum tree height starting with `node', this is an expensive
* operation, since we need to visit all nodes. this could be shortened by
* adding `guint height' to struct _GNode, but then again, this is not very
* often needed, and would make g_node_insert() more time consuming.
*/
guint g_node_max_height (GNode *root);
void g_node_children_foreach (GNode *node,
GTraverseFlags flags,
GNodeForeachFunc func,
gpointer data);
void g_node_reverse_children (GNode *node);
guint g_node_n_children (GNode *node);
GNode* g_node_nth_child (GNode *node,
guint n);
GNode* g_node_last_child (GNode *node);
GNode* g_node_find_child (GNode *node,
GTraverseFlags flags,
gpointer data);
gint g_node_child_position (GNode *node,
GNode *child);
gint g_node_child_index (GNode *node,
gpointer data);
GNode* g_node_first_sibling (GNode *node);
GNode* g_node_last_sibling (GNode *node);
#define g_node_prev_sibling(node) ((node) ? \
((GNode*) (node))->prev : NULL)
#define g_node_next_sibling(node) ((node) ? \
((GNode*) (node))->next : NULL)
#define g_node_first_child(node) ((node) ? \
((GNode*) (node))->children : NULL)
#define g_node_append(parent, node) G_STMT_START { \
g_node_insert_before ((parent), \
NULL, \
(node)); \
} G_STMT_END
/* Memory
*/
......
/* GLIB - Library of useful routines for C programming
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*
* GNode: N-way tree implementation.
* Copyright (C) 1998 Tim Janik
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "glib.h"
#define KEEP_NODES (1024)
/* --- variables --- */
static GMemChunk *g_tree_node_chunk = NULL;
static GNode *free_nodes = NULL;
static guint n_free_nodes = 0;
/* --- functions --- */
GNode*
g_node_new (gpointer data)
{
register GNode *node;
if (!g_tree_node_chunk)
g_tree_node_chunk = g_mem_chunk_create (GNode, 1024, G_ALLOC_AND_FREE);
if (n_free_nodes)
{
node = free_nodes;
free_nodes = free_nodes->next;
n_free_nodes--;
}
else
node = g_chunk_new (GNode, g_tree_node_chunk);
node->prev = NULL;
node->next = NULL;
node->parent = NULL;
node->children = NULL;
node->data = data;
return node;
}
static void
g_node_free (GNode *parent)
{
GNode *node;
node = parent->children;
while (node)
{
register GNode *free_node;
free_node = node;
node = free_node->next;
g_node_free (free_node);
}
if (n_free_nodes < KEEP_NODES)
{
parent->next = free_nodes;
free_nodes = parent;
n_free_nodes++;
}
else
g_chunk_free (parent, g_tree_node_chunk);
}
void
g_node_destroy (GNode *root)
{
g_return_if_fail (root != NULL);
if (!G_NODE_IS_ROOT (root))
g_node_unlink (root);
g_node_free (root);
}
void
g_node_unlink (GNode *node)
{
g_return_if_fail (node != NULL);
if (node->prev)
node->prev->next = node->next;
else if (node->parent)
node->parent->children = node->next;
node->parent = NULL;
if (node->next)
{
node->next->prev = node->prev;
node->next = NULL;
}
node->prev = NULL;
}
void
g_node_insert (GNode *parent,
gint position,
GNode *node)
{
g_return_if_fail (parent != NULL);
g_return_if_fail (node != NULL);
g_return_if_fail (G_NODE_IS_ROOT (node));
if (position > 0)
g_node_insert_before (parent,
g_node_nth_child (parent, position),
node);
else if (position == 0)
g_node_prepend (parent, node);
else if (position < 0)
g_node_append (parent, node);
}
void
g_node_insert_before (GNode *parent,
GNode *sibling,
GNode *node)
{
g_return_if_fail (parent != NULL);
g_return_if_fail (node != NULL);
g_return_if_fail (G_NODE_IS_ROOT (node));
if (sibling)
g_return_if_fail (sibling->parent == parent);
node->parent = parent;
if (sibling)
{
if (sibling->prev)
{
node->prev = sibling->prev;
node->prev->next = node;
node->next = sibling;
sibling->prev = node;
}
else
{
node->parent->children = node;
node->next = sibling;
sibling->prev = node;
}
}
else
{
if (parent->children)
{
sibling = parent->children;
while (sibling->next)
sibling = sibling->next;
node->prev = sibling;
sibling->next = node;
}
else
node->parent->children = node;
}
}
void
g_node_prepend (GNode *parent,
GNode *node)
{
g_return_if_fail (parent != NULL);
g_node_insert_before (parent, parent->children, node);
}
GNode*
g_node_get_root (GNode *node)
{
g_return_val_if_fail (node != NULL, NULL);
while (node->parent)
node = node->parent;
return node;
}
gboolean
g_node_is_ancestor (GNode *node,
GNode *descendant)
{
g_return_val_if_fail (node != NULL, FALSE);
g_return_val_if_fail (descendant != NULL, FALSE);
while (descendant)
{
if (descendant->parent == node)
return TRUE;
descendant = descendant->parent;
}
return FALSE;
}
/* returns 1 for root, 2 for first level children,
* 3 for children's children...
*/
guint
g_node_depth (GNode *node)
{
register guint depth = 0;
while (node)
{
depth++;
node = node->parent;
}
return depth;
}
void
g_node_reverse_children (GNode *node)
{
GNode *child;
GNode *last;
g_return_if_fail (node != NULL);
child = node->children;
last = NULL;
while (child)
{
last = child;
child = last->next;
last->next = last->prev;
last->prev = child;
}
node->children = last;
}
guint
g_node_max_height (GNode *root)
{
register GNode *child;
register guint max_height = 0;
if (!root)
return 0;
child = root->children;
while (child)
{
register guint tmp_height;
tmp_height = g_node_max_height (child);
if (tmp_height > max_height)
max_height = tmp_height;
child = child->next;
}
return max_height + 1;
}
static gboolean
g_node_traverse_pre_order (GNode *node,
GTraverseFlags flags,
GNodeTraverseFunc func,
gpointer data)
{
if (node->children)
{
GNode *child;
if ((flags & G_TRAVERSE_NON_LEAFS) &&
func (node, data))
return TRUE;
child = node->children;
while (child)
{
register GNode *current;
current = child;
child = current->next;
if (g_node_traverse_pre_order (current, flags, func, data))
return TRUE;
}
}
else if ((flags & G_TRAVERSE_LEAFS) &&
func (node, data))
return TRUE;
return FALSE;
}
static gboolean
g_node_depth_traverse_pre_order (GNode *node,
GTraverseFlags flags,
guint depth,