int bsearch2(vector<int> &a,int k,int j1, int j2) { if (j1==j2-1) { if (a[j1]==k) return j1; else return j2; } else { int p = (j1+j2)/2; if (k<a[p]) return bsearch2(a,k,j1,p); else return bsearch2(a,k,p,j2); } }
void bsearch_test(void) { size_t i, j, num; int arr[10000], v, *p; srand((unsigned int)time(NULL)); for (num = i = 0; i < sizeof(arr)/sizeof(arr[0]); i++) { v = rand(); if (0 == bsearch2(&v, arr, (const void**)&p, num, sizeof(int), bsearch_int_compare)) { assert(v == *p); } else { memmove(p + 1, p, (num - (p - arr)) * sizeof(int)); *p = v; ++num; for (j = 1; j < num; j++) { assert(arr[j - 1] < arr[j]); } } } printf("bsearch test ok\n"); }
PMETHOD objrtn findLE(object self, ifun cfun, object key, object *foundKey) { BTreeNode_iv_t *iv = GetIVs(BTreeNode, self); int found, idx; object ret; found = bsearch2(iv, cfun, key, &idx); if (iv->iType == 2) if (found) { if (foundKey) *foundKey = iv->iKeys[idx]; return iv->iObjects[idx]; } else if (idx) { if (foundKey) *foundKey = iv->iKeys[idx-1]; return iv->iObjects[idx-1]; } else { if (foundKey) *foundKey = NULL; return NULL; } for (idx=idx+found ; idx >= 0 ; idx--) if (ret = findLE(iv->iObjects[idx], cfun, key, foundKey)) return ret; return NULL; }
int main() { const int len = 10; int a[] = {-1, -2, -3, 2, 3, 8, 9, 12, 20, 100}; int x = 100; int result = bsearch(a, 0, len-1, x); printf("%d is at index %d\n", x, result); int result2 = bsearch2(a, len-1, x); printf("%d is at index %d\n", x, result2); }
// Trim unused memory, sort scans and calculate neighbours in a spectrum void optimizeSpectrum(Spectrum *spec) { spec->alloc = spec->len; spec->scans = realloc(spec->scans,spec->alloc*sizeof(Scan)); if (spec->scans == NULL) infox ("Couldn't trim scans array.", -1); qsort(spec->scans, spec->len, sizeof(Scan), compScan); for(int a=0; a<spec->len ;++a) { double lo_RT = spec->scans[a].RT - RT_dist; double hi_RT = spec->scans[a].RT + RT_dist; spec->scans[a].lo_nb = bsearch2(&lo_RT, &(spec->scans[0]), a+1, sizeof(Scan), compScan, 1); if (spec->scans[a].lo_nb == NULL) infox ("Couldn't find low scan neighbour.", -5); spec->scans[a].lo_nb = bsearch2(&hi_RT, &(spec->scans[a]), spec->len-a, sizeof(Scan), compScan, -1); if (spec->scans[a].hi_nb == NULL) infox ("Couldn't find high scan neighbour.", -5); optimizeScan(&(spec->scans[a])); } }
PMETHOD objrtn find(object self, ifun cfun, object key, object *foundKey) { BTreeNode_iv_t *iv = GetIVs(BTreeNode, self); int found, idx; found = bsearch2(iv, cfun, key, &idx); if (iv->iType == 2) if (found) { if (foundKey) *foundKey = iv->iKeys[idx]; return iv->iObjects[idx]; } else return NULL; return find(iv->iObjects[found+idx], cfun, key, foundKey); }
int main() { int u,v; for(u=0;u<10;u++) c[u]=0; sieve1(100005); sieve2(100005); // printf("--\n"); // return 0; // for(u=0;u<10;u++) // printf("%d-->%d\n",u,c[u]); scanf("%d",&v); for(u=0;u<v;u++) { int a=0,b=0,d=0,i=0,j=0,sum=0; scanf("%d%d%d",&a,&b,&d); // sum=0; // for(i=a;i<=b;i++) // { // if(mat[i]==1 && chkd(i,d)) // { // sum++; // } // } int b1,b2; b1=bsearch1(a,d); b2=bsearch2(b,d); // printf("b1==%d b2==%d\n",b1,b2); printf("%d\n",b2-b1+1); } return 0; }
PMETHOD objrtn findGT(object self, ifun cfun, object key, object *foundKey) { BTreeNode_iv_t *iv = GetIVs(BTreeNode, self); int found, idx; object ret; found = bsearch2(iv, cfun, key, &idx); if (found) idx++; if (iv->iType == 2) if (idx != iv->iUsed) { if (foundKey) *foundKey = iv->iKeys[idx]; return iv->iObjects[idx]; } else { if (foundKey) *foundKey = NULL; return NULL; } for ( ; idx <= iv->iUsed ; idx++) if (ret = findGT(iv->iObjects[idx], cfun, key, foundKey)) return ret; return NULL; }
delete_intermediate_pointer(iv, lo); } else { int idx; for (idx=0 ; iv->iObjects[idx] != lo ; idx++); iv->iObjects[idx] = save_pointer; gDeepDispose(lo); } } PMETHOD objrtn delete(object self, ifun cfun, object key, int deep, object prev) { BTreeNode_iv_t *iv = GetIVs(BTreeNode, self); int found, idx; object res; found = bsearch2(iv, cfun, key, &idx); if (iv->iType == 2) { if (!found) return NULL; if (iv->iUsed == 1 && prev) { iv->iPrevious = prev; collapse(iv, self, deep, NULL); } else { int n = iv->iUsed - idx - 1; if (deep) { gDeepDispose(iv->iKeys[idx]); gDeepDispose(iv->iObjects[idx]); } MEMMOVE(iv->iKeys+idx, iv->iKeys+idx+1, n*sizeof(object)); MEMMOVE(iv->iObjects+idx, iv->iObjects+idx+1, n*sizeof(object)); iv->iUsed--;
int bsearch(vector<int> &a,int k) { int n = a.size(); if (k<a[0]) return 0; else return bsearch2(a,k,0,n); }