void test_dup() { Object* k1 = Fixnum::from(4); tbl->store(state, k1, cTrue); LookupTable* tbl2 = tbl->duplicate(state); TS_ASSERT_EQUALS(tbl2->aref(state, k1), cTrue); }
void test_all_keys() { Object* k1 = Fixnum::from(4); tbl->store(state, k1, cTrue); Array* ary = tbl->all_keys(state); TS_ASSERT_EQUALS(ary->total()->to_native(), 1); TS_ASSERT_EQUALS(ary->get(state, 0), k1); }
LookupTable* SetCounter::to_ruby(VM* state) { LookupTable* tbl = LookupTable::create(state); tbl->store(state, stats::total(state), Integer::from(state, total_)); tbl->store(state, stats::max(state), Integer::from(state, max())); tbl->store(state, stats::min(state), Integer::from(state, min())); tbl->store(state, stats::average(state), Float::create(state, moving_average())); return tbl; }
LookupTable* CompactLookupTable::to_lookuptable(STATE) { LookupTable* tbl = (LookupTable*)LookupTable::create(state); for(unsigned int i = 0; i < COMPACTLOOKUPTABLE_SIZE; i += 2) { Object* key = at(state, i); if(!key->nil_p()) tbl->store(state, key, at(state, i + 1)); } return tbl; }
void test_find() { Object* k = Fixnum::from(47); tbl->store(state, k, cTrue); Object* out = tbl->find(state, k); TS_ASSERT_EQUALS(out, cTrue); out = tbl->find(state, Fixnum::from(40)); TS_ASSERT(out == cUndef); }
int main() { //test(); LookupTable table; // READ IN THE LIST OF KEYS HERE… ifstream inputFile; inputFile.open("input.txt"); string line; while(getline(inputFile,line)) { remove(line.begin(),line.end(),' '); table.insert(line,createItem(line)); } inputFile.close(); table.display(); Item i = table.retrieve("when"); cout << "count for when is: " << i.count << endl; // should be 1 i = table.retrieve("weather"); cout << "count for weather is: " << i.count << endl; // should be 2 table.remove("when"); table.remove("weather"); i = table.retrieve("weather"); cout << "count for weather is: " << i.count << endl; // should be 1 table.display(); }
/* Look at this class and it's superclass contents (which includes * included modules) and calculate out how to allocate the slots. * * This locks the class so that construction is serialized. */ void Class::auto_pack(STATE) { hard_lock(state); // If another thread did this work while we were waiting on the lock, // don't redo it. if(type_info_->type == PackedObject::type) return; size_t slots = 0; LookupTable* lt = LookupTable::create(state); // If autopacking is enabled, figure out how many slots to use. if(state->shared.config.gc_autopack) { Module* mod = this; int slot = 0; while(!mod->nil_p()) { Array* info = 0; if(Class* cls = try_as<Class>(mod)) { info = cls->seen_ivars(); } else if(IncludedModule* im = try_as<IncludedModule>(mod)) { info = im->module()->seen_ivars(); } if(info && !info->nil_p()) { for(size_t i = 0; i < info->size(); i++) { if(Symbol* sym = try_as<Symbol>(info->get(state, i))) { bool found = false; lt->fetch(state, sym, &found); if(!found) { lt->store(state, sym, Fixnum::from(slot++)); } } // Limit the number of packed ivars to 25. if(slot > 25) break; } } mod = mod->superclass(); } slots = lt->entries()->to_native(); } packed_size_ = sizeof(Object) + (slots * sizeof(Object*)); packed_ivar_info(state, lt); set_object_type(state, PackedObject::type); hard_unlock(state); }
void test_to_lookuptable() { LookupTable* lt; Object* key = Fixnum::from(1); Object* val = Fixnum::from(2); tbl->put(state, 2, key); tbl->put(state, 3, val); lt = tbl->to_lookuptable(state); TS_ASSERT_EQUALS(lt->fetch(state, key), val); }
void test_find_entry() { Object* k = Fixnum::from(47); tbl->store(state, k, cTrue); LookupTableBucket* entry = tbl->find_entry(state, k); TS_ASSERT(entry != cNil); TS_ASSERT_EQUALS(entry->key(),k); TS_ASSERT_EQUALS(entry->value(),cTrue); entry = tbl->find_entry(state, Fixnum::from(40)); TS_ASSERT(entry == cNil); }
Object* PackedObject::packed_ivar_defined(STATE, Symbol* sym) { LookupTable* tbl = this->reference_class()->packed_ivar_info(); bool found = false; Fixnum* which = try_as<Fixnum>(tbl->fetch(state, sym, &found)); if(!found) { return table_ivar_defined(state, sym); } Object* obj = body_as_array()[which->to_native()]; if(obj == Qundef) return Qfalse; return Qtrue; }
Object* PackedObject::get_packed_ivar(STATE, Symbol* sym) { LookupTable* tbl = this->reference_class()->packed_ivar_info(); assert(tbl && !tbl->nil_p()); Fixnum* which = try_as<Fixnum>(tbl->fetch(state, sym)); if(!which) { return get_table_ivar(state, sym); } Object* obj = body_as_array()[which->to_native()]; if(obj == Qundef) return Qnil; return obj; }
static int _gather_names(const UChar *name, const UChar *name_end, int ngroup_num, int *group_nums, regex_t *reg, struct _gather_data *gd) { int gn; STATE; LookupTable* tbl = gd->tbl; state = gd->state; gn = group_nums[0]; tbl->store(state, state->symbol((char*)name), Fixnum::from(gn - 1)); return 0; }
Object* PackedObject::set_packed_ivar(STATE, Symbol* sym, Object* val) { LookupTable* tbl = this->reference_class()->packed_ivar_info(); bool found = false; Fixnum* which = try_as<Fixnum>(tbl->fetch(state, sym, &found)); if(!found) { return set_table_ivar(state, sym, val); } body_as_array()[which->to_native()] = val; if(val->reference_p()) write_barrier(state, val); return val; }
Object* CallFrame::find_breakpoint(STATE) { if(!compiled_code) return 0; LookupTable* tbl = compiled_code->breakpoints(); if(tbl->nil_p()) return 0; bool found = false; Object* obj = tbl->fetch(state, Fixnum::from(ip()), &found); if(found) return obj; return 0; }
void PackedObject::add_packed_ivars(STATE, Array* ary) { LookupTable* tbl = this->reference_class()->packed_ivar_info(); LookupTable::iterator i(tbl); while(i.advance()) { Object* key = i.key(); if(Fixnum* which = try_as<Fixnum>(tbl->fetch(state, key))) { if(body_as_array()[which->to_native()] != Qundef) { ary->append(state, key); } } } }
void Transcoding::declare(STATE, const char* from, const char* to, const char* lib) { LookupTable* map = Encoding::transcoding_map(state); Object* obj = map->fetch(state, encoding_symbol(state, from)); LookupTable* table; if(obj->nil_p()) { table = LookupTable::create(state); map->store(state, encoding_symbol(state, from), table); } else { table = as<LookupTable>(obj); } table->store(state, encoding_symbol(state, to), String::create(state, lib)); }
void test_remove() { Object* k = Fixnum::from(47); tbl->store(state, k, cTrue); Object* out = tbl->find(state, k); TS_ASSERT_EQUALS(out, cTrue); out = tbl->remove(state, k); TS_ASSERT_EQUALS(out, cTrue); TS_ASSERT_EQUALS(as<Integer>(tbl->entries())->to_native(), 0); out = tbl->fetch(state, k); TS_ASSERT_EQUALS(out, cNil); }
void Transcoding::define(STATE, OnigTranscodingType* tr) { LookupTable* map = Encoding::transcoding_map(state); Object* obj = map->fetch(state, encoding_symbol(state, tr->src_encoding)); LookupTable* table; if(obj->nil_p()) { table = LookupTable::create(state); map->store(state, encoding_symbol(state, tr->src_encoding), table); } else { table = as<LookupTable>(obj); } Transcoding* t = Transcoding::create(state, tr); table->store(state, encoding_symbol(state, tr->dst_encoding), t); }
void PackedObject::add_packed_ivars(STATE, Array* ary) { LookupTable* tbl = this->reference_class()->packed_ivar_info(); assert(tbl && !tbl->nil_p()); Array* keys = tbl->all_keys(state); for(size_t i = 0; i < keys->size(); i++) { Object* key = keys->get(state, i); if(Fixnum* which = try_as<Fixnum>(tbl->fetch(state, key))) { if(body_as_array()[which->to_native()] != Qundef) { ary->append(state, key); } } } }
void checkSizes(LookupTable table) { for (int i = 0; i < table.size(); i++) { cout << "Row # " << i << " = " << table[i].size() << endl; } }
Object* PackedObject::packed_ivar_delete(STATE, Symbol* sym, bool* removed) { LookupTable* tbl = this->reference_class()->packed_ivar_info(); bool found = false; Fixnum* which = try_as<Fixnum>(tbl->fetch(state, sym, &found)); if(!found) { return del_table_ivar(state, sym, removed); } if(removed) *removed = true; Object* val = body_as_array()[which->to_native()]; body_as_array()[which->to_native()] = Qundef; return val; }
void test_store_resizes_table() { size_t i; size_t bins = tbl-> bins()->to_native(); for(i = 0; i < bins; i++) { tbl->store(state, Fixnum::from(i), Fixnum::from(i)); } TS_ASSERT_EQUALS(i, (size_t)as<Integer>(tbl->entries())->to_native()); TS_ASSERT((size_t)(tbl-> bins()->to_native()) > bins); for(i = 0; i < bins; i++) { TS_ASSERT_EQUALS(Fixnum::from(i), tbl->aref(state, Fixnum::from(i))); } }
VALUE rb_protect_inspect(VALUE (*func)(VALUE a, VALUE b), VALUE h_obj, VALUE h_arg) { NativeMethodEnvironment* env = NativeMethodEnvironment::get(); STATE = env->state(); Thread* thr = Thread::current(state); LookupTable* rectbl = thr->recursive_objects(); Object* obj = env->get_object(h_obj); Object* id = obj->id(state); bool found = false; rectbl->fetch(state, id, &found); if(found) { return (*func)(h_obj, h_arg); } rectbl->store(state, id, cTrue); VALUE ret = Qnil; ExceptionPoint ep(env); PLACE_EXCEPTION_POINT(ep); bool unwinding = false; if(unlikely(ep.jumped_to())) { unwinding = true; } else { ret = (*func)(h_obj, h_arg); } ep.pop(env); // Get the thread and table again, the GC might have fun. thr = Thread::current(state); rectbl = thr->recursive_objects(); obj = env->get_object(h_obj); id = obj->id(state); rectbl->remove(state, id); if(unwinding) env->current_ep()->return_to(env); return ret; }
Handle Find (FindStyle findStyle, const T_Id & id) { auto iter = m_lookupTable.find(id); if (iter != m_lookupTable.end()) return iter->second; switch (findStyle) { case FindStyle::CREATE_NOW: { return CreateNow(id); } case FindStyle::FAIL: { SDL_assert(iter != m_lookupTable.end()); return Handle(); } } return Handle(); }
void test_store_resizes_table_with_chained_bins() { size_t i; size_t bins = tbl-> bins()->to_native() - 2; Object* k1 = Fixnum::from((4 << 5) | 31); Object* k2 = Fixnum::from((10 << 5) | 31); Object* k3 = Fixnum::from((11 << 5) | 31); tbl->store(state, k1, cTrue); tbl->store(state, k2, cTrue); tbl->store(state, k3, cTrue); for(i = 0; i < bins; i++) { tbl->store(state, Fixnum::from(i), cTrue); } TS_ASSERT((size_t)(tbl-> bins()->to_native()) > bins); }
void test_remove_redistributes() { size_t bins = tbl-> bins()->to_native(); size_t bound = bins * 2; for(size_t i = 0; i < bound; i++) { tbl->store(state, Fixnum::from(i), cTrue); } TS_ASSERT(bins < (size_t)tbl-> bins()->to_native()); for(size_t i = 0; i < bound; i++) { Object* out = tbl->remove(state, Fixnum::from(i)); TS_ASSERT_EQUALS(out, cTrue); } TS_ASSERT_EQUALS(bins, static_cast<unsigned int>(tbl-> bins()->to_native())); }
VALUE rb_inspecting_p(VALUE h_obj) { NativeMethodEnvironment* env = NativeMethodEnvironment::get(); STATE = env->state(); Thread* thr = Thread::current(state); LookupTable* rectbl = thr->recursive_objects(); Object* obj = env->get_object(h_obj); Object* id = obj->id(state); bool found = false; rectbl->fetch(state, id, &found); if(found) return Qtrue; return Qfalse; }
Handle CreateNow (T_Id id) { auto findResult = m_lookupTable.find(id); SDL_assert(findResult == m_lookupTable.end()); if (findResult != m_lookupTable.end()) { SDL_SetError( "ObjectDbTable::Create: Object already exists at {%lu,%lu}.", id.high, id.low ); return Handle(); } Handle handle; T_Object * obj = m_objectPool.Alloc(&handle); SDL_assert(obj); m_lookupTable[id] = handle; return handle; }
LookupTable* LookupTable::dup(STATE) { size_t size, i; LookupTable *dup; size = bins_->to_native(); dup = LookupTable::create(state, size); state->om->set_class(dup, class_object(state)); size_t num = entries_->to_native(); Array* entries = all_entries(state); for(i = 0; i < num; i++) { Tuple* entry = as<Tuple>(entries->get(state, i)); Object* key = entry->at(state, 0); Object* value = entry->at(state, 1); dup->store(state, key, value); } return dup; }
LookupTable* LookupTable::duplicate(STATE) { size_t size, i; LookupTable *dup; size = bins_->to_native(); dup = LookupTable::create(state, size); // Allow for subclassing. dup->klass(state, class_object(state)); size_t num = entries_->to_native(); Array* entries = all_entries(state); for(i = 0; i < num; i++) { LookupTableBucket* entry = as<LookupTableBucket>(entries->get(state, i)); dup->store(state, entry->key(), entry->value()); } return dup; }