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