Mercurial > repos > blastem
annotate tern.c @ 430:7f84090ab1cd
Add config file parser and default config file
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Wed, 10 Jul 2013 09:38:05 -0700 |
parents | f6fdde540791 |
children | 440efd7d27a9 |
rev | line source |
---|---|
429
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1 #include "tern.h" |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
2 #include <stddef.h> |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
3 #include <stdlib.h> |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
4 |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
5 tern_node * tern_insert(tern_node * head, char * key, tern_val value) |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
6 { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
7 tern_node ** cur = &head; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
8 while(*key) |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
9 { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
10 if (*cur) { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
11 while(*cur && (*cur)->el != *key) |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
12 { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
13 if (*key < (*cur)->el) { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
14 cur = &(*cur)->left; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
15 } else { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
16 cur = &(*cur)->right; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
17 } |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
18 } |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
19 } |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
20 if (!*cur) { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
21 *cur = malloc(sizeof(tern_node)); |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
22 (*cur)->left = NULL; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
23 (*cur)->right = NULL; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
24 (*cur)->straight.next = NULL; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
25 (*cur)->el = *key; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
26 } |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
27 cur = &((*cur)->straight.next); |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
28 key++; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
29 } |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
30 while(*cur && (*cur)->el) |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
31 { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
32 cur = &(*cur)->left; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
33 } |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
34 if (!*cur) { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
35 *cur = malloc(sizeof(tern_node)); |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
36 (*cur)->left = NULL; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
37 (*cur)->right = NULL; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
38 (*cur)->el = 0; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
39 } |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
40 (*cur)->straight.value = value; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
41 return head; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
42 } |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
43 |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
44 int tern_find(tern_node * head, char * key, tern_val *ret) |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
45 { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
46 tern_node * cur = head; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
47 while (cur) |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
48 { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
49 if (cur->el == *key) { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
50 if (*key) { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
51 cur = cur->straight.next; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
52 key++; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
53 } else { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
54 *ret = cur->straight.value; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
55 return 1; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
56 } |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
57 } else if (*key < cur->el) { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
58 cur = cur->left; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
59 } else { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
60 cur = cur->right; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
61 } |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
62 } |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
63 return 0; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
64 } |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
65 |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
66 intptr_t tern_find_int(tern_node * head, char * key, intptr_t def) |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
67 { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
68 tern_val ret; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
69 if (tern_find(head, key, &ret)) { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
70 return ret.intval; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
71 } |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
72 return def; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
73 } |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
74 |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
75 tern_node * tern_insert_int(tern_node * head, char * key, intptr_t value) |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
76 { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
77 tern_val val; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
78 val.intval = value; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
79 return tern_insert(head, key, val); |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
80 } |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
81 |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
82 void * tern_find_ptr(tern_node * head, char * key) |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
83 { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
84 tern_val ret; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
85 if (tern_find(head, key, &ret)) { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
86 return ret.ptrval; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
87 } |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
88 return NULL; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
89 } |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
90 |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
91 tern_node * tern_insert_ptr(tern_node * head, char * key, void * value) |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
92 { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
93 tern_val val; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
94 val.ptrval = value; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
95 return tern_insert(head, key, val); |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
96 } |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
97 |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
98 |