int find_keyword(const char* inbytes, size_t* length, const language_zh_map *m, int begin, int end, const int whence) { int location, offset; size_t wwidth, nwidth; if((offset = binary_find(inbytes, length, m, begin, end)) == -1) return -1; /* match the most accurate value */ wwidth = *length; do{ location = offset; *length = wwidth; nwidth = utf8_char_width(const_bin_c_str(inbytes+wwidth)); wwidth += nwidth; } while(nwidth != 0 && (offset = binary_find(inbytes, &wwidth, m, offset, end)) != -1); /* extention word fix */ if(!match_cond(cond_ptr(m, location), inbytes, strlen(map_key(m, location)), whence)) { *length = utf8_char_width(const_bin_c_str(inbytes)); return -1; } return location; }
void* CSlmBuilder::FindChild(int lvl, TNode* root, TSIMWordId id) { int chh = root->child, cht = (root + 1)->child; if (lvl == nlevel - 1) { TLeaf* pleaf = &((*(TLeafLevel*)level[lvl + 1])[0]); return (void*)binary_find(pleaf, chh, cht, TLeaf(id)); } else { TNode* pnode = &((*(TNodeLevel*)level[lvl + 1])[0]); return (void*)binary_find(pnode, chh, cht, TNode(id)); } }
// Query the names of PCI device. void pci_query_names(PCIDevice* pci, const char** pci_vendor, const char** pci_device, const char** pci_class, const char** pci_subclass) { // Query about the vendor. if (pci_vendor != NULL) { VendorData *vendor = binary_find(vendor_data, karraysize(vendor_data), sizeof(vendor_data[0]), pci, compare_vendor); *pci_vendor = (vendor != NULL) ? (vendor->vendor_name) : NULL; } // Query about the device. if (pci_device != NULL) { VendorDeviceData *device = binary_find(vendor_device_data, karraysize(vendor_device_data), sizeof(vendor_device_data[0]), pci, compare_vendor_device); *pci_device = (device != NULL) ? (device->device_name) : NULL; } // Query about the class. if (pci_class != NULL) { ClassData *_class = binary_find(class_data, karraysize(class_data), sizeof(class_data[0]), pci, compare_class); *pci_class = (_class != NULL) ? (_class->class_name) : NULL; } // Query about the subclass. if (pci_subclass != NULL) { SubclassData *subclass = binary_find(subclass_data, karraysize(subclass_data), sizeof(subclass_data[0]), pci, compare_subclass); *pci_subclass = (subclass != NULL) ? (subclass->subclass_name) : NULL; } }
/* {{{ int binary_find(cconv_t cd, const char* inbytes, int length, int begin, int end) */ int binary_find(const char* inbytes, size_t* length, const language_zh_map *m, int begin, int end) { int middle, last, next_fix = 0; int ret, offset = -1; size_t width, wwidth, nwidth; middle = (begin + end) >> 1; width = *length; last = end; while(1) { ret = memcmp(m[middle].key, inbytes, width); if(ret == 0) { if(width == strlen(m[middle].key)) return middle; /* word key */ if(next_fix == 0) { nwidth = utf8_char_width(const_bin_c_str(inbytes+width)); wwidth = width + nwidth; if(nwidth != 0 && memcmp(m[middle].key, inbytes, wwidth) <= 0) { while (nwidth != 0 && (offset = binary_find(inbytes, &wwidth, m, offset, end)) != -1) { if(wwidth == strlen(m[offset].key)) return offset; nwidth = utf8_char_width(const_bin_c_str(inbytes+width)); wwidth += nwidth; } next_fix = 1; } } ret = 1; } if(ret > 0) { end = middle - 1; middle = (begin + end) >> 1; } else if(0 > ret)
int main(int argc, char *argv[]) { int key; rand_a(a, MAX); show_a(a, MAX); qsort(a, MAX, sizeof(int), compare); show_a(a, MAX); printf("pls input key for search: "); scanf("%d", &key); int indx; // indx = binary_search(a, MAX, key); indx = binary_find(a, MAX, key); printf("the indx is %d\n", indx); return 0; }
int main(){ vector<int> primes; int upper = 10000; primeWithin(primes, limit);//2 3 5 7 primes[2] = 3;// ignore 2 5 which could not be primejointer; int cnt = 0; for(unsigned int i = 2; i< primes.size(); ++i){ if(primes[i] < upper) ++cnt; else break; } //candidates are picked for each number vector<vector<int> > plist (cnt, IntVec() ); for(int i =2; i < cnt +2; ++i){ int px = primes[i]; for(int j= i+1; j < cnt +2; ++j){ if(isPrimeJoint(px, primes[j], primes) && isPrimeJoint(primes[j], px, primes)) plist[i-2].push_back(primes[j]); } } vector<int> v12, v123, v1234; for(unsigned int i1 = 0; i1 < plist.size(); ++i1){ //start of all the list int p1 = primes[i1+2]; // first prime owner for(unsigned int i2 =0; i2 < plist[i1].size(); ++i2){ int p2=plist[i1][i2]; int pi2 = binary_find(p2, primes); assert( pi2 > 0); v12.clear(); set_intersection(plist[i1].begin(), plist[i1].end(), plist[pi2-2].begin(), plist[pi2-2].end(), back_inserter(v12)); if(v12.size() < 3) continue; for(unsigned int i3 =0 ; i3 < v12.size(); ++i3){ int p3 = v12[i3]; int pi3 = binary_find(p3, primes); assert(pi3 >0); v123.clear(); set_intersection(plist[pi3-2].begin(), plist[pi3-2].end(), v12.begin(), v12.end(), back_inserter(v123)); if(v123.size() < 2 ) continue; for(unsigned int i4 = 0; i4 < v123.size(); ++i4){ int p4 = v123[i4]; int pi4 = binary_find(p4, primes); assert(pi4 > 0); v1234.clear(); set_intersection(plist[pi4-2].begin(), plist[pi4-2].end(), v123.begin(), v123.end(), back_inserter(v1234)); if( v1234.empty()) continue; else{ int p5 = v1234.front(); printf("%d %d %d %d %d %d\n",p1, p2, p3, p4, p5, p1+p2+p3+p4+p5); } } } } } }
inline errr binary_find(const Target& x, const STLContainer& ref_data) { // actually a binary search return binary_find(x,ref_data.data(),ref_data.size()); }