Mon, 23 Jul 2012 15:13:19 +0200
added solaris 10 support
13 | 1 | #include "dlist.h" |
2 | ||
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
3 | UcxDlist *ucx_dlist_clone(UcxDlist *l, copy_func fnc, void *data) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
4 | UcxDlist *ret = NULL; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
5 | while (l != NULL) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
6 | if (fnc != NULL) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
7 | ret = ucx_dlist_append(ret, fnc(l->data, data)); |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
8 | } else { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
9 | ret = ucx_dlist_append(ret, l->data); |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
10 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
11 | l = l->next; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
12 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
13 | return ret; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
14 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
15 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
16 | int ucx_dlist_equals(UcxDlist *l1, UcxDlist *l2, cmp_func fnc, void* data) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
17 | if (l1 == l2) return 1; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
18 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
19 | while (l1 != NULL && l2 != NULL) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
20 | if (fnc == NULL) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
21 | if (l1->data != l2->data) return 0; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
22 | } else { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
23 | if (fnc(l1->data, l2->data, data) != 0) return 0; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
24 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
25 | l1 = l1->next; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
26 | l2 = l2->next; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
27 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
28 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
29 | return (l1 == NULL && l2 == NULL); |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
30 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
31 | |
13 | 32 | void ucx_dlist_free(UcxDlist *l) { |
33 | UcxDlist *e = l, *f; | |
34 | while (e != NULL) { | |
35 | f = e; | |
36 | e = e->next; | |
37 | free(f); | |
38 | } | |
39 | } | |
40 | ||
41 | UcxDlist *ucx_dlist_append(UcxDlist *l, void *data) { | |
42 | UcxDlist *nl = (UcxDlist*) malloc(sizeof(UcxDlist)); | |
43 | if (nl == NULL) return NULL; | |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
44 | |
13 | 45 | nl->data = data; |
46 | nl->next = NULL; | |
47 | if (l == NULL) { | |
30
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
48 | nl->prev = NULL; |
13 | 49 | return nl; |
50 | } else { | |
51 | UcxDlist *t = ucx_dlist_last(l); | |
52 | t->next = nl; | |
53 | nl->prev = t; | |
54 | return l; | |
55 | } | |
56 | } | |
57 | ||
58 | UcxDlist *ucx_dlist_prepend(UcxDlist *l, void *data) { | |
59 | UcxDlist *nl = ucx_dlist_append(NULL, data); | |
60 | if (nl == NULL) return NULL; | |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
61 | |
13 | 62 | if (l != NULL) { |
63 | nl->next = l; | |
64 | l->prev = nl; | |
65 | } | |
66 | return nl; | |
67 | } | |
68 | ||
69 | UcxDlist *ucx_dlist_concat(UcxDlist *l1, UcxDlist *l2) { | |
70 | if (l1 == NULL) { | |
71 | return l2; | |
72 | } else { | |
73 | UcxDlist *last = ucx_dlist_last(l1); | |
74 | last->next = l2; | |
75 | l2->prev = last; | |
76 | return l1; | |
77 | } | |
78 | } | |
79 | ||
80 | UcxDlist *ucx_dlist_last(UcxDlist *l) { | |
81 | if (l == NULL) return NULL; | |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
82 | |
13 | 83 | UcxDlist *e = l; |
84 | while (e->next != NULL) { | |
85 | e = e->next; | |
86 | } | |
87 | return e; | |
88 | } | |
89 | ||
90 | UcxDlist *ucx_dlist_get(UcxDlist *l, int index) { | |
91 | if (l == NULL) return NULL; | |
92 | ||
93 | UcxDlist *e = l; | |
94 | while (e->next != NULL && index > 0) { | |
95 | e = e->next; | |
96 | index--; | |
97 | } | |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
98 | |
13 | 99 | return index == 0 ? e : NULL; |
100 | } | |
101 | ||
102 | size_t ucx_dlist_size(UcxDlist *l) { | |
103 | if (l == NULL) return 0; | |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
104 | |
13 | 105 | UcxDlist *e = l; |
106 | size_t s = 1; | |
107 | while (e->next != NULL) { | |
108 | e = e->next; | |
109 | s++; | |
110 | } | |
111 | ||
112 | return s; | |
113 | } | |
114 | ||
115 | /* dlist specific functions */ | |
116 | UcxDlist *ucx_dlist_first(UcxDlist *l) { | |
117 | if (l == NULL) return NULL; | |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
118 | |
13 | 119 | UcxDlist *e = l; |
120 | while (e->prev != NULL) { | |
121 | e = e->prev; | |
122 | } | |
123 | return e; | |
124 | } | |
30
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
125 | |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
126 | UcxDlist *ucx_dlist_remove(UcxDlist *l, UcxDlist *e) { |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
127 | if (e->prev == NULL) { |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
128 | if(e->next != NULL) { |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
129 | e->next->prev = NULL; |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
130 | l = e->next; |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
131 | } else { |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
132 | l = NULL; |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
133 | } |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
134 | |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
135 | } else { |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
136 | e->prev->next = e->next; |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
137 | e->next->prev = e->prev; |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
138 | } |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
139 | free(e); |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
140 | return l; |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
141 | } |