예제 #1
0
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();
}
예제 #2
0
  void test_remove_works_for_chained_bins() {
    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, cNil);
    tbl->store(state, k2, cTrue);
    tbl->store(state, k3, cFalse);

    TS_ASSERT_EQUALS(tbl->remove(state, k3), cFalse);
    TS_ASSERT_EQUALS(tbl->remove(state, k2), cTrue);
    TS_ASSERT_EQUALS(tbl->remove(state, k1), cNil);

    TS_ASSERT_EQUALS(0, as<Integer>(tbl->entries())->to_native());
  }
예제 #3
0
  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);
  }
예제 #4
0
void test()
{
  LookupTable table;
  cout << "test(): inserting 'when'" << endl;
  table.insert("when", createItem("when"));        table.display();
  cout << "test(): inserting 'can'" << endl;
  table.insert("can", createItem("can"));          table.display();
  cout << "test(): inserting 'sailing'" << endl;
  table.insert("sailing", createItem("sailing"));  table.display();
  cout << "test(): inserting 'weather'" << endl;
  table.insert("weather", createItem("weather"));  table.display();

  Item i = table.retrieve("when");

  i = table.retrieve("weather");

  table.remove("when");
  table.display();
  table.remove("weather");

  i = table.retrieve("weather");

  table.display();
}
예제 #5
0
  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;
  }
예제 #6
0
  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()));
  }
예제 #7
0
  void test_remove_works_for_unknown_key() {
    Object* k1 = Fixnum::from(4);

    TS_ASSERT_EQUALS(cNil, tbl->remove(state, k1));
  }