Skip to content

Commit

Permalink
0.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bigerl committed Sep 9, 2021
1 parent 4149bd1 commit 21f90b3
Show file tree
Hide file tree
Showing 27 changed files with 1,622 additions and 2,487 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.13)
project(hypertrie VERSION 0.5.3)
project(hypertrie VERSION 0.6.0)
set(CMAKE_CXX_STANDARD 20)

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/version.hpp.in ${CMAKE_CURRENT_SOURCE_DIR}/include/Dice/hypertrie/hypertrie_version.hpp)
Expand Down
35 changes: 32 additions & 3 deletions include/Dice/hypertrie/internal/BulkInserter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,40 @@ namespace hypertrie {
template<typename T>
void add(T &&entry) {
assert(EntryFunctions::key(entry).size() == hypertrie->depth());
hypertrie->set(tr::iterator_entry::key(entry), tr::iterator_entry::value(entry));
if ((*hypertrie)[EntryFunctions::key(entry)] == value_type{}) {
new_entries.insert(std::forward<T>(entry));
if (threshold != 0 and new_entries.size() > threshold)
flush();
}
}

void flush([[maybe_unused]] const bool blocking = false) {

void flush(const bool blocking = false) {
if (insertion_thread.joinable())
insertion_thread.join();
internal::compiled_switch<hypertrie_depth_limit, 1>::switch_void(
hypertrie->depth(),
[&](auto depth_arg) {
this->load_entries = std::move(this->new_entries);
this->new_entries = {};
if (load_entries.size() > 0)
insertion_thread = std::thread([&]() {
// todo: add support for non-boolean
using RawKey = typename tri::template RawKey<depth_arg>;
std::vector<RawKey> keys{};
keys.reserve(load_entries.size());
for (const auto &key : load_entries)
if (not(*hypertrie)[key])
keys.push_back(tri::template rawKey<depth_arg>(key));

if (keys.size() > 0) {
auto &typed_nodec = *reinterpret_cast<internal::raw::NodeContainer<depth_arg, tri> *>(const_cast<hypertrie::internal::raw::RawNodeContainer *>(hypertrie->rawNodeContainer()));
hypertrie->context()->rawContext().template bulk_insert<depth_arg>(typed_nodec, std::move(keys));
}
});
});
if (blocking)
if (insertion_thread.joinable())
insertion_thread.join();
}

[[nodiscard]] size_t size() const {
Expand Down
97 changes: 67 additions & 30 deletions include/Dice/hypertrie/internal/HashDiagonal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "Dice/hypertrie/internal/raw/iterator/Diagonal.hpp"
#include "Dice/hypertrie/internal/HypertrieContext.hpp"
#include "Dice/hypertrie/internal/old_hypertrie/BoolHypertrie.hpp"
#include "Dice/hypertrie/internal/Hypertrie_predeclare.hpp"

namespace hypertrie {
Expand All @@ -19,10 +18,8 @@ namespace hypertrie {
using key_part_type = typename tr::key_part_type;
using value_type = typename tr::value_type;
typedef typename internal::raw::NodeCompression NodeCompression;
template<pos_type depth, pos_type diag_depth>
using RawHashDiagonal = typename hypertrie::internal::interface::rawboolhypertrie<typename tri::key_part_type, tri::template map_type, tri::template set_type>::template RawHashDiagonal<diag_depth, depth>;
template<pos_type depth>
using RawBoolHypertrie = typename hypertrie::internal::interface::rawboolhypertrie<typename tri::key_part_type, tri::template map_type, tri::template set_type>::template RawBoolHypertrie<depth>;
template <size_t diag_depth, size_t depth, NodeCompression compression>
using RawHashDiagonal = typename internal::raw::template HashDiagonal<diag_depth, depth, compression, tri>;
protected:
using Key = typename tr::Key;

Expand All @@ -35,7 +32,7 @@ namespace hypertrie {

key_part_type (*currentKeyPart)(void const *);

const_Hypertrie<tr> (*currentHypertrie)(void const *);
const_Hypertrie<tr> (*currentHypertrie)(void const *, HypertrieContext<tr> *);

value_type (*currentScalar)(void const *);

Expand All @@ -46,54 +43,89 @@ namespace hypertrie {
bool (*ended)(void const *);

size_t (*size)(void const *);
RawMethods(void *(*construct)(const const_Hypertrie<tr> &, const KeyPositions &), void (*destruct)(void *), void (*begin)(void *), key_part_type (*currentKeyPart)(const void *), const_Hypertrie<tr> (*currentHypertrie)(const void *), value_type (*currentScalar)(const void *), bool (*find)(void *, key_part_type), void (*inc)(void *), bool (*ended)(const void *), size_t (*size)(const void *)) : construct(construct), destruct(destruct), begin(begin), currentKeyPart(currentKeyPart), currentHypertrie(currentHypertrie), currentScalar(currentScalar), find(find), inc(inc), ended(ended), size(size) {}
RawMethods(void *(*construct)(const const_Hypertrie<tr> &, const KeyPositions &), void (*destruct)(void *), void (*begin)(void *), key_part_type (*currentKeyPart)(const void *), const_Hypertrie<tr> (*currentHypertrie)(const void *, HypertrieContext<tr> *), value_type (*currentScalar)(const void *), bool (*find)(void *, key_part_type), void (*inc)(void *), bool (*ended)(const void *), size_t (*size)(const void *)) : construct(construct), destruct(destruct), begin(begin), currentKeyPart(currentKeyPart), currentHypertrie(currentHypertrie), currentScalar(currentScalar), find(find), inc(inc), ended(ended), size(size) {}
};

template <size_t diag_depth, size_t depth, NodeCompression compression>
inline static RawMethods generateRawMethods() {
[[maybe_unused]] constexpr static const size_t result_depth = depth - diag_depth;
using RawDiagonalHash_t = RawHashDiagonal<diag_depth, depth>;
using RawDiagonalHash_t = RawHashDiagonal<diag_depth, depth, compression>;
return RawMethods(
// construct
[](const const_Hypertrie<tr> &hypertrie, const KeyPositions &diagonal_poss) -> void * {
const auto &raw_boolhypertrie = *(static_cast<RawBoolHypertrie<depth> const *>(hypertrie.rawNode()));
if constexpr (depth == diag_depth) {
return new RawDiagonalHash_t(raw_boolhypertrie);
} else {
return new RawDiagonalHash_t(raw_boolhypertrie, diagonal_poss);
}
using NodecType = typename internal::raw::template SpecificNodeContainer<depth, compression, tri>;

RawKeyPositions<depth> raw_diag_poss;
for (auto pos : diagonal_poss)
raw_diag_poss[pos] = true;
auto &nodec = *const_cast<NodecType *>(reinterpret_cast<const NodecType *>(hypertrie.rawNodeContainer()));
if constexpr (bool(compression))
return new RawHashDiagonal<diag_depth, depth, NodeCompression::compressed>(
nodec,
raw_diag_poss);
else
return new RawHashDiagonal<diag_depth, depth, NodeCompression::uncompressed>(
nodec,
raw_diag_poss,
hypertrie.context()->rawContext());
},
// destruct
[](void *raw_diagonal_ptr) {
delete reinterpret_cast<RawDiagonalHash_t *>(raw_diagonal_ptr);
},
// begin
&RawDiagonalHash_t::init,
[](void *raw_diagonal_ptr) {
auto &raw_diagonal = *reinterpret_cast<RawDiagonalHash_t *>(raw_diagonal_ptr);
raw_diagonal.begin();
},
// currentKeyPart
&RawDiagonalHash_t::currentKeyPart,
[](void const *raw_diagonal_ptr) -> key_part_type {
const auto &raw_diagonal = *reinterpret_cast<const RawDiagonalHash_t *>(raw_diagonal_ptr);
return raw_diagonal.currentKeyPart();
},
// currentHypertrie
[](void const *raw_diagonal_ptr) -> const_Hypertrie<tr> {
[](void const *raw_diagonal_ptr, HypertrieContext<tr> *context) -> const_Hypertrie<tr> {
if constexpr (diag_depth < depth) {
std::shared_ptr<RawBoolHypertrie<depth - diag_depth>> &value = *RawDiagonalHash_t::currentValue(raw_diagonal_ptr);

return const_Hypertrie<tr>{result_depth, value};
const auto &raw_diagonal = *reinterpret_cast<const RawDiagonalHash_t *>(raw_diagonal_ptr);
const auto &value = raw_diagonal.currentValue();
if (value.is_managed) {
return const_Hypertrie<tr>(result_depth, context, {value.nodec.hash().hash(), value.nodec.node()});
} else {
return const_Hypertrie<tr>(result_depth, nullptr, {value.nodec.hash().hash(), new internal::raw::CompressedNode<result_depth, tri>(*value.nodec.compressed_node())});
}
} else {
assert(false);
}
},
// currentScalar
[]([[maybe_unused]] void const *raw_diagonal_ptr) -> value_type {
return value_type(1);
[](void const *raw_diagonal_ptr) -> value_type {
const auto &raw_diagonal = *reinterpret_cast<const RawDiagonalHash_t *>(raw_diagonal_ptr);
if constexpr (diag_depth == depth) {
return raw_diagonal.currentValue();
} else {
assert(false);
}
},
// find
&RawDiagonalHash_t::contains,
[](void *raw_diagonal_ptr, key_part_type key_part) -> bool {
auto &raw_diagonal = *reinterpret_cast<RawDiagonalHash_t *>(raw_diagonal_ptr);
return raw_diagonal.find(key_part);
},
// inc
&RawDiagonalHash_t::inc,
[](void *raw_diagonal_ptr) {
auto &raw_diagonal = *reinterpret_cast<RawDiagonalHash_t *>(raw_diagonal_ptr);
++raw_diagonal;
},
// ended
&RawDiagonalHash_t::empty,
[](const void *raw_diagonal_ptr) -> bool {
const auto &raw_diagonal = *reinterpret_cast<const RawDiagonalHash_t *>(raw_diagonal_ptr);
return raw_diagonal.ended();
},
// size
&RawDiagonalHash_t::size
);
[](const void *raw_diagonal_ptr) -> size_t {
const auto &raw_diagonal = *reinterpret_cast<const RawDiagonalHash_t *>(raw_diagonal_ptr);
return raw_diagonal.size();
});
}

inline static const std::vector<std::vector<std::vector<RawMethods>>> raw_method_cache = []() {
Expand Down Expand Up @@ -127,16 +159,19 @@ namespace hypertrie {
protected:
RawMethods const * raw_methods = nullptr;
void* raw_hash_diagonal;
HypertrieContext<tr> *context_;

public:
HashDiagonal(const const_Hypertrie<tr> &hypertrie, const KeyPositions &diag_poss)
: raw_methods(&getRawMethods(hypertrie.depth(), diag_poss.size(), hypertrie.size() == 1)),
raw_hash_diagonal(raw_methods->construct(hypertrie, diag_poss)) {}
raw_hash_diagonal(raw_methods->construct(hypertrie, diag_poss)), context_(hypertrie.context()) {}

HashDiagonal(HashDiagonal &&other)
: raw_methods(other.raw_methods),
raw_hash_diagonal(other.raw_hash_diagonal) {
raw_hash_diagonal(other.raw_hash_diagonal),
context_(other.context_) {
other.raw_hash_diagonal = nullptr;
other.context_ = nullptr;
}


Expand All @@ -147,7 +182,9 @@ namespace hypertrie {
}
this->raw_methods = other.raw_methods;
this->raw_hash_diagonal = other.raw_hash_diagonal;
this->context_ = other.context_;
other.raw_hash_diagonal = nullptr;
other.context_ = nullptr;
return *this;
}

Expand All @@ -174,7 +211,7 @@ namespace hypertrie {
}

const_Hypertrie<tr> currentHypertrie() const{
return raw_methods->currentHypertrie(raw_hash_diagonal);
return raw_methods->currentHypertrie(raw_hash_diagonal,context_);
}


Expand Down
Loading

0 comments on commit 21f90b3

Please sign in to comment.