12#include "detray/definitions/indexing.hpp"
13#include "detray/definitions/math.hpp"
23template <
typename value_t>
55template <
typename input_collection_t,
56 typename data_t =
typename input_collection_t::value_type,
57 typename hash_function_t = default_hash<data_t>,
59 decltype(std::declval<hash_function_t>()(data_t{0})),
60 template <
typename>
class vector_t =
dvector>
61 requires std::is_invocable_v<hash_function_t, data_t> &&
62 std::is_invocable_v<hash_function_t, hash_t>
87 const hash_t &
key()
const {
return _key; }
114 const hash_function_t & = {}) {
124 auto root() {
return _tree.back().key(); }
128 std::stringstream ss;
129 for (
const auto &n : _tree) {
130 ss << n.key() << std::endl;
136 const vector_t<hashed_node> &
tree()
const {
return _tree; }
140 void build(
const input_collection_t &input_data)
noexcept(
false) {
142 for (
const auto &data : input_data) {
143 _tree.emplace_back(_hash(data));
146 if (input_data.size() % 2u != 0u) {
147 _tree.emplace_back(_hash(0));
152 auto n_levels =
static_cast<dindex>(math::log(input_data.size()));
153 _tree.reserve(2u * _tree.size() + n_levels);
155 build(_tree.begin(),
static_cast<dindex>(_tree.size()));
163 template <
typename iterator_t>
164 void build(iterator_t &&first_child,
dindex n_prev_level) {
166 if (n_prev_level <= 1u) {
170 auto last_child = first_child +
static_cast<std::ptrdiff_t
>(n_prev_level);
173 for (
auto current_child = first_child; current_child != last_child;
174 current_child += 2u) {
176 _hash(current_child->key(), (current_child + 1u)->key());
177 hashed_node parent = _tree.emplace_back(parent_digest);
181 (current_child + 1)->set_parent(
static_cast<dindex>(_tree.size()) - 1u);
184 auto left_child_idx = std::distance(current_child, _tree.begin());
185 parent.set_children(
static_cast<dindex>(left_child_idx),
186 static_cast<dindex>(left_child_idx) + 1u);
188 dindex n_level = n_prev_level / 2u;
190 if (n_level % 2u != 0u && n_level > 1u) {
191 _tree.emplace_back(0);
195 build(last_child, n_level);
199 hash_function_t _hash{hash_function_t{}};
202 vector_t<hashed_node> _tree = {};
const vector_t< hashed_node > & tree() const
Definition hash_tree.hpp:136
hash_tree()=delete
No empty tree.
auto root()
Definition hash_tree.hpp:124
hash_t hash_type
Type of hashes stored in the tree.
Definition hash_tree.hpp:68
~hash_tree()=default
Default destructor.
hash_function_t hash_function
Hash function used by the tree.
Definition hash_tree.hpp:66
hash_tree(const input_collection_t &data, const hash_function_t &={})
Build from an input collection using the configured hash function.
Definition hash_tree.hpp:113
std::string to_string() const
Definition hash_tree.hpp:127
Detray include(s).
Definition array_operators.hpp:18
vecmem::vector< value_t > dvector
Vector container backed by vecmem.
Definition containers.hpp:34
unsigned int dindex
Default index type.
Definition indexing.hpp:18
Default hash function makes use of default node in the hash tree.
Definition hash_tree.hpp:24
std::hash< hash_type > node_hasher
Hash function used for child hashes.
Definition hash_tree.hpp:30
auto operator()(const hash_type &left, const hash_type &right)
Combine two child hashes.
Definition hash_tree.hpp:41
std::hash< value_t > data_hasher
Hash function used for input data values.
Definition hash_tree.hpp:28
std::size_t hash_type
Type returned by the hash function.
Definition hash_tree.hpp:26
auto operator()(const value_t &v)
Hash an input value.
Definition hash_tree.hpp:35
Default node in the hash tree.
Definition hash_tree.hpp:71
hash_t & key()
Get the mutable node hash.
Definition hash_tree.hpp:90
void set_parent(dindex pi)
Set the parent node index.
Definition hash_tree.hpp:94
hash_t _key
Hash key of this node.
Definition hash_tree.hpp:77
dindex _parent
Index of this node's parent.
Definition hash_tree.hpp:79
dindex _right_child
Index of this node's right child.
Definition hash_tree.hpp:83
hashed_node(hash_t hash)
Construct a node with its hash.
Definition hash_tree.hpp:74
void set_children(dindex lc, dindex rc)
Set the child node indices.
Definition hash_tree.hpp:99
const hash_t & key() const
Get the node hash.
Definition hash_tree.hpp:87
dindex _left_child
Index of this node's left child.
Definition hash_tree.hpp:81