]> git.ipfire.org Git - thirdparty/plymouth.git/commitdiff
Add new list functions
authorCharlie Brej <cbrej@cs.man.ac.uk>
Sat, 20 Sep 2008 22:18:16 +0000 (18:18 -0400)
committerRay Strode <rstrode@redhat.com>
Sat, 20 Sep 2008 22:18:16 +0000 (18:18 -0400)
Add function to clear list of all nodes, function to sort
nodes in list, and function set node user data.

src/libply/ply-list.c
src/libply/ply-list.h

index 1e5cd98bbfa540a82e14625e15bdc8946f8bbf6a..60f16e20178cf75615329774cc762547fd4dc932 100644 (file)
@@ -59,20 +59,7 @@ ply_list_new (void)
 void
 ply_list_free (ply_list_t *list)
 {
-                         ply_list_node_t *node;
-
-  if (list == NULL)
-    return;
-
-  node = list->first_node;
-  while (node != NULL)
-    {
-      ply_list_node_t *next_node;
-      next_node = node->next;
-      ply_list_remove_node (list, node);
-      node = next_node;
-    }
-
+  ply_list_remove_all_nodes (list);
   free (list);
 }
 
@@ -245,6 +232,24 @@ ply_list_remove_node (ply_list_t      *list,
   ply_list_node_free (node);
 }
 
+void
+ply_list_remove_all_nodes (ply_list_t *list)
+{
+  ply_list_node_t *node;
+
+  if (list == NULL)
+    return;
+
+  node = list->first_node;
+  while (node != NULL)
+    {
+      ply_list_node_t *next_node;
+      next_node = node->next;
+      ply_list_remove_node (list, node);
+      node = next_node;
+    }
+}
+
 ply_list_node_t *
 ply_list_get_first_node (ply_list_t *list)
 {
@@ -263,6 +268,34 @@ ply_list_get_next_node (ply_list_t     *list,
 {
   return node->next;
 }
+void ply_list_sort (ply_list_t  *list,
+                    ply_list_compare_func_t *compare)
+{
+  ply_list_node_t *nodea;
+  ply_list_node_t *nodeb;
+  int clean;
+  do
+    {
+      clean=1;
+      nodea = ply_list_get_first_node (list);
+      if (!nodea) return;
+      nodeb = ply_list_get_next_node (list, nodea);
+      while (nodeb)
+        {
+          if ((compare)(ply_list_node_get_data(nodea), ply_list_node_get_data(nodeb))>0)
+            {
+              void* temp = ply_list_node_get_data(nodea);
+              ply_list_node_set_data(nodea, ply_list_node_get_data(nodeb));
+              ply_list_node_set_data(nodeb, temp);
+              clean=0;
+            }
+          nodea = nodeb;
+          nodeb = ply_list_get_next_node (list, nodea);
+        }
+    }
+  while (!clean);
+}
 
 void *
 ply_list_node_get_data (ply_list_node_t *node)
@@ -270,6 +303,13 @@ ply_list_node_get_data (ply_list_node_t *node)
   return node->data;
 }
 
+void 
+ply_list_node_set_data (ply_list_node_t *node, void *data)
+{
+  node->data = data;
+  return;
+}
+
 #ifdef PLY_LIST_ENABLE_TEST
 #include <stdio.h>
 
index 7e0b6bcc5c56c7187db0c9c54797627b1281ee69..3f023188acb953e4cf076899600d363ec670e336 100644 (file)
@@ -24,6 +24,7 @@
 
 typedef struct _ply_list_node ply_list_node_t;
 typedef struct _ply_list ply_list_t;
+typedef int (ply_list_compare_func_t) (void *elementa, void *elementb);
 
 #ifndef PLY_HIDE_FUNCTION_DECLARATIONS
 ply_list_t *ply_list_new (void);
@@ -42,12 +43,15 @@ void ply_list_remove_data (ply_list_t *list,
                           void        *data);
 void ply_list_remove_node (ply_list_t      *list,
                            ply_list_node_t *node);
+void ply_list_remove_all_nodes (ply_list_t *list);
+void ply_list_sort (ply_list_t  *list,
+                     ply_list_compare_func_t *compare);
 ply_list_node_t *ply_list_get_first_node (ply_list_t *list);
 ply_list_node_t *ply_list_get_last_node (ply_list_t *list);
 ply_list_node_t *ply_list_get_next_node (ply_list_t  *list,
                                          ply_list_node_t *node);
-
 void *ply_list_node_get_data (ply_list_node_t *node);
+void ply_list_node_set_data (ply_list_node_t *node, void *data);
 #endif
 
 #endif /* PLY_LIST_H */