| 1 |
|
| #include <ctype.h> |
| 2 |
|
| #include <stdio.h> |
| 3 |
|
| #include <stdlib.h> |
| 4 |
|
| #include <string.h> |
| 5 |
|
| #include "glog.h" |
| 6 |
|
| #include "gmem.h" |
| 7 |
|
| #include "header.h" |
| 8 |
|
| #include "plist.h" |
| 9 |
|
| #include "hlist.h" |
| 10 |
|
| |
| 11 |
|
| static void hlist_del_pos(HList* hlist, int pos, int clear); |
| 12 |
|
| static void hlist_grow(HList* hlist); |
| 13 |
|
| static int hlist_cmp(const void* v1, const void* v2); |
| 14 |
|
| static HNode* hlist_lookup(HList* hlist, const char* name, int type, int add, int del); |
| 15 |
|
| |
| 16 |
|
| |
| 17 |
86 |
| HList* hlist_create(void) { |
| 18 |
86 |
| HList* p = 0; |
| 19 |
86 |
| GMEM_NEW(p, HList*, sizeof(HList)); |
| 20 |
86 |
▶ | if (!p) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!p) { |
0 |
86 |
86 |
|
| 21 |
0 |
| return 0; |
| 22 |
|
| } |
| 23 |
|
| |
| 24 |
86 |
| hlist_init(p); |
| 25 |
86 |
| return p; |
| 26 |
|
| } |
| 27 |
|
| |
| 28 |
86 |
| void hlist_destroy(HList* hlist) { |
| 29 |
86 |
▶ | if (!hlist) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hlist) { |
0 |
86 |
86 |
|
| 30 |
0 |
| return; |
| 31 |
|
| } |
| 32 |
|
| |
| 33 |
86 |
| hlist_clear(hlist); |
| 34 |
86 |
| GMEM_DEL(hlist, HList*, sizeof(HList)); |
| 35 |
|
| } |
| 36 |
|
| |
| 37 |
13 |
| HList* hlist_clone(HList* hlist) { |
| 38 |
13 |
▶ | if (!hlist) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hlist) { |
0 |
13 |
13 |
|
| 39 |
0 |
| return 0; |
| 40 |
|
| } |
| 41 |
|
| |
| 42 |
|
| GLOG(("=C= Cloning hlist %p", hlist)); |
| 43 |
|
| /* hlist_dump(hlist, stderr); */ |
| 44 |
13 |
| HList* p = hlist_create(); |
| 45 |
13 |
| p->flags = hlist->flags; |
| 46 |
|
| int j; |
| 47 |
59 |
▶ | for (j = 0; j < hlist->ulen; ++j) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < hlist->ulen; ++j) { |
46 |
13 |
59 |
|
| 48 |
46 |
| hlist_grow(p); |
| 49 |
46 |
| p->data[j].header = header_clone(hlist->data[j].header); |
| 50 |
46 |
| p->data[j].values = plist_clone(hlist->data[j].values); |
| 51 |
46 |
| ++p->ulen; |
| 52 |
|
| } |
| 53 |
13 |
| return p; |
| 54 |
|
| } |
| 55 |
|
| |
| 56 |
178 |
| void hlist_init(HList* hlist) { |
| 57 |
178 |
▶ | if (!hlist) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hlist) { |
0 |
178 |
178 |
|
| 58 |
0 |
| return; |
| 59 |
|
| } |
| 60 |
|
| |
| 61 |
178 |
| hlist->data = 0; |
| 62 |
178 |
| hlist->alen = hlist->ulen = 0; |
| 63 |
178 |
| hlist->flags = 0; |
| 64 |
|
| } |
| 65 |
|
| |
| 66 |
|
| /* TODO: perhaps we should leave hlist as empty but not delete the chunks we */ |
| 67 |
|
| /* already allocated for it... */ |
| 68 |
92 |
| void hlist_clear(HList* hlist) { |
| 69 |
92 |
▶ | if (!hlist) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hlist) { |
0 |
92 |
92 |
|
| 70 |
0 |
| return; |
| 71 |
|
| } |
| 72 |
|
| |
| 73 |
|
| int j; |
| 74 |
251 |
▶ | for (j = 0; j < hlist->ulen; ++j) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < hlist->ulen; ++j) { |
159 |
92 |
251 |
|
| 75 |
159 |
| HNode* n = &hlist->data[j]; |
| 76 |
159 |
| header_destroy(n->header); |
| 77 |
159 |
| plist_destroy(n->values); |
| 78 |
|
| } |
| 79 |
92 |
| GMEM_DELARR(hlist->data, HNode*, hlist->alen, sizeof(HNode)); |
| 80 |
92 |
| hlist_init(hlist); |
| 81 |
|
| } |
| 82 |
|
| |
| 83 |
29 |
| int hlist_size(const HList* hlist) |
| 84 |
|
| { |
| 85 |
29 |
▶ | return hlist ? hlist->ulen : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| return hlist ? hlist->ulen : 0; |
29 |
0 |
29 |
|
| 86 |
|
| } |
| 87 |
|
| |
| 88 |
223 |
| HNode* hlist_get(HList* hlist, const char* name) { |
| 89 |
223 |
▶ | if (!hlist) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hlist) { |
0 |
223 |
223 |
|
| 90 |
0 |
| return 0; |
| 91 |
|
| } |
| 92 |
|
| |
| 93 |
|
| GLOG(("=C= Getting [%s]", name)); |
| 94 |
223 |
| return hlist_lookup(hlist, name, HEADER_TYPE_NONE, 0, 0); |
| 95 |
|
| } |
| 96 |
|
| |
| 97 |
164 |
| HNode* hlist_add(HList* hlist, const char* name, const void* obj) { |
| 98 |
164 |
▶ | if (!hlist) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hlist) { |
0 |
164 |
164 |
|
| 99 |
0 |
| return 0; |
| 100 |
|
| } |
| 101 |
|
| |
| 102 |
164 |
| HNode* n = hlist_lookup(hlist, name, HEADER_TYPE_NONE, 0, 0); |
| 103 |
164 |
▶ | if (!n) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n) { |
131 |
33 |
164 |
|
| 104 |
131 |
| Header* h = header_lookup_standard(HEADER_TYPE_NONE, name); |
| 105 |
131 |
▶ | if (!h) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!h) { |
57 |
74 |
131 |
|
| 106 |
57 |
| h = header_create(name); |
| 107 |
|
| } |
| 108 |
131 |
| hlist_grow(hlist); |
| 109 |
131 |
| n = &hlist->data[hlist->ulen++]; |
| 110 |
131 |
| n->header = h; |
| 111 |
131 |
| n->values = plist_create(); |
| 112 |
131 |
| HLIST_FLAG_CLR(hlist, HLIST_FLAGS_SORTED); |
| 113 |
|
| } |
| 114 |
|
| |
| 115 |
164 |
| plist_add(n->values, obj); |
| 116 |
|
| GLOG(("=C= Added [%s] => %p (%d)", name, obj, n->header->order)); |
| 117 |
164 |
| return n; |
| 118 |
|
| } |
| 119 |
|
| |
| 120 |
18 |
| void hlist_del(HList* hlist, const char* name) { |
| 121 |
18 |
▶ | if (!hlist) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hlist) { |
0 |
18 |
18 |
|
| 122 |
0 |
| return; |
| 123 |
|
| } |
| 124 |
|
| |
| 125 |
|
| GLOG(("=C= Deleting [%s]", name)); |
| 126 |
18 |
| hlist_lookup(hlist, name, HEADER_TYPE_NONE, 0, 1); |
| 127 |
|
| } |
| 128 |
|
| |
| 129 |
46 |
| void hlist_sort(HList* hlist) { |
| 130 |
46 |
▶ | if (!hlist) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hlist) { |
0 |
46 |
46 |
|
| 131 |
0 |
| return; |
| 132 |
|
| } |
| 133 |
|
| |
| 134 |
46 |
▶ | if (HLIST_FLAG_GET(hlist, HLIST_FLAGS_SORTED)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (HLIST_FLAG_GET(hlist, HLIST_FLAGS_SORTED)) { |
30 |
16 |
46 |
|
| 135 |
|
| GLOG(("=C= Already sorted hlist")); |
| 136 |
|
| } else { |
| 137 |
|
| GLOG(("=C= Sorting hlist")); |
| 138 |
30 |
| qsort(hlist->data, hlist->ulen, sizeof(HNode), hlist_cmp); |
| 139 |
|
| GLOG(("=C= Sorted hlist")); |
| 140 |
30 |
| HLIST_FLAG_SET(hlist, HLIST_FLAGS_SORTED); |
| 141 |
|
| } |
| 142 |
|
| } |
| 143 |
|
| |
| 144 |
0 |
| void hlist_dump(const HList* hlist, FILE* fp) { |
| 145 |
0 |
▶ | if (!hlist) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hlist) { |
0 |
0 |
0 |
|
| 146 |
0 |
| return; |
| 147 |
|
| } |
| 148 |
|
| |
| 149 |
|
| int j; |
| 150 |
0 |
▶ | for (j = 0; j < hlist->ulen; ++j) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < hlist->ulen; ++j) { |
0 |
0 |
0 |
|
| 151 |
0 |
| HNode* n = &hlist->data[j]; |
| 152 |
0 |
| header_dump(n->header, fp); |
| 153 |
0 |
| plist_dump(n->values, fp); |
| 154 |
|
| } |
| 155 |
0 |
| fflush(fp); |
| 156 |
|
| } |
| 157 |
|
| |
| 158 |
20 |
| void hlist_transfer_header(HList* from, int pos, HList* to) |
| 159 |
|
| { |
| 160 |
20 |
▶ | if (!from || !to) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!from || !to) { |
20 |
0 |
20 |
| if (!from || !to) { |
0 |
20 |
20 |
|
| 161 |
0 |
| return; |
| 162 |
|
| } |
| 163 |
20 |
▶ | if (pos >= hlist_size(from)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pos >= hlist_size(from)) { |
0 |
20 |
20 |
|
| 164 |
0 |
| return; |
| 165 |
|
| } |
| 166 |
20 |
| hlist_grow(to); |
| 167 |
20 |
| to->data[to->ulen++] = from->data[pos]; |
| 168 |
20 |
| hlist_del_pos(from, pos, 0); |
| 169 |
|
| } |
| 170 |
|
| |
| 171 |
|
| |
| 172 |
38 |
| static void hlist_del_pos(HList* hlist, int pos, int clear) { |
| 173 |
38 |
| HNode* n = &hlist->data[pos]; |
| 174 |
38 |
| --hlist->ulen; |
| 175 |
38 |
▶ | if (clear) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (clear) { |
18 |
20 |
38 |
|
| 176 |
18 |
| header_destroy(n->header); |
| 177 |
18 |
| plist_destroy(n->values); |
| 178 |
|
| } |
| 179 |
|
| int j; |
| 180 |
132 |
▶ | for (j = pos; j < hlist->ulen; ++j) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = pos; j < hlist->ulen; ++j) { |
94 |
38 |
132 |
|
| 181 |
94 |
| hlist->data[j] = hlist->data[j+1]; |
| 182 |
|
| } |
| 183 |
38 |
| } |
| 184 |
|
| |
| 185 |
197 |
| static void hlist_grow(HList* hlist) { |
| 186 |
197 |
▶ | if (!hlist) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hlist) { |
0 |
197 |
197 |
|
| 187 |
0 |
| return; |
| 188 |
|
| } |
| 189 |
197 |
▶ | if (hlist->ulen < hlist->alen) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hlist->ulen < hlist->alen) { |
100 |
97 |
197 |
|
| 190 |
100 |
| return; |
| 191 |
|
| } |
| 192 |
|
| |
| 193 |
97 |
▶ | int count = hlist->alen == 0 ? HLIST_INITIAL_SIZE : 2*hlist->alen; |
Branch: 1 branch
| Branch | True | False | Total |
| int count = hlist->alen == 0 ? HLIST_INITIAL_SIZE : 2*hlist->alen; |
12 |
85 |
97 |
|
| 194 |
|
| GLOG(("=C= Growing HList from %d to %d", hlist->alen, count)); |
| 195 |
97 |
| GMEM_REALLOC(hlist->data, HNode*, sizeof(HNode) * hlist->alen, sizeof(HNode) * count); |
| 196 |
97 |
| hlist->alen = count; |
| 197 |
|
| } |
| 198 |
|
| |
| 199 |
108 |
| static int hlist_cmp(const void* v1, const void* v2) { |
| 200 |
108 |
| const HNode* n1 = (const HNode*) v1; |
| 201 |
108 |
| const HNode* n2 = (const HNode*) v2; |
| 202 |
108 |
| const Header* h1 = n1->header; |
| 203 |
108 |
| const Header* h2 = n2->header; |
| 204 |
|
| |
| 205 |
108 |
| int delta = h1->order - h2->order; |
| 206 |
108 |
▶ | return delta ? delta : header_compare(h1->name, h2->name); |
Branch: 1 branch
| Branch | True | False | Total |
| return delta ? delta : header_compare(h1->name, h2->name); |
25 |
83 |
108 |
|
| 207 |
|
| } |
| 208 |
|
| |
| 209 |
|
| /* |
| 210 |
|
| * TODO: leave this as pure lookup, move insert and delete to the caller functions? |
| 211 |
|
| */ |
| 212 |
405 |
| static HNode* hlist_lookup(HList* hlist, const char* name, int type, int add, int del) { |
| 213 |
405 |
▶ | if (!hlist) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hlist) { |
0 |
405 |
405 |
|
| 214 |
0 |
| return 0; |
| 215 |
|
| } |
| 216 |
|
| |
| 217 |
405 |
| int j = 0; |
| 218 |
405 |
| HNode* n = 0; |
| 219 |
975 |
▶ | for (j = 0; j < hlist->ulen; ++j) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < hlist->ulen; ++j) { |
725 |
250 |
975 |
|
| 220 |
725 |
| n = &hlist->data[j]; |
| 221 |
725 |
▶ | if (header_matches_type_or_name(n->header, type, name)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (header_matches_type_or_name(n->header, type, name)) { |
155 |
570 |
725 |
|
| 222 |
155 |
| break; |
| 223 |
|
| } |
| 224 |
|
| } |
| 225 |
405 |
▶ | if (j >= hlist->ulen) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (j >= hlist->ulen) { |
250 |
155 |
405 |
|
| 226 |
250 |
| n = 0; |
| 227 |
|
| } |
| 228 |
|
| |
| 229 |
|
| do { |
| 230 |
405 |
▶ | if (add) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (add) { |
0 |
405 |
405 |
|
| 231 |
0 |
▶ | if (n) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n) { |
0 |
0 |
0 |
|
| 232 |
0 |
| break; |
| 233 |
|
| } |
| 234 |
0 |
| hlist_grow(hlist); |
| 235 |
0 |
| n = &hlist->data[hlist->ulen++]; |
| 236 |
0 |
| Header* h = header_create(name); |
| 237 |
0 |
| n->header = h; |
| 238 |
0 |
| n->values = plist_create(); |
| 239 |
0 |
| HLIST_FLAG_CLR(hlist, HLIST_FLAGS_SORTED); |
| 240 |
0 |
| break; |
| 241 |
|
| } |
| 242 |
|
| |
| 243 |
405 |
▶ | if (del) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (del) { |
18 |
387 |
405 |
|
| 244 |
18 |
▶ | if (!n) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n) { |
0 |
18 |
18 |
|
| 245 |
0 |
| break; |
| 246 |
|
| } |
| 247 |
18 |
| hlist_del_pos(hlist, j, 1); |
| 248 |
18 |
| n = 0; |
| 249 |
18 |
| break; |
| 250 |
|
| } |
| 251 |
|
| } while (0); |
| 252 |
|
| |
| 253 |
405 |
| return n; |
| 254 |
|
| } |