86 lines
2.6 KiB
C++
86 lines
2.6 KiB
C++
#include <iostream>
|
|
#include <string>
|
|
|
|
#include <xcdat.hpp>
|
|
|
|
int main() {
|
|
// Input keys
|
|
std::vector<std::string> keys = {
|
|
"AirPods", "AirTag", "Mac", "MacBook", "MacBook_Air", "MacBook_Pro",
|
|
"Mac_Mini", "Mac_Pro", "iMac", "iPad", "iPhone", "iPhone_SE",
|
|
};
|
|
|
|
// The input keys must be sorted and unique (although they have already satisfied in this case).
|
|
std::sort(keys.begin(), keys.end());
|
|
keys.erase(std::unique(keys.begin(), keys.end()), keys.end());
|
|
|
|
const char* index_filename = "tmp.idx";
|
|
|
|
// The trie index type
|
|
using trie_type = xcdat::trie_8_type;
|
|
|
|
// Build and save the trie index.
|
|
{
|
|
const trie_type trie(keys);
|
|
xcdat::save(trie, index_filename);
|
|
}
|
|
|
|
// Load the trie index.
|
|
const auto trie = xcdat::load<trie_type>(index_filename);
|
|
|
|
// 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
|
|
{
|
|
const auto id = trie.lookup("Mac_Pro");
|
|
std::cout << "Lookup(Mac_Pro) = " << id.value_or(UINT64_MAX) << std::endl;
|
|
}
|
|
{
|
|
const auto id = trie.lookup("Google_Pixel");
|
|
std::cout << "Lookup(Google_Pixel) = " << id.value_or(UINT64_MAX) << std::endl;
|
|
}
|
|
|
|
// Decode keys from IDs
|
|
{
|
|
const auto dec = trie.decode(4);
|
|
std::cout << "Decode(4) = " << dec << std::endl;
|
|
}
|
|
|
|
// Common prefix search
|
|
{
|
|
std::cout << "CommonPrefixSearch(MacBook_Air) = {" << std::endl;
|
|
auto itr = trie.make_prefix_iterator("MacBook_Air");
|
|
while (itr.next()) {
|
|
std::cout << " (" << itr.decoded_view() << ", " << itr.id() << ")," << std::endl;
|
|
}
|
|
std::cout << "}" << std::endl;
|
|
}
|
|
|
|
// Predictive search
|
|
{
|
|
std::cout << "PredictiveSearch(Mac) = {" << std::endl;
|
|
auto itr = trie.make_predictive_iterator("Mac");
|
|
while (itr.next()) {
|
|
std::cout << " (" << itr.decoded_view() << ", " << itr.id() << ")," << std::endl;
|
|
}
|
|
std::cout << "}" << std::endl;
|
|
}
|
|
|
|
// Enumerate all the keys (in lex order).
|
|
{
|
|
std::cout << "Enumerate() = {" << std::endl;
|
|
auto itr = trie.make_enumerative_iterator();
|
|
while (itr.next()) {
|
|
std::cout << " (" << itr.decoded_view() << ", " << itr.id() << ")," << std::endl;
|
|
}
|
|
std::cout << "}" << std::endl;
|
|
}
|
|
|
|
std::remove(index_filename);
|
|
return 0;
|
|
}
|