comparison tern.c @ 487:c08a4efeee7f opengl

Update opengl branch from default. Fix build breakage unrelated to merge
author Mike Pavone <pavone@retrodev.com>
date Sat, 26 Oct 2013 22:38:47 -0700
parents 140af5509ce7
children 51bf87f76d15
comparison
equal deleted inserted replaced
449:7696d824489d 487:c08a4efeee7f
1 /*
2 Copyright 2013 Michael Pavone
3 This file is part of BlastEm.
4 BlastEm is free software distributed under the terms of the GNU General Public License version 3 or greater. See COPYING for full license text.
5 */
6 #include "tern.h"
7 #include <stddef.h>
8 #include <stdlib.h>
9
10 tern_node * tern_insert(tern_node * head, char * key, tern_val value)
11 {
12 tern_node ** cur = &head;
13 while(*key)
14 {
15 if (*cur) {
16 while(*cur && (*cur)->el != *key)
17 {
18 if (*key < (*cur)->el) {
19 cur = &(*cur)->left;
20 } else {
21 cur = &(*cur)->right;
22 }
23 }
24 }
25 if (!*cur) {
26 *cur = malloc(sizeof(tern_node));
27 (*cur)->left = NULL;
28 (*cur)->right = NULL;
29 (*cur)->straight.next = NULL;
30 (*cur)->el = *key;
31 }
32 cur = &((*cur)->straight.next);
33 key++;
34 }
35 while(*cur && (*cur)->el)
36 {
37 cur = &(*cur)->left;
38 }
39 if (!*cur) {
40 *cur = malloc(sizeof(tern_node));
41 (*cur)->left = NULL;
42 (*cur)->right = NULL;
43 (*cur)->el = 0;
44 }
45 (*cur)->straight.value = value;
46 return head;
47 }
48
49 int tern_find(tern_node * head, char * key, tern_val *ret)
50 {
51 tern_node * cur = head;
52 while (cur)
53 {
54 if (cur->el == *key) {
55 if (*key) {
56 cur = cur->straight.next;
57 key++;
58 } else {
59 *ret = cur->straight.value;
60 return 1;
61 }
62 } else if (*key < cur->el) {
63 cur = cur->left;
64 } else {
65 cur = cur->right;
66 }
67 }
68 return 0;
69 }
70
71 tern_node * tern_find_prefix(tern_node * head, char * key)
72 {
73 tern_node * cur = head;
74 while (cur && *key)
75 {
76 if (cur->el == *key) {
77 cur = cur->straight.next;
78 key++;
79 } else if (*key < cur->el) {
80 cur = cur->left;
81 } else {
82 cur = cur->right;
83 }
84 }
85 return cur;
86 }
87
88 intptr_t tern_find_int(tern_node * head, char * key, intptr_t def)
89 {
90 tern_val ret;
91 if (tern_find(head, key, &ret)) {
92 return ret.intval;
93 }
94 return def;
95 }
96
97 tern_node * tern_insert_int(tern_node * head, char * key, intptr_t value)
98 {
99 tern_val val;
100 val.intval = value;
101 return tern_insert(head, key, val);
102 }
103
104 void * tern_find_ptr(tern_node * head, char * key)
105 {
106 tern_val ret;
107 if (tern_find(head, key, &ret)) {
108 return ret.ptrval;
109 }
110 return NULL;
111 }
112
113 tern_node * tern_insert_ptr(tern_node * head, char * key, void * value)
114 {
115 tern_val val;
116 val.ptrval = value;
117 return tern_insert(head, key, val);
118 }
119
120