• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 ABSL_NAMESPACE_BEGIN
24 namespace container_internal {
25 
26 constexpr size_t Group::kWidth;
27 
28 // Returns "random" seed.
RandomSeed()29 inline size_t RandomSeed() {
30 #if ABSL_HAVE_THREAD_LOCAL
31   static thread_local size_t counter = 0;
32   size_t value = ++counter;
33 #else   // ABSL_HAVE_THREAD_LOCAL
34   static std::atomic<size_t> counter(0);
35   size_t value = counter.fetch_add(1, std::memory_order_relaxed);
36 #endif  // ABSL_HAVE_THREAD_LOCAL
37   return value ^ static_cast<size_t>(reinterpret_cast<uintptr_t>(&counter));
38 }
39 
ShouldInsertBackwards(size_t hash,ctrl_t * ctrl)40 bool ShouldInsertBackwards(size_t hash, ctrl_t* ctrl) {
41   // To avoid problems with weak hashes and single bit tests, we use % 13.
42   // TODO(kfm,sbenza): revisit after we do unconditional mixing
43   return (H1(hash, ctrl) ^ RandomSeed()) % 13 > 6;
44 }
45 
46 }  // namespace container_internal
47 ABSL_NAMESPACE_END
48 }  // namespace absl
49