void BufferManager::deleteFileFromBuffer(string fileName){ int fd = open(fileName.c_str(), O_RDONLY); struct stat sb; if(fd == -1){ fprintf(stderr, "Cannot open file: %s, in BufferManager::deleteFileFromBuffer.\n", fileName.c_str()); throw openFileError; } if (fstat(fd, &sb)) { fprintf(stderr, "File: %s, fstat error, in BufferManager::deleteFileFromBuffer.\n", fileName.c_str()); throw fstatError; } int blockCount = (int)(sb.st_size >> BUFFERSIZESHIFT); for (int part = 0; part < blockCount; part++) { if (IndexTable.find(hashKey(fileName, part)) != IndexTable.end()) { int index = IndexTable[hashKey(fileName, part)]; blocks[index].sign = 0; IndexTable.erase(hashKey(fileName, part)); } } if (close(fd) == -1) { fprintf(stderr, "Cannot close file: %s, in BufferManager::deleteFileFromBuffer.\n", fileName.c_str()); throw closeFileError; } }
int BufferManager::lookUpIndex(string fileName, int part){ if (IndexTable.find(hashKey(fileName, part)) != IndexTable.end()) { return IndexTable[hashKey(fileName, part)]; }else{ return fileToBuffer(fileName, part); } }
void Lvk::FE::ChatHistoryWidget::addDateContactTableRow(const Lvk::Cmn::Conversation::Entry &entry) { QString date = entry.dateTime.toString(DATE_FORMAT); QString username = getUsername(entry.from); QString fullname = getFullname(entry.from); if (fullname.isEmpty()) { fullname = username; } int nextRow = ui->dateContactTable->rowCount(); ui->dateContactTable->insertRow(nextRow); ui->dateContactTable->setItem(nextRow, DateColumnn, new QTableWidgetItem(date)); ui->dateContactTable->setItem(nextRow, UsernameColumn, new QTableWidgetItem(fullname)); ui->dateContactTable->item(nextRow, DateColumnn)->setData(HashKeyRole, hashKey(entry)); ui->dateContactTable->item(nextRow, UsernameColumn)->setData(HashKeyRole, hashKey(entry)); ui->dateContactTable->item(nextRow, UsernameColumn)->setData(EntryFromRole, entry.from); if (username.contains("@gmail.com")) { ui->dateContactTable->item(nextRow, UsernameColumn)->setIcon(QIcon(GMAIL_ICON)); } else if (username.contains("@chat.facebook.com")) { ui->dateContactTable->item(nextRow, UsernameColumn)->setIcon(QIcon(FB_ICON)); } else if (username.contains(tr("(test)"))) { ui->dateContactTable->item(nextRow, UsernameColumn)->setIcon(QIcon(LOCAL_TEST_ICON)); } }
int BufferManager::bufferToFile(string fileName, int part){ if (IndexTable.find(hashKey(fileName, part)) == IndexTable.end()) { fprintf(stderr, "buffer not exists, in BufferManager::bufferToFile.\n"); return -1; } int index = IndexTable[hashKey(fileName, part)]; return bufferToFile(index); }
void MIconEnginePrivate::loadDataForModeAndState(QSvgRenderer* renderer, QIcon::Mode mode, QIcon::State state) { QByteArray buf; if (svgBuffers) { buf = svgBuffers->value(hashKey(mode, state)); if (buf.isEmpty()) buf = svgBuffers->value(hashKey(QIcon::Normal, QIcon::Off)); } if (!buf.isEmpty()) { buf = qUncompress(buf); renderer->load(buf); } else { QString svgFile = svgFiles.value(hashKey(mode, state)); if (svgFile.isEmpty()) svgFile = svgFiles.value(hashKey(QIcon::Normal, QIcon::Off)); if (!svgFile.isEmpty()) { QFile f(svgFile); f.open(QIODevice::ReadOnly); QByteArray ba = f.readAll(); if (mode == QIcon::Disabled) { if (Ms::preferences.globalStyle == Ms::MuseScoreStyleType::LIGHT) { if (state == QIcon::On) ba.replace("fill:#3b3f45", "fill:#8daac7"); else ba.replace("fill:#3b3f45", "fill:#a0a0a0"); } else { if (state == QIcon::On) ba.replace("fill:#3b3f45", "fill:#4171a2"); else ba.replace("fill:#3b3f45", "fill:#a0a0a0"); } } else { if (Ms::preferences.globalStyle == Ms::MuseScoreStyleType::LIGHT) { if (state == QIcon::On) ba.replace("fill:#3b3f45", "fill:#4171a2"); } else { if (state == QIcon::On) ba.replace("fill:#3b3f45", "fill:#78afe6"); else ba.replace("fill:#3b3f45", "fill:#eff0f1"); } } renderer->load(ba); } } }
void forceForwardQuery(char *commandName, unsigned int key, unsigned int value, unsigned long responseAddress, unsigned int responsePort) { convertToInternalCommand(commandName); // sleep(1); unsigned char buffer[INTERNAL_BUFFER_LENGTH]; int sockfd; if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { printf("Socket not created!"); exit(1); } packDataForInternal(buffer, commandName, key, value, responseAddress, responsePort); printf("FORWARD> [%s] %d:%d (origin: %lu:%d)\n", commandName, key, value, responseAddress, responsePort); if(shouldFastForwardQuery(key)){ printf(" -> using FastForward [hash:%d]\n", hashKey(key)); if(sendto(sockfd, buffer, sizeof(buffer), 0, (const struct sockaddr *)&succ_successor_addr, succ_successor_addrlen) < 0){ printf("FATAL: sending error."); } }else{ if(sendto(sockfd, buffer, sizeof(buffer), 0, (const struct sockaddr *)&successor_their_addr, successor_addrlen) < 0){ printf("FATAL: sending error."); } } close(sockfd); return; }
//Checks the key given to see if it exists in the hashtable. int checkKey(char* key) { //Hashes the key int hash = hashKey(key); //Mods the hash to find the index to search in. int index = hash % TABLE_SIZE; //Start at the begining of the linked list. dataNode* current = kvTable->table[index]; //If there isn't a linked list then we know the key doesn't exist. if(!current) return 0; //Iterate through the linked list. while(current) { //If the hashes match... if(current->hash == hash) { //...Then check the key. If it matches return true that the key is found. if(strcmp(current->key, key) == 0) { return 1; } } current = current->next; } //If the key is not found return false. return 0; }
//Returns the data associated with the key given. char* getValue(char* key, int* vSize) { //Hashes the key int hash = hashKey(key); //Mods the hash to find the index to search in. int index = hash % TABLE_SIZE; //Start at the begining of the linked list. dataNode* current = kvTable->table[index]; //If there isn't a linked list then we know the key doesn't exist. if(!current) return NULL; //Iterate through the linked list. while(current) { //If the hashes match... if(current->hash == hash) { //...Then check the key. If the key matches then set the vSize to the size and returns the value data. if(strcmp(current->key, key) == 0) { *vSize = current->vSize; return current->value; } } current = current->next; } //If the key is not found return a NULL pointer. return NULL; }
void* hashmapPut(Hashmap* map, void* key, void* value) { int hash = hashKey(map, key); size_t index = calculateIndex(map->bucketCount, hash); Entry** p = &(map->buckets[index]); while (true) { Entry* current = *p; // Add a new entry. if (current == NULL) { *p = createEntry(key, hash, value); if (*p == NULL) { errno = ENOMEM; return NULL; } map->size++; expandIfNecessary(map); return NULL; } // Replace existing entry. if (equalKeys(current->key, current->hash, key, hash, map->equals)) { void* oldValue = current->value; current->value = value; return oldValue; } // Move to next entry. p = ¤t->next; } }
int hashDel(struct hashnode *table, int key) { // jump to bucket int hash = hashKey(key); struct hashnode *node; node = table + hash; // traverse linked list until found or empty while(node != 0 && node->init != 0){ if(node->key == key){ if(node->next != 0){ node->key = node->next->key; node->value = node->next->value; node->next = node->next->next; }else{ node->init = 0; node->key = 0; node->value = 0; node->next = 0; } return 0; } node = (struct hashnode *)node->next; } return -1; }
DiskMultiMap::Iterator DiskMultiMap::search(const std::string& key){ int hashedKey = hashKey(key); int index = hashToOffset(hashedKey); HashTableBucket bucket; m_bf.read(bucket, index); return Iterator(&m_bf, key, bucket.m_start); }
int* LBEHashMap::get(std::string key) { lock.lock(); char* hash = hashKey(key, keySize); Node* local = head; for (int R = 0; R < keySize; R += 1) { int pos = (int) hash[R]; Node* node = getNode(local, pos); if (isArrayNode(node)) { local = node; } else { DataNode* dataNode = dynamic_cast<DataNode*>(node); if ((dataNode != nullptr) && hashEqual(dataNode->getHash(), hash, keySize)) { lock.unlock(); return &(dataNode->value); } else { lock.unlock(); return nullptr; } } } lock.unlock(); return nullptr; }
bool LBEHashMap::put(std::string key, int value) { lock.lock(); char* hash = hashKey(key, keySize); Node* insertThis = allocateNode(value, key, keySize); Node* local = head; for (int R = 0; R < keySize; R++) { int pos = (int) hash[R]; Node* node = getNode(local, pos); if (isArrayNode(node)) { local = node; } else { DataNode* dataNode = dynamic_cast<DataNode*>(node); if (dataNode == nullptr) { //adding new node dynamic_cast<ArrayNode*>(local)->array[pos] = insertThis; lock.unlock(); return true; } else if (hashEqual(dataNode->getHash(), hash, keySize)) { //replace old node dynamic_cast<ArrayNode*>(local)->array[pos] = insertThis; delete node; lock.unlock(); return true; } else { //expand local = expandTable(local, pos, insertThis, R); } } } lock.unlock(); return false; }
bool LBEHashMap::remove(std::string key) { lock.lock(); char* hash = hashKey(key, keySize); Node* local = head; for (int R = 0; R < keySize; R++) { int pos = (int) hash[R]; Node* node = getNode(local, pos); if (node == nullptr) { lock.unlock(); return false; } else if (isArrayNode(node)) { local = node; } else { if (hashEqual(dynamic_cast<DataNode*>(node)->getHash(), hash, keySize)) { dynamic_cast<ArrayNode*>(local)->array[pos] = nullptr; delete node; lock.unlock(); return true; } else { lock.unlock(); return false; } } } lock.unlock(); return false; }
void* hashmapMemoize(Hashmap* map, void* key, void* (*initialValue)(void* key, void* context), void* context) { int hash = hashKey(map, key); size_t index = calculateIndex(map->bucketCount, hash); Entry** p = &(map->buckets[index]); while (true) { Entry* current = *p; // Add a new entry. if (current == NULL) { *p = createEntry(key, hash, NULL); if (*p == NULL) { errno = ENOMEM; return NULL; } void* value = initialValue(key, context); (*p)->value = value; map->size++; expandIfNecessary(map); return value; } // Return existing value. if (equalKeys(current->key, current->hash, key, hash, map->equals)) { return current->value; } // Move to next entry. p = ¤t->next; } }
void Lvk::FE::ChatHistoryWidget::addConversationEntry(const Lvk::Cmn::Conversation::Entry &entry) { // Do not display Facebook's own messages if (entry.from.startsWith(OWN_MESSAGE_TOKEN)) { return; } QString key = hashKey(entry); if (!m_entries.contains(key)) { m_entries[key] = EntryList(); addDateContactTableRow(entry); if (ui->dateContactTable->rowCount() == 1) { ui->dateContactTable->selectRow(0); } } m_entries[key].append(entry); if (ui->dateContactTable->rowCount() == 1) { addConversationTableRow(entry); } else if (ui->dateContactTable->selectionModel()->hasSelection()) { QModelIndex selectedIndex = ui->dateContactTable->selectionModel()->currentIndex(); const QString &selectedKey = selectedIndex.data(HashKeyRole).toString(); if (key == selectedKey) { addConversationTableRow(entry); } } ui->removeHistoryButton->setEnabled(true); ui->filter->setEnabled(true); }
int DiskMultiMap::erase(const std::string& key, const std::string& value, const std::string& context){ Header head; m_bf.read(head, 0); size_t hashedKey = hashKey(key); int index = hashToOffset(hashedKey); HashTableBucket bucket; m_bf.read(bucket, index); //read to proper bucket Node node; int address = bucket.m_start; int deletedBuckets = 0; while(address != -1){ m_bf.read(node, address); //go through bucket int nextIndex = node.m_next; string m_Key = node.m_key; string m_Value = node.m_value; string m_Context = node.m_context; if(m_Key == key && m_Value == value && m_Context == context){ deletedBuckets++; int temp = head.m_deleted; //keep track of the prior deleted node location head.m_deleted = address; //update the deleted location for the Header node.m_next = temp; //update the nodes next position updateNode(node, address); updateHeader(head); } address = nextIndex; } return deletedBuckets; }
//private int BufferManager::ReplaceBlockIndex(){ static int tempIndex = 0; while (1) { if(tempIndex == NUMOFBUFFER){ tempIndex -= NUMOFBUFFER; } if (!blocks[tempIndex].getPinBit()) { if (blocks[tempIndex].getReferenceBit()) { blocks[tempIndex].setReferenceBit(0); } else{ if (blocks[tempIndex].getDirtyBit()) { if(bufferToFile(tempIndex)){ goto CONTINUELOOP; } } //erase the hash key if (blocks[tempIndex].getInTableBit()) { IndexTable.erase(hashKey(blocks[tempIndex].getFileName(), blocks[tempIndex].getPartOfFile())); blocks[tempIndex].setInTableBit(0); } return tempIndex++; } } CONTINUELOOP: tempIndex++; } }
void ZendArray::Bucket::dump() { printf("ZendArray::Bucket: %llx, %p, %p, %p\n", hashKey(), pListNext, pListLast, pNext); if (hasStrKey()) { skey->dump(); } data.dump(); }
int BufferManager::wholeFileToBuffer(string fileName){ int fd = open(fileName.c_str(), O_RDONLY); struct stat sb; if(fd == -1){ fprintf(stderr, "Cannot open file: %s, in BufferManager::wholeFileToBuffer.\n", fileName.c_str()); throw openFileError; } if (fstat(fd, &sb)) { fprintf(stderr, "File: %s, fstat error, in BufferManager::wholeFileToBuffer.\n", fileName.c_str()); throw fstatError; } int blockCount = (int)(sb.st_size >> BUFFERSIZESHIFT); char* mbuf; int index; for (int i = 0; i < blockCount; i++) { if (IndexTable.find(hashKey(fileName, i)) == IndexTable.end()) { mbuf = (char *) mmap(NULL, BUFFERSIZE ,PROT_READ, MAP_PRIVATE, fd, i << BUFFERSIZESHIFT); index = ReplaceBlockIndex(); memcpy(blocks[index].buffer, mbuf, BUFFERSIZE); munmap(mbuf, BUFFERSIZE); // blocks[index].setSize(BUFFERSIZE); blocks[index].setReferenceBit(1); // may be not needed blocks[index].setDirtyBit(0); blocks[index].setPinBit(0); // may be not needed blocks[index].setFileInfo(fileName, i); IndexTable[hashKey(fileName, i)] = index; blocks[index].setInTableBit(1); } } if(close(fd)){ fprintf(stderr, "Cannot close file: %s, in BufferManager::wholeFileToBuffer.\n", fileName.c_str()); throw closeFileError; } return 0; }
void MIconEnginePrivate::loadDataForModeAndState(QSvgRenderer* renderer, QIcon::Mode mode, QIcon::State state) { QByteArray buf; if (svgBuffers) { buf = svgBuffers->value(hashKey(mode, state)); if (buf.isEmpty()) buf = svgBuffers->value(hashKey(QIcon::Normal, QIcon::Off)); } if (!buf.isEmpty()) { buf = qUncompress(buf); renderer->load(buf); } else { QString svgFile = svgFiles.value(hashKey(mode, state)); if (svgFile.isEmpty()) svgFile = svgFiles.value(hashKey(QIcon::Normal, QIcon::Off)); if (!svgFile.isEmpty()) renderer->load(svgFile); } }
static void STinsert(EnglishWord englishWord){ /*a cada inserćão N é incrementado*/ int i = hashFunction(hashKey(englishWord), M); heads[i] = NEW(englishWord, heads[i]); N++; load_Factor = (1.0*N)/M; if (load_Factor >= 0.5){ primeN++; rebuildHash(primetable[primeN]); } }
bool DiskMultiMap::insert(const std::string& key, const std::string& value, const std::string& context){ if(key.size() > 120 || value.size() > 120 || context.size() > 120){ return false; } MultiMapTuple tuple; tuple.key = key; tuple.value = value; tuple.context = context; Node nodeToAdd(tuple); int lastNode = -1; //this is to track the deleted nodes size_t index_hash = hashKey(key); int bucketIndex = hashToOffset(index_hash); HashTableBucket bucket; m_bf.read(bucket, bucketIndex); Header head; m_bf.read(head, 0); string k = nodeToAdd.m_key; string v = nodeToAdd.m_value; string c = nodeToAdd.m_context; if(head.m_deleted == -1){ lastNode = head.m_tail; head.m_tail += sizeof(Node); }else{ lastNode = head.m_deleted; Node newNode; m_bf.read(newNode, head.m_deleted); head.m_deleted = newNode.m_next; } nodeToAdd.m_next = bucket.m_start; bucket.m_start = lastNode; updateBucket(bucket, bucketIndex); if(!updateHeader(head)){ return false; } if(updateNode(nodeToAdd, lastNode)){ return true; }else{ return false; } }
bool hashmapContainsKey(Hashmap* map, void* key) { int hash = hashKey(map, key); size_t index = calculateIndex(map->bucketCount, hash); Entry* entry = map->buckets[index]; while (entry != NULL) { if (equalKeys(entry->key, entry->hash, key, hash, map->equals)) { return true; } entry = entry->next; } return false; }
const ResourceBundle *RBDataMap::getItem(const char* key, UErrorCode &status) const { if(U_FAILURE(status)) { return NULL; } UnicodeString hashKey(key, -1, US_INV); const ResourceBundle *r = (ResourceBundle *)fData->get(hashKey); if(r != NULL) { return r; } else { status = U_MISSING_RESOURCE_ERROR; return NULL; } }
void Lvk::FE::ChatHistoryWidget::removeDateContactRow(int row) { QString date = ui->dateContactTable->item(row, DateColumnn)->text(); QString from = ui->dateContactTable->item(row, UsernameColumn)->data(EntryFromRole).toString(); m_entries.remove(hashKey(date, from)); ui->dateContactTable->removeRow(row); if (ui->dateContactTable->rowCount() == 0) { ui->removeHistoryButton->setEnabled(false); ui->filter->setEnabled(false); } ui->teachRuleButton->setEnabled(false); ui->showRuleButton->setEnabled(false); }
void* getKeyValue(string key, map m){ linkedList list = m->values[hashKey(key) % m->size]; if(!list || !list->head) return NULL; linkedNode node = list->head; var v = (var) node->content; while(node && !streq(key, v->name)){ node = node->next; v = node ? (var) node->content : NULL; } return node ? node->content : NULL; }
void insertMapValue(string key, void* value, map m){ if(m->count >= m->size * m->load) resizeMap(m, 2); size_t index = hashKey(key) % m->size; var v = newVar(key, value); linkedNode node = newLinkedNode(v); linkedList list = m->values[index]; if(!list) list = m->values[index] = newLinkedList(NULL); appendLinkedNode(node, list); m->count++; }
/* Searching the data from the hash table as well as the synonyms in the linked lists PRE: hash - the pointer to the header of the hash table target - a string ( the user's input) POST: data is printed out if found Notification is printed out if not found */ void searchHash(HASH *hash, char target[]) { int found = 0; int key; DATA *temp; DATA *success; if (!(temp = (DATA*) malloc (sizeof(DATA)))) { printf( "Not enough memory\n"), exit(101); } strcpy(temp->CCN, target); key = hashKey(target, hash->size); if(hash->table[key].card.name != NULL) { if(!strcmp(target, hash->table[key].card.CCN)) { printf("CCN: %19s; Name: %-15s; Exp: %5s; CVC: %4d\n\n", hash->table[key].card.CCN , hash->table[key].card.name, hash->table[key].card.exp, hash->table[key].card.cvc); found = 1; } else { if (hash->table[key].list->count != 0) { found = retrieveSNODE(hash->table[key].list, temp, (void**)&success); if(found) printf("CCN: %-19s; Name: %-25s; Exp: %-5s; CVC: %-4d\n\n", success->CCN , success->name, success->exp, success->cvc); } } } if(!found) printf("Not found!!!\n\n"); free(temp); return; }
void* hashmapRemove(Hashmap* map, void* key) { int hash = hashKey(map, key); size_t index = calculateIndex(map->bucketCount, hash); // Pointer to the current entry. Entry** p = &(map->buckets[index]); Entry* current; while ((current = *p) != NULL) { if (equalKeys(current->key, current->hash, key, hash, map->equals)) { void* value = current->value; *p = current->next; free(current); map->size--; return value; } p = ¤t->next; } return NULL; }