static void setElement(int protons, int group, int period, char *parentSymbol, char *symbol, char *name, double mass, double vanDerWaalsRadius, double e_vanDerWaals, int n_bonds, double covalentRadius, double charge, int isVirtual) { struct atomType *entry; struct atomType *oldEntry; char buf[256]; if (e_vanDerWaals < 0.0) { e_vanDerWaals = 0.3 + protons * protons / 190.0; } entry = (struct atomType *)allocate(sizeof(struct atomType)); entry->protons = protons; entry->group = group; entry->period = period; entry->name = copy_string(name); entry->symbol = copy_string(symbol); entry->mass = mass; entry->vanDerWaalsRadius = vanDerWaalsRadius; entry->e_vanDerWaals = e_vanDerWaals; entry->n_bonds = n_bonds; entry->covalentRadius = covalentRadius; entry->charge = charge; entry->refCount = 2; entry->isVirtual = isVirtual; entry->parent = getAtomTypeByName(parentSymbol); oldEntry = hashtable_put(periodicHashtable, symbol, entry); if (oldEntry != NULL) { oldEntry->refCount--; if (oldEntry->refCount < 1) { free(oldEntry->name); free(oldEntry->symbol); free(oldEntry); } } sprintf(buf, "%d", protons); oldEntry = hashtable_put(periodicHashtable, buf, entry); if (oldEntry != NULL) { oldEntry->refCount--; if (oldEntry->refCount < 1) { free(oldEntry->name); free(oldEntry->symbol); free(oldEntry); } } }
void initHashTable(HashTable * hashtable) { hashtable_put(hashtable, "a", "v"); hashtable_put(hashtable, "b", "dxxx"); hashtable_put(hashtable, "d", "dxxxxxxxxxxxxxxxxx"); hashtable_put(hashtable, "d", "d"); printf("%s", hashtable_get(hashtable, "d")); printf("%s", hashtable_get(hashtable, "c")); printf("%d", hashtable->item_size); }
main() { struct hashtable *ht; ht = hashtable_new(0); hashtable_print(stdout, ht); hashtable_put(ht, "one", (void *)0x1); hashtable_print(stdout, ht); hashtable_put(ht, "two", (void *)0x2); hashtable_print(stdout, ht); hashtable_put(ht, "three", (void *)0x3); hashtable_print(stdout, ht); hashtable_put(ht, "four", (void *)0x4); hashtable_print(stdout, ht); hashtable_put(ht, "five", (void *)0x5); hashtable_print(stdout, ht); hashtable_put(ht, "six", (void *)0x6); hashtable_print(stdout, ht); hashtable_put(ht, "seven", (void *)0x7); hashtable_print(stdout, ht); hashtable_put(ht, "eight", (void *)0x8); hashtable_print(stdout, ht); hashtable_put(ht, "nine", (void *)0x9); hashtable_print(stdout, ht); }
int bind_node(cmsg_bytes_t name, node_t *n) { if (name.len == 0) { warn("Binding to empty name forbidden\n"); return 0; } if (hashtable_contains(&directory, name)) { return 0; } hashtable_put(&directory, name, n); hashtable_put(&n->names, name, NULL); info("Binding node <<%.*s>> of class %s\n", name.len, name.bytes, n->node_class->name); announce_binding(name, 1); return 1; }
int main(void) { size_t initcap = 10; HashTable *newht, *ht = hashtable_new(initcap); if (!ht) testfail("Could not create hash table!"); if (ht->items != 0) testfail("New hash table should be empty, instead has size %lu", ht->items); hashtable_put(ht, "Elvis", "Presley"); if (ht->items != 1) testfail("Hash table should have size 1 after put instead has size %lu", ht->items); expect_to_get(ht, "Elvis", "Presley"); hashtable_put(ht, "Elvis", "Costello"); if (ht->items != 1) testfail("Hash table should have remained size 1 after put instead has size %lu", ht->items); expect_to_get(ht, "Elvis", "Costello"); hashtable_put(ht, "Janis", "Joplin"); if (ht->items != 2) testfail("Hash table should have grown to size 2 after put instead has size %lu", ht->items); expect_to_get(ht, "Janis", "Joplin"); newht = hashtable_rehash(ht, 100); if (newht != ht) testfail("Expected hashtable instance to be the same; should not have changed!"); newht = hashtable_rehash(ht, 0); if (newht == ht) testfail("Expected hashtable instance to be the different; should have changed!"); ht = newht; if (newht->capacity <= initcap) testfail("Expected new hashtable to be larger than %lu, instead got %lu", initcap, newht->capacity); if (newht->items != 2) testfail("Expected 2 items in new hash table!"); expect_to_get(ht, "Elvis", "Costello"); expect_to_get(ht, "Janis", "Joplin"); hashtable_destroy(ht); return 0; }
// ks in N/m // r0 in pm, or 1e-12 m // de in aJ, or 1e-18 J // beta in 1e10 m^-1 static void addInitialBondStretch(double ks, double r0, double de, double beta, double inflectionR, int quality, int quadratic, char *bondName) { struct bondStretch *stretch; struct bondStretch *old; char *canonicalName; canonicalName = canonicalizeBondName(bondName); if (beta < 0) { beta = sqrt(ks / (2.0 * de)) / 10.0 ; } if (inflectionR < 0) { inflectionR = r0 * 1.5; } stretch = newBondStretch(canonicalName, ks, r0, de, beta*1e-2, inflectionR, quality, quadratic); old = hashtable_put(bondStretchHashtable, canonicalName, stretch); if (old != NULL) { free(old->bondName); free(old); } }
static void _hashtable_resize(HashTable * h_table) { int size = h_table->size; int new_size = (int)(h_table->size * h_table->resize_factor); HNode ** new_table = calloc(sizeof(HNode *), new_size); HNode ** old_table = h_table->table; //slog(INFO,LOG_UTIL,"Resizing table to %d", new_size); h_table->table = new_table; h_table->size = new_size; h_table->count = 0; int i = 0; for (i = 0; i < size; i++) { HNode * curr = old_table[i]; while (curr != 0) { HNode * prev = curr; curr = curr->next; hashtable_put(h_table, prev->key, prev->key_len, prev->value, prev->value_len,false); //free the memory for only the struct //note, this does NOT free the key or the value free(prev); } } //free the memory for the old array free(old_table); }
// kb in aJ / rad^2 (1e-18 J/rad^2) // theta0 in radians static void addInitialBendData(double kb, double theta0, int quality, char *bendName) { struct bendData *bend; struct bendData *old; char *bn; char *canonicalName; bn = copy_string(bendName); canonicalName = canonicalizeBendName(bn); if (canonicalName == NULL) { fprintf(stderr, "malformed bend name: %s\n", bendName); // we need a name, so we use the bad one -- this entry will // probably never be found. canonicalName = bendName; } // typical kb values around 1 aJ/rad^2 bend = newBendData(canonicalName, kb*1e6, theta0, quality); old = hashtable_put(bendDataHashtable, canonicalName, bend); if (old != NULL) { fprintf(stderr, "duplicate bend entry: %s\n", bendName); free(old->bendName); free(old); } }
static struct electrostaticParameters * addElectrostaticInteraction(char *electrostaticName, double q1, double q2, double cutoffRadiusStart, double cutoffRadiusEnd) { struct electrostaticParameters *es; struct electrostaticParameters *old; es = (struct electrostaticParameters *)allocate(sizeof(struct electrostaticParameters)); es->electrostaticName = copy_string(electrostaticName); es->k = COULOMB * q1 * q2 / DielectricConstant; if (cutoffRadiusEnd < 0.0) { es->cutoffRadiusEnd = es->k / MinElectrostaticSensitivity; } else { es->cutoffRadiusEnd = cutoffRadiusEnd; } if (cutoffRadiusStart < 0.0) { es->cutoffRadiusStart = 0.9 * es->cutoffRadiusEnd; } else { es->cutoffRadiusStart = cutoffRadiusStart; } initializeElectrostaticInterpolator(es); old = hashtable_put(electrostaticHashtable, electrostaticName, es); if (old != NULL) { fprintf(stderr, "duplicate electrostatic: %s\n", electrostaticName); free(old->electrostaticName); free(old); } return es; }
syscall_t * syscall_new(const char * name, const char * sig, syscall_f func, const char * desc, exception_t ** err) { // Sanity check { if (exception_check(err)) { return NULL; } if (name == NULL || sig == NULL) { exception_set(err, EINVAL, "Bad arguments!"); return NULL; } } syscall_t * syscall = kobj_new("Syscall", name, syscall_desc, syscall_destroy, sizeof(syscall_t)); syscall->name = strdup(name); syscall->signature = strdup(sig); syscall->func = func; syscall->description = (desc == NULL)? NULL : strdup(desc); syscall->ffi = function_build(syscall->func, syscall->signature, err); if (syscall->ffi == NULL || exception_check(err)) { return NULL; } hashtable_put(&syscalls, syscall->name, &syscall->global_entry); LOGK(LOG_DEBUG, "Registered syscall %s with sig %s", name, sig); return syscall; }
static command_status_t fetch_domains(domain_set_t *ds, bool force, error_t **error) { if (!ds->uptodate || force) { request_t *req; bool request_success; json_document_t *doc; req = request_new(REQUEST_FLAG_SIGN, HTTP_GET, NULL, error, API_BASE_URL "/domain"); request_success = request_execute(req, RESPONSE_JSON, (void **) &doc, error); request_destroy(req); if (request_success) { Iterator it; json_value_t root; root = json_document_get_root(doc); hashtable_clear(ds->domains); json_array_to_iterator(&it, root); for (iterator_first(&it); iterator_is_valid(&it); iterator_next(&it)) { json_value_t v; v = (json_value_t) iterator_current(&it, NULL); hashtable_put(ds->domains, 0, json_get_string(v), domain_new(), NULL); // ds->domains has strdup as key_duper, don't need to strdup it ourself } iterator_close(&it); ds->uptodate = TRUE; json_document_destroy(doc); } else { return COMMAND_FAILURE; } } return COMMAND_SUCCESS; }
void register_node_class(node_class_t *nc) { cmsg_bytes_t key = cmsg_cstring_bytes(nc->name); if (hashtable_contains(&node_class_table, key)) { die("Duplicate node class name %s\n", nc->name); } hashtable_put(&node_class_table, key, nc); }
exchange_t *declare_exchange(int *status, vhost_t *vhost, amqp_bytes_t name, exchange_type_t *type, amqp_boolean_t durable, amqp_boolean_t auto_delete, amqp_table_t arguments) { exchange_t *x = internal_lookup_exchange(vhost, name); if (x == NULL) { x = malloc(sizeof(exchange_t)); x->name = amqp_bytes_malloc_dup(name); x->type = type; x->type_data = NULL; x->durable = durable; x->auto_delete = auto_delete; x->arguments = AMQP_EMPTY_TABLE; /* TODO: copy arguments */ init_hashtable(&x->fanout, 127, NULL, NULL); init_hashtable(&x->direct, 127, NULL, NULL); type->init(x); info("Exchange \"%.*s\" of type %.*s created", name.len, name.bytes, type->name.len, type->name.bytes); hashtable_put(&vhost->exchanges, name, x); } return x; }
static struct vanDerWaalsParameters * addVanDerWaalsInteraction(char *bondName, double rvdW, double evdW, double cutoffRadiusStart, double cutoffRadiusEnd) { struct vanDerWaalsParameters *vdw; struct vanDerWaalsParameters *old; char *canonicalName; canonicalName = canonicalizeBondName(bondName); vdw = (struct vanDerWaalsParameters *)allocate(sizeof(struct vanDerWaalsParameters)); vdw->vdwName = copy_string(canonicalName); vdw->rvdW = rvdW; vdw->evdW = evdW; if (cutoffRadiusStart < 0.0) { vdw->cutoffRadiusStart = rvdW; } else { vdw->cutoffRadiusStart = cutoffRadiusStart; } if (cutoffRadiusEnd < 0.0) { vdw->cutoffRadiusEnd = VanDerWaalsCutoffFactor * rvdW; } else { vdw->cutoffRadiusEnd = cutoffRadiusEnd; } initializeVanDerWaalsInterpolator(vdw); old = hashtable_put(vanDerWaalsHashtable, bondName, vdw); if (old != NULL) { fprintf(stderr, "duplicate vdw: %s\n", bondName); free(old->vdwName); free(old); } return vdw; }
void getopt_add_bool(getopt_t *gopt, char sopt, const char *lname, int def, const char *help) { char sname[2]; sname[0] = sopt; sname[1] = 0; getopt_option_t *goo = (getopt_option_t*) calloc(1, sizeof(getopt_option_t)); goo->sname=strdup(sname); goo->lname=strdup(lname); goo->svalue=strdup(def ? "true" : "false"); goo->type=GOO_BOOL_TYPE; goo->help=strdup(help); hashtable_put(gopt->lopts, goo->lname, goo); hashtable_put(gopt->sopts, goo->sname, goo); dgc_vector_add(gopt->options, goo); }
void getopt_add_string(getopt_t *gopt, char sopt, const char *lname, const char *def, const char *help) { char sname[2]; sname[0] = sopt; sname[1] = 0; getopt_option_t *goo = (getopt_option_t*) calloc(1, sizeof(getopt_option_t)); goo->sname=strdup(sname); goo->lname=strdup(lname); goo->svalue=strdup(def); goo->type=GOO_STRING_TYPE; goo->help=strdup(help); hashtable_put(gopt->lopts, goo->lname, goo); hashtable_put(gopt->sopts, goo->sname, goo); dgc_vector_add(gopt->options, goo); }
static int register_fd(void *x, int *fd, const char *path) { char full[_MAX_PATH]; if (NULL == _fullpath(full, path, _MAX_PATH)) { return 0; } else { char *copy = mem_dup(full); *fd = --FD_FROM_P(x); hashtable_put(HT_FROM_P(x), UINT_TO_POINTER(*fd), copy); return 1; } }
static void addDeTableEntry(char *bondName, double de) { struct deTableEntry *entry; struct deTableEntry *old; entry = (struct deTableEntry *)allocate(sizeof(struct deTableEntry)); entry->de = de; old = hashtable_put(deHashtable, bondName, entry); if (old != NULL) { fprintf(stderr, "duplicate de entry: %s\n", bondName); free(old); } }
int _open(t_ext2* ext2,const char* fullpath, int flags) { u32 fd; u32 ret_code; struct t_process_context* current_process_context; t_inode* inode; t_llist_node* node; char path[NAME_MAX]; char filename[NAME_MAX]; inode=kmalloc(sizeof(t_inode)); CURRENT_PROCESS_CONTEXT(current_process_context); fd=current_process_context->next_fd++; //current_process_context->file_desc=kmalloc(sizeof(t_hashtable)); //hashtable_init(current_process_context->file_desc,10); if (flags & O_CREAT & O_RDWR) { alloc_inode(fullpath,0,system.root_fs,inode); hashtable_put(current_process_context->file_desc,fd,inode); } else if (flags & (O_APPEND | O_RDWR)) { ret_code=lookup_inode(fullpath,ext2,inode); if (ret_code==-1) { return -1; } hashtable_put(current_process_context->file_desc,fd,inode); } else { return -1; } inode->file_offset=0; return fd; }
// ks in N/m // r0 in pm, or 1e-12 m // de in aJ, or 1e-18 J // beta in 1e12 m^-1 static struct bondStretch * addBondStretch(char *bondName, double ks, double r0, double de, double beta, double inflectionR, int quality, int quadratic) { struct bondStretch *stretch; struct bondStretch *old; stretch = newBondStretch(bondName, ks, r0, de, beta, inflectionR, quality, quadratic); old = hashtable_put(bondStretchHashtable, bondName, stretch); if (old != NULL) { fprintf(stderr, "duplicate bondStretch: %s\n", bondName); free(old->bondName); free(old); } return stretch; }
static void addPatternParameter(char *name, double value, double angleUnits, char *stringValue) { struct patternParameter *param = (struct patternParameter *)allocate(sizeof(struct patternParameter)); param->value = value; param->angleUnits = angleUnits; param->stringValue = stringValue ? copy_string(stringValue) : NULL ; param = (struct patternParameter *)hashtable_put(patternParameterHashtable, name, (void *)param); if (param) { free(param->stringValue); free(param); } }
// kb in yoctoJoules / radian^2 (1e-24 J/rad^2) static struct bendData * addBendData(char *bendName, double kb, double theta0, int quality) { struct bendData *bend; struct bendData *old; bend = newBendData(bendName, kb, theta0, quality); old = hashtable_put(bendDataHashtable, bendName, bend); if (old != NULL) { fprintf(stderr, "duplicate bend data: %s\n", bendName); free(old->bendName); free(old); } return bend; }
void set_variable_value(object* var, object* val, environment* env) { object* value = NULL; while(env != NULL){ value = hashtable_lookup(env->variables, var); if(value != NULL){ hashtable_put(env->variables, var, val); return; } else { env = env->enclosing_environment; } } fprintf(stderr, "set: unbound variable: %s\n", var->data.symbol.value); exit(1); }
static bool get_domain_records(const char *domain, domain_t **d, bool force, error_t **error) { domain_set_t *ds; bool request_success; *d = NULL; FETCH_ACCOUNT_DOMAINS(ds); request_success = TRUE; // TODO: hashtable_clear((*d)->records) if force if (!hashtable_get(ds->domains, domain, d) || !(*d)->uptodate || force) { request_t *req; json_document_t *doc; if (NULL == *d) { *d = domain_new(); hashtable_put(ds->domains, 0, domain, *d, NULL); } req = request_new(REQUEST_FLAG_SIGN, HTTP_GET, NULL, error, API_BASE_URL "/domain/zone/%s/record", domain); request_success = request_execute(req, RESPONSE_JSON, (void **) &doc, error); request_destroy(req); // result if (request_success) { Iterator it; json_value_t root; root = json_document_get_root(doc); json_array_to_iterator(&it, root); for (iterator_first(&it); request_success && iterator_is_valid(&it); iterator_next(&it)) { json_value_t v; json_document_t *doc; v = (json_value_t) iterator_current(&it, NULL); req = request_new(REQUEST_FLAG_SIGN, HTTP_GET, NULL, error, API_BASE_URL "/domain/zone/%s/record/%u", domain, json_get_integer(v)); request_success = request_execute(req, RESPONSE_JSON, (void **) &doc, error); request_destroy(req); // result parse_record((*d)->records, doc); } iterator_close(&it); json_document_destroy(doc); (*d)->uptodate = TRUE; } } return request_success ? COMMAND_SUCCESS : COMMAND_FAILURE; }
struct atomType * getAtomTypeByIndex(int atomTypeIndex) { char buf[256]; struct atomType *entry; if (periodicHashtable == NULL) { ERROR("getAtomTypeByIndex called before periodic table initialization"); return NULL; } sprintf(buf, "%d", atomTypeIndex); entry = (struct atomType *)hashtable_get(periodicHashtable, buf); if (entry == NULL) { entry = (struct atomType *)hashtable_get(periodicHashtable, "0"); WARNING1("using undefined atomType %d", atomTypeIndex); hashtable_put(periodicHashtable, buf, entry); // this should suppress further warnings } return entry; }
int sst_put(sstable_t* sst,data_t* data) { int ret = 2; //do nothing if (sst->status == WRITE) { ret = hashtable_put(sst->htable,data); } if (ret == 1) //change the sstable's status { // TakeLock(sst->lock); // printf("[%d]sst : %d status : %d\n",GetCurrentThreadId(),sst->id,sst->status); // sst->status = WFULL; // printf("[%d]sst : %d status : %d\n",GetCurrentThreadId(),sst->id,sst->status); // unTakeLock(sst->lock); } return ret; }
void env_set(environment_t *env, String name, data_t* value) { data_t *old_slot; assert(env); assert(value); assert(name); /* TODO: do a recursive copy here of the data tree and name */ /* Only look for an old slot in the _current_ environment, not * parent environments or this will break the set in that * case. For instance if "i" were a variable inside of a new * environment we wish to set, then if "i" is found in a higher * parent environment it will be destroyed first. */ // old_slot = hashtable_get(env->bindings, name); // if(old_slot) // data_destroy(old_slot); hashtable_put(&env->bindings, name, value); }
/* * Link an item into the hash table */ static void _item_link(struct item *it) { ASSERT(it->magic == ITEM_MAGIC); ASSERT(!(it->is_linked)); ASSERT(!(it->in_freeq)); log_verb("link it %p of id %"PRIu8" at offset %"PRIu32, it, it->id, it->offset); it->is_linked = 1; slab_deref(item_to_slab(it)); /* slab ref'ed in _item_alloc */ hashtable_put(it, hash_table); INCR(slab_metrics, item_linked_curr); INCR(slab_metrics, item_link); /* TODO(yao): how do we track optional storage? Separate or treat as val? */ INCR_N(slab_metrics, item_keyval_byte, it->klen + it->vlen); INCR_N(slab_metrics, item_val_byte, it->vlen); PERSLAB_INCR_N(it->id, item_keyval_byte, it->klen + it->vlen); PERSLAB_INCR_N(it->id, item_val_byte, it->vlen); }
void exampleHashtable() { // Use pooling for efficiency, if you don't want to use pooling // then comment out this line. pool_hashtable(16); Hashtable* H = newHashtable(8); hashtable_put(H, 23, "Hello"); hashtable_put(H, 16, "World"); // Hash another entry to 0 (besides World) hashtable_put(H, 8, "Again"); hashtable_put(H, 24, "And again"); hashtable_put(H, 40, "And again again"); // Print out the size printf("The hashtable has %d entries.\n", H->size); hashtable_display(H, &toString); // Test the get function based on the keys printf("%s ", (char*)hashtable_get(H, 23)); printf("%s\n", (char*)hashtable_get(H, 16)); printf("%s\n", (char*)hashtable_get(H, 8)); printf("%s\n", (char*)hashtable_get(H, 24)); // Try one that doesn't exist and goes to a completely empty entry if (hashtable_get(H, 1) == NULL) printf("Entry with key 1 not found.\n"); // Try one that doesn't exist and goes to an existing entry if (hashtable_get(H, 32) == NULL) printf("Entry with key 32 not found.\n"); // Test exists on all added data if (hashtable_exists(H, 23) && hashtable_exists(H, 16) && hashtable_exists(H, 8) && hashtable_exists(H, 24) && hashtable_exists(H, 40)) printf("The hashtable's entries are sound.\n"); // Test removing of an entry that doesn't exist in the list printf("Removed: %s\n", (char*)hashtable_remove(H, 32)); // Test removing of the first entry in the list printf("Removed: %s\n", (char*)hashtable_remove(H, 16)); // Test removing of a middle entry in the list printf("Removed: %s\n", (char*)hashtable_remove(H, 24)); // Test removing of an end entry in the list printf("Removed: %s\n", (char*)hashtable_remove(H, 40)); // Test removing of the only entry printf("Removed: %s\n", (char*)hashtable_remove(H, 8)); // Test setting the last entry remaining printf("Before: %s", (char*)hashtable_get(H, 23)); hashtable_set(H, 23, "Changed!"); printf("\tAfter: %s\n", (char*)hashtable_get(H, 23)); // Test the set method for a non existing key if (!hashtable_set(H, 45, "Foo")) printf("Cannot set 45 to 'Foo', key 45 doesn't exist.\n"); // Print out the size printf("The hashtable has %d entries.\n", H->size); // Clear the table and print out the size hashtable_clear(H); printf("Cleared. The hashtable has %d entries.\n", H->size); // This will clear the list of any nodes and pool them and then free // the list itself from memory hashtable_free(H); // If you're not using pooling this can be commented out. This will // free all pooled nodes from memory. Always call this at the end // of using any List. unpool_hashtable(); }
void ht_insert_simple(HashTable *table, char *key, void *value) { //slog(INFO,LOG_UTIL,"insert %s",key); assert(table); assert(key); hashtable_put(table, strdup(key), strlen(key)+1, value, sizeof(void*),true); }