• 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 // A single block of empty control bytes for tables without any slots allocated.
27 // This enables removing a branch in the hot path of find().
28 alignas(16) ABSL_CONST_INIT ABSL_DLL const ctrl_t kEmptyGroup[16] = {
29     ctrl_t::kSentinel, ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty,
30     ctrl_t::kEmpty,    ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty,
31     ctrl_t::kEmpty,    ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty,
32     ctrl_t::kEmpty,    ctrl_t::kEmpty, ctrl_t::kEmpty, ctrl_t::kEmpty};
33 
34 constexpr size_t Group::kWidth;
35 
36 // Returns "random" seed.
RandomSeed()37 inline size_t RandomSeed() {
38 #ifdef ABSL_HAVE_THREAD_LOCAL
39   static thread_local size_t counter = 0;
40   size_t value = ++counter;
41 #else   // ABSL_HAVE_THREAD_LOCAL
42   static std::atomic<size_t> counter(0);
43   size_t value = counter.fetch_add(1, std::memory_order_relaxed);
44 #endif  // ABSL_HAVE_THREAD_LOCAL
45   return value ^ static_cast<size_t>(reinterpret_cast<uintptr_t>(&counter));
46 }
47 
ShouldInsertBackwards(size_t hash,const ctrl_t * ctrl)48 bool ShouldInsertBackwards(size_t hash, const ctrl_t* ctrl) {
49   // To avoid problems with weak hashes and single bit tests, we use % 13.
50   // TODO(kfm,sbenza): revisit after we do unconditional mixing
51   return (H1(hash, ctrl) ^ RandomSeed()) % 13 > 6;
52 }
53 
ConvertDeletedToEmptyAndFullToDeleted(ctrl_t * ctrl,size_t capacity)54 void ConvertDeletedToEmptyAndFullToDeleted(ctrl_t* ctrl, size_t capacity) {
55   assert(ctrl[capacity] == ctrl_t::kSentinel);
56   assert(IsValidCapacity(capacity));
57   for (ctrl_t* pos = ctrl; pos < ctrl + capacity; pos += Group::kWidth) {
58     Group{pos}.ConvertSpecialToEmptyAndFullToDeleted(pos);
59   }
60   // Copy the cloned ctrl bytes.
61   std::memcpy(ctrl + capacity + 1, ctrl, NumClonedBytes());
62   ctrl[capacity] = ctrl_t::kSentinel;
63 }
64 // Extern template instantiotion for inline function.
65 template FindInfo find_first_non_full(const ctrl_t*, size_t, size_t);
66 
67 }  // namespace container_internal
68 ABSL_NAMESPACE_END
69 }  // namespace absl
70