2
0
Эх сурвалжийг харах

(#157) not an aids upgrade. 0.27.0 never happened

rexim 4 жил өмнө
parent
commit
88a6ed2eb2
1 өөрчлөгдсөн 19 нэмэгдсэн , 16 устгасан
  1. 19 16
      src/aids.hpp

+ 19 - 16
src/aids.hpp

@@ -21,7 +21,7 @@
 //
 //
 // ============================================================
 // ============================================================
 //
 //
-// aids — 0.27.0 — std replacement for C++. Designed to aid developers
+// aids — 0.28.0 — std replacement for C++. Designed to aid developers
 // to a better programming experience.
 // to a better programming experience.
 //
 //
 // https://github.com/rexim/aids
 // https://github.com/rexim/aids
@@ -30,7 +30,8 @@
 //
 //
 // ChangeLog (https://semver.org/ is implied)
 // ChangeLog (https://semver.org/ is implied)
 //
 //
-//   0.27.0 struct Hash_Map
+//   0.28.0 struct Hash_Map
+//   0.27.0 NEVER HAPPENED
 //   0.26.0 panic() is marked with [[noreturn]] attribute
 //   0.26.0 panic() is marked with [[noreturn]] attribute
 //          code_to_utf8() implementation is refactored in a backward compatible way
 //          code_to_utf8() implementation is refactored in a backward compatible way
 //   0.25.0 void print1(FILE *stream, Hex<char> hex)
 //   0.25.0 void print1(FILE *stream, Hex<char> hex)
@@ -1054,11 +1055,11 @@ namespace aids
         struct Bucket
         struct Bucket
         {
         {
             Key key;
             Key key;
-            Maybe<Value> value;
+            Value value;
         };
         };
 
 
         // TODO: Maybe<Bucket> *buckets
         // TODO: Maybe<Bucket> *buckets
-        Bucket *buckets;
+        Maybe<Bucket> *buckets;
         size_t capacity;
         size_t capacity;
         size_t size;
         size_t size;
 
 
@@ -1070,21 +1071,21 @@ namespace aids
                 assert(capacity == 0);
                 assert(capacity == 0);
                 assert(size == 0);
                 assert(size == 0);
 
 
-                buckets = (Bucket*) calloc(HASH_MAP_INITIAL_CAPACITY, sizeof(Bucket));
+                buckets = (Maybe<Bucket>*) calloc(HASH_MAP_INITIAL_CAPACITY, sizeof(*buckets));
                 capacity = HASH_MAP_INITIAL_CAPACITY;
                 capacity = HASH_MAP_INITIAL_CAPACITY;
                 size = 0;
                 size = 0;
             } else {
             } else {
                 Hash_Map<Key, Value> new_hash_map = {
                 Hash_Map<Key, Value> new_hash_map = {
-                    (Bucket*) calloc(capacity * 2, sizeof(Bucket)),
+                    (Maybe<Bucket>*) calloc(capacity * 2, sizeof(*buckets)),
                     capacity * 2,
                     capacity * 2,
                     0
                     0
                 };
                 };
 
 
                 for (size_t i = 0; i < capacity; ++i) {
                 for (size_t i = 0; i < capacity; ++i) {
-                    if (buckets[i].value.has_value) {
+                    if (buckets[i].has_value) {
                         new_hash_map.insert(
                         new_hash_map.insert(
-                            buckets[i].key,
-                            buckets[i].value.unwrap);
+                            buckets[i].unwrap.key,
+                            buckets[i].unwrap.value);
                     }
                     }
                 }
                 }
 
 
@@ -1101,11 +1102,13 @@ namespace aids
             }
             }
 
 
             auto hk = hash(key) & (capacity - 1);
             auto hk = hash(key) & (capacity - 1);
-            while (buckets[hk].value.has_value && buckets[hk].key != key) {
+            while (buckets[hk].has_value && buckets[hk].unwrap.key != key) {
                 hk = (hk + 1) & (capacity - 1);
                 hk = (hk + 1) & (capacity - 1);
             }
             }
-            buckets[hk].key = key;
-            buckets[hk].value = {true, value};
+            buckets[hk].has_value = true;
+            buckets[hk].unwrap.key = key;
+            buckets[hk].unwrap.value = value;
+            size += 1;
         }
         }
 
 
         Maybe<Value*> get(Key key)
         Maybe<Value*> get(Key key)
@@ -1113,14 +1116,14 @@ namespace aids
             auto hk = hash(key) & (capacity - 1);
             auto hk = hash(key) & (capacity - 1);
             for (size_t i = 0;
             for (size_t i = 0;
                  i < capacity
                  i < capacity
-                     && buckets[hk].value.has_value
-                     && buckets[hk].key != key;
+                     && buckets[hk].has_value
+                     && buckets[hk].unwrap.key != key;
                  ++i) {
                  ++i) {
                 hk = (hk + 1) & (capacity - 1);
                 hk = (hk + 1) & (capacity - 1);
             }
             }
 
 
-            if (buckets && buckets[hk].value.has_value && buckets[hk].key == key) {
-                return {true, &buckets[hk].value.unwrap};
+            if (buckets && buckets[hk].has_value && buckets[hk].unwrap.key == key) {
+                return {true, &buckets[hk].unwrap.value};
             } else {
             } else {
                 return {};
                 return {};
             }
             }