0
-#define MAX_DENSITY 0.75
0
#define find_bin(hash, total) (hash & (total - 1))
0
-#define hash_redistribute_p(hash) (hash->entries->n2i() >= MAX_DENSITY * hash->bins->n2i())
0
@@ -26,8 +24,8 @@ namespace rubinius {
0
Hash* Hash::dup(STATE) {
0
- OBJECT tmp, ent, next;
0
+ Tuple *vals, *lst, *ent, *next;
0
dup = Hash::create(state, size);
0
@@ -42,26 +40,26 @@ namespace rubinius {
0
SET(dup, values, vals);
0
for(i = 0; i < size; i++) {
0
- tmp = (Tuple*)values->at(i);
0
- if(tmp->nil_p()) continue;
0
+ tmp = try_as<Tuple>(values->at(i));
0
- ent =
tmp->dup(state);
0
+ ent =
as<Tuple>(tmp->dup(state));
0
vals->put(state, i, ent);
0
+ next = try_as<Tuple>(ent->at(3));
0
- while(!next->nil_p()) {
0
- next = next->dup(state);
0
+ next = as<Tuple>(next->dup(state));
0
lst->put(state, 3, next);
0
+ next = try_as<Tuple>(next->at(3));
0
-
OBJECT Hash::entry_new(STATE, hashval hsh, OBJECT key, OBJECT data) {
0
+
Tuple* Hash::entry_new(STATE, hashval hsh, OBJECT key, OBJECT data) {
0
Tuple* tup = Tuple::create(state, 4);
0
tup->put(state, 0, Object::i2n(hsh));
0
tup->put(state, 1, key);
0
@@ -70,14 +68,14 @@ namespace rubinius {
0
-
OBJECT Hash::entry_append(STATE, OBJECT top, OBJECT nxt) {
0
+
Tuple* Hash::entry_append(STATE, Tuple* top, Tuple* nxt) {
0
- Tuple* cur = (Tuple*)top->at(3);
0
- Tuple* last = (Tuple*)top;
0
+ Tuple* cur = try_as<Tuple>(top->at(3));
0
- while(
!cur->nil_p()) {
0
- cur =
(Tuple*)cur->at(3);
0
+ cur =
try_as<Tuple>(cur->at(3));
0
@@ -85,17 +83,13 @@ namespace rubinius {
0
- OBJECT Hash::add_entry(STATE, hashval hsh, OBJECT ent) {
0
+ OBJECT Hash::add_entry(STATE, hashval hsh, Tuple* ent) {
0
size_t bin = find_bin(hsh, bins->n2i());
0
- entry = values->at(bin);
0
- values->put(state, bin, ent);
0
+ if(Tuple* entry = try_as<Tuple>(values->at(bin))) {
0
entry_append(state, entry, ent);
0
+ values->put(state, bin, ent);
0
entries = Object::i2n(entries->n2i() + 1);
0
@@ -112,20 +106,18 @@ namespace rubinius {
0
Tuple* new_values = Tuple::create(state, size);
0
for(size_t i = 0; i < num; i++) {
0
- Tuple* entry =
(Tuple*)values->at(i);
0
+ Tuple* entry =
try_as<Tuple>(values->at(i));
0
- while(!entry->nil_p()) {
0
- Tuple* next = (Tuple*)entry->at(3);
0
+ Tuple* next = try_as<Tuple>(entry->at(3));
0
entry->put(state, 3, Qnil);
0
native_int hash = as<Integer>(entry->at(0))->n2i();
0
size_t bin = find_bin(hash, size);
0
- OBJECT slot = new_values->at(bin);
0
- new_values->put(state, bin, entry);
0
+ if(Tuple* slot = try_as<Tuple>(new_values->at(bin))) {
0
entry_append(state, slot, entry);
0
+ new_values->put(state, bin, entry);
0
@@ -136,25 +128,29 @@ namespace rubinius {
0
bins = Object::i2n(size);
0
- OBJECT Hash::find_entry(STATE, hashval hsh) {
0
+ /* Find the entry that has a hash value of +hsh+. Return NULL if nothing
0
+ Tuple* Hash::find_entry(STATE, hashval hsh) {
0
size_t num = bins->n2i();
0
size_t bin = find_bin(hsh, num);
0
- OBJECT entry = values->at(bin);
0
+ Tuple* entry = try_as<Tuple>(values->at(bin));
0
+ if(!entry) return NULL;
0
-
while(!entry->nil_p()) {
0
INTEGER th = as<Integer>(entry->at(0));
0
if((hashval)th->n2i() == hsh) {
0
+ if((entry = try_as<Tuple>(entry->at(3))) == NULL) return NULL;
0
OBJECT Hash::add(STATE, hashval hsh, OBJECT key, OBJECT data) {
0
- Tuple* entry =
(Tuple*)find_entry(state, hsh);
0
+ Tuple* entry =
find_entry(state, hsh);
0
entry->put(state, 2, data);
0
@@ -171,8 +167,8 @@ namespace rubinius {
0
OBJECT Hash::get(STATE, hashval hsh) {
0
- OBJECT entry = find_entry(state, hsh);
0
+ Tuple* entry = find_entry(state, hsh);
0
@@ -183,20 +179,20 @@ namespace rubinius {
0
* Uses +==+ to verify the key in the table matches +key+.
0
* Return TRUE if key was found, and *value will be filled. */
0
int Hash::lookup(STATE, OBJECT key, hashval hash, OBJECT *value) {
0
- ent = find_entry(state, hash);
0
- if(ent->reference_p()) {
0
- while((hashval)as<Integer>(ent->at(0))->n2i() == hash) {
0
+ Tuple* ent = find_entry(state, hash);
0
+ if(!ent) return FALSE;
0
- if(ent->nil_p()) break;
0
+ while((hashval)as<Integer>(ent->at(0))->n2i() == hash) {
0
+ ent = try_as<Tuple>(ent->at(3));
0
@@ -207,20 +203,20 @@ namespace rubinius {
0
* Return TRUE if key was found, and *value will be filled. */
0
int Hash::lookup2(STATE, int (*compare)(STATE, OBJECT, OBJECT),
0
OBJECT key, hashval hash, OBJECT *value) {
0
- ent = find_entry(state, hash);
0
- if(ent->reference_p()) {
0
- while((hashval)as<Integer>(ent->at(0))->n2i() == hash) {
0
- if(compare(state, hk, key)) {
0
+ Tuple* ent = find_entry(state, hash);
0
+ if(!ent) return FALSE;
0
- if(ent->nil_p()) break;
0
+ while((hashval)as<Integer>(ent->at(0))->n2i() == hash) {
0
+ if(compare(state, hk, key)) {
0
+ ent = try_as<Tuple>(ent->at(3));
0
@@ -232,8 +228,8 @@ namespace rubinius {
0
- base = ent = (Tuple*)find_entry(state, hash);
0
- if(ent->reference_p()) {
0
+ base = ent = find_entry(state, hash);
0
while((hashval)as<Integer>(ent->at(0))->n2i() == hash) {
0
if(compare(state, hk, key)) {
0
@@ -241,8 +237,8 @@ namespace rubinius {
0
- ent = (Tuple*)ent->at(3);
0
- if(ent->nil_p()) break;
0
+ ent = try_as<Tuple>(ent->at(3));
0
@@ -251,14 +247,14 @@ namespace rubinius {
0
- if(base->reference_p()) {
0
- ent = (Tuple*)entry_new(state, hash, key, value);
0
+ ent = entry_new(state, hash, key, value);
0
entry_append(state, base, ent);
0
entries = Object::i2n(entries->n2i() + 1);
0
- ent =
(Tuple*)entry_new(state, hash, key, value);
0
+ ent =
entry_new(state, hash, key, value);
0
add_entry(state, hash, ent);
0
@@ -268,10 +264,8 @@ namespace rubinius {
0
OBJECT Hash::get_undef(STATE, hashval hsh) {
0
- entry = find_entry(state, hsh);
0
+ Tuple* entry = find_entry(state, hsh);
0
@@ -313,7 +307,7 @@ namespace rubinius {
0
- Hash* Hash::from_tuple(STATE,
OBJECT tup) {
0
+ Hash* Hash::from_tuple(STATE,
Tuple* tup) {