Przeglądaj źródła

remove hash from entry

szdytom 1 rok temu
rodzic
commit
d25afcaeae
1 zmienionych plików z 15 dodań i 20 usunięć
  1. 15 20
      src/objects/dict.c

+ 15 - 20
src/objects/dict.c

@@ -4,10 +4,7 @@
 #include <assert.h>
 #include <assert.h>
 #include <string.h>
 #include <string.h>
 
 
-#define HASH_MASK ((int64_t)0xffffffff)
-
 struct pkpy_DictEntry {
 struct pkpy_DictEntry {
-    int32_t hash;
     pkpy_Var key;
     pkpy_Var key;
     pkpy_Var val;
     pkpy_Var val;
 };
 };
@@ -71,10 +68,10 @@ static void pkpy_Dict__htset(pkpy_Dict* self, int h, int v) {
     }
     }
 }
 }
 
 
-static int pkpy_Dict__probe0(const pkpy_Dict* self, void* vm, pkpy_Var key, int hash) {
+static int pkpy_Dict__probe0(const pkpy_Dict* self, void* vm, pkpy_Var key, int64_t hash) {
     const int null = pkpy_Dict__idx_null(self);
     const int null = pkpy_Dict__idx_null(self);
     const int mask = self->_htcap - 1;
     const int mask = self->_htcap - 1;
-    for(int h = hash & mask;; h = (h + 1) & mask) {
+    for(int h = hash & mask;; h = (h * 5 + 1) & mask) {
         int idx = pkpy_Dict__htget(self, h);
         int idx = pkpy_Dict__htget(self, h);
         if(idx == null) return h;
         if(idx == null) return h;
 
 
@@ -84,16 +81,16 @@ static int pkpy_Dict__probe0(const pkpy_Dict* self, void* vm, pkpy_Var key, int
     PK_UNREACHABLE();
     PK_UNREACHABLE();
 }
 }
 
 
-static int pkpy_Dict__probe1(const pkpy_Dict* self, void* vm, pkpy_Var key, int hash) {
+static int pkpy_Dict__probe1(const pkpy_Dict* self, void* vm, pkpy_Var key, int64_t hash) {
     const int null = pkpy_Dict__idx_null(self);
     const int null = pkpy_Dict__idx_null(self);
     const int mask = self->_htcap - 1;
     const int mask = self->_htcap - 1;
-    for(int h = hash & mask;; h = (h + 1) & mask) {
+    for(int h = hash & mask;; h = (h * 5 + 1) & mask) {
         int idx = pkpy_Dict__htget(self, h);
         int idx = pkpy_Dict__htget(self, h);
         if(idx == null) return h;
         if(idx == null) return h;
 
 
         struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
         struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
         if(pkpy_Var__is_null(&entry->key)) continue;
         if(pkpy_Var__is_null(&entry->key)) continue;
-        if(entry->hash == hash && pkpy_Var__eq__(vm, entry->key, key)) return h;
+        if(pkpy_Var__eq__(vm, entry->key, key)) return h;
     }
     }
     PK_UNREACHABLE();
     PK_UNREACHABLE();
 }
 }
@@ -109,13 +106,13 @@ static void pkpy_Dict__extendht(pkpy_Dict* self, void* vm) {
         struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, i);
         struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, i);
         if(pkpy_Var__is_null(&entry->key)) continue;
         if(pkpy_Var__is_null(&entry->key)) continue;
 
 
-        int h = pkpy_Dict__probe0(self, vm, entry->key, entry->hash);
+        int h = pkpy_Dict__probe0(self, vm, entry->key, pkpy_Var__hash__(vm, entry->key));
         pkpy_Dict__htset(self, h, i);
         pkpy_Dict__htset(self, h, i);
     }
     }
 }
 }
 
 
 bool pkpy_Dict__set(pkpy_Dict* self, void* vm, pkpy_Var key, pkpy_Var val) {
 bool pkpy_Dict__set(pkpy_Dict* self, void* vm, pkpy_Var key, pkpy_Var val) {
-    int hash = pkpy_Var__hash__(vm, key) & HASH_MASK;
+    int64_t hash = pkpy_Var__hash__(vm, key);
     int h = pkpy_Dict__probe1(self, vm, key, hash);
     int h = pkpy_Dict__probe1(self, vm, key, hash);
 
 
     int idx = pkpy_Dict__htget(self, h);
     int idx = pkpy_Dict__htget(self, h);
@@ -125,7 +122,6 @@ bool pkpy_Dict__set(pkpy_Dict* self, void* vm, pkpy_Var key, pkpy_Var val) {
         c11_vector__push(struct pkpy_DictEntry,
         c11_vector__push(struct pkpy_DictEntry,
                          &self->_entries,
                          &self->_entries,
                          ((struct pkpy_DictEntry){
                          ((struct pkpy_DictEntry){
-                             .hash = hash,
                              .key = key,
                              .key = key,
                              .val = val,
                              .val = val,
                          }));
                          }));
@@ -138,7 +134,7 @@ bool pkpy_Dict__set(pkpy_Dict* self, void* vm, pkpy_Var key, pkpy_Var val) {
 
 
     struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
     struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
 
 
-    if(entry->hash == hash || pkpy_Var__eq__(vm, entry->key, key)) {
+    if(pkpy_Var__eq__(vm, entry->key, key)) {
         entry->val = val;
         entry->val = val;
     } else {
     } else {
         self->_version += 1;
         self->_version += 1;
@@ -148,20 +144,19 @@ bool pkpy_Dict__set(pkpy_Dict* self, void* vm, pkpy_Var key, pkpy_Var val) {
         struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
         struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
         entry->key = key;
         entry->key = key;
         entry->val = val;
         entry->val = val;
-        entry->hash = hash;
     }
     }
     return false;
     return false;
 }
 }
 
 
 bool pkpy_Dict__contains(const pkpy_Dict* self, void* vm, pkpy_Var key) {
 bool pkpy_Dict__contains(const pkpy_Dict* self, void* vm, pkpy_Var key) {
-    int hash = pkpy_Var__hash__(vm, key) & HASH_MASK;
+    int64_t hash = pkpy_Var__hash__(vm, key);
     int h = pkpy_Dict__probe1(self, vm, key, hash);
     int h = pkpy_Dict__probe1(self, vm, key, hash);
 
 
     int idx = pkpy_Dict__htget(self, h);
     int idx = pkpy_Dict__htget(self, h);
     if(idx == pkpy_Dict__idx_null(self)) return false;
     if(idx == pkpy_Dict__idx_null(self)) return false;
 
 
     struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
     struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
-    assert(entry->hash == hash && pkpy_Var__eq__(vm, entry->key, key));
+    assert(pkpy_Var__eq__(vm, entry->key, key));
     return true;
     return true;
 }
 }
 
 
@@ -185,7 +180,7 @@ static bool pkpy_Dict__refactor(pkpy_Dict* self, void* vm) {
 
 
         int j = self->_entries.count;
         int j = self->_entries.count;
         c11_vector__push(struct pkpy_DictEntry, &self->_entries, *entry);
         c11_vector__push(struct pkpy_DictEntry, &self->_entries, *entry);
-        int h = pkpy_Dict__probe0(self, vm, entry->key, entry->hash);
+        int h = pkpy_Dict__probe0(self, vm, entry->key, pkpy_Var__hash__(vm, entry->key));
         pkpy_Dict__htset(self, h, j);
         pkpy_Dict__htset(self, h, j);
     }
     }
     c11_vector__dtor(&old_entries);
     c11_vector__dtor(&old_entries);
@@ -193,13 +188,13 @@ static bool pkpy_Dict__refactor(pkpy_Dict* self, void* vm) {
 }
 }
 
 
 bool pkpy_Dict__del(pkpy_Dict* self, void* vm, pkpy_Var key) {
 bool pkpy_Dict__del(pkpy_Dict* self, void* vm, pkpy_Var key) {
-    int hash = pkpy_Var__hash__(vm, key) & HASH_MASK;
+    int64_t hash = pkpy_Var__hash__(vm, key);
     int h = pkpy_Dict__probe1(self, vm, key, hash);
     int h = pkpy_Dict__probe1(self, vm, key, hash);
     int idx = pkpy_Dict__htget(self, h), null = pkpy_Dict__idx_null(self);
     int idx = pkpy_Dict__htget(self, h), null = pkpy_Dict__idx_null(self);
     if(idx == null) return false;
     if(idx == null) return false;
     
     
     struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
     struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
-    assert(entry->hash == hash && pkpy_Var__eq__(vm, entry->key, key));
+    assert(pkpy_Var__eq__(vm, entry->key, key));
     self->_version += 1;
     self->_version += 1;
     pkpy_Var__set_null(&entry->key);
     pkpy_Var__set_null(&entry->key);
     self->count -= 1;
     self->count -= 1;
@@ -208,14 +203,14 @@ bool pkpy_Dict__del(pkpy_Dict* self, void* vm, pkpy_Var key) {
 }
 }
 
 
 const pkpy_Var *pkpy_Dict__try_get(const pkpy_Dict* self, void* vm, pkpy_Var key) {
 const pkpy_Var *pkpy_Dict__try_get(const pkpy_Dict* self, void* vm, pkpy_Var key) {
-    int hash = pkpy_Var__hash__(vm, key) & HASH_MASK;
+    int64_t hash = pkpy_Var__hash__(vm, key);
     int h = pkpy_Dict__probe1(self, vm, key, hash);
     int h = pkpy_Dict__probe1(self, vm, key, hash);
     
     
     int idx = pkpy_Dict__htget(self, h);
     int idx = pkpy_Dict__htget(self, h);
     if(idx == pkpy_Dict__idx_null(self)) return NULL;
     if(idx == pkpy_Dict__idx_null(self)) return NULL;
     
     
     struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
     struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
-    assert(entry->hash == hash && pkpy_Var__eq__(vm, entry->key, key));
+    assert(pkpy_Var__eq__(vm, entry->key, key));
     return &entry->val;
     return &entry->val;
 }
 }