int hash_remove(hash *h, const uint64_t key) { char tmp[30]; struct record *recs; unsigned int off, ind, size, code; memset(&tmp, 0, sizeof(tmp)); snprintf(tmp, sizeof(tmp), "%llu", key); code = stringhash(tmp); recs = h->records; size = sizes[h->size_index]; ind = code % size; off = 0; while (recs[ind].key) { if (key == recs[ind].key) { recs[ind].key = 0; h->records_count--; return 1; } ind = (code + (int)pow(++off, 2)) % size; } return 0; }
int dict_put(DICT *dict, const char *key, void *value) { uint32_t hash; int l; char *buf; int idx; struct dict_entry *entry, *prev; /* check if we should grow the hashtable */ if (dict->num >= ((dict->size * DICT_LOADPERCENTAGE) / 100)) growhashtable(dict); /* calculate the hash and position in the hashtable */ hash = stringhash(key); idx = hash % dict->size; /* check if the entry is already present */ for (entry = dict->table[idx], prev = NULL; entry != NULL; prev = entry, entry = entry->next) { if ((entry->hash == hash) && (strcmp(entry->key, key) == 0)) { /* check if we should unset the entry */ if (value == NULL) { /* remove from linked list */ if (prev == NULL) dict->table[idx] = entry->next; else prev->next = entry->next; /* free entry memory and register removal */ free(entry); dict->num--; return 0; } /* just set the new value */ entry->value = value; return 0; } } /* if entry should be unset we're done */ if (value == NULL) return 0; /* entry is not present, make new entry */ l = strlen(key) + 1; buf = (char *)malloc(sizeof(struct dict_entry) + l); if (buf == NULL) return -1; entry = (struct dict_entry *)(void *)buf; buf += sizeof(struct dict_entry); strcpy(buf, key); entry->hash = hash; entry->key = buf; entry->value = value; /* insert into hashtable/linked list */ entry->next = dict->table[idx]; dict->table[idx] = entry; /* increment number of stored items */ dict->num++; return 0; }
void send(const IpAddress& ipAddress, DataType data) { auto& clientData = clients.at(ipAddress); Packet<DataType, Message> cameraPacket = { stringhash("swordbow-magic"), clientData.sequence++, data, sizeof(data) }; packetManager.send<DataType>(ipAddress, cameraPacket); }
int hash_add(hash *h, uint64_t key, char *value, time_t lifetime) { int rc; char tmp[30]; struct record *recs; unsigned int off, ind, size, code; if (key <= 0 ) return -2; if (h->records_count > sizes[h->size_index] * load_factor) { rc = hash_grow(h); if (rc) return rc; } memset(&tmp, 0, sizeof(tmp)); snprintf(tmp, sizeof(tmp), "%llu", key); recs = h->records; size = sizes[h->size_index]; code = stringhash(tmp); ind = code % size; off = 0; rc = 1; while (recs[ind].key) { rc = _timeout(recs[ind].lifetime); if(!rc) { //超时的记录 h->records_count--; break; } if(key == recs[ind].key) { return 0; } ind = (code + (int)pow(++off,2)) % size; } recs[ind].key = key; recs[ind].lifetime = lifetime; memcpy(recs[ind].value, value, sizeof(recs[ind].value)); h->records_count++; return 1; }
void *dict_get(DICT *dict, const char *key) { uint32_t hash; struct dict_entry *entry; /* calculate the hash */ hash = stringhash(key); /* loop over the linked list in the hashtable */ for (entry = dict->table[hash % dict->size]; entry != NULL; entry = entry->next) { if ((entry->hash == hash) && (strcmp(entry->key, key) == 0)) return entry->value; } /* no matches found */ return NULL; }
const char *hash_get(hash *h, const uint64_t key) { int rc; char tmp[30]; struct record *recs; unsigned int off, ind, size, code; memset(&tmp, 0, sizeof(tmp)); snprintf(tmp, sizeof(tmp), "%llu", key); code = stringhash(tmp); recs = h->records; size = sizes[h->size_index]; ind = code % size; off = 0; while (recs[ind].key) { rc = _timeout(recs[ind].lifetime); if(!rc) { //超时的记录,将这条记录置为删除的标志 recs[ind].key = 0; h->records_count--; break; } if (key == recs[ind].key) { return recs[ind].value; } ind = (code + (int)pow(++off,2)) % size; } return NULL; }