Skip to content

Commit c75a843

Browse files
committed
Wrap everything in libcuckoo namespace.
This is a breaking change. Users will have to add the namespace prefix `libcuckoo::` to types and constants, or bring them into the global namespace with something like `using libcuckoo::cuckoohash_map`. This also allows us to get rid of the `std::swap` specialization, which is not allowed by C++. Instead, we provide a specialization within the `libcuckoo` namespace, which can be found using argument-dependent lookup.
1 parent 68f8d9d commit c75a843

25 files changed

+163
-148
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.swp

examples/count_freq.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include <libcuckoo/cuckoohash_map.hh>
1515

1616
typedef uint32_t KeyType;
17-
typedef cuckoohash_map<KeyType, size_t> Table;
17+
typedef libcuckoo::cuckoohash_map<KeyType, size_t> Table;
1818
const size_t thread_num = 8;
1919
const size_t total_inserts = 10000000;
2020

examples/hellohash.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <libcuckoo/cuckoohash_map.hh>
55

66
int main() {
7-
cuckoohash_map<int, std::string> Table;
7+
libcuckoo::cuckoohash_map<int, std::string> Table;
88

99
for (int i = 0; i < 100; i++) {
1010
Table.insert(i, "hello");

examples/nested_table.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
#include <libcuckoo/cuckoohash_map.hh>
1111

12-
typedef cuckoohash_map<std::string, std::string> InnerTable;
13-
typedef cuckoohash_map<std::string, std::unique_ptr<InnerTable>> OuterTable;
12+
typedef libcuckoo::cuckoohash_map<std::string, std::string> InnerTable;
13+
typedef libcuckoo::cuckoohash_map<std::string, std::unique_ptr<InnerTable>> OuterTable;
1414

1515
int main() {
1616
OuterTable tbl;

libcuckoo-c/cuckoo_table_template.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
extern "C" {
2424
#endif
2525

26-
typedef cuckoohash_map<CUCKOO_KEY_TYPE, CUCKOO_MAPPED_TYPE> tbl_t;
26+
typedef libcuckoo::cuckoohash_map<CUCKOO_KEY_TYPE, CUCKOO_MAPPED_TYPE> tbl_t;
2727

2828
struct CUCKOO_TABLE_NAME {
2929
tbl_t t;
@@ -45,7 +45,7 @@ CUCKOO_TABLE_NAME *CUCKOO(_init)(size_t n) {
4545
return NULL;
4646
}
4747
tbl->t.minimum_load_factor(0);
48-
tbl->t.maximum_hashpower(LIBCUCKOO_NO_MAXIMUM_HASHPOWER);
48+
tbl->t.maximum_hashpower(libcuckoo::NO_MAXIMUM_HASHPOWER);
4949
return tbl;
5050
}
5151

libcuckoo/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ FILES
4545
cuckoohash_config.hh
4646
cuckoohash_map.hh
4747
cuckoohash_util.hh
48-
libcuckoo_bucket_container.hh
48+
bucket_container.hh
4949
DESTINATION
5050
${CMAKE_INSTALL_PREFIX}/include/libcuckoo
5151
)

libcuckoo/libcuckoo_bucket_container.hh libcuckoo/bucket_container.hh

+30-26
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef LIBCUCKOO_BUCKET_CONTAINER_H
2-
#define LIBCUCKOO_BUCKET_CONTAINER_H
1+
#ifndef BUCKET_CONTAINER_H
2+
#define BUCKET_CONTAINER_H
33

44
#include <array>
55
#include <atomic>
@@ -12,8 +12,10 @@
1212

1313
#include "cuckoohash_util.hh"
1414

15+
namespace libcuckoo {
16+
1517
/**
16-
* libcuckoo_bucket_container manages storage of key-value pairs for the table.
18+
* bucket_container manages storage of key-value pairs for the table.
1719
* It stores the items inline in uninitialized memory, and keeps track of which
1820
* slots have live data and which do not. It also stores a partial hash for
1921
* each live key. It is sized by powers of two.
@@ -26,7 +28,7 @@
2628
*/
2729
template <class Key, class T, class Allocator, class Partial,
2830
std::size_t SLOT_PER_BUCKET>
29-
class libcuckoo_bucket_container {
31+
class bucket_container {
3032
public:
3133
using key_type = Key;
3234
using mapped_type = T;
@@ -84,7 +86,7 @@ public:
8486
bool &occupied(size_type ind) { return occupied_[ind]; }
8587

8688
private:
87-
friend class libcuckoo_bucket_container;
89+
friend class bucket_container;
8890

8991
using storage_value_type = std::pair<Key, T>;
9092

@@ -105,48 +107,48 @@ public:
105107
std::array<bool, SLOT_PER_BUCKET> occupied_;
106108
};
107109

108-
libcuckoo_bucket_container(size_type hp, const allocator_type &allocator)
110+
bucket_container(size_type hp, const allocator_type &allocator)
109111
: allocator_(allocator), bucket_allocator_(allocator), hashpower_(hp),
110112
buckets_(bucket_allocator_.allocate(size())) {
111113
// The bucket default constructor is nothrow, so we don't have to
112114
// worry about dealing with exceptions when constructing all the
113115
// elements.
114116
static_assert(std::is_nothrow_constructible<bucket>::value,
115-
"libcuckoo_bucket_container requires bucket to be nothrow "
117+
"bucket_container requires bucket to be nothrow "
116118
"constructible");
117119
for (size_type i = 0; i < size(); ++i) {
118120
traits_::construct(allocator_, &buckets_[i]);
119121
}
120122
}
121123

122-
~libcuckoo_bucket_container() noexcept { destroy_buckets(); }
124+
~bucket_container() noexcept { destroy_buckets(); }
123125

124-
libcuckoo_bucket_container(const libcuckoo_bucket_container &bc)
126+
bucket_container(const bucket_container &bc)
125127
: allocator_(
126128
traits_::select_on_container_copy_construction(bc.allocator_)),
127129
bucket_allocator_(allocator_), hashpower_(bc.hashpower()),
128130
buckets_(transfer(bc.hashpower(), bc, std::false_type())) {}
129131

130-
libcuckoo_bucket_container(const libcuckoo_bucket_container &bc,
132+
bucket_container(const bucket_container &bc,
131133
const allocator_type &a)
132134
: allocator_(a), bucket_allocator_(allocator_),
133135
hashpower_(bc.hashpower()),
134136
buckets_(transfer(bc.hashpower(), bc, std::false_type())) {}
135137

136-
libcuckoo_bucket_container(libcuckoo_bucket_container &&bc)
138+
bucket_container(bucket_container &&bc)
137139
: allocator_(std::move(bc.allocator_)), bucket_allocator_(allocator_),
138140
hashpower_(bc.hashpower()), buckets_(std::move(bc.buckets_)) {
139141
// De-activate the other buckets container
140142
bc.buckets_ = nullptr;
141143
}
142144

143-
libcuckoo_bucket_container(libcuckoo_bucket_container &&bc,
145+
bucket_container(bucket_container &&bc,
144146
const allocator_type &a)
145147
: allocator_(a), bucket_allocator_(allocator_) {
146148
move_assign(bc, std::false_type());
147149
}
148150

149-
libcuckoo_bucket_container &operator=(const libcuckoo_bucket_container &bc) {
151+
bucket_container &operator=(const bucket_container &bc) {
150152
destroy_buckets();
151153
copy_allocator(allocator_, bc.allocator_,
152154
typename traits_::propagate_on_container_copy_assignment());
@@ -156,13 +158,13 @@ public:
156158
return *this;
157159
}
158160

159-
libcuckoo_bucket_container &operator=(libcuckoo_bucket_container &&bc) {
161+
bucket_container &operator=(bucket_container &&bc) {
160162
destroy_buckets();
161163
move_assign(bc, typename traits_::propagate_on_container_move_assignment());
162164
return *this;
163165
}
164166

165-
void swap(libcuckoo_bucket_container &bc) noexcept {
167+
void swap(bucket_container &bc) noexcept {
166168
swap_allocator(allocator_, bc.allocator_,
167169
typename traits_::propagate_on_container_swap());
168170
swap_allocator(bucket_allocator_, bc.bucket_allocator_,
@@ -223,7 +225,7 @@ public:
223225
static_assert(
224226
std::is_nothrow_destructible<key_type>::value &&
225227
std::is_nothrow_destructible<mapped_type>::value,
226-
"libcuckoo_bucket_container requires key and value to be nothrow "
228+
"bucket_container requires key and value to be nothrow "
227229
"destructible");
228230
for (size_type i = 0; i < size(); ++i) {
229231
bucket &b = buckets_[i];
@@ -263,15 +265,15 @@ private:
263265
template <typename A> void swap_allocator(A &, A &, std::false_type) {}
264266

265267
// true here means the bucket allocator should be propagated
266-
void move_assign(libcuckoo_bucket_container &src, std::true_type) {
268+
void move_assign(bucket_container &src, std::true_type) {
267269
allocator_ = std::move(src.allocator_);
268270
bucket_allocator_ = allocator_;
269271
hashpower(src.hashpower());
270272
buckets_ = src.buckets_;
271273
src.buckets_ = nullptr;
272274
}
273275

274-
void move_assign(libcuckoo_bucket_container &src, std::false_type) {
276+
void move_assign(bucket_container &src, std::false_type) {
275277
hashpower(src.hashpower());
276278
if (allocator_ == src.allocator_) {
277279
buckets_ = src.buckets_;
@@ -289,7 +291,7 @@ private:
289291
// worry about dealing with exceptions when constructing all the
290292
// elements.
291293
static_assert(std::is_nothrow_destructible<bucket>::value,
292-
"libcuckoo_bucket_container requires bucket to be nothrow "
294+
"bucket_container requires bucket to be nothrow "
293295
"destructible");
294296
clear();
295297
for (size_type i = 0; i < size(); ++i) {
@@ -315,11 +317,11 @@ private:
315317
template <bool B>
316318
bucket_pointer transfer(
317319
size_type dst_hp,
318-
typename std::conditional<B, libcuckoo_bucket_container &,
319-
const libcuckoo_bucket_container &>::type src,
320+
typename std::conditional<B, bucket_container &,
321+
const bucket_container &>::type src,
320322
std::integral_constant<bool, B> move) {
321323
assert(dst_hp >= src.hashpower());
322-
libcuckoo_bucket_container dst(dst_hp, get_allocator());
324+
bucket_container dst(dst_hp, get_allocator());
323325
// Move/copy all occupied slots of the source buckets
324326
for (size_t i = 0; i < src.size(); ++i) {
325327
for (size_t j = 0; j < SLOT_PER_BUCKET; ++j) {
@@ -359,7 +361,7 @@ private:
359361
std::is_trivial<ThisT>::value,
360362
std::ostream &>::type
361363
operator<<(std::ostream &os,
362-
const libcuckoo_bucket_container<ThisKey, ThisT, Allocator,
364+
const bucket_container<ThisKey, ThisT, Allocator,
363365
Partial, SLOT_PER_BUCKET> &bc) {
364366
size_type hp = bc.hashpower();
365367
os.write(reinterpret_cast<const char *>(&hp), sizeof(size_type));
@@ -373,16 +375,18 @@ private:
373375
std::is_trivial<ThisT>::value,
374376
std::istream &>::type
375377
operator>>(std::istream &is,
376-
libcuckoo_bucket_container<ThisKey, ThisT, Allocator,
378+
bucket_container<ThisKey, ThisT, Allocator,
377379
Partial, SLOT_PER_BUCKET> &bc) {
378380
size_type hp;
379381
is.read(reinterpret_cast<char *>(&hp), sizeof(size_type));
380-
libcuckoo_bucket_container new_bc(hp, bc.get_allocator());
382+
bucket_container new_bc(hp, bc.get_allocator());
381383
is.read(reinterpret_cast<char *>(new_bc.buckets_),
382384
new_bc.size() * sizeof(bucket));
383385
bc.swap(new_bc);
384386
return is;
385387
}
386388
};
387389

388-
#endif // LIBCUCKOO_BUCKET_CONTAINER_H
390+
} // namespace libcuckoo
391+
392+
#endif // BUCKET_CONTAINER_H

libcuckoo/cuckoohash_config.hh

+10-6
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,30 @@
66
#include <cstddef>
77
#include <limits>
88

9+
namespace libcuckoo {
10+
911
//! The default maximum number of keys per bucket
10-
constexpr size_t LIBCUCKOO_DEFAULT_SLOT_PER_BUCKET = 4;
12+
constexpr size_t DEFAULT_SLOT_PER_BUCKET = 4;
1113

1214
//! The default number of elements in an empty hash table
13-
constexpr size_t LIBCUCKOO_DEFAULT_SIZE =
14-
(1U << 16) * LIBCUCKOO_DEFAULT_SLOT_PER_BUCKET;
15+
constexpr size_t DEFAULT_SIZE =
16+
(1U << 16) * DEFAULT_SLOT_PER_BUCKET;
1517

1618
//! The default minimum load factor that the table allows for automatic
1719
//! expansion. It must be a number between 0.0 and 1.0. The table will throw
18-
//! libcuckoo_load_factor_too_low if the load factor falls below this value
20+
//! load_factor_too_low if the load factor falls below this value
1921
//! during an automatic expansion.
20-
constexpr double LIBCUCKOO_DEFAULT_MINIMUM_LOAD_FACTOR = 0.05;
22+
constexpr double DEFAULT_MINIMUM_LOAD_FACTOR = 0.05;
2123

2224
//! An alias for the value that sets no limit on the maximum hashpower. If this
2325
//! value is set as the maximum hashpower limit, there will be no limit. This
2426
//! is also the default initial value for the maximum hashpower in a table.
25-
constexpr size_t LIBCUCKOO_NO_MAXIMUM_HASHPOWER =
27+
constexpr size_t NO_MAXIMUM_HASHPOWER =
2628
std::numeric_limits<size_t>::max();
2729

2830
//! set LIBCUCKOO_DEBUG to 1 to enable debug output
2931
#define LIBCUCKOO_DEBUG 0
3032

33+
} // namespace libcuckoo
34+
3135
#endif // _CUCKOOHASH_CONFIG_HH

0 commit comments

Comments
 (0)