ERR_Code aCatalogue::initObject() { ERR_Code err = aObject::initObject(); if ( err ) return err; aCfgItem g = md->find( obj, md_group ), e = md->find( obj, md_element ); err = tableInsert( aDatabase::tableDbName( *md, e ), e ); if ( err ) return err; return tableInsert( aDatabase::tableDbName( *md, g ), g, md_group ); }
ERR_Code aDocJournal::initObject() { ERR_Code err = err_noerror; aCfgItem mditem, docitem, header; journalType=0; setInited( true ); md = 0; if ( db ) md = &db->cfg; if(!md) { aLog::print(aLog::Error, tr("aDocJournal md object not exists")); return err_objnotfound; } // aObject::initObject(); journalType= md->attr(obj,mda_type).toInt(); if (journalType) { mditem= md->find(obj,md_fieldid); if (mditem.isNull()) { aLog::print(aLog::Error, tr("aDocJournal columns not defined")); return err; } else { aLog::print(aLog::Debug, tr("aDocJournal column defined")); } docitem= md->parent( md->parent( md->find(md->text(mditem).toLong()))); header = md->find( docitem, md_header ); if(header.isNull()) aLog::print(aLog::Error, tr("aDocJournal invalid column define")); //table for special journal err = tableInsert( aDatabase::tableDbName( *md, header ), header ); // printf("table name is %s\n", aDatabase::tableDbName( *md, header ).ascii()); } else { //table for common journal err = tableInsert( "a_journ" ); } return err; }
static int load_actions(table_t* t) { size_t i; for(i=0; i<rtf_action_mapping_cnt; i++) if(tableInsert(t, rtf_action_mapping[i].controlword, rtf_action_mapping[i].action) == -1) return -1; return 0; }
/*! *\~english * Init object. * We can work with object only after inited. * Auto call from constructor. *\~russian *\brief Инициализирует объект элементом конфигурации. * * Мы можем работать с объектом после его инициализации. * Функция вызывается из конструктора. *\~ * \return \en error code. \_en \ru код ошибки.\_ru */ ERR_Code aCatGroup::initObject() { ERR_Code err = aObject::initObject(); if ( err ) return err; aCfgItem g = md->find( obj, md_group ); return tableInsert( aDatabase::tableDbName( *md, g ), g ); }
int main() { char* line; line = getNextLine(); Hashtable table = tableCreate(); char* author; author = findAuthor(line); tableInsert(table, author, line); free(author); printf("%s\n",line); char* line2; while(line2 = getNextLine()) { if(strcmp(line,line2) == 0) //if line2 = prev line..print ibid { printf("%s\n","ibid."); } else { author = findAuthor(line2); char* authorLine; authorLine = (char*)tableSearch(table, author); if(authorLine != NULL && !strcmp(authorLine, line2)) { printf("%s%s\n","op. cit. ",author); } else { tableInsert(table, author, line2); printf("%s\n",line2); } free(author); } free(line); line = strdup(line2); free(line2); } free(line); tableDestroy(table); return 0; }
rettype kobbeltDotProd(const fptype *v1, const fptype *v2, const unsigned int size) { /* Start by inserting the exact products * of the values into a table ordered by their genus */ std::map<int, fptype> table; for(unsigned int i = 0; i < size; i++) { std::array<fptype, 2> prod = twoProd(v1[i], v2[i]); tableInsert(table, prod[0]); tableInsert(table, prod[1]); } /* Now add them together in the order * from least genus to greatest */ rettype ret = 0.0; for(auto kvpair : table) { ret += kvpair.second; } return ret; }
void tableInsert(std::map<int, fptype> &table, fptype val) { /* First determine where in the table the value is to go */ int genus = computeGenus(val); if(table.count(genus) == 0) { /* There is no value here * We might be able to exactly add this number * and the one with the same exponent * but different final bit in the mantissa, * though, so check for that one. * If it exists and is of opposite sign, * then it will work */ int otherGenus = genus ^ 1; if(table.count(otherGenus) > 0 && sign(val) != sign(table[otherGenus])) { /* The other value exists and is of the correct sign, * so add them together, remove the other value, * and insert their sum */ val += table[otherGenus]; table.erase(otherGenus); tableInsert(table, val); } else { /* Nothing else to do, just insert it */ table.insert({genus, val}); } } else { /* There is already a value of the same genus, * so we can add them exactly, * remove the other value from the table, * and then insert their sum */ val += table[genus]; table.erase(genus); tableInsert(table, val); } }
/* grows the hashtable by GROWTH_FACTOR */ static void grow(Hashtable h) { Hashtable h2; /* new hashtable to be created */ struct hashtable swap; /* temp hashtable for swapping */ struct citation* c; h2 = internalTableCreate(h->size*GROWTH_FACTOR); int i; for(i = 0; i < h->size; i++) { for(c = h->table[i]; c != 0; c = c->next) { tableInsert(h2,c->author,c->line); } } swap = *h; *h = *h2; *h2 = swap; tableDestroy(h2); }
/* * Change a value in the table. If the key isn't in the table insert it * Returns -1 for error, otherwise the new value */ int tableUpdate(table_t *table, const char *key, int new_value) { tableEntry *tableItem; assert(table != NULL); if(key == NULL) return -1; /* not treated as a fatal error */ for(tableItem = table->tableHead; tableItem; tableItem = tableItem->next) if(tableItem->key && (strcasecmp(tableItem->key, key) == 0)) { tableItem->value = new_value; return new_value; } /* not found */ return tableInsert(table, key, new_value); }
//initialize hash table void initHashTable(HT hashTable, char *dictName, int (*hashFun)(ElemType *word, int size)) { FILE *fr; fr = fopen(dictName, "rt"); if (fr == NULL) { printf("Can't open file\n"); return; } Node *table; int tableSize = TSIZE; int i; char word[keySize]; int r; while (1) { table = (Node *)malloc(tableSize * sizeof(Node)); hashTable->theList = table; for(i = tableSize - 1; i >= 0; i--){ hashTable->theList[i].key = malloc(keySize * sizeof(ElemType)); hashTable->theList[i].key[0] = '\0'; //initialize key element? hashTable->theList[i].next = NULL; } while((r = readAWord(fr, word)) != 0) { tableInsert(hashTable, word, hashFun); if ((float)(hashTable->nonEmpBucket) / (float)(hashTable->tableSize) > 0.75) { freeHashTable(hashTable); tableSize *= 2; hashTable->tableSize = tableSize; hashTable->numEntry = 0; hashTable->nonEmpBucket = 0; hashTable->maxListLeng = 0; hashTable->avgListLeng = 0; break; } } if (r == 0) { break; } } }
Table tablePrune( Table table, int window, int pos, int *numbits ) { int curbits; int maxcode; int code; int swp; Table newTable; int i; newTable = tableMake(table->maxbits); curbits = STARTING_BITS; maxcode = 1 << curbits; for (i = 0; i < table->size; i++) { if (table->entries[i] && (pos - table->entries[i]->accessed < window || i <= NUM_SPECIALS + UCHAR_MAX)) { code = i; while (code >= 0 && table->entries[code]) { tableInsert(table->entries[code], newTable); if (maxcode <= getSize(newTable)) { curbits++; maxcode *= 2; } swp = table->entries[code]->prefix; table->entries[code] = 0; code = swp; } } } *numbits = curbits; freeTable(table); // printf("Table Size: %d\n", newTable->size); // tableDump(newTable); return newTable; }
void testInsertSort() { myRDT d[N]={{49,1},{38,2},{65,3},{97,4},{76,5},{13,6},{27,7},{49,8}}; mySQLIST l1,l2,l3,l4,l5,l6,l7,l8,l9; int i ; for(i=0;i<N;++i) l1.r[i+1]=d[i]; l1.length=N; l2=l3=l1; l5=l1; l6=l1; l7=l1; l8=l1; l9=l1; printf("еепРг╟:\n"); print(l1); insertSort(l1); printf("ж╠╫с╡ЕхКеепР╨С:\n"); print(l1); BInsertSort(l2); printf("уш╟К╡ЕхКеепР╨С:\n"); print(l2); printf("2_б╥╡ЕхКеепР╨С:\n"); p2_insertSort(l3); print(l3); mySLKT sl1,sl2; int *adr; tableInsert(sl1,d,N); sl2=sl1; printf("еепРг╟:\n"); print2(sl1); arrange(sl1); printf("l1еепР╨С:\n"); print2(sl1); adr=(int *)malloc((sl2.length+1)*sizeof(int)); sort(sl2,adr); for(i=1;i<=sl2.length;++i) printf("adr[%d]=%d ",i,adr[i]); printf("\n"); rearrange(sl2,adr); printf("l2еепР╨С:\n"); print2(sl2); int dt[]={5,3,1}; for(i=0;i<N;++i) l4.r[i+1]=d[i]; l4.length=N; printf("еепРг╟: \n"); shellSort<int>(l4,dt,3,print); printf("еепР╨С: "); print(l4); int d1[N]= {1,2,4,5,6,77,88,99}; //{49,38,65,97,76,13,27,49}; printf("еепРг╟:\n"); print3(d1,N); printf("еепР╨С:\n"); bubble_sort(d1,N,print3); printf("©ЛкыеепР╨С: \n"); quickSort(l5); print(l5); printf("я║тЯеепР╨С: \n"); selectSort(l6); print(l6); printf("йВпня║тЯеепР╨С: \n"); treeSort(l7); print(l7); printf("╤яеепР╨С: \n"); heapSort(l8); print(l8); printf("╧И╡╒еепР╨С: \n"); mergeSort(l9); print(l9); myRDT d3[N+2]={{278,1},{109,2},{63,3},{930,4},{589,5},{184,6},{505,7},{269,8},{8,9},{83,10}}; mySLLIST l10; int *adr1; intiList1_(l10,d3,N+2); printf("еепРг╟(nextсР╩╧ц╩╦Ёж╣):\n"); print5(l10); radixSort<int>(l10,print4); printf("еепР╨С(╬╡л╛а╢╠М):\n"); print5(l10); adr1=(int*)malloc(l10.recnum*sizeof(int)); sSort(l10,adr1); sRearrange(l10,adr1); printf("еепР╨С(жьее╪гб╪):\n"); print5(l10); }
/* * 向cuckoo hashing中插入数据 * 成功则返回插入后的哈希表, 否则返回NULL */ CuckooHash* cuckooInsertItem(CuckooHash *cuckoo_hash, void *data, void *info) { int func_num = cuckoo_hash->func_num; int max_steps = cuckoo_hash->max_steps; int mid = max_steps / 2; int free_pos, pos, table_index; void *old_data, *old_info; int opt_pos; /* 标示是否当前kick out的上次插入操作是否也有kick out */ int pre_kickout_flag = 0; /* 记录下kick out时的选择 */ HashValue pre_hash_value; HashValue *hv_arr; int optimize_flag = global_lsh_param->config->optimize_kickout; hv_arr = cuckoo_hash->hv_arr; #ifdef _EVALUATION_ struct timeval start, end; gettimeofday(&start, NULL); #endif while(max_steps >= 0) { free_pos = cuckoo_hash->find_opt_pos(cuckoo_hash, hv_arr, data); //有空位可用 if(free_pos > 0) { pre_kickout_flag = 0; if(optimize_flag == OPTIMIZE_ON && max_steps < mid) { qsort(hv_arr, free_pos, sizeof(HashValue), ch_comp); pos = 0; } else { //随机选位置 pos = rand() % free_pos; } table_index = hv_arr[pos].table_index; if(tableInsert(cuckoo_hash->hash_tables[table_index], hv_arr[pos].hv, data, info) == NULL) { fprintf(stderr, "Error: insert used position, have to exit!\n"); exit(-1); } //成功则跳出循环 break; } else if(max_steps != 0 && free_pos != 0) { // 当max_steps为0时,禁止kick out opt_pos = -free_pos; if(optimize_flag == OPTIMIZE_ON && max_steps < mid) { qsort(hv_arr, opt_pos, sizeof(HashValue), ch_comp); pos = 0; } else { //无空位,需要kick out //同样,随机选择kick out的位置 pos = rand() % opt_pos; } table_index = hv_arr[pos].table_index; //判断是否选择的位置是上次刚被kick out的位置 //如果是,將位置加1,以避开该位置 if(pre_kickout_flag == 1) { if(table_index == pre_hash_value.table_index && \ hv_arr[pos].hv == pre_hash_value.hv) { pos++; pos = pos % opt_pos; table_index = hv_arr[pos].table_index; } } tableKickOut(cuckoo_hash->hash_tables[table_index], hv_arr[pos].hv, data, info, &old_data, &old_info); data = old_data; info = old_info; pre_kickout_flag = 1; pre_hash_value.hv = hv_arr[pos].hv; pre_hash_value.func_index = hv_arr[pos].func_index; pre_hash_value.table_index = hv_arr[pos].table_index; } max_steps--; } //把kick out次数记录下来 cuckoo_hash->kickout_counter += (cuckoo_hash->max_steps - max_steps); #ifdef _EVALUATION_ gettimeofday(&end, NULL); insert_item_time = 1000000*(end.tv_sec - start.tv_sec) + end.tv_usec - start.tv_usec; insert_items_time += insert_item_time; #endif //超过kick out上限或者禁止了kick out if(max_steps < 0) { return NULL; } return cuckoo_hash; }
NODE * tmap_insertR(TMAP *t, NODE *r, NODE *parent, char * name, double val, char side, char parentBalance, int depth){ //------------- //need to check whether is size-balanced each time insert char balanced; balanced = Balance(r, name, val); //------------ NODE *leaf; depth++; if(r == NULL){ leaf = (NODE *)malloc(sizeof(NODE)); leaf->left = NULL; leaf->right = NULL; leaf->nameVal.name = name; leaf->nameVal.value = val; (*t)->size++; if (depth > (*t)->height) { (*t)->height = depth; } ((*t)->numOfInsertion)++; tableInsert((*t)->hashTable, name, leaf, h_One); return leaf; } // the tie is broken by the name // (by standard alphabetical order) if(r->nameVal.value == val){ if(strcmp(r->nameVal.name, name) > 0){ r->left = tmap_insertR(t, r->left, r, name, val, 0, balanced, depth); }else{ r->right = tmap_insertR(t, r->right, r, name, val, 1, balanced, depth); } } else if(val < r->nameVal.value){ r->left = tmap_insertR(t, r->left, r, name, val, 0, balanced, depth); } else { r->right = tmap_insertR(t, r->right, r, name, val, 1, balanced, depth); } // if the nearest node is not found and the sub-tree is not balanced if (parentBalance == BALANCED && balanced == INBALANCED) { ((*t)->numOfRebalance)++; if (parent != NULL) { // if non-root subtree is balanced if (side == 0) { parent->left = reBalance(r); (*t)->height = calHeight((*t)->root); return parent->left; } else if (side == 1) { parent->right = reBalance(r); (*t)->height = calHeight((*t)->root); return parent->right; } } else { (*t)->root = reBalance(r); (*t)->height = calHeight((*t)->root); return (*t)->root; } } else { return r; } }// end of tmap_insertR