BDE 4.39.x 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 private:
118 // NOT IMPLEMENTED
122
123 public:
124 // PUBLIC CLASS DATA
125 static const bsl::uint8_t k_EMPTY = 0x80; // = 0b10000000
126 static const bsl::uint8_t k_ERASED = 0xC0; // = 0b11000000
127 static const bsl::size_t k_SIZE = sizeof(Storage);
128
129 // CREATORS
130
131 /// Create a group control query object using the specified `data`. The
132 /// bytes of `data` have no alignment requirement.
133 ///
134 /// \pre The behavior is undefined unless `data` has at least `k_SIZE` bytes available.
135 explicit FlatHashTable_GroupControl(const bsl::uint8_t *data);
136
138 // Destroy this object.
139
140 // ACCESSORS
141
142 /// Return a bit mask of the `k_SIZE` entries that are empty or erased.
143 /// The bit at index `i` corresponds to the result for `data[i]`.
144 bsl::uint32_t available() const;
145
146 /// Return a bit mask of the `k_SIZE` entries that are in use (i.e., not
147 /// empty or erased). The bit at index `i` corresponds to the result
148 /// for `data[i]`.
149 bsl::uint32_t inUse() const;
150
151 /// Return a bit mask of the `k_SIZE` entries that have the specified
152 /// `value`. The bit at index `i` corresponds to the result for `data[i]`.
153 ///
154 /// \pre The behavior is undefined unless `0 == (0x80 & value)`.
155 bsl::uint32_t match(bsl::uint8_t value) const;
156
157 /// Return `true` if this group control was never full (i.e., has a
158 /// value that is empty, but not erased).
159 bool neverFull() const;
160};
161
162// ============================================================================
163// INLINE DEFINITIONS
164// ============================================================================
165
166 // --------------------------------
167 // class FlatHashTable_GroupControl
168 // --------------------------------
169
170// PRIVATE ACCESSORS
171inline
172bsl::uint32_t FlatHashTable_GroupControl::matchRaw(bsl::uint8_t value) const
173{
174#if defined(BSLS_PLATFORM_CPU_SSE2)
175 return _mm_movemask_epi8(_mm_cmpeq_epi8(
176 _mm_set1_epi8(static_cast<char>(value)),
177 d_value));
178#else
179 Storage t = d_value ^ (k_MULT * value);
180
181 t = t | (t << 4);
182 t = t | (t << 2);
183 t = t | (t << 1);
184
185 return static_cast<bsl::uint32_t>(
186 (((~t) & k_MSB_MASK) * k_DEFLATE) >> k_DEFLATE_SHIFT);
187#endif
188}
189
190// CREATORS
191inline
192FlatHashTable_GroupControl::FlatHashTable_GroupControl(
193 const bsl::uint8_t *data)
194{
195#if defined(BSLS_PLATFORM_CPU_SSE2)
196 d_value = _mm_loadu_si128(static_cast<const Storage *>(
197 static_cast<const void *>(data)));
198#else
199 bsl::memcpy(&d_value, data, k_SIZE);
200 d_value = BSLS_BYTEORDER_HOST_U64_TO_LE(d_value);
201#endif
202}
203
204// ACCESSORS
205inline
207{
208#if defined(BSLS_PLATFORM_CPU_SSE2)
209 return _mm_movemask_epi8(d_value);
210#else
211 return static_cast<bsl::uint32_t>(
212 ((d_value & k_MSB_MASK) * k_DEFLATE) >> k_DEFLATE_SHIFT);
213#endif
214}
215
216inline
218{
219#if defined(BSLS_PLATFORM_CPU_SSE2)
220 return (~available()) & 0xFFFF;
221#else
222 return (~available()) & 0xFF;
223#endif
224}
225
226inline
227bsl::uint32_t FlatHashTable_GroupControl::match(bsl::uint8_t value) const
228{
229 BSLS_ASSERT_SAFE(0 == (value & 0x80));
230
231 return matchRaw(value);
232}
233
234inline
236{
237 return 0 != matchRaw(k_EMPTY);
238}
239
240} // close package namespace
241
242
243#endif
244
245// ----------------------------------------------------------------------------
246// Copyright 2020 Bloomberg Finance L.P.
247//
248// Licensed under the Apache License, Version 2.0 (the "License");
249// you may not use this file except in compliance with the License.
250// You may obtain a copy of the License at
251//
252// http://www.apache.org/licenses/LICENSE-2.0
253//
254// Unless required by applicable law or agreed to in writing, software
255// distributed under the License is distributed on an "AS IS" BASIS,
256// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
257// See the License for the specific language governing permissions and
258// limitations under the License.
259// ----------------------------- END-OF-FILE ----------------------------------
260
261/** @} */
262/** @} */
263/** @} */
Definition bdlc_flathashtable_groupcontrol.h:91
static const bsl::uint8_t k_EMPTY
Definition bdlc_flathashtable_groupcontrol.h:125
static const bsl::size_t k_SIZE
Definition bdlc_flathashtable_groupcontrol.h:127
static const bsl::uint8_t k_ERASED
Definition bdlc_flathashtable_groupcontrol.h:126
bsl::uint32_t match(bsl::uint8_t value) const
Definition bdlc_flathashtable_groupcontrol.h:227
bsl::uint32_t inUse() const
Definition bdlc_flathashtable_groupcontrol.h:217
bsl::uint32_t available() const
Definition bdlc_flathashtable_groupcontrol.h:206
bsl::uint64_t Storage
Definition bdlc_flathashtable_groupcontrol.h:97
bool neverFull() const
Definition bdlc_flathashtable_groupcontrol.h:235
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1917
#define BSLS_BYTEORDER_HOST_U64_TO_LE(x)
Definition bsls_byteorder.h:366
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlc_bitarray.h:506