« third_party/modest/source/mycore/utils/mcsimple.c
↑ Summary
third_party/modest/source/mycore/utils/mhash.c »
| 1 | /* | ||||||||||||||
| 2 | Copyright (C) 2015-2017 Alexander Borisov | ||||||||||||||
| 3 | |||||||||||||||
| 4 | This library is free software; you can redistribute it and/or | ||||||||||||||
| 5 | modify it under the terms of the GNU Lesser General Public | ||||||||||||||
| 6 | License as published by the Free Software Foundation; either | ||||||||||||||
| 7 | version 2.1 of the License, or (at your option) any later version. | ||||||||||||||
| 8 | |||||||||||||||
| 9 | This library is distributed in the hope that it will be useful, | ||||||||||||||
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||||||||||||
| 12 | Lesser General Public License for more details. | ||||||||||||||
| 13 | |||||||||||||||
| 14 | You should have received a copy of the GNU Lesser General Public | ||||||||||||||
| 15 | License along with this library; if not, write to the Free Software | ||||||||||||||
| 16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||||||||||||||
| 17 | |||||||||||||||
| 18 | Author: lex.borisov@gmail.com (Alexander Borisov) | ||||||||||||||
| 19 | */ | ||||||||||||||
| 20 | |||||||||||||||
| 21 | #include "mycore/utils/resources.h" | ||||||||||||||
| 22 | #include "mycore/utils/mctree.h" | ||||||||||||||
| 23 | |||||||||||||||
| 24 | 0 | mctree_t * mctree_create(size_t start_size) | |||||||||||||
| 25 | { | ||||||||||||||
| 26 | 0 | mctree_t* mctree = (mctree_t*)mycore_malloc(sizeof(mctree_t)); | |||||||||||||
| 27 | |||||||||||||||
| 28 | 0 | ▶ | if(mctree == NULL) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 29 | 0 | return NULL; | |||||||||||||
| 30 | |||||||||||||||
| 31 | 0 | mctree->nodes_size = start_size + 512; | |||||||||||||
| 32 | 0 | mctree->nodes_length = start_size + 1; | |||||||||||||
| 33 | 0 | mctree->nodes = (mctree_node_t*)mycore_calloc(mctree->nodes_size, sizeof(mctree_node_t)); | |||||||||||||
| 34 | |||||||||||||||
| 35 | 0 | ▶ | if(mctree->nodes == NULL) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 36 | 0 | mycore_free(mctree); | |||||||||||||
| 37 | 0 | return NULL; | |||||||||||||
| 38 | } | ||||||||||||||
| 39 | |||||||||||||||
| 40 | 0 | mctree->start_size = start_size; | |||||||||||||
| 41 | |||||||||||||||
| 42 | 0 | return mctree; | |||||||||||||
| 43 | } | ||||||||||||||
| 44 | |||||||||||||||
| 45 | 0 | void mctree_clean(mctree_t* mctree) | |||||||||||||
| 46 | { | ||||||||||||||
| 47 | 0 | mctree->nodes_length = mctree->start_size + 1; | |||||||||||||
| 48 | 0 | memset(mctree->nodes, 0, sizeof(mctree_node_t) * mctree->nodes_size); | |||||||||||||
| 49 | 0 | } | |||||||||||||
| 50 | |||||||||||||||
| 51 | 0 | mctree_t * mctree_destroy(mctree_t* mctree) | |||||||||||||
| 52 | { | ||||||||||||||
| 53 | 0 | ▶ | if(mctree == NULL) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 54 | 0 | return NULL; | |||||||||||||
| 55 | |||||||||||||||
| 56 | 0 | ▶ | if(mctree->nodes) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 57 | 0 | mycore_free(mctree->nodes); | |||||||||||||
| 58 | |||||||||||||||
| 59 | 0 | mycore_free(mctree); | |||||||||||||
| 60 | |||||||||||||||
| 61 | 0 | return NULL; | |||||||||||||
| 62 | } | ||||||||||||||
| 63 | |||||||||||||||
| 64 | 0 | mctree_index_t __mtree_search_lowercase_to_start(mctree_t* mctree, mctree_index_t idx, const char* key, size_t key_size) | |||||||||||||
| 65 | { | ||||||||||||||
| 66 | 0 | mctree_node_t* nodes = mctree->nodes; | |||||||||||||
| 67 | |||||||||||||||
| 68 | 0 | ▶ | while (idx) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 69 | { | ||||||||||||||
| 70 | 0 | ▶ | if(key_size == nodes[idx].str_size) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 71 | 0 | ▶ | if(mycore_strncasecmp(key, nodes[idx].str, key_size) == 0) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 72 | 0 | return idx; | |||||||||||||
| 73 | } | ||||||||||||||
| 74 | |||||||||||||||
| 75 | 0 | idx = nodes[idx].child; | |||||||||||||
| 76 | } | ||||||||||||||
| 77 | 0 | ▶ | else if(key_size > nodes[idx].str_size) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 78 | { | ||||||||||||||
| 79 | 0 | ▶ | if(key_size < nodes[ nodes[idx].next ].str_size) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 80 | 0 | return 0; | |||||||||||||
| 81 | } | ||||||||||||||
| 82 | |||||||||||||||
| 83 | 0 | idx = nodes[idx].next; | |||||||||||||
| 84 | } | ||||||||||||||
| 85 | else { | ||||||||||||||
| 86 | 0 | ▶ | if(key_size > nodes[ nodes[idx].prev ].str_size) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 87 | 0 | return 0; | |||||||||||||
| 88 | } | ||||||||||||||
| 89 | |||||||||||||||
| 90 | 0 | idx = nodes[idx].prev; | |||||||||||||
| 91 | } | ||||||||||||||
| 92 | } | ||||||||||||||
| 93 | |||||||||||||||
| 94 | 0 | return 0; | |||||||||||||
| 95 | } | ||||||||||||||
| 96 | |||||||||||||||
| 97 | 0 | mctree_index_t __mtree_search_to_start(mctree_t* mctree, mctree_index_t idx, const char* key, size_t key_size) | |||||||||||||
| 98 | { | ||||||||||||||
| 99 | 0 | mctree_node_t* nodes = mctree->nodes; | |||||||||||||
| 100 | |||||||||||||||
| 101 | 0 | ▶ | while (idx) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 102 | { | ||||||||||||||
| 103 | 0 | ▶ | if(key_size == nodes[idx].str_size) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 104 | 0 | ▶ | if(memcmp((const void *)key, (const void *)(nodes[idx].str), key_size) == 0) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 105 | 0 | return idx; | |||||||||||||
| 106 | } | ||||||||||||||
| 107 | |||||||||||||||
| 108 | 0 | idx = nodes[idx].child; | |||||||||||||
| 109 | } | ||||||||||||||
| 110 | 0 | ▶ | else if(key_size > nodes[idx].str_size) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 111 | { | ||||||||||||||
| 112 | 0 | ▶ | if(key_size < nodes[ nodes[idx].next ].str_size) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 113 | 0 | return 0; | |||||||||||||
| 114 | } | ||||||||||||||
| 115 | |||||||||||||||
| 116 | 0 | idx = nodes[idx].next; | |||||||||||||
| 117 | } | ||||||||||||||
| 118 | else { | ||||||||||||||
| 119 | 0 | ▶ | if(key_size > nodes[ nodes[idx].prev ].str_size) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 120 | 0 | return 0; | |||||||||||||
| 121 | } | ||||||||||||||
| 122 | |||||||||||||||
| 123 | 0 | idx = nodes[idx].prev; | |||||||||||||
| 124 | } | ||||||||||||||
| 125 | } | ||||||||||||||
| 126 | |||||||||||||||
| 127 | 0 | return 0; | |||||||||||||
| 128 | } | ||||||||||||||
| 129 | |||||||||||||||
| 130 | 0 | mctree_index_t mctree_insert_child(mctree_t* mctree, mctree_index_t idx, const char* key, size_t key_size, void* value) | |||||||||||||
| 131 | { | ||||||||||||||
| 132 | 0 | mctree_node_t* nodes = mctree->nodes; | |||||||||||||
| 133 | 0 | mctree_index_t new_idx = mctree_node_get_free_id(mctree); | |||||||||||||
| 134 | |||||||||||||||
| 135 | 0 | nodes[idx].child = new_idx; | |||||||||||||
| 136 | |||||||||||||||
| 137 | 0 | nodes[new_idx].str = key; | |||||||||||||
| 138 | 0 | nodes[new_idx].str_size = key_size; | |||||||||||||
| 139 | 0 | nodes[new_idx].value = value; | |||||||||||||
| 140 | |||||||||||||||
| 141 | 0 | ▶ | mctree_node_add(mctree); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 142 | |||||||||||||||
| 143 | 0 | return new_idx; | |||||||||||||
| 144 | } | ||||||||||||||
| 145 | |||||||||||||||
| 146 | 0 | mctree_index_t mctree_insert_after(mctree_t* mctree, mctree_index_t idx, const char* key, size_t key_size, void* value) | |||||||||||||
| 147 | { | ||||||||||||||
| 148 | 0 | mctree_node_t* nodes = mctree->nodes; | |||||||||||||
| 149 | 0 | mctree_index_t new_idx = mctree_node_get_free_id(mctree); | |||||||||||||
| 150 | |||||||||||||||
| 151 | 0 | ▶ | if(nodes[idx].next) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 152 | 0 | nodes[ nodes[idx].next ].prev = new_idx; | |||||||||||||
| 153 | 0 | nodes[new_idx].next = nodes[idx].next; | |||||||||||||
| 154 | } | ||||||||||||||
| 155 | |||||||||||||||
| 156 | 0 | nodes[idx].next = new_idx; | |||||||||||||
| 157 | 0 | nodes[new_idx].prev = idx; | |||||||||||||
| 158 | |||||||||||||||
| 159 | 0 | nodes[new_idx].str = key; | |||||||||||||
| 160 | 0 | nodes[new_idx].str_size = key_size; | |||||||||||||
| 161 | 0 | nodes[new_idx].value = value; | |||||||||||||
| 162 | |||||||||||||||
| 163 | 0 | ▶ | mctree_node_add(mctree); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 164 | |||||||||||||||
| 165 | 0 | return new_idx; | |||||||||||||
| 166 | } | ||||||||||||||
| 167 | |||||||||||||||
| 168 | 0 | mctree_index_t mctree_insert_before(mctree_t* mctree, mctree_index_t idx, const char* key, size_t key_size, void* value) | |||||||||||||
| 169 | { | ||||||||||||||
| 170 | 0 | mctree_node_t* nodes = mctree->nodes; | |||||||||||||
| 171 | 0 | mctree_index_t new_idx = mctree_node_get_free_id(mctree); | |||||||||||||
| 172 | |||||||||||||||
| 173 | 0 | ▶ | if(nodes[idx].prev) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 174 | 0 | nodes[ nodes[idx].prev ].next = new_idx; | |||||||||||||
| 175 | 0 | nodes[new_idx].prev = nodes[idx].prev; | |||||||||||||
| 176 | } | ||||||||||||||
| 177 | |||||||||||||||
| 178 | 0 | nodes[idx].prev = new_idx; | |||||||||||||
| 179 | 0 | nodes[new_idx].next = idx; | |||||||||||||
| 180 | |||||||||||||||
| 181 | 0 | nodes[new_idx].str = key; | |||||||||||||
| 182 | 0 | nodes[new_idx].str_size = key_size; | |||||||||||||
| 183 | 0 | nodes[new_idx].value = value; | |||||||||||||
| 184 | |||||||||||||||
| 185 | 0 | ▶ | mctree_node_add(mctree); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 186 | |||||||||||||||
| 187 | 0 | return new_idx; | |||||||||||||
| 188 | } | ||||||||||||||
| 189 | |||||||||||||||
| 190 | 0 | mctree_index_t __mtree_insert_to_start(mctree_t* mctree, mctree_index_t idx, const char* key, size_t key_size, void* value, mctree_before_insert_f b_insert) | |||||||||||||
| 191 | { | ||||||||||||||
| 192 | 0 | mctree_node_t* nodes = mctree->nodes; | |||||||||||||
| 193 | |||||||||||||||
| 194 | 0 | ▶ | while (idx) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 195 | { | ||||||||||||||
| 196 | 0 | ▶ | if(key_size == nodes[idx].str_size) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 197 | 0 | ▶ | if(memcmp((const void *)key, (const void *)nodes[idx].str, key_size) == 0) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 198 | { | ||||||||||||||
| 199 | 0 | ▶ | if(value) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 200 | 0 | nodes[idx].value = value; | |||||||||||||
| 201 | |||||||||||||||
| 202 | 0 | return idx; | |||||||||||||
| 203 | } | ||||||||||||||
| 204 | |||||||||||||||
| 205 | 0 | ▶ | if(nodes[idx].child == 0) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 206 | 0 | ▶ | if(b_insert) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 207 | 0 | b_insert(key, key_size, &value); | |||||||||||||
| 208 | |||||||||||||||
| 209 | 0 | return mctree_insert_child(mctree, idx, key, key_size, value); | |||||||||||||
| 210 | } | ||||||||||||||
| 211 | |||||||||||||||
| 212 | 0 | idx = nodes[idx].child; | |||||||||||||
| 213 | } | ||||||||||||||
| 214 | 0 | ▶ | else if(key_size > nodes[idx].str_size) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 215 | { | ||||||||||||||
| 216 | 0 | ▶ | if(nodes[idx].next == 0 || key_size < nodes[ nodes[idx].next ].str_size) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 217 | 0 | ▶ | if(b_insert) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 218 | 0 | b_insert(key, key_size, &value); | |||||||||||||
| 219 | |||||||||||||||
| 220 | 0 | return mctree_insert_after(mctree, idx, key, key_size, value); | |||||||||||||
| 221 | } | ||||||||||||||
| 222 | |||||||||||||||
| 223 | 0 | idx = nodes[idx].next; | |||||||||||||
| 224 | } | ||||||||||||||
| 225 | else { | ||||||||||||||
| 226 | 0 | ▶ | if(nodes[idx].prev == 0 || key_size > nodes[ nodes[idx].prev ].str_size) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 227 | 0 | ▶ | if(b_insert) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 228 | 0 | b_insert(key, key_size, &value); | |||||||||||||
| 229 | |||||||||||||||
| 230 | 0 | return mctree_insert_before(mctree, idx, key, key_size, value); | |||||||||||||
| 231 | } | ||||||||||||||
| 232 | |||||||||||||||
| 233 | 0 | idx = nodes[idx].prev; | |||||||||||||
| 234 | } | ||||||||||||||
| 235 | } | ||||||||||||||
| 236 | |||||||||||||||
| 237 | 0 | return 0; | |||||||||||||
| 238 | } | ||||||||||||||
| 239 | |||||||||||||||
| 240 | 0 | mctree_index_t mctree_insert(mctree_t* mctree, const char* key, size_t key_size, void* value, mctree_before_insert_f b_insert) | |||||||||||||
| 241 | { | ||||||||||||||
| 242 | 0 | mctree_node_t* start = mctree->nodes; | |||||||||||||
| 243 | |||||||||||||||
| 244 | 0 | ▶ | if(key_size > 0) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 245 | 0 | mctree_index_t idx = mctree_make_first_idx(mctree, key, key_size); | |||||||||||||
| 246 | |||||||||||||||
| 247 | 0 | ▶ | if(start[idx].child) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 248 | 0 | return __mtree_insert_to_start(mctree, start[idx].child, key, key_size, value, b_insert); | |||||||||||||
| 249 | } | ||||||||||||||
| 250 | else { | ||||||||||||||
| 251 | 0 | ▶ | if(b_insert) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 252 | 0 | b_insert(key, key_size, &value); | |||||||||||||
| 253 | |||||||||||||||
| 254 | 0 | return mctree_insert_child(mctree, idx, key, key_size, value); | |||||||||||||
| 255 | } | ||||||||||||||
| 256 | } | ||||||||||||||
| 257 | |||||||||||||||
| 258 | 0 | return 0; | |||||||||||||
| 259 | } | ||||||||||||||
| 260 | |||||||||||||||
| 261 | 0 | mctree_index_t mctree_search(mctree_t* mctree, const char* key, size_t key_size) | |||||||||||||
| 262 | { | ||||||||||||||
| 263 | 0 | mctree_node_t* start = mctree->nodes; | |||||||||||||
| 264 | |||||||||||||||
| 265 | 0 | ▶ | if(key_size > 0) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 266 | 0 | mctree_index_t idx = mctree_make_first_idx(mctree, key, key_size); | |||||||||||||
| 267 | |||||||||||||||
| 268 | 0 | ▶ | if(start[idx].child) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 269 | 0 | return __mtree_search_to_start(mctree, start[idx].child, key, key_size); | |||||||||||||
| 270 | } | ||||||||||||||
| 271 | } | ||||||||||||||
| 272 | |||||||||||||||
| 273 | 0 | return 0; | |||||||||||||
| 274 | } | ||||||||||||||
| 275 | |||||||||||||||
| 276 | 0 | mctree_index_t mctree_search_lowercase(mctree_t* mctree, const char* key, size_t key_size) | |||||||||||||
| 277 | { | ||||||||||||||
| 278 | 0 | mctree_node_t* start = mctree->nodes; | |||||||||||||
| 279 | |||||||||||||||
| 280 | 0 | ▶ | if(key_size > 0) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 281 | 0 | mctree_index_t idx = mctree_make_first_idx(mctree, key, key_size); | |||||||||||||
| 282 | |||||||||||||||
| 283 | 0 | ▶ | if(start[idx].child) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 284 | 0 | return __mtree_search_lowercase_to_start(mctree, start[idx].child, key, key_size); | |||||||||||||
| 285 | } | ||||||||||||||
| 286 | } | ||||||||||||||
| 287 | |||||||||||||||
| 288 | 0 | return 0; | |||||||||||||
| 289 | } | ||||||||||||||
| 290 | |||||||||||||||
| 291 | |||||||||||||||
| 292 | |||||||||||||||