You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by am...@apache.org on 2018/08/22 14:55:29 UTC

[trafficserver] 04/08: IntrusiveHashMap: Change range to be std::pair based for better compatibility.

This is an automated email from the ASF dual-hosted git repository.

amc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git

commit 8913fa332862fcc610265f671ef33604d7da94e2
Author: Alan M. Carroll <am...@apache.org>
AuthorDate: Sat Jul 21 19:49:35 2018 -0500

    IntrusiveHashMap: Change range to be std::pair based for better compatibility.
---
 lib/ts/IntrusiveHashMap.h                  | 37 +++++++++++-------------------
 lib/ts/unit-tests/test_IntrusiveHashMap.cc |  4 ++--
 2 files changed, 16 insertions(+), 25 deletions(-)

diff --git a/lib/ts/IntrusiveHashMap.h b/lib/ts/IntrusiveHashMap.h
index 84a64fa..c69a9a5 100644
--- a/lib/ts/IntrusiveHashMap.h
+++ b/lib/ts/IntrusiveHashMap.h
@@ -151,12 +151,9 @@ public:
   /// A range of elements in the map.
   /// It is a half open range, [first, last) in the usual STL style.
   /// @internal I tried @c std::pair as a base for this but was unable to get STL container operations to work.
-  struct range {
-    iterator first; ///< First element.
-    iterator last;  ///< Past last element.
-
-    /// Construct from two iterators.
-    range(iterator const &lhs, iterator const &rhs);
+  struct range : public std::pair<iterator, iterator> {
+    using super_type = std::pair<iterator, iterator>; ///< Super type.
+    using super_type::super_type;                     ///< Use super type constructors.
 
     // These methods enable treating the range as a view in to the hash map.
 
@@ -167,12 +164,13 @@ public:
   };
 
   /// A range of constant elements in the map.
-  struct const_range {
-    const_iterator first; ///< First element.
-    const_iterator last;  ///< Past last element.
+  struct const_range : public std::pair<const_iterator, const_iterator> {
+    using super_type = std::pair<const_iterator, const_iterator>; ///< Super type.
+
+    /// Allow implicit conversion of range to const_range.
+    const_range(range const &r);
 
-    /// Construct from two iterators.
-    const_range(const_iterator const &lhs, const_iterator const &rhs);
+    using super_type::super_type; ///< Use super type constructors.
 
     // These methods enable treating the range as a view in to the hash map.
 
@@ -353,38 +351,31 @@ IntrusiveHashMap<H>::Bucket::contains(value_type *v) const
 }
 
 // ---------------------
-template <typename H> IntrusiveHashMap<H>::range::range(iterator const &lhs, iterator const &rhs) : first(lhs), last(rhs) {}
-
 template <typename H>
 auto
 IntrusiveHashMap<H>::range::begin() const -> iterator const &
 {
-  return first;
+  return super_type::first;
 }
 template <typename H>
 auto
 IntrusiveHashMap<H>::range::end() const -> iterator const &
 {
-  return last;
-}
-
-template <typename H>
-IntrusiveHashMap<H>::const_range::const_range(const_iterator const &lhs, const_iterator const &rhs) : first(lhs), last(rhs)
-{
+  return super_type::second;
 }
 
 template <typename H>
 auto
 IntrusiveHashMap<H>::const_range::begin() const -> const_iterator const &
 {
-  return first;
+  return super_type::first;
 }
 
 template <typename H>
 auto
 IntrusiveHashMap<H>::const_range::end() const -> const_iterator const &
 {
-  return last;
+  return super_type::second;
 }
 
 // ---------------------
@@ -476,7 +467,7 @@ IntrusiveHashMap<H>::equal_range(key_type key) -> range
     ++last;
   }
 
-  return {first, last};
+  return range{first, last};
 }
 
 template <typename H>
diff --git a/lib/ts/unit-tests/test_IntrusiveHashMap.cc b/lib/ts/unit-tests/test_IntrusiveHashMap.cc
index 67a0377..6828e3a 100644
--- a/lib/ts/unit-tests/test_IntrusiveHashMap.cc
+++ b/lib/ts/unit-tests/test_IntrusiveHashMap.cc
@@ -120,7 +120,7 @@ TEST_CASE("IntrusiveHashMap", "[libts][IntrusiveHashMap]")
   map.insert(new Thing("dup"sv, 81));
 
   auto r = map.equal_range("dup"sv);
-  REQUIRE(r.first != r.last);
+  REQUIRE(r.first != r.second);
   REQUIRE(r.first->_payload == "dup"sv);
 
   Map::iterator idx;
@@ -131,7 +131,7 @@ TEST_CASE("IntrusiveHashMap", "[libts][IntrusiveHashMap]")
       map.erase(map.iterator_for(&thing));
   });
   r = map.equal_range("dup"sv);
-  REQUIRE(r.first != r.last);
+  REQUIRE(r.first != r.second);
   idx = r.first;
   REQUIRE(idx->_payload == "dup"sv);
   REQUIRE((++idx)->_payload == "dup"sv);