-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdense_set_store.cpp
42 lines (35 loc) · 1.26 KB
/
dense_set_store.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <pomagma/util/dense_set_store.hpp>
#include <pomagma/third_party/farmhash/farmhash.h>
namespace pomagma {
inline SetId fingerprint(const Word *words, size_t byte_dim) {
return util::Fingerprint64(reinterpret_cast<const char *>(words), byte_dim);
}
DenseSetStore::DenseSetStore(size_t item_dim)
: m_item_dim(item_dim), m_byte_dim(1 + item_dim / 8) {}
DenseSetStore::~DenseSetStore() {
if (POMAGMA_DEBUG_LEVEL) {
POMAGMA_INFO("Validating DenseSetStore");
for (const auto &i : m_index) {
POMAGMA_ASSERT(
i.first == fingerprint(i.second, m_byte_dim),
"DenseSet was changed after storing in DenseSetStore");
}
}
}
inline Word *DenseSetStore::insert(SetId id, Word *data) {
SharedMutex::UniqueLock lock(m_mutex);
return m_index.insert({id, data}).first->second;
}
SetId DenseSetStore::store(DenseSet &&set) {
POMAGMA_ASSERT1(set.item_dim() == m_item_dim, "size mismatch");
Word *data = set.raw_data();
SetId id = fingerprint(data, m_byte_dim);
Word *stored = insert(id, data);
if (data == stored) {
set.move_ownership();
} else {
POMAGMA_ASSERT(not memcmp(data, stored, m_byte_dim), "hash conflict")
}
return id;
}
} // namespace pomagma