High speed 8 byte symbols

 #include <stdio.h>
 #include <stdlib.h>
 typedef union {
long l;
char s[8];
} Text8;

Text8 t;

Text8 NewText8(char * sym) {
int i;
Text8 t;
// printf("Str %s\n",sym);
t.l=0;
     for(i=0;i < 8;i++) {
if(!sym[i]) break;;
t.l = t.l << 8 |(sym[i]);
}
  //printf("Sym %lx\n",t.l);
  return(t);
}
Text8 EmitText8(Text8 t) {
int i;
//printf("Text8 ");
     for(i=7;i >= 0;i--) {
if(t.s[i])
printf("%c",t.s[i]);
}
  printf("\n");
}
typedef struct {
Text8 val;
void *  p1;
void * p2;
}Sym;
typedef Sym * PSym;
Sym top= {0x4a4b4c4d4c4b4a49,0,0};
PSym  syms= &top;
void PrintSyms(PSym s) {
if(s) {
EmitText8(s->val);
if(s->p1) PrintSyms(s->p1);
if(s->p2) PrintSyms(s->p2);
}
}
Sym * GetText8Sym(Text8 t,PSym * prev) {
Text8 s;
PSym  sm= syms; // enter at syms[0];
while(sm) {
if(*prev) *prev=sm;
s=sm->val;
if(s.l == t.l) return(sm);
if(t.l < s.l ) {
if(!sm->p1 )  {
sm->p1 = (void *) calloc(1,sizeof(Sym));
((PSym) sm->p1)->val= t;
return(sm->p1);
} else
sm = sm->p1;
}
else {
if(!sm->p2) {
sm->p2=( void *) calloc(1,sizeof(Sym));
((PSym) sm->p2)->val = t;
return(sm->p2);
} else
   sm =  sm->p2;
  }

}
return(sm);
}
void DelText8Syms(PSym s) {
if(s) {
if(s->p1) {
DelText8Syms( s->p1);
free(s->p1);
s->p1=0;
}
if(s->p2) {
DelText8Syms(s->p2);
free(s->p2);
s->p2=0;
}
}
}
#ifdef SHARED
int Entry(int * argc,void (=* args[]) {
int idx=*aargc;
if(!strcmp("DelSym") {}
if(!strcmp("NewSym") {}
}
#else
extern char * words[];
extern int nwords;
int main(void) {
int i,j;
PSym get,p;
char ch = 65;
  printf("Num Words: %d\n",nwords);
   for(i=0;i < 40;i++) {
  j = rand() % nwords;
  printf("%3d %s ",j, words[j]);
  t = NewText8(words[j]);
  EmitText8(t);
GetText8Sym(t,&p);
   }
  PrintSyms(syms);
  DelText8Syms(syms);
return(0);
}
#endif

No comments: