]> Pileus Git - ~andy/gtk/commitdiff
Added g_list_insert_sorted and g_slist_insert_sorted to glib.
authorManish Singh <yosh@src.gnome.org>
Wed, 7 Jan 1998 07:37:36 +0000 (07:37 +0000)
committerManish Singh <yosh@src.gnome.org>
Wed, 7 Jan 1998 07:37:36 +0000 (07:37 +0000)
-Yosh

glib/glib.h
glib/glist.c
glib/gslist.c
glib/testglib.c

index 22c12254ba6dcc0f23a3aa01df7d1bb67926b24e..c77ccc3d7d51022705220fe0762806f985920339 100644 (file)
@@ -397,6 +397,9 @@ GList* g_list_prepend         (GList     *list,
 GList* g_list_insert     (GList     *list,
                           gpointer   data,
                           gint       position);
+GList* g_list_insert_sorted (GList   *list,
+                            gpointer data,
+                            GCompareFunc func);                            
 GList* g_list_concat     (GList     *list1, 
                           GList     *list2);
 GList* g_list_remove     (GList     *list,
@@ -415,6 +418,8 @@ void   g_list_foreach         (GList     *list,
                           GFunc      func,
                           gpointer   user_data);
 
+#define g_list_previous(list) (((GList *)list)->prev)
+#define g_list_next(list) (((GList *)list)->next)
 
 /* Singly linked lists
  */
@@ -428,6 +433,9 @@ GSList* g_slist_prepend         (GSList   *list,
 GSList* g_slist_insert     (GSList   *list,
                             gpointer  data,
                             gint      position);
+GSList* g_slist_insert_sorted (GSList   *list,
+                              gpointer data,
+                              GCompareFunc func);                          
 GSList* g_slist_concat     (GSList   *list1, 
                             GSList   *list2);
 GSList* g_slist_remove     (GSList   *list,
@@ -445,6 +453,7 @@ void        g_slist_foreach     (GSList   *list,
                             GFunc     func,
                             gpointer  user_data);
 
+#define g_slist_next(list) (((GSList *)list)->next)
 
 /* List Allocators
  */
index 679bfac88b4f78dd8d221798dc7fab2363294a14..9af3c79a6c18426c43d6ccbd77ea34c957a5494e 100644 (file)
@@ -363,3 +363,55 @@ g_list_foreach (GList       *list,
       list = list->next;
     }
 }
+
+
+GList*
+g_list_insert_sorted (GList    *list,
+                      gpointer  data,
+                      GCompareFunc  func)
+{
+  GList *tmp_list = list;
+  GList *new_list;
+  gint cmp;
+  
+  if (!list) 
+    {
+      new_list = g_list_alloc();
+      new_list->data = data;
+      return new_list;
+    }
+  
+  cmp = (*func) (tmp_list->data, data);
+  
+  while ((tmp_list->next) && (cmp > 0))
+    {
+      tmp_list = tmp_list->next;
+      cmp = (*func) (tmp_list->data, data);
+    }
+
+  if (cmp == 0)
+    return list;
+  
+  new_list = g_list_alloc();
+  new_list->data = data;
+
+  if ((!tmp_list->next) && (cmp > 0))
+    {
+      tmp_list->next = new_list;
+      new_list->prev = tmp_list;
+      return list;
+    }
+   
+  if (tmp_list->prev)
+    {
+      tmp_list->prev->next = new_list;
+      new_list->prev = tmp_list->prev;
+    }
+  new_list->next = tmp_list;
+  tmp_list->prev = new_list;
+  if (tmp_list == list)
+    return new_list;
+  else
+    return list;
+}
index a0782673eb84427dc9a9e0fe7b498c0b8d9796c6..248bfdb99aee871ed06dae6880f6c61e31354917 100644 (file)
@@ -336,3 +336,54 @@ g_slist_foreach (GSList   *list,
       list = list->next;
     }
 }
+
+GSList*
+g_slist_insert_sorted (GSList    *list,
+                       gpointer  data,
+                       GCompareFunc  func)
+{
+  GSList *tmp_list = list;
+  GSList *prev_list = NULL;
+  GSList *new_list;
+  gint cmp;
+  if (!list)
+    {
+      new_list = g_slist_alloc();
+      new_list->data = data;
+      return new_list;
+    }
+  cmp = (*func) (tmp_list->data, data);
+  while ((tmp_list->next) && (cmp > 0))
+    {
+      prev_list = tmp_list;
+      tmp_list = tmp_list->next;
+      cmp = (*func) (tmp_list->data, data);
+    }
+
+  if (cmp == 0)
+    return list;
+  new_list = g_slist_alloc();
+  new_list->data = data;
+
+  if ((!tmp_list->next) && (cmp > 0))
+    {
+      tmp_list->next = new_list;
+      return list;
+    }
+  
+  if (prev_list)
+    {
+      prev_list->next = new_list;
+      new_list->next = tmp_list;
+      return list;
+    }
+  else
+    {
+      new_list->next = list;
+      return new_list;
+    }
+}
index 4357b2364d8ffb736d8e23425756d7ced4352d0a..74231b5eceb9484adfb948df5da3b951fe7d1d11 100644 (file)
@@ -43,6 +43,29 @@ my_hash_compare (gpointer a,
   return *((gint*) a) == *((gint*) b);
 }
 
+gint 
+my_list_compare_one (gpointer a, gpointer b)
+{
+  gint one = *((gint*)a);
+  gint two = *((gint*)b);
+  return one-two;
+};
+
+gint 
+my_list_compare_two (gpointer a, gpointer b)
+{
+  gint one = *((gint*)a);
+  gint two = *((gint*)b);
+  return two-one;
+};
+
+/* void
+my_list_print (gpointer a, gpointer b)
+{
+  gint three = *((gint*)a);
+  g_print("%d", three);
+}; */
+
 gint
 my_compare (gpointer a,
            gpointer b)
@@ -74,6 +97,8 @@ main (int   argc,
   GStringChunk *string_chunk;
   GTimer *timer;
   gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
+  gint morenums[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6};
+
   gchar *mem[10000], *tmp_string, *tmp_string_2;
   gint i, j;
   GArray *garray;
@@ -96,9 +121,45 @@ main (int   argc,
     {
       t = g_list_nth (list, i);
       if (*((gint*) t->data) != (9 - i))
-       g_error ("failed");
+       g_error ("Regular insert failed");
     }
 
+  g_list_free (list);
+  list = NULL;
+  
+  for (i = 0; i < 10; i++)
+    list = g_list_insert_sorted (list, &morenums[i], my_list_compare_one);
+
+  /*
+  g_print("\n");
+  g_list_foreach (list, my_list_print, NULL);
+  */
+
+  for (i = 0; i < 10; i++)
+    {
+      t = g_list_nth (list, i);
+      if (*((gint*) t->data) != (9 - i))
+         g_error ("Sorted insert failed");
+    }
+    
+  g_list_free (list);
+  list = NULL;
+  
+  for (i = 0; i < 10; i++)
+    list = g_list_insert_sorted (list, &morenums[i], my_list_compare_two);
+
+  /*
+  g_print("\n");
+  g_list_foreach (list, my_list_print, NULL);
+  */
+
+  for (i = 0; i < 10; i++)
+    {
+      t = g_list_nth (list, i);
+      if (*((gint*) t->data) != i)
+         g_error ("Sorted insert failed");
+    }
+    
   g_list_free (list);
 
   g_print ("ok\n");
@@ -119,6 +180,42 @@ main (int   argc,
     }
 
   g_slist_free (slist);
+  slist = NULL;
+
+  for (i = 0; i < 10; i++)
+    slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_one);
+
+  /*
+  g_print("\n");
+  g_slist_foreach (slist, my_list_print, NULL);
+  */
+
+  for (i = 0; i < 10; i++)
+    {
+      st = g_slist_nth (slist, i);
+      if (*((gint*) st->data) != (9 - i))
+         g_error ("Sorted insert failed");
+    }
+     
+  g_slist_free(slist);
+  slist = NULL;
+   
+  for (i = 0; i < 10; i++)
+    slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_two);
+
+  /*
+  g_print("\n");
+  g_slist_foreach (slist, my_list_print, NULL);
+  */
+
+  for (i = 0; i < 10; i++)
+    {
+      st = g_slist_nth (slist, i);
+      if (*((gint*) st->data) != i)
+         g_error("Sorted insert failed");
+    }
+    
+  g_slist_free(slist);
 
   g_print ("ok\n");