From 88c85c116d7eeb80bf96b4a7e3aaca003bb1f81e Mon Sep 17 00:00:00 2001 From: Charlie Brej Date: Sat, 20 Sep 2008 18:18:16 -0400 Subject: [PATCH] Add new list functions 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 | 68 ++++++++++++++++++++++++++++++++++--------- src/libply/ply-list.h | 6 +++- 2 files changed, 59 insertions(+), 15 deletions(-) diff --git a/src/libply/ply-list.c b/src/libply/ply-list.c index 1e5cd98b..60f16e20 100644 --- a/src/libply/ply-list.c +++ b/src/libply/ply-list.c @@ -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 diff --git a/src/libply/ply-list.h b/src/libply/ply-list.h index 7e0b6bcc..3f023188 100644 --- a/src/libply/ply-list.h +++ b/src/libply/ply-list.h @@ -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 */ -- 2.47.3