瀏覽代碼

Adding operator==/!= for hash set and hash map.
Adding test case for reserve, demonstrating that all elements remain present (TODO: extend to prove that they are in the same order because a resize/rehash was not needed?).

Samuel Jaffe 8 年之前
父節點
當前提交
1925fba47e
共有 4 個文件被更改,包括 35 次插入0 次删除
  1. 14 0
      bucket_hash_map.hpp
  2. 6 0
      bucket_hash_map.t.h
  3. 9 0
      bucket_hash_set.hpp
  4. 6 0
      bucket_hash_set.t.h

+ 14 - 0
bucket_hash_map.hpp

@@ -197,6 +197,20 @@ public:
     swap(key_equals_, other.key_equals_);
   }
   friend void swap(bucket_hash_map & lhs, bucket_hash_map & rhs) { lhs.swap(rhs); }
+  friend bool operator==(bucket_hash_map const & lhs, bucket_hash_map const & rhs) {
+    if (lhs.size() != rhs.size()) { return false; }
+    auto end = rhs.end();
+    for (auto & pair : lhs) {
+      auto it = rhs.find(pair.first);
+      if (it == end || it->second != pair.second) {
+        return false;
+      }
+    }
+    return true;
+  }
+  friend bool operator!=(bucket_hash_map const & lhs, bucket_hash_map const & rhs) {
+    return !(lhs == rhs);
+  }
 private:
   void maybe_expand(size_type add) {
     if ( static_cast<float>(size() + add) / bucket_count() > max_load_factor() ) {

+ 6 - 0
bucket_hash_map.t.h

@@ -249,4 +249,10 @@ public:
     TS_ASSERT(hm.empty());
   }
 
+  void test_resize_not_destructive() {
+    hashmap hm{{0, 1}, {1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}};
+    hashmap const copy{hm};
+    hm.reserve(6);
+    TS_ASSERT_EQUALS(hm, copy);
+  }
 };

+ 9 - 0
bucket_hash_set.hpp

@@ -219,6 +219,15 @@ private:
     return { ::iterator::in_place, make_end_aware_iterator(b.current(), b.end()),
       make_end_aware_iterator(b->erase(l.current()), l.end()) };
   }
+private:
+  friend bool operator==(bucket_hash_set const & lhs, bucket_hash_set const & rhs) {
+    if (lhs.size() != rhs.size()) { return false; }
+    for (auto & val : lhs) { if (rhs.count(val) == 0) { return false; } }
+    return true;
+  }
+  friend bool operator!=(bucket_hash_set const & lhs, bucket_hash_set const & rhs) {
+    return !(lhs == rhs);
+  }
 private: // members
   impl_type buckets_{default_buckets};
   hasher hasher_;

+ 6 - 0
bucket_hash_set.t.h

@@ -201,4 +201,10 @@ public:
     TS_ASSERT(hm.empty());
   }
   
+  void test_resize_not_destructive() {
+    hashset hm{0, 1, 2, 3, 4, 5};
+    hashset const copy{hm};
+    hm.reserve(6);
+    TS_ASSERT_EQUALS(hm, copy);
+  }
 };