summaryrefslogtreecommitdiff
path: root/src/sp_list.c
diff options
context:
space:
mode:
authorjvoisin2017-12-21 14:03:18 +0100
committerjvoisin2017-12-21 14:03:18 +0100
commit0d98f51e7dbde4a40c0039910d43ad378eaefa83 (patch)
tree78749747bc46c80f85bc2c3cdfb629b331d200f1 /src/sp_list.c
parentf91cf7390c060986e51b77990f4e472165b97dad (diff)
Rename sp_node_t to sp_list_node
Since we now have sp_list and sp_tree, it makes sense to specify that nodes are only for lists.
Diffstat (limited to 'src/sp_list.c')
-rw-r--r--src/sp_list.c24
1 files changed, 12 insertions, 12 deletions
diff --git a/src/sp_list.c b/src/sp_list.c
index 70d0ebe..2a9d680 100644
--- a/src/sp_list.c
+++ b/src/sp_list.c
@@ -3,35 +3,35 @@
3#include <stdlib.h> 3#include <stdlib.h>
4#include "php_snuffleupagus.h" 4#include "php_snuffleupagus.h"
5 5
6void sp_list_free(sp_node_t *node) { 6void sp_list_free(sp_list_node *node) {
7 while(node) { 7 while(node) {
8 sp_node_t *tmp = node->next; 8 sp_list_node *tmp = node->next;
9 pefree(node, 1); 9 pefree(node, 1);
10 node = tmp; 10 node = tmp;
11 } 11 }
12} 12}
13 13
14sp_node_t *sp_list_new() { 14sp_list_node *sp_list_new() {
15 sp_node_t *new = pecalloc(sizeof(*new), 1, 1); 15 sp_list_node *new = pecalloc(sizeof(*new), 1, 1);
16 new->next = new->data = new->head = NULL; 16 new->next = new->data = new->head = NULL;
17 return new; 17 return new;
18} 18}
19 19
20// Thanks to https://en.wikipedia.org/wiki/Insertion_sort :> 20// Thanks to https://en.wikipedia.org/wiki/Insertion_sort :>
21sp_node_t *sp_list_sort(sp_node_t *pList, int (*cmp_func)(sp_node_t *, sp_node_t *)) { 21sp_list_node *sp_list_sort(sp_list_node *pList, int (*cmp_func)(sp_list_node *, sp_list_node *)) {
22 sp_node_t *head = NULL; 22 sp_list_node *head = NULL;
23 23
24 if (pList == NULL || pList->next == NULL) { 24 if (pList == NULL || pList->next == NULL) {
25 return pList; 25 return pList;
26 } 26 }
27 while (pList != NULL) { 27 while (pList != NULL) {
28 sp_node_t *current = pList; 28 sp_list_node *current = pList;
29 pList = pList->next; 29 pList = pList->next;
30 if (head == NULL || 0 > cmp_func(current, head)) { 30 if (head == NULL || 0 > cmp_func(current, head)) {
31 current->next = head; 31 current->next = head;
32 head = current; 32 head = current;
33 } else { 33 } else {
34 sp_node_t *p = head; 34 sp_list_node *p = head;
35 while (p != NULL) { 35 while (p != NULL) {
36 if (p->next == NULL || 0 > cmp_func(current, p->next)) { 36 if (p->next == NULL || 0 > cmp_func(current, p->next)) {
37 current->next = p->next; 37 current->next = p->next;
@@ -45,13 +45,13 @@ sp_node_t *sp_list_sort(sp_node_t *pList, int (*cmp_func)(sp_node_t *, sp_node_t
45 return head; 45 return head;
46} 46}
47 47
48void sp_list_insert(sp_node_t *list, void *data) { 48void sp_list_insert(sp_list_node *list, void *data) {
49 if (list->head == NULL) { 49 if (list->head == NULL) {
50 list->data = data; 50 list->data = data;
51 list->next = NULL; 51 list->next = NULL;
52 list->head = list; 52 list->head = list;
53 } else { 53 } else {
54 sp_node_t *new = pecalloc(sizeof(*new), 1, 1); 54 sp_list_node *new = pecalloc(sizeof(*new), 1, 1);
55 55
56 new->data = data; 56 new->data = data;
57 new->next = NULL; 57 new->next = NULL;
@@ -64,13 +64,13 @@ void sp_list_insert(sp_node_t *list, void *data) {
64 } 64 }
65} 65}
66 66
67void sp_list_prepend(sp_node_t *list, void *data) { 67void sp_list_prepend(sp_list_node *list, void *data) {
68 if (list->head == NULL) { 68 if (list->head == NULL) {
69 list->data = data; 69 list->data = data;
70 list->next = NULL; 70 list->next = NULL;
71 list->head = list; 71 list->head = list;
72 } else { 72 } else {
73 sp_node_t *new = pecalloc(sizeof(*new), 1, 1); 73 sp_list_node *new = pecalloc(sizeof(*new), 1, 1);
74 74
75 new->next = list->next; 75 new->next = list->next;
76 list->next = new; 76 list->next = new;