Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Initial import of the derived files for those who don't want to insta…

…ll cweb
  • Loading branch information...
commit 101aecc0558fb64f568b12a7b02cc7678274a0c9 1 parent e404058
Adam Langley agl authored

Showing 2 changed files with 374 additions and 0 deletions. Show diff stats Hide diff stats

  1. +374 0 critbit.c
  2. BIN  critbit.pdf
374 critbit.c
... ... @@ -0,0 +1,374 @@
  1 +#define _POSIX_C_SOURCE 200112
  2 +#define uint8 uint8_t
  3 +#define uint32 uint32_t
  4 +/*2:*/
  5 +#line 45 "critbit.w"
  6 +
  7 +#include <stdint.h>
  8 +#include <string.h>
  9 +#include <stdlib.h>
  10 +
  11 +#include <sys/types.h>
  12 +#include <errno.h>
  13 +
  14 +typedef struct{
  15 +void*child[2];
  16 +uint32 byte;
  17 +uint8 otherbits;
  18 +}critbit0_node;
  19 +
  20 +typedef struct{
  21 +void*root;
  22 +}critbit0_tree;
  23 +
  24 +/*:2*//*3:*/
  25 +#line 69 "critbit.w"
  26 +
  27 +int
  28 +critbit0_contains(critbit0_tree*t,const char*u){
  29 +const uint8*ubytes= (void*)u;
  30 +const size_t ulen= strlen(u);
  31 +uint8*p= t->root;
  32 +
  33 +/*4:*/
  34 +#line 86 "critbit.w"
  35 +
  36 +if(!p)return 0;
  37 +
  38 +/*:4*/
  39 +#line 76 "critbit.w"
  40 +
  41 +/*5:*/
  42 +#line 110 "critbit.w"
  43 +
  44 +while(1&(intptr_t)p){
  45 +critbit0_node*q= (void*)(p-1);
  46 +/*6:*/
  47 +#line 136 "critbit.w"
  48 +
  49 +uint8 c= 0;
  50 +if(q->byte<ulen)c= ubytes[q->byte];
  51 +const int direction= (1+(q->otherbits|c))>>8;
  52 +
  53 +/*:6*/
  54 +#line 113 "critbit.w"
  55 +
  56 +p= q->child[direction];
  57 +}
  58 +
  59 +/*:5*/
  60 +#line 77 "critbit.w"
  61 +
  62 +/*7:*/
  63 +#line 152 "critbit.w"
  64 +
  65 +return 0==strcmp(u,(const char*)p);
  66 +
  67 +/*:7*/
  68 +#line 78 "critbit.w"
  69 +
  70 +}
  71 +
  72 +/*:3*//*8:*/
  73 +#line 167 "critbit.w"
  74 +
  75 +int critbit0_insert(critbit0_tree*t,const char*u)
  76 +{
  77 +const uint8*const ubytes= (void*)u;
  78 +const size_t ulen= strlen(u);
  79 +uint8*p= t->root;
  80 +
  81 +/*9:*/
  82 +#line 191 "critbit.w"
  83 +
  84 +if(!p){
  85 +char*x;
  86 +int a= posix_memalign((void**)&x,sizeof(void*),ulen+1);
  87 +if(a)return 0;
  88 +memcpy(x,u,ulen+1);
  89 +t->root= x;
  90 +return 2;
  91 +}
  92 +
  93 +/*:9*/
  94 +#line 174 "critbit.w"
  95 +
  96 +/*5:*/
  97 +#line 110 "critbit.w"
  98 +
  99 +while(1&(intptr_t)p){
  100 +critbit0_node*q= (void*)(p-1);
  101 +/*6:*/
  102 +#line 136 "critbit.w"
  103 +
  104 +uint8 c= 0;
  105 +if(q->byte<ulen)c= ubytes[q->byte];
  106 +const int direction= (1+(q->otherbits|c))>>8;
  107 +
  108 +/*:6*/
  109 +#line 113 "critbit.w"
  110 +
  111 +p= q->child[direction];
  112 +}
  113 +
  114 +/*:5*/
  115 +#line 175 "critbit.w"
  116 +
  117 +/*10:*/
  118 +#line 203 "critbit.w"
  119 +
  120 +/*11:*/
  121 +#line 218 "critbit.w"
  122 +
  123 +uint32 newbyte;
  124 +uint32 newotherbits;
  125 +
  126 +for(newbyte= 0;newbyte<ulen;++newbyte){
  127 +if(p[newbyte]!=ubytes[newbyte]){
  128 +newotherbits= p[newbyte]^ubytes[newbyte];
  129 +goto different_byte_found;
  130 +}
  131 +}
  132 +
  133 +if(p[newbyte]!=0){
  134 +newotherbits= p[newbyte];
  135 +goto different_byte_found;
  136 +}
  137 +return 1;
  138 +
  139 +different_byte_found:
  140 +
  141 +/*:11*/
  142 +#line 204 "critbit.w"
  143 +
  144 +/*12:*/
  145 +#line 267 "critbit.w"
  146 +
  147 +while(newotherbits&(newotherbits-1))newotherbits&= newotherbits-1;
  148 +newotherbits^= 255;
  149 +uint8 c= p[newbyte];
  150 +int newdirection= (1+(newotherbits|c))>>8;
  151 +
  152 +/*:12*/
  153 +#line 205 "critbit.w"
  154 +
  155 +
  156 +/*:10*/
  157 +#line 176 "critbit.w"
  158 +
  159 +/*13:*/
  160 +#line 275 "critbit.w"
  161 +
  162 +/*14:*/
  163 +#line 286 "critbit.w"
  164 +
  165 +critbit0_node*newnode;
  166 +if(posix_memalign((void**)&newnode,sizeof(void*),sizeof(critbit0_node)))return 0;
  167 +
  168 +char*x;
  169 +if(posix_memalign((void**)&x,sizeof(void*),ulen+1)){
  170 +free(newnode);
  171 +return 0;
  172 +}
  173 +memcpy(x,ubytes,ulen+1);
  174 +
  175 +newnode->byte= newbyte;
  176 +newnode->otherbits= newotherbits;
  177 +newnode->child[1-newdirection]= x;
  178 +
  179 +/*:14*/
  180 +#line 276 "critbit.w"
  181 +
  182 +/*15:*/
  183 +#line 340 "critbit.w"
  184 +
  185 +void**wherep= &t->root;
  186 +for(;;){
  187 +uint8*p= *wherep;
  188 +if(!(1&(intptr_t)p))break;
  189 +critbit0_node*q= (void*)(p-1);
  190 +if(q->byte> newbyte)break;
  191 +if(q->byte==newbyte&&q->otherbits> newotherbits)break;
  192 +uint8 c= 0;
  193 +if(q->byte<ulen)c= ubytes[q->byte];
  194 +const int direction= (1+(q->otherbits|c))>>8;
  195 +wherep= q->child+direction;
  196 +}
  197 +
  198 +newnode->child[newdirection]= *wherep;
  199 +*wherep= (void*)(1+(char*)newnode);
  200 +
  201 +/*:15*/
  202 +#line 277 "critbit.w"
  203 +
  204 +
  205 +/*:13*/
  206 +#line 177 "critbit.w"
  207 +
  208 +
  209 +return 2;
  210 +}
  211 +
  212 +/*:8*//*16:*/
  213 +#line 363 "critbit.w"
  214 +
  215 +int critbit0_delete(critbit0_tree*t,const char*u){
  216 +const uint8*ubytes= (void*)u;
  217 +const size_t ulen= strlen(u);
  218 +uint8*p= t->root;
  219 +void**wherep= &t->root;
  220 +void**whereq= 0;
  221 +critbit0_node*q= 0;
  222 +int direction= 0;
  223 +
  224 +/*17:*/
  225 +#line 386 "critbit.w"
  226 +
  227 +if(!p)return 0;
  228 +
  229 +/*:17*/
  230 +#line 373 "critbit.w"
  231 +
  232 +/*18:*/
  233 +#line 419 "critbit.w"
  234 +
  235 +while(1&(intptr_t)p){
  236 +whereq= wherep;
  237 +q= (void*)(p-1);
  238 +uint8 c= 0;
  239 +if(q->byte<ulen)c= ubytes[q->byte];
  240 +direction= (1+(q->otherbits|c))>>8;
  241 +wherep= q->child+direction;
  242 +p= *wherep;
  243 +}
  244 +
  245 +/*:18*/
  246 +#line 374 "critbit.w"
  247 +
  248 +/*19:*/
  249 +#line 437 "critbit.w"
  250 +
  251 +if(0!=strcmp(u,(const char*)p))return 0;
  252 +free(p);
  253 +
  254 +/*:19*/
  255 +#line 375 "critbit.w"
  256 +
  257 +/*20:*/
  258 +#line 451 "critbit.w"
  259 +
  260 +if(!whereq){
  261 +t->root= 0;
  262 +return 1;
  263 +}
  264 +
  265 +*whereq= q->child[1-direction];
  266 +free(q);
  267 +
  268 +/*:20*/
  269 +#line 376 "critbit.w"
  270 +
  271 +
  272 +return 1;
  273 +}
  274 +
  275 +/*:16*//*21:*/
  276 +#line 468 "critbit.w"
  277 +
  278 +static void
  279 +traverse(void*top){
  280 +/*22:*/
  281 +#line 486 "critbit.w"
  282 +
  283 +uint8*p= top;
  284 +
  285 +if(1&(intptr_t)p){
  286 +critbit0_node*q= (void*)(p-1);
  287 +traverse(q->child[0]);
  288 +traverse(q->child[1]);
  289 +free(q);
  290 +}else{
  291 +free(p);
  292 +}
  293 +
  294 +/*:22*/
  295 +#line 471 "critbit.w"
  296 +
  297 +}
  298 +
  299 +void critbit0_clear(critbit0_tree*t)
  300 +{
  301 +if(t->root)traverse(t->root);
  302 +t->root= NULL;
  303 +}
  304 +
  305 +/*:21*//*23:*/
  306 +#line 514 "critbit.w"
  307 +
  308 +static int
  309 +allprefixed_traverse(uint8*top,
  310 +int(*handle)(const char*,void*),void*arg){
  311 +/*26:*/
  312 +#line 574 "critbit.w"
  313 +
  314 +if(1&(intptr_t)top){
  315 +critbit0_node*q= (void*)(top-1);
  316 +for(int direction= 0;direction<2;++direction)
  317 +switch(allprefixed_traverse(q->child[direction],handle,arg)){
  318 +case 1:break;
  319 +case 0:return 0;
  320 +default:return-1;
  321 +}
  322 +return 1;
  323 +}
  324 +
  325 +/*:26*/
  326 +#line 518 "critbit.w"
  327 +
  328 +/*27:*/
  329 +#line 591 "critbit.w"
  330 +
  331 +return handle((const char*)top,arg);/*:27*/
  332 +#line 519 "critbit.w"
  333 +
  334 +}
  335 +
  336 +int
  337 +critbit0_allprefixed(critbit0_tree*t,const char*prefix,
  338 +int(*handle)(const char*,void*),void*arg){
  339 +const uint8*ubytes= (void*)prefix;
  340 +const size_t ulen= strlen(prefix);
  341 +uint8*p= t->root;
  342 +uint8*top= p;
  343 +
  344 +if(!p)return 1;
  345 +/*24:*/
  346 +#line 545 "critbit.w"
  347 +
  348 +while(1&(intptr_t)p){
  349 +critbit0_node*q= (void*)(p-1);
  350 +uint8 c= 0;
  351 +if(q->byte<ulen)c= ubytes[q->byte];
  352 +const int direction= (1+(q->otherbits|c))>>8;
  353 +p= q->child[direction];
  354 +if(q->byte<ulen)top= p;
  355 +}
  356 +
  357 +/*:24*/
  358 +#line 531 "critbit.w"
  359 +
  360 +/*25:*/
  361 +#line 561 "critbit.w"
  362 +
  363 +for(size_t i= 0;i<ulen;++i){
  364 +if(p[i]!=ubytes[i])return 1;
  365 +}
  366 +
  367 +/*:25*/
  368 +#line 532 "critbit.w"
  369 +
  370 +
  371 +return allprefixed_traverse(top,handle,arg);
  372 +}
  373 +
  374 +/*:23*/
BIN  critbit.pdf
Binary file not shown

0 comments on commit 101aecc

Please sign in to comment.
Something went wrong with that request. Please try again.