comparison tern.c @ 1522:63659fb92db4 nuklear_ui

Key binding menu is now functional
author Michael Pavone <pavone@retrodev.com>
date Tue, 06 Feb 2018 22:42:16 -0800
parents e890971f3757
children 193b804c9845
comparison
equal deleted inserted replaced
1521:a51721408c15 1522:63659fb92db4
136 return ret.ptrval; 136 return ret.ptrval;
137 } 137 }
138 return NULL; 138 return NULL;
139 } 139 }
140 140
141 uint8_t tern_delete(tern_node **head, char const *key, tern_val *out)
142 {
143 tern_node *cur = *head, **last = head;
144 while (cur)
145 {
146 if (cur->el == *key) {
147 if (*key) {
148 last = &cur->straight.next;
149 cur = cur->straight.next;
150 key++;
151 } else {
152 break;
153 }
154 } else if (*key < cur->el) {
155 last = &cur->left;
156 cur = cur->left;
157 } else {
158 last = &cur->right;
159 cur = cur->right;
160 }
161 }
162 if (!cur) {
163 return TVAL_NONE;
164 }
165 *last = cur->right;
166 uint8_t valtype = cur->valtype;
167 if (out) {
168 *out = cur->straight.value;
169 }
170 free(cur);
171 return valtype;
172 }
173
141 tern_val tern_find_path_default(tern_node *head, char const *key, tern_val def, uint8_t req_valtype) 174 tern_val tern_find_path_default(tern_node *head, char const *key, tern_val def, uint8_t req_valtype)
142 { 175 {
143 tern_val ret; 176 tern_val ret;
144 while (*key) 177 while (*key)
145 { 178 {
191 } else { 224 } else {
192 return tern_insert(head, key, val, valtype); 225 return tern_insert(head, key, val, valtype);
193 } 226 }
194 } 227 }
195 228
229 uint8_t tern_delete_path(tern_node **head, char const *key, tern_val *out)
230 {
231 const char *next_key = key + strlen(key) + 1;
232 if (*next_key) {
233 tern_node *child = tern_find_node(*head, key);
234 if (!child) {
235 return TVAL_NONE;
236 }
237 tern_node *tmp = child;
238 uint8_t valtype = tern_delete_path(&tmp, next_key, out);
239 if (tmp != child) {
240 *head = tern_insert_node(*head, key, tmp);
241 }
242 return valtype;
243 } else {
244 return tern_delete(head, key, out);
245 }
246 }
247
196 uint32_t tern_count(tern_node *head) 248 uint32_t tern_count(tern_node *head)
197 { 249 {
198 uint32_t count = 0; 250 uint32_t count = 0;
199 if (head->left) { 251 if (head->left) {
200 count += tern_count(head->left); 252 count += tern_count(head->left);
218 fun(keybuf, head->straight.value, head->valtype, data); 270 fun(keybuf, head->straight.value, head->valtype, data);
219 } 271 }
220 if (head->left) { 272 if (head->left) {
221 tern_foreach_int(head->left, fun, data, keybuf, pos); 273 tern_foreach_int(head->left, fun, data, keybuf, pos);
222 } 274 }
223 if (head->el) { 275 if (head->el && head->straight.next) {
224 if (pos == MAX_ITER_KEY) { 276 if (pos == MAX_ITER_KEY) {
225 fatal_error("tern_foreach_int: exceeded maximum key size"); 277 fatal_error("tern_foreach_int: exceeded maximum key size");
226 } 278 }
227 keybuf[pos] = head->el; 279 keybuf[pos] = head->el;
228 tern_foreach_int(head->straight.next, fun, data, keybuf, pos+1); 280 tern_foreach_int(head->straight.next, fun, data, keybuf, pos+1);