int main(void) { size_t max = 100; size_t n = 20; char buf[strsiz]; size_t i; st_t st = st_init(max); for (i = 0; i < n; i++) { item_t t = newitem(randkey(), randstr(buf, strsiz)); st_insert(st, t); } st_sort(st, print); printf("\nThe item with key 11 is: "); print(st_search(st, 11)); printf("\nThe 4th smallest key is: "); print(st_select(st, 4)); st_delete(st, st_search(st, 11)); printf("\n delete the item with key 11.\n"); st_sort(st, print); /* delete all item */ while (!st_empty(st)) { item_t x = st_select(st, 0); printf("delete item: "); print(x); st_delete(st, st_select(st, 0)); } st_finalize(&st); return 0; }
int main(void) { int n = 10; int i; st_t st; st = st_init(n); for (i = 0; i < n; i++) { st_insert(st, i); } for (i = 0; i < n; i++) { if (st_search(st, i)) { printf("Found key %2d\n", i); } st_delete(st, i); } printf("The size of the table is: %u\n", st_size(st)); st_finalize(&st); return 0; }
void st_searchinsert(T t, item_t item) { if (st_search(t, getkey(item)) == NULL) { st_insert(t, item); } }
/* the main thread */ void search_start(void *v) { SearchThread *st = (SearchThread *)v; PRIntervalTime timer; int notBound = 1, res = LDAP_SUCCESS, searches = 0; PRUint32 span; st_seed(st); st->alive = 1; st->ld = 0; while (1) { timer = PR_IntervalNow(); /* bind if we need to */ if (doBind || notBound) { res = st_bind(st); if (noDelay) st_enableTCPnodelay(st); if (!res) { st_unbind(st); continue; /* error */ } notBound = 0; } /* do the operation */ if (!noOp) { switch(opType) { case op_modify: res = st_modify_nonidx(st); break; case op_idxmodify: res = st_modify_idx(st); break; case op_search: res = st_search(st); break; case op_compare: res = st_compare(st); break; case op_delete: res = st_delete(st); break; default: fprintf(stderr, "Illegal operation type specified.\n"); return; } } else { /* Fake status for NOOP */ res = LDAP_SUCCESS; } if (LDAP_SUCCESS == res) { st->retry = 0; } else if (LDAP_CONNECT_ERROR == res && st->retry < 10) { st->retry++; } else { break; /* error */ } if (doBind) { if (noUnBind) st_disconnect(st); st_unbind(st); } else if (reconnect) { searches++; if (searches >= reconnect) { /* unceremoniously disconnect, reconnect next cycle */ st_disconnect(st); st_unbind(st); notBound = 1; searches = 0; } } span = PR_IntervalToMilliseconds(PR_IntervalNow()-timer); /* update data */ PR_Lock(st->lock); if (0 == st->retry) { /* only when succeeded */ st->searchCount++; if (st->mintime > span) st->mintime = span; if (st->maxtime < span) st->maxtime = span; } st->alive = 1; PR_Unlock(st->lock); } }