/* メイン */ int main(void) { int i, target, work[N], index; clock_t start,finish; for( i = 0; i < N; i++ ) work[i] = rand() % N; start=clock(); #if LINEAR #else quick(work,0,N-1); #endif for( target = N/2-5000; target < N/2+5000; target++ ) { #if LINEAR index = l_search(work,target); #else index = b_search(work,0,N-1,target); #endif printf("TARGET=%d: ", target); if ( index != -1 ) printf("○\n"); else printf("×\n"); } finish=clock(); printf("%d\n",finish-start); return 0; }
void l_remove(List **root, int v){ List *node, *left, *right; if(!root) return; if(!*root) return; node = l_search(*root, v, &left, &right); if(!node) return; // there's no such element if(node == *root) *root = node->right_p; if(!*root) *root = node->left_p; FREE(node); if(left) left->right_p = right; if(right) right->left_p = left; }
List *l_insert(List *root, int v){ List *node, *L, *R; node = l_search(root, v, &L, &R); if(node) return node; // we found exact value V // there's no exact value: insert new if((node = (List*) Malloc(1, sizeof(List))) == 0) return NULL; // allocation error node->data = v; // insert data // add neighbours: node->left_p = L; node->right_p = R; // fix neighbours: if(L) L->right_p = node; if(R) R->left_p = node; return node; }
int main(void) { List *tp, *root_p = NULL; int i, ins[] = {4,2,6,1,3,4,7}; for(i = 0; i < 7; i++) if(!(root_p = l_insert(root_p, ins[i]))) err(1, "Malloc error"); // can't insert for(i = 0; i < 9; i++){ tp = l_search(root_p, i, NULL, NULL); printf("%d ", i); if(!tp) printf("not "); printf("found\n"); } printf("now delete items 1, 4 and 8\n"); l_remove(&root_p, 1); l_remove(&root_p, 4); l_remove(&root_p, 8); for(i = 0; i < 9; i++){ tp = l_search(root_p, i, NULL, NULL); printf("%d ", i); if(!tp) printf("not "); printf("found\n"); } return 0; }
/* RETURN a pointer to an element with the given key (if a hash is not unique and there're many elements with this key - the "first" matching element) NULL if nothing is found MY_ERRPTR if OOM NOTE see l_search() for pin usage notes */ void *lf_hash_search(LF_HASH *hash, LF_PINS *pins, const void *key, uint keylen) { LF_SLIST * volatile *el, *found; uint bucket, hashnr= calc_hash(hash, (uchar *)key, keylen); bucket= hashnr % hash->size; lf_rwlock_by_pins(pins); el= _lf_dynarray_lvalue(&hash->array, bucket); if (unlikely(!el)) return MY_ERRPTR; if (*el == NULL && unlikely(initialize_bucket(hash, el, bucket, pins))) return MY_ERRPTR; found= l_search(el, hash->charset, my_reverse_bits(hashnr) | 1, (uchar *)key, keylen, pins); lf_rwunlock_by_pins(pins); return found ? found+1 : 0; }
/* RETURN a pointer to an element with the given key (if a hash is not unique and there're many elements with this key - the "first" matching element) NULL if nothing is found NOTE see l_search() for pin usage notes */ void *lf_hash_search_using_hash_value(LF_HASH *hash, LF_PINS *pins, my_hash_value_type hashnr, const void *key, uint keylen) { LF_SLIST * volatile *el, *found; uint bucket; /* hide OOM errors - if we cannot initalize a bucket, try the previous one */ for (bucket= hashnr % hash->size; ;bucket= my_clear_highest_bit(bucket)) { el= lf_dynarray_lvalue(&hash->array, bucket); if (el && (*el || initialize_bucket(hash, el, bucket, pins) == 0)) break; if (unlikely(bucket == 0)) return 0; /* if there's no bucket==0, the hash is empty */ } found= l_search(el, hash->charset, my_reverse_bits(hashnr) | 1, (uchar *)key, keylen, pins); return found ? found+1 : 0; }
/* メイン */ int main(void) { int a[N], i, target, work[N], index; for( i = 0; i < N; i++ ) work[i] = rand() % N; #if LINEAR #else quick(work,0,N-1); #endif for( target = N/2-500; target < N/2+500; target++ ) { #if LINEAR index = l_search(work,target); #else index = b_search(work,0,N-1,target); #endif printf("TARGET=%d: ", target); if ( index != -1 ) printf("○\n"); else printf("×\n"); } return 0; }