Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:31:37

0001 // Copyright 2018 The Abseil Authors.
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //      https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 //
0015 // Utilities to help tests verify that hash tables properly handle stateful
0016 // allocators and hash functions.
0017 
0018 #ifndef ABSL_CONTAINER_INTERNAL_HASH_POLICY_TESTING_H_
0019 #define ABSL_CONTAINER_INTERNAL_HASH_POLICY_TESTING_H_
0020 
0021 #include <cstdlib>
0022 #include <limits>
0023 #include <memory>
0024 #include <ostream>
0025 #include <type_traits>
0026 #include <utility>
0027 #include <vector>
0028 
0029 #include "absl/hash/hash.h"
0030 #include "absl/strings/string_view.h"
0031 
0032 namespace absl {
0033 ABSL_NAMESPACE_BEGIN
0034 namespace container_internal {
0035 namespace hash_testing_internal {
0036 
0037 template <class Derived>
0038 struct WithId {
0039   WithId() : id_(next_id<Derived>()) {}
0040   WithId(const WithId& that) : id_(that.id_) {}
0041   WithId(WithId&& that) : id_(that.id_) { that.id_ = 0; }
0042   WithId& operator=(const WithId& that) {
0043     id_ = that.id_;
0044     return *this;
0045   }
0046   WithId& operator=(WithId&& that) {
0047     id_ = that.id_;
0048     that.id_ = 0;
0049     return *this;
0050   }
0051 
0052   size_t id() const { return id_; }
0053 
0054   friend bool operator==(const WithId& a, const WithId& b) {
0055     return a.id_ == b.id_;
0056   }
0057   friend bool operator!=(const WithId& a, const WithId& b) { return !(a == b); }
0058 
0059  protected:
0060   explicit WithId(size_t id) : id_(id) {}
0061 
0062  private:
0063   size_t id_;
0064 
0065   template <class T>
0066   static size_t next_id() {
0067     // 0 is reserved for moved from state.
0068     static size_t gId = 1;
0069     return gId++;
0070   }
0071 };
0072 
0073 }  // namespace hash_testing_internal
0074 
0075 struct NonStandardLayout {
0076   NonStandardLayout() {}
0077   explicit NonStandardLayout(std::string s) : value(std::move(s)) {}
0078   virtual ~NonStandardLayout() {}
0079 
0080   friend bool operator==(const NonStandardLayout& a,
0081                          const NonStandardLayout& b) {
0082     return a.value == b.value;
0083   }
0084   friend bool operator!=(const NonStandardLayout& a,
0085                          const NonStandardLayout& b) {
0086     return a.value != b.value;
0087   }
0088 
0089   template <typename H>
0090   friend H AbslHashValue(H h, const NonStandardLayout& v) {
0091     return H::combine(std::move(h), v.value);
0092   }
0093 
0094   std::string value;
0095 };
0096 
0097 struct StatefulTestingHash
0098     : absl::container_internal::hash_testing_internal::WithId<
0099           StatefulTestingHash> {
0100   template <class T>
0101   size_t operator()(const T& t) const {
0102     return absl::Hash<T>{}(t);
0103   }
0104 };
0105 
0106 struct StatefulTestingEqual
0107     : absl::container_internal::hash_testing_internal::WithId<
0108           StatefulTestingEqual> {
0109   template <class T, class U>
0110   bool operator()(const T& t, const U& u) const {
0111     return t == u;
0112   }
0113 };
0114 
0115 // It is expected that Alloc() == Alloc() for all allocators so we cannot use
0116 // WithId base. We need to explicitly assign ids.
0117 template <class T = int>
0118 struct Alloc : std::allocator<T> {
0119   using propagate_on_container_swap = std::true_type;
0120 
0121   // Using old paradigm for this to ensure compatibility.
0122   explicit Alloc(size_t id = 0) : id_(id) {}
0123 
0124   Alloc(const Alloc&) = default;
0125   Alloc& operator=(const Alloc&) = default;
0126 
0127   template <class U>
0128   Alloc(const Alloc<U>& that) : std::allocator<T>(that), id_(that.id()) {}
0129 
0130   template <class U>
0131   struct rebind {
0132     using other = Alloc<U>;
0133   };
0134 
0135   size_t id() const { return id_; }
0136 
0137   friend bool operator==(const Alloc& a, const Alloc& b) {
0138     return a.id_ == b.id_;
0139   }
0140   friend bool operator!=(const Alloc& a, const Alloc& b) { return !(a == b); }
0141 
0142  private:
0143   size_t id_ = (std::numeric_limits<size_t>::max)();
0144 };
0145 
0146 template <class Map>
0147 auto items(const Map& m) -> std::vector<
0148     std::pair<typename Map::key_type, typename Map::mapped_type>> {
0149   using std::get;
0150   std::vector<std::pair<typename Map::key_type, typename Map::mapped_type>> res;
0151   res.reserve(m.size());
0152   for (const auto& v : m) res.emplace_back(get<0>(v), get<1>(v));
0153   return res;
0154 }
0155 
0156 template <class Set>
0157 auto keys(const Set& s)
0158     -> std::vector<typename std::decay<typename Set::key_type>::type> {
0159   std::vector<typename std::decay<typename Set::key_type>::type> res;
0160   res.reserve(s.size());
0161   for (const auto& v : s) res.emplace_back(v);
0162   return res;
0163 }
0164 
0165 }  // namespace container_internal
0166 ABSL_NAMESPACE_END
0167 }  // namespace absl
0168 
0169 // ABSL_UNORDERED_SUPPORTS_ALLOC_CTORS is false for glibcxx versions
0170 // where the unordered containers are missing certain constructors that
0171 // take allocator arguments. This test is defined ad-hoc for the platforms
0172 // we care about (notably Crosstool 17) because libstdcxx's useless
0173 // versioning scheme precludes a more principled solution.
0174 // From GCC-4.9 Changelog: (src: https://gcc.gnu.org/gcc-4.9/changes.html)
0175 // "the unordered associative containers in <unordered_map> and <unordered_set>
0176 // meet the allocator-aware container requirements;"
0177 #if defined(__GLIBCXX__) && __GLIBCXX__ <= 20140425
0178 #define ABSL_UNORDERED_SUPPORTS_ALLOC_CTORS 0
0179 #else
0180 #define ABSL_UNORDERED_SUPPORTS_ALLOC_CTORS 1
0181 #endif
0182 
0183 #endif  // ABSL_CONTAINER_INTERNAL_HASH_POLICY_TESTING_H_