This page provides an example of how to use the zip tree. For information on what a zip tree is and how it behaves, please see Overview over the Data Structures.
The code can also be found in the examples directory.
#include "../src/ygg.hpp"
using MyTreeOptions =
TreeOptions<TreeFlags::ZTREE_USE_HASH,
TreeFlags::ZTREE_RANK_HASH_UNIVERSALIZE_MODUL<42>,
TreeFlags::ZTREE_RANK_HASH_UNIVERSALIZE_COEFFICIENT<1701>,
TreeFlags::ZTREE_RANK_TYPE<uint8_t>>;
public:
int key;
std::string value;
Node() = delete;
Node(int key_in, std::string value_in)
: key(key_in), value(value_in)
{
this->update_rank();
}
bool
operator<(const Node & other) const
{
return this->key < other.key;
}
};
template<>
struct hash<Node> {
size_t operator()(const Node & n) const {
return std::hash<int>{}(n.key);
}
};
}
bool
operator<(const Node & lhs, const int rhs)
{
return lhs.key < rhs;
}
bool
operator<(const int lhs, const Node & rhs)
{
return lhs < rhs.key;
}
int
main(int argc, char ** argv)
{
(void)argc;
(void)argv;
MyTree t;
std::vector<Node> nodes;
for (int i = 0; i < 5; ++i) {
nodes.emplace_back(i, std::string("The key is ") + std::to_string(i));
}
for (size_t i = 0; i < 5; ++i) {
t.insert(nodes[i]);
}
auto it = t.find(3);
assert(it != t.end());
std::string retrieved_value = it->value;
t.remove(*it);
return 0;
}