/** * \brief Returns an equipment item. * \param item_name Name of the item to get. * \return The corresponding item. */ const EquipmentItem& Equipment::get_item(const std::string& item_name) const { Debug::check_assertion(item_exists(item_name), std::string("No such item: '") + item_name + "'"); return *items.find(item_name)->second; }
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); }
/** * \brief Returns an equipment item. * \param item_name Name of the item to get. * \return The corresponding item. */ const EquipmentItem& Equipment::get_item(const std::string& item_name) const { Debug::check_assertion(item_exists(item_name), StringConcat() << "Cannot find item with name '" << item_name << "'"); return *items.find(item_name)->second; }
bool DOSBoxMenu::item_exists(const std::string &name) { auto i = name_map.find(name); if (i == name_map.end()) return false; return item_exists(i->second); }
int main (int argc, char **argv) { int *table; int table_size = 4; // 0,1,2,3 are special int nbits = 9; // Always start with 512 item table. int max_bits = 0, prev_code = 0; int maxed = 0; int choose; choose = which(argv); table = calloc( (1 << nbits), sizeof(int)); init_table(table, nbits, &table_size); // Encode if ( choose == 1 ) { // Prove that ncode created the file. putBits(nbits, MAGIC1); putBits(nbits, MAGIC2); // Get max_bits int tmp; tmp = get_arg(argv, argc, 'm'); max_bits = get_m_arg(argv, tmp); if (max_bits < 0) { exit(0); } putBits(nbits, max_bits); // Get ratio int c, check_time; int code_bits = 0, char_bits = 0, count = 0; double ratio; tmp = get_arg(argv, argc, 'r'); check_time = get_r_arg(argv, tmp, &ratio); if (check_time < 0) { exit(0); } // Let's go while ((c = getchar()) != EOF) { char_bits += 8; // Item found, read next char if ( (tmp = item_exists(prev_code, c, table, nbits))) { prev_code = tmp; continue; } // item not found. Add item to table. Output prev_code. // new prev_code is c's index else { code_bits += nbits; count++; if (!maxed) { add_item(prev_code, c, table, nbits, &table_size); } putBits(nbits, prev_code); prev_code = item_exists(0, c, table, nbits); // Resize table if (table_size > (0.99 * TABLE_SIZE) && !maxed) { if (nbits != max_bits) { code_bits += (2 * nbits) + 1; putBits(nbits++, 1); putBits(nbits, c); table = init_new(table, TABLE_SIZE, table_size, nbits); } else { maxed = 1; putBits(nbits, 2); code_bits += nbits; } } // -r if (check_time && !(count % check_time) && (code_bits > ratio * char_bits)) { // re init putBits(nbits, 3); nbits = 9; free(table); table = calloc( (1 << nbits), sizeof(int)); init_table(table, nbits, &table_size); maxed = 0; code_bits = 0; char_bits = 0; } } } putBits(nbits, prev_code); putBits(nbits, 0); flushBits(); free(table); } // Decode else { int i, code, hold = 0, tmp_code, mx=0; len_array outstring; outstring.array = malloc(MAX_LEN * sizeof(int)); // Check that there are no args if (argc != 1) { fprintf(stderr, "Unexpected argument to decode\n"); exit(0); } // Check that this is actually a file we encoded if ( (getBits(nbits) != MAGIC1) || (getBits(nbits) != MAGIC2)) { fprintf(stderr, "Wrong file"); exit(0); } // Let's go max_bits = getBits(nbits); while ( (code = getBits(nbits)) ) { outstring.len = 0; // Table resize if (code == 1) { nbits++; // get the next letter, add and resize code = getBits(nbits); tmp_code = add_item(prev_code, code, table, nbits-1, &table_size); table = init_new(table, TABLE_SIZE, table_size, nbits); hold = 1; continue; } // Size is maxed, set maxed after next add else if (code == 2) { mx = 1; continue; } // Reinit table else if (code == 3) { nbits = 9; free(table); table = calloc( (1 << nbits), sizeof(int)); init_table(table, nbits, &table_size); prev_code = 0; maxed = 0; mx = 0; continue; } // Code in table else if ( (table[code] % 2)) { backtrace(table, code, &outstring); for (i=0; i<outstring.len; i++) { printf("%c", outstring.array[i]); } // If !prev_code, item has len 1 & will be there already // If maxed, table full // If hold, first add after resize - already there if (prev_code && !hold && !maxed) { add_item(prev_code, outstring.array[0], table, nbits, &table_size); } prev_code = code; } // Item not found in table. Item must be the result of // table[prev_code] + table[prev_code][0] // We also MUST be able to add it - no worries about max_bits else { backtrace(table, prev_code, &outstring); outstring.array[outstring.len++] = outstring.array[0]; for (i=0; i<outstring.len; i++) { printf("%c", outstring.array[i]); } // If it is not the first one after a resize if (!hold) { prev_code = add_item(prev_code, outstring.array[0], table, nbits, &table_size); } else { prev_code = tmp_code; } // Check that we the code we get from adding is the expected if (prev_code != code) { fprintf(stderr, "File corrupted\n"); exit(0); } } if (hold) { hold = 0; } if (mx) { mx = 0; maxed = 1; } } free(table); free(outstring.array); } return(0); }