1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
#include <stddef.h> /* size_t, uintptr_t */
#include <stdint.h> /* for uint8_t, uint32_t */
#include <string.h>
#include <stdlib.h>
#include "critbit.h"
typedef struct {
void* child[2];
uint32_t byte;
uint8_t otherbits;
} critbit0_node;
#if 0
typedef struct{
void* root;
} critbit0_tree;
#endif
int critbit0_contains(critbit0_tree* t,const char* u) {
const uint8_t* ubytes= (void*)u;
const size_t ulen= strlen(u);
uint8_t* p= t->root;
if (!p) return 0;
while ((uintptr_t)p & 1) {
critbit0_node* q = (void*)(p-1);
uint8_t c = 0;
if (q->byte<ulen)
c = ubytes[q->byte];
const int direction = (1+(q->otherbits|c))>>8;
p = q->child[direction];
}
return 0==strcmp(u,(const char*)p);
}
int critbit0_insert(critbit0_tree* t,const char* u) {
const uint8_t* const ubytes = (void*)u;
const size_t ulen = strlen(u);
uint8_t* p = t->root;
if (!p) {
char* x = malloc(ulen+1);
if (!x) return 0;
memcpy(x,u,ulen+1);
t->root= x;
return 2;
}
while (1&(intptr_t)p) {
critbit0_node* q = (void*)(p-1);
uint8_t c = 0;
if (q->byte<ulen)
c = ubytes[q->byte];
const int direction = (1+(q->otherbits|c))>>8;
p = q->child[direction];
}
uint32_t newbyte;
uint32_t newotherbits;
for (newbyte = 0; newbyte < ulen; ++newbyte) {
if (p[newbyte] != ubytes[newbyte]) {
newotherbits = p[newbyte]^ubytes[newbyte];
goto different_byte_found;
}
}
if (p[newbyte]!=0) {
newotherbits = p[newbyte];
goto different_byte_found;
}
return 1;
different_byte_found:
newotherbits |= newotherbits>>1;
newotherbits |= newotherbits>>2;
newotherbits |= newotherbits>>4;
newotherbits = (newotherbits&~(newotherbits>>1))^255;
uint8_t c = p[newbyte];
int newdirection = (1+(newotherbits|c))>>8;
critbit0_node* newnode;
if (!(newnode=malloc(sizeof(critbit0_node))))
return 0;
char* x;
if (!(x = malloc(ulen+1))) {
free(newnode);
return 0;
}
memcpy(x,ubytes,ulen+1);
newnode->byte= newbyte;
newnode->otherbits= newotherbits;
newnode->child[1-newdirection]= x;
void** wherep= &t->root;
for(;;) {
uint8_t* p = *wherep;
if (!((intptr_t)p&1))
break;
critbit0_node* q = (void*)(p-1);
if (q->byte > newbyte)break;
if (q->byte==newbyte && q->otherbits>newotherbits)break;
uint8_t c = 0;
if (q->byte<ulen)
c = ubytes[q->byte];
const int direction = (1+(q->otherbits|c))>>8;
wherep = q->child+direction;
}
newnode->child[newdirection]= *wherep;
*wherep= (void*)(1+(char*)newnode); // gcc -fanalyze false positive
return 2;
}
int critbit0_delete(critbit0_tree* t,const char* u) {
const uint8_t* ubytes = (void*)u;
const size_t ulen = strlen(u);
uint8_t* p = t->root;
void** wherep = &t->root;
void** whereq = 0;
critbit0_node* q = 0;
int direction = 0;
if (!p) return 0;
while ((intptr_t)p&1) {
whereq = wherep;
q = (void*)(p-1);
uint8_t c = 0;
if (q->byte<ulen)
c = ubytes[q->byte];
direction = (1+(q->otherbits|c))>>8;
wherep = q->child+direction;
p = *wherep;
}
if (0!=strcmp(u,(const char*)p))
return 0;
free(p);
if (!whereq) {
t->root = 0;
return 1;
}
*whereq = q->child[1-direction];
free(q);
return 1;
}
static void traverse(void* top) {
uint8_t* p = top;
if ((intptr_t)p&1) {
critbit0_node* q = (void*)(p-1);
traverse(q->child[0]);
traverse(q->child[1]);
free(q);
} else {
free(p);
}
}
void critbit0_clear(critbit0_tree* t) {
if (t->root)
traverse(t->root);
t->root = NULL;
}
static int allprefixed_traverse(uint8_t* top,int(*handle)(const char*,void*),void* arg) {
if ((uintptr_t)top&1) {
critbit0_node* q = (void*)(top-1);
int direction;
for (direction=0; direction<2; ++direction)
switch (allprefixed_traverse(q->child[direction],handle,arg)) {
case 1: break;
case 0: return 0;
default: return-1;
}
return 1;
}
return handle((const char*)top,arg);
}
int critbit0_allprefixed(critbit0_tree* t,const char* prefix,int(*handle)(const char*,void*),void* arg) {
const uint8_t* ubytes = (void*)prefix;
const size_t ulen = strlen(prefix);
uint8_t* p = t->root;
uint8_t* top = p;
if (!p) return 1;
while ((uintptr_t)p&1) {
critbit0_node* q = (void*)(p-1);
uint8_t c = 0;
if (q->byte<ulen)
c=ubytes[q->byte];
const int direction = (1+(q->otherbits|c))>>8;
p = q->child[direction];
if (q->byte<ulen)
top = p;
}
size_t i;
for (i=0; i<ulen; ++i) {
if (p[i]!=ubytes[i])
return 1;
}
return allprefixed_traverse(top,handle,arg);
}
|