Przeglądaj źródła

Fixed nasty bug in hashset implementation

Billy Barrow 1 tydzień temu
rodzic
commit
2f14b57902
1 zmienionych plików z 7 dodań i 6 usunięć
  1. 7 6
      src/lib/DataStructures/HashSet.vala

+ 7 - 6
src/lib/DataStructures/HashSet.vala

@@ -50,7 +50,7 @@ namespace Invercargill.DataStructures {
         private void double_buckets() {
             n_buckets *= 2;
             var old_buckets = buckets;
-            var buckets = new HashSetItem<T>*[n_buckets];
+            buckets = new HashSetItem<T>*[n_buckets];
             n_collissions = 0;
 
             for(var i = 0; i < old_buckets.length; i++) {
@@ -59,7 +59,7 @@ namespace Invercargill.DataStructures {
                     continue;
                 }
 
-                var bucket_index = bucket->hash % n_buckets;;
+                var bucket_index = bucket->hash % n_buckets;
                 while(buckets[bucket_index] != null) {
                     bucket_index++;
                     n_collissions++;
@@ -68,7 +68,6 @@ namespace Invercargill.DataStructures {
 
                 buckets[bucket_index] = bucket;
             }
-            
         }
 
         private uint bucket_for(T item) {
@@ -108,15 +107,17 @@ namespace Invercargill.DataStructures {
             
             var bucket_hash = hash_func(item);
             var bucket_index = bucket_hash % n_buckets;
-            while(buckets[bucket_index] != null && buckets[bucket_index] != tombstone) {
-                if(equal_func(buckets[bucket_index]->item, item)) {
+            var bucket = buckets[bucket_index];
+            while(bucket != null && bucket != tombstone) {
+                if(bucket->hash == bucket_hash && equal_func(bucket->item, item)) {
                     if(overwrite)
-                        buckets[bucket_index]->item = (owned)item;
+                        bucket->item = (owned)item;
                     return overwrite;
                 }
                 bucket_index++;
                 n_collissions++;
                 ensure_room_for_bucket(bucket_index);
+                bucket = buckets[bucket_index];
             }
             
             HashSetItem<T>* new_bucket = new HashSetItem<T>();