/* Resize the hash table so that it cantains "new_size" buckets. ** "new_size" must be a power of 2. The hash table might fail ** to resize if sqlite3_malloc() fails. */ static void rehash(Hash *pH, int new_size){ struct _ht *new_ht; /* The new hash table */ HashElem *elem, *next_elem; /* For looping over existing elements */ #ifdef SQLITE_MALLOC_SOFT_LIMIT if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){ new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht); } if( new_size==pH->htsize ) return; #endif /* There is a call to sqlite3_malloc() inside rehash(). If there is ** already an allocation at pH->ht, then if this malloc() fails it ** is benign (since failing to resize a hash table is a performance ** hit only, not a fatal error). */ if( pH->htsize>0 ) sqlite3BeginBenignMalloc(); new_ht = (struct _ht *)sqlite3MallocZero( new_size*sizeof(struct _ht) ); if( pH->htsize>0 ) sqlite3EndBenignMalloc(); if( new_ht==0 ) return; sqlite3_free(pH->ht); pH->ht = new_ht; pH->htsize = new_size; for(elem=pH->first, pH->first=0; elem; elem = next_elem){ int h = strHash(elem->pKey, elem->nKey) & (new_size-1); next_elem = elem->next; insertElement(pH, &new_ht[h], elem); } }
/* Resize the hash table so that it cantains "new_size" buckets. ** ** The hash table might fail to resize if sqlite3_malloc() fails or ** if the new size is the same as the prior size. ** Return TRUE if the resize occurs and false if not. */ static int rehash(Hash *pH, unsigned int new_size){ struct _ht *new_ht; /* The new hash table */ HashElem *elem, *next_elem; /* For looping over existing elements */ #if SQLITE_MALLOC_SOFT_LIMIT>0 if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){ new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht); } if( new_size==pH->htsize ) return 0; #endif /* The inability to allocates space for a larger hash table is ** a performance hit but it is not a fatal error. So mark the ** allocation as a benign. */ sqlite3BeginBenignMalloc(); new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) ); sqlite3EndBenignMalloc(); if( new_ht==0 ) return 0; sqlite3_free(pH->ht); pH->ht = new_ht; pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht); memset(new_ht, 0, new_size*sizeof(struct _ht)); for(elem=pH->first, pH->first=0; elem; elem = next_elem){ unsigned int h = strHash(elem->pKey, elem->nKey) % new_size; next_elem = elem->next; insertElement(pH, &new_ht[h], elem); } return 1; }
/* Insert an element into the hash table pH. The key is pKey,nKey ** and the data is "data". ** ** If no element exists with a matching key, then a new ** element is created. A copy of the key is made if the copyKey ** flag is set. NULL is returned. ** ** If another element already exists with the same key, then the ** new data replaces the old data and the old data is returned. ** The key is not copied in this instance. If a malloc fails, then ** the new data is returned and the hash table is unchanged. ** ** If the "data" parameter to this function is NULL, then the ** element corresponding to "key" is removed from the hash table. */ void *sqlite3HashInsert(Hash *pH, const void *pKey, int nKey, void *data){ int hraw; /* Raw hash value of the key */ int h; /* the hash of the key modulo hash table size */ HashElem *elem; /* Used to loop thru the element list */ HashElem *new_elem; /* New element added to the pH */ assert( pH!=0 ); hraw = strHash(pKey, nKey); if( pH->htsize ){ h = hraw % pH->htsize; elem = findElementGivenHash(pH,pKey,nKey,h); if( elem ){ void *old_data = elem->data; if( data==0 ){ removeElementGivenHash(pH,elem,h); }else{ elem->data = data; if( !pH->copyKey ){ elem->pKey = (void *)pKey; } assert(nKey==elem->nKey); } return old_data; } } if( data==0 ) return 0; new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) ); if( new_elem==0 ) return data; if( pH->copyKey && pKey!=0 ){ new_elem->pKey = sqlite3Malloc( nKey ); if( new_elem->pKey==0 ){ sqlite3_free(new_elem); return data; } memcpy((void*)new_elem->pKey, pKey, nKey); }else{ new_elem->pKey = (void*)pKey; } new_elem->nKey = nKey; pH->count++; if( pH->htsize==0 ){ rehash(pH, 128/sizeof(pH->ht[0])); if( pH->htsize==0 ){ pH->count = 0; if( pH->copyKey ){ sqlite3_free(new_elem->pKey); } sqlite3_free(new_elem); return data; } } if( pH->count > pH->htsize ){ rehash(pH,pH->htsize*2); } assert( pH->htsize>0 ); h = hraw % pH->htsize; insertElement(pH, &pH->ht[h], new_elem); new_elem->data = data; return 0; }
/* This function (for internal use only) locates an element in an * hash table that matches the given key. The hash for this key is * also computed and returned in the *pH parameter. */ static HashElem * findElementWithHash(const Hash * pH, /* The pH to be searched */ const char *pKey, /* The key we are searching for */ unsigned int *pHash /* Write the hash value here */ ) { HashElem *elem; /* Used to loop thru the element list */ int count; /* Number of elements left to test */ unsigned int h; /* The computed hash */ if (pH->ht) { /*OPTIMIZATION-IF-TRUE */ struct _ht *pEntry; h = strHash(pKey) % pH->htsize; pEntry = &pH->ht[h]; elem = pEntry->chain; count = pEntry->count; } else { h = 0; elem = pH->first; count = pH->count; } *pHash = h; while (count--) { assert(elem != 0); if (strcmp(elem->pKey, pKey) == 0) { return elem; } elem = elem->next; } return 0; }
/* Resize the hash table so that it cantains "new_size" buckets. ** ** The hash table might fail to resize if sqlite3_malloc() fails or ** if the new size is the same as the prior size. ** Return TRUE if the resize occurs and false if not. */ static int rehash(Hash *pH, unsigned int new_size){ struct _ht *new_ht; /* The new hash table */ HashElem *elem, *next_elem; /* For looping over existing elements */ #if SQLITE_MALLOC_SOFT_LIMIT>0 if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){ new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht); } if( new_size==pH->htsize ) return 0; #endif /* The inability to allocates space for a larger hash table is ** a performance hit but it is not a fatal error. So mark the ** allocation as a benign. Use sqlite3Malloc()/memset(0) instead of ** sqlite3MallocZero() to make the allocation, as sqlite3MallocZero() ** only zeroes the requested number of bytes whereas this module will ** use the actual amount of space allocated for the hash table (which ** may be larger than the requested amount). */ sqlite3BeginBenignMalloc(); new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) ); sqlite3EndBenignMalloc(); if( new_ht==0 ) return 0; sqlite3_free(pH->ht); pH->ht = new_ht; pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht); memset(new_ht, 0, new_size*sizeof(struct _ht)); for(elem=pH->first, pH->first=0; elem; elem = next_elem){ unsigned int h = strHash(elem->pKey, elem->nKey) % new_size; next_elem = elem->next; insertElement(pH, &new_ht[h], elem); } return 1; }
// Resize the hash table so that it cantains "new_size" buckets. // The hash table might fail to resize if system_malloc() fails or if the new size is the same as the prior size. // Return TRUE if the resize occurs and false if not. static int rehash(Hash *pH, unsigned int newSize) { struct _hashTable *newHashTable; // The new hash table HashElement *element, *nextElement; // For looping over existing elements #if SYSTEM_MALLOC_SOFTLIMIT > 0 if (newSize*sizeof(struct _hashTable) > SYSTEM_MALLOC_SOFTLIMIT) newSize = SYSTEM_MALLOC_SOFTLIMIT/sizeof(struct _hashTable); if (newSize == pH->TableSize) return 0; #endif // The inability to allocates space for a larger hash table is a performance hit but it is not a fatal error. So mark the allocation as a benign. systemBeginBenignMalloc(); newHashTable = (struct _hashTable *)systemMalloc(newSize*sizeof(struct _hashTable)); systemEndBenignMalloc(); if (newHashTable == 0) return 0; system_free(pH->Table); pH->Table = newHashTable; pH->TableSize = newSize = systemMallocSize(newHashTable)/sizeof(struct _hashTable); memset(newHashTable, 0, newSize*sizeof(struct _hashTable)); for(element = pH->First, pH->First = 0; element; element = nextElement) { unsigned int h = strHash(element->pKey, element->nKey) % newSize; nextElement = element->Next; insertElement(pH, &newHashTable[h], element); } return 1; }
/* Insert an element into the hash table pH. The key is pKey ** and the data is "data". ** ** If no element exists with a matching key, then a new ** element is created and NULL is returned. ** ** If another element already exists with the same key, then the ** new data replaces the old data and the old data is returned. ** The key is not copied in this instance. If a malloc fails, then ** the new data is returned and the hash table is unchanged. ** ** If the "data" parameter to this function is NULL, then the ** element corresponding to "key" is removed from the hash table. */ void *sqlite3HashInsert(Hash *pH, const char *pKey, void *data) { unsigned int h; /* the hash of the key modulo hash table size */ HashElem *elem; /* Used to loop thru the element list */ HashElem *new_elem; /* New element added to the pH */ assert( pH!=0 ); assert( pKey!=0 ); elem = findElementWithHash(pH,pKey,&h); if( elem ) { void *old_data = elem->data; if( data==0 ) { removeElementGivenHash(pH,elem,h); } else { elem->data = data; elem->pKey = pKey; } return old_data; } if( data==0 ) return 0; new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) ); if( new_elem==0 ) return data; new_elem->pKey = pKey; new_elem->data = data; pH->count++; if( pH->count>=10 && pH->count > 2*pH->htsize ) { if( rehash(pH, pH->count*2) ) { assert( pH->htsize>0 ); h = strHash(pKey) % pH->htsize; } } insertElement(pH, pH->ht ? &pH->ht[h] : 0, new_elem); return 0; }
static void addLock(Client cntxt, OLTPlocks locks, MalBlkPtr mb, InstrPtr p, int sch, int tbl) { BUN hash; char *r,*s; r =(sch?getVarConstant(mb, getArg(p,sch)).val.sval : "sqlcatalog"); s =(tbl? getVarConstant(mb, getArg(p,tbl)).val.sval : ""); hash = (strHash(r) ^ strHash(s)) % MAXOLTPLOCKS ; hash += (hash == 0); locks[hash] = 1; #ifdef _DEBUG_OLP_ fprintf(stderr,"#addLock %s "BUNFMT", %s "BUNFMT" combined "BUNFMT"\n", r, strHash(r), s, strHash(s),hash); #else (void) cntxt; #endif }
vector<string> findRepeatedDnaSequences(string s) { std::hash<std::string> strHash; if (s.length() < 10) return vector<string>(); for (int ind=0; ind <= s.length() - 10; ind++) { string cand = s.substr(ind, 10); auto it = seqCount.find(strHash(cand)); if (it == seqCount.end()) { seqCount[strHash(cand)] = 1; } else { seqCount[strHash(cand)] += 1; if (seqCount[strHash(cand)] == 2) { strMap.push_back(cand); } } } return strMap; }
unsigned long int Board::getHash() { std::string s; for (int row = 0; row < BOARD_SIZE; row++) for (int column = 0; column < BOARD_SIZE; column++) s += (char)positions[row*BOARD_SIZE + column].color; std::hash<std::string> strHash; return strHash(s); }
void *sqlite3HashInsert(Hash *pH, const char *pKey, int nKey, void *data){ unsigned int h; /* the hash of the key modulo hash table size */ HashElem *elem; /* Used to loop thru the element list */ HashElem *new_elem; /* New element added to the pH */ assert( pH!=0 ); assert( pKey!=0 ); assert( nKey>=0 ); if( pH->htsize ){ h = strHash(pKey, nKey) % pH->htsize;/*如果pH的htsize成员变量不为0,即使用哈希表存放数据项那么使用 strHash 函数计算桶号h*/ }else{ h = 0;/*如果pH的htsize成员变量为0,那么h=0*/ } elem = findElementGivenHash(pH,pKey,nKey,h); if( elem ){ void *old_data = elem->data; if( data==0 ){ removeElementGivenHash(pH,elem,h); }else{ elem->data = data; elem->pKey = pKey; assert(nKey==elem->nKey); } return old_data; } if( data==0 ) return 0; new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) ); if( new_elem==0 ) return data; new_elem->pKey = pKey; new_elem->nKey = nKey; new_elem->data = data; pH->count++; if( pH->count>=10 && pH->count > 2*pH->htsize ){ if( rehash(pH, pH->count*2) ){ assert( pH->htsize>0 ); h = strHash(pKey, nKey) % pH->htsize; } } if( pH->ht ){ insertElement(pH, &pH->ht[h], new_elem); }else{ insertElement(pH, 0, new_elem); } return 0; }
/* Attempt to locate an element of the hash table pH with a key ** that matches pKey,nKey. Return a pointer to the corresponding ** HashElem structure for this element if it is found, or NULL ** otherwise. */ HashElem *sqlite3HashFindElem(const Hash *pH, const void *pKey, int nKey){ int h; /* A hash on key */ HashElem *elem; /* The element that matches key */ if( pH==0 || pH->ht==0 ) return 0; h = strHash(pKey,nKey); elem = findElementGivenHash(pH,pKey,nKey, h % pH->htsize); return elem; }
// Attempt to locate an element of the hash table pH with a key that matches pKey,nKey. Return the data for this element if it is // found, or NULL if there is no match. void *systemHashFind(const Hash *pH, const char *pKey, int nKey) { HashElement *element; // The element that matches key unsigned int h; // A hash on key assert(pH != 0); assert(pKey != 0); assert(nKey >= 0); h = (pH->Table ? strHash(pKey, nKey) % pH->TableSize : 0); element = findElementGivenHash(pH, pKey, nKey, h); return (element ? element->Data : 0); }
static int intarrayMapPut(intarray_map *map, sqlite3_intarray *a) { unsigned int h1 = strHash(a->zName); int rc = mapPut_(map->hashtable, map->size, a, h1); if (rc != SQLITE_OK) return rc; map->count++; if (map->count >= map->rehashSize) { rc = rehash(map); } return rc; }
virtual void InitProp() { if(!folder_parsed) { wxArrayString files_found; wxString folderiso(ApplicationConfiguration::CONST_RESOURCE_FOLDER+ApplicationConfiguration::CONST_RESOURCE_DATA_FOLDER+ApplicationConfiguration::CONST_RESOURCE_ISO_FOLDER); int defaultIso(0); Element* isoprop(NULL); wxStringHash strHash; if(this->IsPropertyExist("isofilename",&isoprop)) { defaultIso=this->GetListConfig("isofilename"); this->DeleteElementByXmlId(isoprop->GetXmlId()); }else{ defaultIso=strHash("jet"); } std::vector<wxString> gplFileName; std::vector<int> gplFileId; if(wxDirExists(folderiso)) { if(wxDir::GetAllFiles(folderiso,&files_found,"*.gpl")) { gplFileName.reserve(files_found.size()); gplFileId.reserve(files_found.size()); for(wxArrayString::iterator itfile=files_found.begin();itfile!=files_found.end();itfile++) { fullPathIso.push_back((*itfile)); wxFileName fichGPL((*itfile)); int idFichier=(int)strHash(fichGPL.GetName()); gplFileId.push_back(idFichier); gplFileName.push_back(fichGPL.GetName()); } } } this->AppendPropertyList("isofilename","Iso-levels color",gplFileName,defaultIso,false,1,gplFileId,false); _("Iso-levels color"); folder_parsed=true; } }
/* Insert an element into the hash table pH. The key is pKey,nKey ** and the data is "data". ** ** If no element exists with a matching key, then a new ** element is created and NULL is returned. ** ** If another element already exists with the same key, then the ** new data replaces the old data and the old data is returned. ** The key is not copied in this instance. If a malloc fails, then ** the new data is returned and the hash table is unchanged. ** ** If the "data" parameter to this function is NULL, then the ** element corresponding to "key" is removed from the hash table. */ void *systemHashInsert(Hash *pH, const char *pKey, int nKey, void *data) { unsigned int h; // the hash of the key modulo hash table size HashElement *element; // Used to loop thru the element list HashElement *newElement; // New element added to the pH assert(pH != 0); assert(pKey != 0); assert(nKey >= 0); h = (pH->Table ? strHash(pKey, nKey) % pH->TableSize : 0); element = findElementGivenHash(pH, pKey, nKey, h); if (element) { void *lastData = element->Data; if (data == 0) removeElementGivenHash(pH, element, h); else { element->Data = data; element->pKey = pKey; assert(nKey == elem->nKey); } return lastData; } if (data == 0) return 0; newElement = (HashElement*)systemMalloc(sizeof(HashElement)); if (newElement == 0) return data; newElement->pKey = pKey; newElement->nKey = nKey; newElement->Data = data; pH->Count++; if ((pH->Count >= 10) && pH->Count > (2*pH->TableSize)) if (rehash(pH, pH->Count*2)) { assert(pH->TableSize > 0); h = strHash(pKey, nKey) % pH->TableSize; } if (pH->Table) insertElement(pH, &pH->Table[h], newElement); else insertElement(pH, 0, newElement); return 0; }
Doc docNewFrString(String s) { Doc doc; Length cc = strLength(s) + 1; doc = docNewEmpty(cc); doc->corpus = strcpy(doc->corpus, s); doc->hash = strHash(s); return doc; }
static sqlite3_intarray* intarrayMapFind(intarray_map *map, const char *zName) { unsigned int hash = strHash(zName); unsigned int i = hash % map->size; int j = map->size; intarray_map_entry *t = map->hashtable; while ((t[i].key || t[i].hash == -1) && j > 0) { if (hash == (unsigned int)t[i].hash && !stricmp(t[i].key, zName)) { return (sqlite3_intarray*)t[i].value; } i = (i + 1) % map->size; j--; } return 0; }
/* Attempt to locate an element of the hash table pH with a key ** that matches pKey,nKey. Return the data for this element if it is ** found, or NULL if there is no match. */ void *sqlite3HashFind(const Hash *pH, const char *pKey, int nKey){ HashElem *elem; /* The element that matches key */ unsigned int h; /* A hash on key */ assert( pH!=0 ); assert( pKey!=0 ); assert( nKey>=0 ); if( pH->ht ){ h = strHash(pKey, nKey) % pH->htsize; }else{ h = 0; } elem = findElementGivenHash(pH, pKey, nKey, h); return elem ? elem->data : 0; }
std::string Utils::getCalculationConfigHash(const std::string inputCloudFile_, const double normalEstimationRadius_, const DescriptorParamsPtr &descriptorParams_, const CloudSmoothingParams &smoothingParams_) { std::string str = ""; str += "input=" + getFileChecksum(inputCloudFile_); str += "-normalEstimationRadius=" + boost::lexical_cast<std::string>(normalEstimationRadius_); str += "-" + descriptorParams_->toString(); if (smoothingParams_.useSmoothing) str += "-" + smoothingParams_.toString(); boost::hash<std::string> strHash; return Utils::num2Hex(strHash(str)); }
void addSymbol(const char *name, type_t type) { // fprintf(stderr, "addSymbol begin\n"); int hash = strHash(name); int pos = hash; while (symbol_table[pos].valid) { pos = (pos + 1) % ID_MAX; if (pos == hash) { attrError("Too many variables"); return; } } symbol_table[pos].valid = 1; symbol_table[pos].type = type; symbol_table[pos].name = makeStr(name); // fprintf(stderr, "Saving %s to position %d\n", name, hash); // fprintf(stderr, "addSymbol succeeded\n"); }
type_t lookupSymbol(const char *name) { // fprintf(stderr, "lookupSymbol begin\n"); int hash = strHash(name); int pos = hash; while (1) { if (symbol_table[pos].valid) { if (strcmp(symbol_table[pos].name, name) == 0) { // fprintf(stderr, "lookupSymbol succeeded\n"); return symbol_table[pos].type; } } pos = (pos + 1) % ID_MAX; if (pos == hash) { break; } } return NULL; }
virtual void Modified(Element* eModif) { if(eModif->GetElementInfos().libelleElement=="isofilename") { int selectedEl=this->GetListConfig("isofilename"); wxStringHash strHash; for(std::list<wxString>::iterator itpal=this->fullPathIso.begin();itpal!=this->fullPathIso.end();itpal++) { wxFileName fileiso(*itpal); if((int)strHash(fileiso.GetName())==selectedEl) { this->UpdateStringConfig("path_isofile",(*itpal)); break; } } } E_UserPreferenceItem::Modified(eModif); }
std::pair<uint64_t, std::shared_ptr<std::string>> StringTable::get(const std::string& str) { std::hash<std::string> strHash; uint64_t hash = strHash(str); auto it = m_trackedStrings.find(hash); if (it == m_trackedStrings.end()) { m_autoCleanupCounter++; if (m_autoCleanupCounter > AUTO_CLEANUP_COUNT_THRESHOLD) { cleanup(); } auto strPtr = std::make_shared<std::string>(str); m_trackedStrings[hash] = strPtr; return std::make_pair(hash, strPtr); } else { return std::make_pair(hash, it->second); } }
static void intarrayMapRemove(intarray_map *map, sqlite3_intarray *a) { unsigned int hash = strHash(a->zName); unsigned int i = hash % map->size; int j = map->size; intarray_map_entry *t = map->hashtable; while (t[i].key && j > 0) { if (hash == (unsigned int)t[i].hash && !stricmp(t[i].key, a->zName)) { break; } i = (i + 1) % map->size; j--; } if (j > 0 && t[i].key) { /* mark for further traversal and removal */ t[i].key = 0; t[i].hash = -1; t[i].value = 0; map->count--; } }
Doc docNewFrList(TokenList tl) { Doc doc; TokenList l; Length cc = 1; for (l = tl; l; l = cdr(l)) cc += strLength(car(l)->val.str) + 1; doc = docNewEmpty(cc); doc->corpus[0] = 0; for (l = tl; l; l = cdr(l)) { strcat(doc->corpus, car(l)->val.str); strcat(doc->corpus, "\n"); } doc->hash = strHash(doc->corpus); phaseDEBUG(dbOut, "docNewFrList: cc = %d; strlen = %d\n", (int) cc, (int) strLength(doc->corpus)); return doc; }
static ObjectHandle findClassWithMeta(const char* name, ObjectHandle metaObj) { ObjectHandle newObj, nameObj, methTable; int size; newObj = globalSymbol(name); if (newObj == nilobj) { size = getInteger(metaObj->basicAt(sizeInClass)); newObj = MemoryManager::Instance()->allocObject(size); newObj->_class = metaObj; /* now make name */ nameObj = createSymbol(name); newObj->basicAtPut(nameInClass, nameObj); methTable = newDictionary(39); newObj->basicAtPut(methodsInClass, methTable); newObj->basicAtPut(sizeInClass, newInteger(size)); /* now put in global symbols table */ nameTableInsert(symbols, strHash(name), nameObj, newObj); } return newObj; }
bool Board::isPositionalSuperKo(Point* p, std::list<unsigned long int> previousHashes) { std::string s; for (int i = 0; i < BOARD_SIZE*BOARD_SIZE; i++) { if (p != &positions[i]) { if (positions[i].group != nullptr && positions[i].group->isAdjacent(p) && positions[i].group->numberLiberties == 1) { s += (char)Empty; } else { s += (char)positions[i].color; } } else { s += (char)turn; } } std::hash<std::string> strHash; unsigned long int pHash = strHash(s); for (unsigned long int pastHash : previousHashes) { if (pastHash == pHash) { return true; } } return false; }
Hash symeNameCode(Syme syme) { return strHash(symeString(syme)); }
static int getModuleIndex(str name) { return (int) (strHash(name) % MODULE_HASH_SIZE); }