From 8dbd3591def0c638b103f87ec9f478603a942127 Mon Sep 17 00:00:00 2001 From: greg Date: Wed, 1 Sep 2021 18:26:43 -0400 Subject: [PATCH] issue #106: make btree_container's size_type unsigned for consistency --- parallel_hashmap/btree.h | 4 ++-- tests/btree_test.cc | 34 ++++++++++++++-------------- tests/btree_test.h | 48 ++++++++++++++++++++-------------------- 3 files changed, 43 insertions(+), 43 deletions(-) diff --git a/parallel_hashmap/btree.h b/parallel_hashmap/btree.h index 97a6c10..ae1aec5 100644 --- a/parallel_hashmap/btree.h +++ b/parallel_hashmap/btree.h @@ -837,7 +837,7 @@ namespace priv { using allocator_type = Alloc; using key_type = Key; - using size_type = std::make_signed::type; + using size_type = std::size_t ; using difference_type = ptrdiff_t; // True if this is a multiset or multimap. @@ -2755,7 +2755,7 @@ namespace priv { return {0, _begin}; } - if (count == size_) { + if (count == (difference_type)size_) { clear(); return {count, this->end()}; } diff --git a/tests/btree_test.cc b/tests/btree_test.cc index 1dc64cd..4630610 100644 --- a/tests/btree_test.cc +++ b/tests/btree_test.cc @@ -40,12 +40,12 @@ namespace phmap { namespace test_internal { - int BaseCountedInstance::num_instances_ = 0; - int BaseCountedInstance::num_live_instances_ = 0; - int BaseCountedInstance::num_moves_ = 0; - int BaseCountedInstance::num_copies_ = 0; - int BaseCountedInstance::num_swaps_ = 0; - int BaseCountedInstance::num_comparisons_ = 0; + size_t BaseCountedInstance::num_instances_ = 0; + size_t BaseCountedInstance::num_live_instances_ = 0; + size_t BaseCountedInstance::num_moves_ = 0; + size_t BaseCountedInstance::num_copies_ = 0; + size_t BaseCountedInstance::num_swaps_ = 0; + size_t BaseCountedInstance::num_comparisons_ = 0; } // namespace test_internal } // namespace phmap\ @@ -210,7 +210,7 @@ public: } int erase(const key_type &key) { - int size = tree_.size(); + size_t size = tree_.size(); int res = (int)checker_.erase(key); EXPECT_EQ(res, tree_.count(key)); EXPECT_EQ(res, tree_.erase(key)); @@ -221,8 +221,8 @@ public: } iterator erase(iterator iter) { key_type key = iter.key(); - int size = tree_.size(); - int count = tree_.count(key); + size_t size = tree_.size(); + size_t count = tree_.count(key); auto checker_iter = checker_.lower_bound(key); for (iterator tmp(tree_.lower_bound(key)); tmp != iter; ++tmp) { ++checker_iter; @@ -231,7 +231,7 @@ public: ++checker_next; checker_.erase(checker_iter); iter = tree_.erase(iter); - EXPECT_EQ(tree_.size(), checker_.size()); + EXPECT_EQ(tree_.size(), (size_t)checker_.size()); EXPECT_EQ(tree_.size(), size - 1); EXPECT_EQ(tree_.count(key), count - 1); if (count == 1) { @@ -241,7 +241,7 @@ public: } void erase(iterator begin, iterator end) { - int size = tree_.size(); + size_t size = tree_.size(); int count = std::distance(begin, end); auto checker_begin = checker_.lower_bound(begin.key()); for (iterator tmp(tree_.lower_bound(begin.key())); tmp != begin; ++tmp) { @@ -281,7 +281,7 @@ public: } // Move through the forward iterators using decrement. - for (int n = tree_.size() - 1; n >= 0; --n) { + for (int n = (int)tree_.size() - 1; n >= 0; --n) { iter_check(tree_iter, checker_iter); --tree_iter; --checker_iter; @@ -297,7 +297,7 @@ public: } // Move through the reverse iterators using decrement. - for (int n = tree_.size() - 1; n >= 0; --n) { + for (int n = (int)tree_.size() - 1; n >= 0; --n) { riter_check(tree_riter, checker_riter); --tree_riter; --checker_riter; @@ -344,7 +344,7 @@ namespace { // Insertion routines. std::pair insert(const value_type &x) { - int size = this->tree_.size(); + size_t size = this->tree_.size(); std::pair checker_res = this->checker_.insert(x); std::pair tree_res = this->tree_.insert(x); @@ -355,7 +355,7 @@ namespace { return tree_res; } iterator insert(iterator position, const value_type &x) { - int size = this->tree_.size(); + size_t size = this->tree_.size(); std::pair checker_res = this->checker_.insert(x); iterator tree_res = this->tree_.insert(position, x); @@ -392,7 +392,7 @@ namespace { // Insertion routines. iterator insert(const value_type &x) { - int size = this->tree_.size(); + size_t size = this->tree_.size(); auto checker_res = this->checker_.insert(x); iterator tree_res = this->tree_.insert(x); CheckPairEquals(*tree_res, *checker_res); @@ -401,7 +401,7 @@ namespace { return tree_res; } iterator insert(iterator position, const value_type &x) { - int size = this->tree_.size(); + size_t size = this->tree_.size(); auto checker_res = this->checker_.insert(x); iterator tree_res = this->tree_.insert(position, x); CheckPairEquals(*tree_res, *checker_res); diff --git a/tests/btree_test.h b/tests/btree_test.h index d096553..07d1f38 100644 --- a/tests/btree_test.h +++ b/tests/btree_test.h @@ -67,7 +67,7 @@ namespace test_internal { // InstanceTracker in tests to track the number of instances. class BaseCountedInstance { public: - explicit BaseCountedInstance(int x) : value_(x) { + explicit BaseCountedInstance(size_t x) : value_(x) { ++num_instances_; ++num_live_instances_; } @@ -143,7 +143,7 @@ namespace test_internal { : phmap::weak_ordering::greater; } - int value() const { + size_t value() const { if (!is_live_) std::abort(); return value_; } @@ -166,28 +166,28 @@ namespace test_internal { private: friend class InstanceTracker; - int value_; + size_t value_; // Indicates if the value is live, ie it hasn't been moved away from. bool is_live_ = true; // Number of instances. - static int num_instances_; + static size_t num_instances_; // Number of live instances (those that have not been moved away from.) - static int num_live_instances_; + static size_t num_live_instances_; // Number of times that BaseCountedInstance objects were moved. - static int num_moves_; + static size_t num_moves_; // Number of times that BaseCountedInstance objects were copied. - static int num_copies_; + static size_t num_copies_; // Number of times that BaseCountedInstance objects were swapped. - static int num_swaps_; + static size_t num_swaps_; // Number of times that BaseCountedInstance objects were compared. - static int num_comparisons_; + static size_t num_comparisons_; }; // Helper to track the BaseCountedInstance instance counters. Expects that the @@ -208,33 +208,33 @@ namespace test_internal { // Returns the number of BaseCountedInstance instances both containing valid // values and those moved away from compared to when the InstanceTracker was // constructed - int instances() const { + size_t instances() const { return BaseCountedInstance::num_instances_ - start_instances_; } // Returns the number of live BaseCountedInstance instances compared to when // the InstanceTracker was constructed - int live_instances() const { + size_t live_instances() const { return BaseCountedInstance::num_live_instances_ - start_live_instances_; } // Returns the number of moves on BaseCountedInstance objects since // construction or since the last call to ResetCopiesMovesSwaps(). - int moves() const { return BaseCountedInstance::num_moves_ - start_moves_; } + size_t moves() const { return BaseCountedInstance::num_moves_ - start_moves_; } // Returns the number of copies on BaseCountedInstance objects since // construction or the last call to ResetCopiesMovesSwaps(). - int copies() const { + size_t copies() const { return BaseCountedInstance::num_copies_ - start_copies_; } // Returns the number of swaps on BaseCountedInstance objects since // construction or the last call to ResetCopiesMovesSwaps(). - int swaps() const { return BaseCountedInstance::num_swaps_ - start_swaps_; } + size_t swaps() const { return BaseCountedInstance::num_swaps_ - start_swaps_; } // Returns the number of comparisons on BaseCountedInstance objects since // construction or the last call to ResetCopiesMovesSwaps(). - int comparisons() const { + size_t comparisons() const { return BaseCountedInstance::num_comparisons_ - start_comparisons_; } @@ -250,18 +250,18 @@ namespace test_internal { } private: - int start_instances_; - int start_live_instances_; - int start_moves_; - int start_copies_; - int start_swaps_; - int start_comparisons_; + size_t start_instances_; + size_t start_live_instances_; + size_t start_moves_; + size_t start_copies_; + size_t start_swaps_; + size_t start_comparisons_; }; // Copyable, not movable. class CopyableOnlyInstance : public BaseCountedInstance { public: - explicit CopyableOnlyInstance(int x) : BaseCountedInstance(x) {} + explicit CopyableOnlyInstance(size_t x) : BaseCountedInstance(x) {} CopyableOnlyInstance(const CopyableOnlyInstance& rhs) = default; CopyableOnlyInstance& operator=(const CopyableOnlyInstance& rhs) = default; @@ -275,7 +275,7 @@ namespace test_internal { // Copyable and movable. class CopyableMovableInstance : public BaseCountedInstance { public: - explicit CopyableMovableInstance(int x) : BaseCountedInstance(x) {} + explicit CopyableMovableInstance(size_t x) : BaseCountedInstance(x) {} CopyableMovableInstance(const CopyableMovableInstance& rhs) = default; CopyableMovableInstance(CopyableMovableInstance&& rhs) = default; CopyableMovableInstance& operator=(const CopyableMovableInstance& rhs) = @@ -292,7 +292,7 @@ namespace test_internal { // Only movable, not default-constructible. class MovableOnlyInstance : public BaseCountedInstance { public: - explicit MovableOnlyInstance(int x) : BaseCountedInstance(x) {} + explicit MovableOnlyInstance(size_t x) : BaseCountedInstance(x) {} MovableOnlyInstance(MovableOnlyInstance&& other) = default; MovableOnlyInstance& operator=(MovableOnlyInstance&& other) = default;