BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlc_flathashtable_groupcontrol.h
Go to the documentation of this file.
1/// @file bdlc_flathashtable_groupcontrol.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlc_flathashtable_groupcontrol.h -*-C++-*-
8#ifndef INCLUDED_BDLC_FLATHASHTABLE_GROUPCONTROL
9#define INCLUDED_BDLC_FLATHASHTABLE_GROUPCONTROL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlc_flathashtable_groupcontrol bdlc_flathashtable_groupcontrol
15/// @brief Provide inquiries to a flat hash table group of control values.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlc
19/// @{
20/// @addtogroup bdlc_flathashtable_groupcontrol
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlc_flathashtable_groupcontrol-purpose"> Purpose</a>
25/// * <a href="#bdlc_flathashtable_groupcontrol-classes"> Classes </a>
26/// * <a href="#bdlc_flathashtable_groupcontrol-description"> Description </a>
27/// * <a href="#bdlc_flathashtable_groupcontrol-usage"> Usage </a>
28///
29/// # Purpose {#bdlc_flathashtable_groupcontrol-purpose}
30/// Provide inquiries to a flat hash table group of control values.
31///
32/// # Classes {#bdlc_flathashtable_groupcontrol-classes}
33///
34/// - bdlc::FlatHashTable_GroupControl: flat hash table group control inquiries
35///
36/// # Description {#bdlc_flathashtable_groupcontrol-description}
37/// This component implements the class,
38/// `bdlc::FlatHashTable_GroupControl`, that provides query methods to a group
39/// of flat hash table control values. Note that the number of entries in a
40/// group control and the inquiry performance is platform dependant.
41///
42/// The flat hash map/set/table data structures are inspired by Google's
43/// flat_hash_map CppCon presentations (available on youtube). The
44/// implementations draw from Google's open source `raw_hash_set.h` file at:
45/// https://github.com/abseil/abseil-cpp/blob/master/absl/container/internal.
46///
47/// ## Usage {#bdlc_flathashtable_groupcontrol-usage}
48///
49///
50/// There is no usage example for this component since it is not meant for
51/// direct client use.
52/// @}
53/** @} */
54/** @} */
55
56/** @addtogroup bdl
57 * @{
58 */
59/** @addtogroup bdlc
60 * @{
61 */
62/** @addtogroup bdlc_flathashtable_groupcontrol
63 * @{
64 */
65
66#include <bdlscm_version.h>
67
68#include <bsls_assert.h>
69#include <bsls_byteorder.h>
70#include <bsls_platform.h>
71
72#include <bsl_cstddef.h>
73#include <bsl_cstdint.h>
74#include <bsl_cstring.h>
75
76#if defined(BSLS_PLATFORM_CPU_SSE2)
77#include <immintrin.h>
78#include <emmintrin.h>
79#endif
80
81
82namespace bdlc {
83
84 // ================================
85 // class FlatHashTable_GroupControl
86 // ================================
87
88/// This class provides methods for making inquires to the data of a group
89/// control loading during construction.
91{
92 public:
93 // TYPES
94#if defined(BSLS_PLATFORM_CPU_SSE2)
95 typedef __m128i Storage;
96#else
97 typedef bsl::uint64_t Storage;
98#endif
99
100 private:
101 // CLASS DATA
102 static const bsl::uint64_t k_MULT = 0x0101010101010101ull;
103 static const bsl::uint64_t k_DEFLATE = 0x0002040810204081ull;
104 static const int k_DEFLATE_SHIFT = 56;
105 static const bsl::uint64_t k_MSB_MASK = 0x8080808080808080ull;
106
107 // DATA
108 Storage d_value; // efficiently cached value for inquiries
109
110 // PRIVATE ACCESSORS
111
112 /// Return a bit mask of the `k_SIZE` entries that have the specified
113 /// `value`. The bit at index `i` corresponds to the result for
114 /// `data[i]`.
115 bsl::uint32_t matchRaw(bsl::uint8_t value) const;
116
117 // NOT IMPLEMENTED
121
122 public:
123 // PUBLIC CLASS DATA
124 static const bsl::uint8_t k_EMPTY = 0x80; // = 0b10000000
125 static const bsl::uint8_t k_ERASED = 0xC0; // = 0b11000000
126 static const bsl::size_t k_SIZE = sizeof(Storage);
127
128 // CREATORS
129
130 /// Create a group control query object using the specified `data`. The
131 /// bytes of `data` have no alignment requirement. The behavior is
132 /// undefined unless `data` has at least `k_SIZE` bytes available.
133 explicit FlatHashTable_GroupControl(const bsl::uint8_t *data);
134
136 // Destroy this object.
137
138 // ACCESSORS
139
140 /// Return a bit mask of the `k_SIZE` entries that are empty or erased.
141 /// The bit at index `i` corresponds to the result for `data[i]`.
142 bsl::uint32_t available() const;
143
144 /// Return a bit mask of the `k_SIZE` entries that are in use (i.e., not
145 /// empty or erased). The bit at index `i` corresponds to the result
146 /// for `data[i]`.
147 bsl::uint32_t inUse() const;
148
149 /// Return a bit mask of the `k_SIZE` entries that have the specified
150 /// `value`. The bit at index `i` corresponds to the result for
151 /// `data[i]`. The behavior is undefined unless `0 == (0x80 & value)`.
152 bsl::uint32_t match(bsl::uint8_t value) const;
153
154 /// Return `true` if this group control was never full (i.e., has a
155 /// value that is empty, but not erased).
156 bool neverFull() const;
157};
158
159// ============================================================================
160// INLINE DEFINITIONS
161// ============================================================================
162
163 // --------------------------------
164 // class FlatHashTable_GroupControl
165 // --------------------------------
166
167// PRIVATE ACCESSORS
168inline
169bsl::uint32_t FlatHashTable_GroupControl::matchRaw(bsl::uint8_t value) const
170{
171#if defined(BSLS_PLATFORM_CPU_SSE2)
172 return _mm_movemask_epi8(_mm_cmpeq_epi8(
173 _mm_set1_epi8(static_cast<char>(value)),
174 d_value));
175#else
176 Storage t = d_value ^ (k_MULT * value);
177
178 t = t | (t << 4);
179 t = t | (t << 2);
180 t = t | (t << 1);
181
182 return static_cast<bsl::uint32_t>(
183 (((~t) & k_MSB_MASK) * k_DEFLATE) >> k_DEFLATE_SHIFT);
184#endif
185}
186
187// CREATORS
188inline
189FlatHashTable_GroupControl::FlatHashTable_GroupControl(
190 const bsl::uint8_t *data)
191{
192#if defined(BSLS_PLATFORM_CPU_SSE2)
193 d_value = _mm_loadu_si128(static_cast<const Storage *>(
194 static_cast<const void *>(data)));
195#else
196 bsl::memcpy(&d_value, data, k_SIZE);
197 d_value = BSLS_BYTEORDER_HOST_U64_TO_LE(d_value);
198#endif
199}
200
201// ACCESSORS
202inline
204{
205#if defined(BSLS_PLATFORM_CPU_SSE2)
206 return _mm_movemask_epi8(d_value);
207#else
208 return static_cast<bsl::uint32_t>(
209 ((d_value & k_MSB_MASK) * k_DEFLATE) >> k_DEFLATE_SHIFT);
210#endif
211}
212
213inline
215{
216#if defined(BSLS_PLATFORM_CPU_SSE2)
217 return (~available()) & 0xFFFF;
218#else
219 return (~available()) & 0xFF;
220#endif
221}
222
223inline
224bsl::uint32_t FlatHashTable_GroupControl::match(bsl::uint8_t value) const
225{
226 BSLS_ASSERT_SAFE(0 == (value & 0x80));
227
228 return matchRaw(value);
229}
230
231inline
233{
234 return 0 != matchRaw(k_EMPTY);
235}
236
237} // close package namespace
238
239
240#endif
241
242// ----------------------------------------------------------------------------
243// Copyright 2020 Bloomberg Finance L.P.
244//
245// Licensed under the Apache License, Version 2.0 (the "License");
246// you may not use this file except in compliance with the License.
247// You may obtain a copy of the License at
248//
249// http://www.apache.org/licenses/LICENSE-2.0
250//
251// Unless required by applicable law or agreed to in writing, software
252// distributed under the License is distributed on an "AS IS" BASIS,
253// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
254// See the License for the specific language governing permissions and
255// limitations under the License.
256// ----------------------------- END-OF-FILE ----------------------------------
257
258/** @} */
259/** @} */
260/** @} */
Definition bdlc_flathashtable_groupcontrol.h:91
static const bsl::uint8_t k_EMPTY
Definition bdlc_flathashtable_groupcontrol.h:124
static const bsl::size_t k_SIZE
Definition bdlc_flathashtable_groupcontrol.h:126
static const bsl::uint8_t k_ERASED
Definition bdlc_flathashtable_groupcontrol.h:125
bsl::uint32_t match(bsl::uint8_t value) const
Definition bdlc_flathashtable_groupcontrol.h:224
bsl::uint32_t inUse() const
Definition bdlc_flathashtable_groupcontrol.h:214
bsl::uint32_t available() const
Definition bdlc_flathashtable_groupcontrol.h:203
bsl::uint64_t Storage
Definition bdlc_flathashtable_groupcontrol.h:97
bool neverFull() const
Definition bdlc_flathashtable_groupcontrol.h:232
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1762
#define BSLS_BYTEORDER_HOST_U64_TO_LE(x)
Definition bsls_byteorder.h:366
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlc_bitarray.h:503