1 | // Copyright 2018 The Abseil Authors. |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | #include "absl/container/internal/raw_hash_set.h" |
16 | |
17 | #include <atomic> |
18 | #include <cstddef> |
19 | |
20 | #include "absl/base/config.h" |
21 | |
22 | namespace absl { |
23 | namespace container_internal { |
24 | |
25 | constexpr size_t Group::kWidth; |
26 | |
27 | // Returns "random" seed. |
28 | inline size_t RandomSeed() { |
29 | #if ABSL_HAVE_THREAD_LOCAL |
30 | static thread_local size_t counter = 0; |
31 | size_t value = ++counter; |
32 | #else // ABSL_HAVE_THREAD_LOCAL |
33 | static std::atomic<size_t> counter(0); |
34 | size_t value = counter.fetch_add(1, std::memory_order_relaxed); |
35 | #endif // ABSL_HAVE_THREAD_LOCAL |
36 | return value ^ static_cast<size_t>(reinterpret_cast<uintptr_t>(&counter)); |
37 | } |
38 | |
39 | bool ShouldInsertBackwards(size_t hash, ctrl_t* ctrl) { |
40 | // To avoid problems with weak hashes and single bit tests, we use % 13. |
41 | // TODO(kfm,sbenza): revisit after we do unconditional mixing |
42 | return (H1(hash, ctrl) ^ RandomSeed()) % 13 > 6; |
43 | } |
44 | |
45 | } // namespace container_internal |
46 | } // namespace absl |
47 | |