xcdat/sample/sample.cpp

59 lines
1.6 KiB
C++
Raw Normal View History

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 00:23:02 +00:00
using trie_type = xcdat::trie_8_type;
2021-06-27 17:15:09 +00:00
const std::string index_filename = "tmp.idx";
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
std::cout << "Basic operations" << std::endl;
{
const auto id = trie.lookup("MacBook_Pro");
if (id.has_value()) {
2021-06-27 03:57:34 +00:00
std::cout << trie.decode(id.value()) << " -> " << id.has_value() << std::endl;
2021-06-26 16:40:11 +00:00
} else {
std::cout << "Not found" << std::endl;
}
}
std::cout << "Common prefix search" << std::endl;
{
auto itr = trie.make_prefix_iterator("MacBook_Air");
while (itr.next()) {
2021-06-27 03:57:34 +00:00
std::cout << itr.decoded_view() << " -> " << itr.id() << std::endl;
2021-06-26 16:40:11 +00:00
}
}
std::cout << "Predictive search" << std::endl;
{
auto itr = trie.make_predictive_iterator("Mac");
while (itr.next()) {
2021-06-27 03:57:34 +00:00
std::cout << itr.decoded_view() << " -> " << itr.id() << std::endl;
2021-06-26 16:40:11 +00:00
}
}
2021-06-27 17:15:09 +00:00
std::remove(index_filename.c_str());
2021-06-27 14:37:38 +00:00
2021-06-26 16:40:11 +00:00
return 0;
}