struct isl_hash_table_entry *isl_hash_table_find(struct isl_ctx *ctx, struct isl_hash_table *table, uint32_t key_hash, int (*eq)(const void *entry, const void *val), const void *val, int reserve) { size_t size; uint32_t h, key_bits; key_bits = isl_hash_bits(key_hash, table->bits); size = 1 << table->bits; for (h = key_bits; table->entries[h].data; h = (h+1) % size) if (table->entries[h].hash == key_hash && eq(table->entries[h].data, val)) return &table->entries[h]; if (!reserve) return NULL; if (4 * table->n >= 3 * size) { if (grow_table(ctx, table) < 0) return NULL; return isl_hash_table_find(ctx, table, key_hash, eq, val, 1); } table->n++; table->entries[h].hash = key_hash; return &table->entries[h]; }
const char * string_set_add(const char * source_string, String_set * ss) { char * str; size_t len; unsigned int p; assert(source_string != NULL, "STRING_SET: Can't insert a null string"); p = find_place(source_string, ss); if (ss->table[p] != NULL) return ss->table[p]; len = strlen(source_string); #ifdef DEBUG /* Store the String_set structure address for debug verifications */ len = ((len+1)&~(sizeof(ss)-1)) + 2*sizeof(ss); str = (char *) xalloc(len); *(String_set **)&str[len-sizeof(ss)] = ss; #else str = (char *) xalloc(len+1); #endif strcpy(str, source_string); ss->table[p] = str; ss->count++; /* We just added it to the table. If the table got too big, we grow it. Too big is defined as being more than 3/4 full */ if ((4 * ss->count) > (3 * ss->size)) grow_table(ss); return str; }
static void hash_insert(struct hentry x) { assert(ATOM_VALID(x.atom)); // fprintf(stderr,"hash_insert(%x,%p:%i,%x,%x,[%x,%x]", x.atom, ATOM_PTR(x.atom), ATOM_LEN(x.atom), x.hashes[0], x.hashes[1],HASH_INDEX(0,x.hashes[0]),HASH_INDEX(1,x.hashes[1])); assert(!atom_exists(x.atom)); assert(!item_exists(ATOM_PTR(x.atom),ATOM_LEN(x.atom))); atom_t start = x.atom; for(int loop = 0; loop < DEPTH_LIMIT;loop++) { for(int i = 0; i < CUCKOO_HASHES; i++) { // int e = HASH_INDEX(i,FHASH(x,i)); for(int j = 0; j < CUCKOO_BUCKETS; j++) { //#struct hentry *b = &(htable[(e + j) & HASHJMASK ]); //struct hentry *b = &htable[HASH_BUCKET(e,j)]; struct hentry *b = &htable[HASH_INDEX(i,FHASH(x,i) + j)]; if(!ATOM_VALID(b->atom)) { *b = x; // fprintf(stderr,")\n"); return; } struct hentry tb = x; x = *b; *b = tb; } // struct hentry *b = &(htable[e]); } if(x.atom == start) { break; } } grow_table(); // fprintf(stderr,"R"); return hash_insert(x); }
int string_id_add(const char *source_string, String_id *ss) { char * str; size_t len; unsigned int p; assert(source_string != NULL, "STRING_SET: Can't insert a null string"); p = find_place(source_string, ss); if (ss->table[p].str != NULL) return ss->table[p].id; len = strlen(source_string); str = (char *) malloc(len+1); strcpy(str, source_string); ss->table[p].str = str; ss->table[p].id = ss->count; ss->count++; int keep_id = ss->table[p].id; /* We just added it to the table. If the table got too big, * we grow it. Too big is defined as being more than 3/8 full. * There's a huge boost from keeping this sparse. */ if ((8 * ss->count) > (3 * ss->size)) grow_table(ss); return keep_id; }
pmix_status_t pmix_pointer_array_set_size(pmix_pointer_array_t *array, int new_size) { if(new_size > array->size) { if (!grow_table(array, new_size, new_size)) { return PMIX_ERROR; } } return PMIX_SUCCESS; }
/** * Test whether a certain element is already in use. If not yet * in use, reserve it. * * @param array Pointer to array (IN) * @param index Index of element to be tested (IN) * @param value New value to be set at element index (IN) * * @return true/false True if element could be reserved * False if element could not be reserved (e.g.in use). * * In contrary to array_set, this function does not allow to overwrite * a value, unless the previous value is NULL ( equiv. to free ). */ bool pmix_pointer_array_test_and_set_item (pmix_pointer_array_t *table, int index, void *value) { assert(table != NULL); assert(index >= 0); #if 0 pmix_output(0,"pmix_pointer_array_test_and_set_item: IN: " " table %p (size %ld, lowest free %ld, number free %ld)" " addr[%d] = %p\n", table, table->size, table->lowest_free, table->number_free, index, table->addr[index]); #endif /* expand table if required to set a specific index */ if ( index < table->size && table->addr[index] != NULL ) { /* This element is already in use */ return false; } /* Do we need to grow the table? */ if (table->size <= index) { if (!grow_table(table, (((index / TABLE_GROW) + 1) * TABLE_GROW), index)) { return false; } } /* * allow a specific index to be changed. */ table->addr[index] = value; table->number_free--; /* Reset lowest_free if required */ if ( index == table->lowest_free ) { int i; table->lowest_free = table->size; for ( i=index; i<table->size; i++) { if ( NULL == table->addr[i] ){ table->lowest_free = i; break; } } } #if 0 pmix_output(0,"pmix_pointer_array_test_and_set_item: OUT: " " table %p (size %ld, lowest free %ld, number free %ld)" " addr[%d] = %p\n", table, table->size, table->lowest_free, table->number_free, index, table->addr[index]); #endif return true; }
void ChHashTable<T>::insert(const char *Key, const T& Data) { try { if( ((stats.Count_ + 1) / static_cast<double>(stats.TableSize_)) > config.MaxLoadFactor_) grow_table(); // place in hash table where this will be inserted unsigned index = config.HashFunc_(Key, stats.TableSize_); HeadNode head = &table[index]; // get head of the list of keys DataNode list = head->Nodes; // increment probe counter for initally finding place // in hash table ++stats.Probes_; // check list for duplicate while(list) { // increment probe counter ++stats.Probes_; // found a duplicate, throw exception if(strncmp(Key, list->Key, MAX_KEYLEN) == 0) throw (HashTableException(HashTableException::E_DUPLICATE, "Item being inserted is a duplicate")); // move to next node list = list->Next; } // duplicate was not found, start inserting // make the node DataNode insert_node = make_node(Data); strncpy(insert_node->Key, Key, MAX_KEYLEN); // insert at the front of the list at the correct index insert_node->Next = head->Nodes; head->Nodes = insert_node; // increment item counter ++stats.Count_; // increment counter for list at table[index] ++head->Count; } // catch ecept from make_node call catch (HashTableException &exception) { // throw it back to client throw exception; } }
/** * Set the value of the dynamic array at a specified location. * * * @param table Pointer to opal_pointer_array_t object (IN) * @param ptr Pointer to be added to table (IN) * * @return Error code * * Assumption: NULL element is free element. */ int opal_pointer_array_set_item(opal_pointer_array_t *table, int index, void * value) { assert(table != NULL); /* expand table if required to set a specific index */ OPAL_THREAD_LOCK(&(table->lock)); if (table->size <= index) { if (!grow_table(table, ((index / TABLE_GROW) + 1) * TABLE_GROW, index)) { OPAL_THREAD_UNLOCK(&(table->lock)); return OPAL_ERROR; } } /* mark element as free, if NULL element */ if( NULL == value ) { if (index < table->lowest_free) { table->lowest_free = index; } if( NULL != table->addr[index] ) { table->number_free++; } } else { if (NULL == table->addr[index]) { table->number_free--; } /* Reset lowest_free if required */ if ( index == table->lowest_free ) { int i; table->lowest_free = table->size; for ( i=index + 1; i<table->size; i++) { if ( NULL == table->addr[i] ){ table->lowest_free = i; break; } } } } table->addr[index] = value; #if 0 opal_output(0,"opal_pointer_array_set_item: OUT: " " table %p (size %ld, lowest free %ld, number free %ld)" " addr[%d] = %p\n", table, table->size, table->lowest_free, table->number_free, index, table->addr[index]); #endif OPAL_THREAD_UNLOCK(&(table->lock)); return OPAL_SUCCESS; }
int opal_pointer_array_set_size(opal_pointer_array_t *array, int new_size) { OPAL_THREAD_LOCK(&(array->lock)); if(new_size > array->size) { if (!grow_table(array, new_size, new_size)) { OPAL_THREAD_UNLOCK(&(array->lock)); return OPAL_ERROR; } } OPAL_THREAD_UNLOCK(&(array->lock)); return OPAL_SUCCESS; }
isc_result_t isc_symtab_define(isc_symtab_t *symtab, const char *key, unsigned int type, isc_symvalue_t value, isc_symexists_t exists_policy) { unsigned int bucket; elt_t *elt; REQUIRE(VALID_SYMTAB(symtab)); REQUIRE(key != NULL); REQUIRE(type != 0); FIND(symtab, key, type, bucket, elt); if (exists_policy != isc_symexists_add && elt != NULL) { if (exists_policy == isc_symexists_reject) return (ISC_R_EXISTS); INSIST(exists_policy == isc_symexists_replace); UNLINK(symtab->table[bucket], elt, link); if (symtab->undefine_action != NULL) (symtab->undefine_action)(elt->key, elt->type, elt->value, symtab->undefine_arg); } else { elt = (elt_t *)isc_mem_get(symtab->mctx, sizeof(*elt)); if (elt == NULL) return (ISC_R_NOMEMORY); ISC_LINK_INIT(elt, link); symtab->count++; } /* * Though the "key" can be const coming in, it is not stored as const * so that the calling program can easily have writable access to * it in its undefine_action function. In the event that it *was* * truly const coming in and then the caller modified it anyway ... * well, don't do that! */ DE_CONST(key, elt->key); elt->type = type; elt->value = value; /* * We prepend so that the most recent definition will be found. */ PREPEND(symtab->table[bucket], elt, link); if (symtab->count > symtab->maxload) grow_table(symtab); return (ISC_R_SUCCESS); }
size_t Processes::add(process* proc) { /* Register a new process. The process struct should have already been allocated. First process is 0, we always increment from there on. @return: index of process in the process table */ if (num_tasks >= ptable_size) grow_table(num_tasks - ptable_size +1); // TODO: Check pointer logic tasks[num_tasks] = proc; proc->id = num_tasks++; /* Process id is the number -1 as we zero index */ return num_tasks-1; }
/** * add a pointer to dynamic pointer table * * @param table Pointer to opal_pointer_array_t object (IN) * @param ptr Pointer to be added to table (IN) * * @return Array index where ptr is inserted or OPAL_ERROR if it fails */ int opal_pointer_array_add(opal_pointer_array_t *table, void *ptr) { int i, index; OPAL_THREAD_LOCK(&(table->lock)); if (table->number_free == 0) { /* need to grow table */ if (!grow_table(table, (NULL == table->addr ? TABLE_INIT : table->size * TABLE_GROW), INT_MAX)) { OPAL_THREAD_UNLOCK(&(table->lock)); return OPAL_ERR_OUT_OF_RESOURCE; } } assert( (table->addr != NULL) && (table->size > 0) ); assert( (table->lowest_free >= 0) && (table->lowest_free < table->size) ); assert( (table->number_free > 0) && (table->number_free <= table->size) ); /* * add pointer to table, and return the index */ index = table->lowest_free; assert(table->addr[index] == NULL); table->addr[index] = ptr; table->number_free--; if (table->number_free > 0) { for (i = table->lowest_free + 1; i < table->size; i++) { if (table->addr[i] == NULL) { table->lowest_free = i; break; } } } else { table->lowest_free = table->size; } OPAL_THREAD_UNLOCK(&(table->lock)); return index; }
wchar_t * string_set_add(wchar_t * source_string, String_set * ss) { wchar_t * str; int len, p; assert(source_string != NULL, L"STRING_SET: Can't insert a null string"); p = find_place(source_string, ss); if (ss->table[p] != NULL) return ss->table[p]; len = wcslen(source_string); str = (wchar_t *) xalloc(sizeof(wchar_t)*(len+1)); wcscpy(str, source_string); ss->table[p] = str; ss->count++; /* We just added it to the table. If the table got too big, we grow it. Too big is defined as being more than 3/4 full */ if ((4 * ss->count) > (3 * ss->size)) grow_table(ss); return str; }
void hash_insert(hash_t *hash, hnode_t *node, const void *key) { hash_val_t hkey, chain; assert (hash_val_t_bit != 0); assert (node->next == NULL); assert (hash->nodecount < hash->maxcount); /* 1 */ assert (hash_lookup(hash, key) == NULL); /* 2 */ if (hash->dynamic && hash->nodecount >= hash->highmark) /* 3 */ grow_table(hash); hkey = hash->function(key); chain = hkey & hash->mask; /* 4 */ node->key = key; node->hkey = hkey; node->next = hash->table[chain]; hash->table[chain] = node; hash->nodecount++; assert (hash_verify(hash)); }