Added function to keep symetry with g_node_insert_before. 2000-09-29
[platform/upstream/glib.git] / glib / gnode.c
index 6f48560..5b72300 100644 (file)
@@ -5,21 +5,28 @@
  * 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
+ * modify it under the terms of the GNU Lesser 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.
+ * Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU Library General Public
+ * You should have received a copy of the GNU Lesser 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.
  */
 
+/*
+ * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
+ * file for a list of people on the GLib Team.  See the ChangeLog
+ * files for a list of changes.  These files are distributed with
+ * GLib at ftp://ftp.gtk.org/pub/gtk/. 
+ */
+
 /* 
  * MT safe
  */
@@ -39,7 +46,7 @@ struct _GAllocator /* from gmem.c */
   GNode         *free_nodes; /* implementation specific */
 };
 
-G_LOCK_DECLARE_STATIC (current_allocator);
+G_LOCK_DEFINE_STATIC (current_allocator);
 static GAllocator *current_allocator = NULL;
 
 /* HOLDS: current_allocator_lock */
@@ -75,7 +82,7 @@ void
 g_node_push_allocator (GAllocator *allocator)
 {
   G_LOCK (current_allocator);
-  g_node_validate_allocator ( allocator );
+  g_node_validate_allocator (allocator);
   allocator->last = current_allocator;
   current_allocator = allocator;
   G_UNLOCK (current_allocator);
@@ -141,6 +148,14 @@ g_nodes_free (GNode *node)
     {
       if (parent->children)
        g_nodes_free (parent->children);
+
+#ifdef ENABLE_GC_FRIENDLY
+      parent->data = NULL;
+      parent->prev = NULL;
+      parent->parent = NULL;
+      parent->children = NULL;
+#endif /* ENABLE_GC_FRIENDLY */
+
       if (parent->next)
        parent = parent->next;
       else
@@ -183,6 +198,24 @@ g_node_unlink (GNode *node)
 }
 
 GNode*
+g_node_copy (GNode *node)
+{
+  GNode *new_node = NULL;
+  
+  if (node)
+    {
+      GNode *child;
+      
+      new_node = g_node_new (node->data);
+      
+      for (child = g_node_last_child (node); child; child = child->prev)
+       g_node_prepend (new_node, g_node_copy (child));
+    }
+  
+  return new_node;
+}
+
+GNode*
 g_node_insert (GNode *parent,
               gint   position,
               GNode *node)
@@ -248,6 +281,42 @@ g_node_insert_before (GNode *parent,
 }
 
 GNode*
+g_node_insert_after (GNode *parent,
+                    GNode *sibling,
+                    GNode *node)
+{
+  g_return_val_if_fail (parent != NULL, node);
+  g_return_val_if_fail (node != NULL, node);
+  g_return_val_if_fail (G_NODE_IS_ROOT (node), node);
+  if (sibling)
+    g_return_val_if_fail (sibling->parent == parent, node);
+
+  node->parent = parent;
+
+  if (sibling)
+    {
+      if (sibling->next)
+       {
+         sibling->next->prev = node;
+       }
+      node->next = sibling->next;
+      node->prev = sibling;
+      sibling->next = node;
+    }
+  else
+    {
+      if (parent->children)
+       {
+         node->next = parent->children;
+         parent->children->prev = node;
+       }
+      parent->children = node;
+    }
+
+  return node;
+}
+
+GNode*
 g_node_prepend (GNode *parent,
                GNode *node)
 {
@@ -910,6 +979,9 @@ g_node_first_sibling (GNode *node)
 {
   g_return_val_if_fail (node != NULL, NULL);
   
+  if (node->parent)
+    return node->parent->children;
+  
   while (node->prev)
     node = node->prev;