2021-06-26 16:40:11 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <xcdat.hpp>
|
|
|
|
|
|
|
|
int main() {
|
2021-06-29 00:06:40 +00:00
|
|
|
// Input keys
|
2021-06-26 16:40:11 +00:00
|
|
|
std::vector<std::string> keys = {
|
2021-06-27 03:57:34 +00:00
|
|
|
"AirPods", "AirTag", "Mac", "MacBook", "MacBook_Air", "MacBook_Pro",
|
|
|
|
"Mac_Mini", "Mac_Pro", "iMac", "iPad", "iPhone", "iPhone_SE",
|
2021-06-26 16:40:11 +00:00
|
|
|
};
|
|
|
|
|
2021-06-29 00:23:02 +00:00
|
|
|
// The input keys must be sorted and unique (although they have already satisfied in this case).
|
2021-06-26 16:40:11 +00:00
|
|
|
std::sort(keys.begin(), keys.end());
|
|
|
|
keys.erase(std::unique(keys.begin(), keys.end()), keys.end());
|
|
|
|
|
2021-06-29 01:27:02 +00:00
|
|
|
const char* index_filename = "tmp.idx";
|
|
|
|
|
|
|
|
// The trie index type
|
2021-06-29 00:23:02 +00:00
|
|
|
using trie_type = xcdat::trie_8_type;
|
2021-06-27 17:15:09 +00:00
|
|
|
|
2021-06-29 00:23:02 +00:00
|
|
|
// Build and save the trie index.
|
2021-06-27 17:15:09 +00:00
|
|
|
{
|
2021-06-29 00:06:40 +00:00
|
|
|
const trie_type trie(keys);
|
|
|
|
xcdat::save(trie, index_filename);
|
2021-06-27 17:15:09 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 00:23:02 +00:00
|
|
|
// Load the trie index.
|
2021-06-29 00:06:40 +00:00
|
|
|
const auto trie = xcdat::load<trie_type>(index_filename);
|
2021-06-26 16:40:11 +00:00
|
|
|
|
2021-06-29 01:36:00 +00:00
|
|
|
// Basic statistics
|
|
|
|
std::cout << "NumberKeys: " << trie.num_keys() << std::endl;
|
|
|
|
std::cout << "MaxLength: " << trie.max_length() << std::endl;
|
|
|
|
std::cout << "AlphabetSize: " << trie.alphabet_size() << std::endl;
|
|
|
|
std::cout << "Memory: " << xcdat::memory_in_bytes(trie) << " bytes" << std::endl;
|
|
|
|
|
|
|
|
// Lookup IDs from keys
|
2021-06-26 16:40:11 +00:00
|
|
|
{
|
2021-06-29 01:27:02 +00:00
|
|
|
const auto id = trie.lookup("Mac_Pro");
|
2021-06-29 01:36:00 +00:00
|
|
|
std::cout << "Lookup(Mac_Pro) = " << id.value_or(UINT64_MAX) << std::endl;
|
2021-06-29 01:27:02 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const auto id = trie.lookup("Google_Pixel");
|
2021-06-29 01:36:00 +00:00
|
|
|
std::cout << "Lookup(Google_Pixel) = " << id.value_or(UINT64_MAX) << std::endl;
|
2021-06-29 01:27:02 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 01:36:00 +00:00
|
|
|
// Decode keys from IDs
|
2021-06-29 01:27:02 +00:00
|
|
|
{
|
|
|
|
const auto dec = trie.decode(4);
|
2021-06-29 01:36:00 +00:00
|
|
|
std::cout << "Decode(4) = " << dec << std::endl;
|
2021-06-26 16:40:11 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 01:27:02 +00:00
|
|
|
// Common prefix search
|
2021-06-26 16:40:11 +00:00
|
|
|
{
|
2021-06-29 01:36:00 +00:00
|
|
|
std::cout << "CommonPrefixSearch(MacBook_Air) = {" << std::endl;
|
2021-06-26 16:40:11 +00:00
|
|
|
auto itr = trie.make_prefix_iterator("MacBook_Air");
|
|
|
|
while (itr.next()) {
|
2021-06-29 01:27:02 +00:00
|
|
|
std::cout << " (" << itr.decoded_view() << ", " << itr.id() << ")," << std::endl;
|
2021-06-26 16:40:11 +00:00
|
|
|
}
|
2021-06-29 01:27:02 +00:00
|
|
|
std::cout << "}" << std::endl;
|
2021-06-26 16:40:11 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 01:27:02 +00:00
|
|
|
// Predictive search
|
2021-06-26 16:40:11 +00:00
|
|
|
{
|
2021-06-29 01:36:00 +00:00
|
|
|
std::cout << "PredictiveSearch(Mac) = {" << std::endl;
|
2021-06-26 16:40:11 +00:00
|
|
|
auto itr = trie.make_predictive_iterator("Mac");
|
|
|
|
while (itr.next()) {
|
2021-06-29 01:27:02 +00:00
|
|
|
std::cout << " (" << itr.decoded_view() << ", " << itr.id() << ")," << std::endl;
|
|
|
|
}
|
|
|
|
std::cout << "}" << std::endl;
|
|
|
|
}
|
|
|
|
|
2021-06-29 01:36:00 +00:00
|
|
|
// Enumerate all the keys (in lex order).
|
2021-06-29 01:27:02 +00:00
|
|
|
{
|
2021-06-29 01:36:00 +00:00
|
|
|
std::cout << "Enumerate() = {" << std::endl;
|
2021-06-29 01:27:02 +00:00
|
|
|
auto itr = trie.make_enumerative_iterator();
|
|
|
|
while (itr.next()) {
|
|
|
|
std::cout << " (" << itr.decoded_view() << ", " << itr.id() << ")," << std::endl;
|
2021-06-26 16:40:11 +00:00
|
|
|
}
|
2021-06-29 01:27:02 +00:00
|
|
|
std::cout << "}" << std::endl;
|
2021-06-26 16:40:11 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 01:27:02 +00:00
|
|
|
std::remove(index_filename);
|
2021-06-26 16:40:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|