/* * * Find a Node based on the key, return the node and the index. * The index is valid even if the return value is NULL, in which * case the index is the corect row in which the node should be * created. * */ static SFXHASH_NODE * sfxhash_find_node_row( SFXHASH * t, void * key, int * rindex ) { unsigned hashkey; int index; SFXHASH_NODE *hnode; hashkey = t->sfhashfcn->hash_fcn( t->sfhashfcn, (unsigned char*)key, t->keysize ); /* printf("hashkey: %u t->keysize: %d\n", hashkey, t->keysize); */ /* flowkey_fprint(stdout, key); */ /* printf("****\n"); */ index = hashkey % t->nrows; *rindex = index; for( hnode=t->table[index]; hnode; hnode=hnode->next ) { if( !t->sfhashfcn->keycmp_fcn(hnode->key,key,t->keysize) ) { if( t->splay > 0 ) movetofront(t,hnode); t->find_success++; return hnode; } } t->find_fail++; return NULL; }
CEntry* cachelookup(Cache *c, char *name, int create) { int h; CEntry *e; h = hash(name) % c->nhash; for(e=c->hash[h]; e; e=e->hash.next){ if(strcmp(name, e->name) == 0){ movetofront(c, e); return e; } } if(!create) return nil; if(c->nentry >= c->maxentry) e = evict(c); else{ e = emalloc(c->sizeofentry); insertfront(c, e); c->nentry++; } e->name = estrdup(name); h = hash(name) % c->nhash; e->hash.next = c->hash[h]; c->hash[h] = e; return e; }
/* * Find a Node based on the key, return users data. */ static SFGHASH_NODE * sfghash_find_node( SFGHASH * t, void * key) { unsigned hashkey; int index, klen; SFGHASH_NODE *hnode; if( t->keysize ) { klen = t->keysize; } else { klen = strlen( (char*) key ) + 1; } hashkey = t->sfhashfcn->hash_fcn( t->sfhashfcn, (unsigned char*) key, klen ); index = hashkey % t->nrows; for( hnode=t->table[index]; hnode; hnode=hnode->next ) { if( t->keysize == 0 ) { if( !strcmp((char*)hnode->key,(char*)key) ) { if( t->splay > 0 ) movetofront(t,index,hnode); return hnode; } } else { if( !t->sfhashfcn->keycmp_fcn(hnode->key,key,t->keysize) ) { if( t->splay > 0 ) movetofront(t,index,hnode); return hnode; } } } return NULL; }
int main() { struct node *start = NULL; push(&start, 5); push(&start, 4); push(&start, 3); push(&start, 2); push(&start, 1); printlist(start); movetofront(&start); printlist(start); return 0; }