comparison tern.c @ 1186:110251ea369e

Consting up some parameters to ternary tree functions
author Michael Pavone <pavone@retrodev.com>
date Sun, 22 Jan 2017 16:13:02 -0800
parents 724bbec47f86
children 72ea3885e7b5
comparison
equal deleted inserted replaced
1185:9de9d2c6ebe5 1186:110251ea369e
8 #include <stdlib.h> 8 #include <stdlib.h>
9 #include <string.h> 9 #include <string.h>
10 #include <stdio.h> 10 #include <stdio.h>
11 #include "util.h" 11 #include "util.h"
12 12
13 tern_node * tern_insert(tern_node * head, char * key, tern_val value) 13 tern_node * tern_insert(tern_node * head, char const * key, tern_val value)
14 { 14 {
15 tern_node ** cur = &head; 15 tern_node ** cur = &head;
16 while(*key) 16 while(*key)
17 { 17 {
18 if (*cur) { 18 if (*cur) {
47 } 47 }
48 (*cur)->straight.value = value; 48 (*cur)->straight.value = value;
49 return head; 49 return head;
50 } 50 }
51 51
52 int tern_find(tern_node * head, char * key, tern_val *ret) 52 int tern_find(tern_node * head, char const * key, tern_val *ret)
53 { 53 {
54 tern_node * cur = head; 54 tern_node * cur = head;
55 while (cur) 55 while (cur)
56 { 56 {
57 if (cur->el == *key) { 57 if (cur->el == *key) {
69 } 69 }
70 } 70 }
71 return 0; 71 return 0;
72 } 72 }
73 73
74 tern_node * tern_find_prefix(tern_node * head, char * key) 74 tern_node * tern_find_prefix(tern_node * head, char const * key)
75 { 75 {
76 tern_node * cur = head; 76 tern_node * cur = head;
77 while (cur && *key) 77 while (cur && *key)
78 { 78 {
79 if (cur->el == *key) { 79 if (cur->el == *key) {
86 } 86 }
87 } 87 }
88 return cur; 88 return cur;
89 } 89 }
90 90
91 intptr_t tern_find_int(tern_node * head, char * key, intptr_t def) 91 intptr_t tern_find_int(tern_node * head, char const * key, intptr_t def)
92 { 92 {
93 tern_val ret; 93 tern_val ret;
94 if (tern_find(head, key, &ret)) { 94 if (tern_find(head, key, &ret)) {
95 return ret.intval; 95 return ret.intval;
96 } 96 }
97 return def; 97 return def;
98 } 98 }
99 99
100 tern_node * tern_insert_int(tern_node * head, char * key, intptr_t value) 100 tern_node * tern_insert_int(tern_node * head, char const * key, intptr_t value)
101 { 101 {
102 tern_val val; 102 tern_val val;
103 val.intval = value; 103 val.intval = value;
104 return tern_insert(head, key, val); 104 return tern_insert(head, key, val);
105 } 105 }
106 106
107 void * tern_find_ptr_default(tern_node * head, char * key, void * def) 107 void * tern_find_ptr_default(tern_node * head, char const * key, void * def)
108 { 108 {
109 tern_val ret; 109 tern_val ret;
110 if (tern_find(head, key, &ret)) { 110 if (tern_find(head, key, &ret)) {
111 if (ret.intval & 1) { 111 if (ret.intval & 1) {
112 return (void *)(ret.intval & ~1); 112 return (void *)(ret.intval & ~1);
115 } 115 }
116 } 116 }
117 return def; 117 return def;
118 } 118 }
119 119
120 void * tern_find_ptr(tern_node * head, char * key) 120 void * tern_find_ptr(tern_node * head, char const * key)
121 { 121 {
122 return tern_find_ptr_default(head, key, NULL); 122 return tern_find_ptr_default(head, key, NULL);
123 } 123 }
124 124
125 tern_val tern_find_path_default(tern_node *head, char *key, tern_val def) 125 tern_val tern_find_path_default(tern_node *head, char const *key, tern_val def)
126 { 126 {
127 tern_val ret; 127 tern_val ret;
128 while (*key) 128 while (*key)
129 { 129 {
130 if (!tern_find(head, key, &ret)) { 130 if (!tern_find(head, key, &ret)) {
139 } 139 }
140 } 140 }
141 return ret; 141 return ret;
142 } 142 }
143 143
144 tern_val tern_find_path(tern_node *head, char *key) 144 tern_val tern_find_path(tern_node *head, char const *key)
145 { 145 {
146 tern_val def; 146 tern_val def;
147 def.ptrval = NULL; 147 def.ptrval = NULL;
148 return tern_find_path_default(head, key, def); 148 return tern_find_path_default(head, key, def);
149 } 149 }
150 150
151 tern_node * tern_insert_ptr(tern_node * head, char * key, void * value) 151 tern_node * tern_insert_ptr(tern_node * head, char const * key, void * value)
152 { 152 {
153 tern_val val; 153 tern_val val;
154 val.ptrval = value; 154 val.ptrval = value;
155 return tern_insert(head, key, val); 155 return tern_insert(head, key, val);
156 } 156 }
157 157
158 tern_node * tern_insert_node(tern_node *head, char *key, tern_node *value) 158 tern_node * tern_insert_node(tern_node *head, char const *key, tern_node *value)
159 { 159 {
160 tern_val val; 160 tern_val val;
161 val.intval = ((intptr_t)value) | 1; 161 val.intval = ((intptr_t)value) | 1;
162 return tern_insert(head, key, val); 162 return tern_insert(head, key, val);
163 } 163 }