#include #include #include #include namespace { class Keyset { private: std::vector m_keys; public: Keyset() = default; virtual ~Keyset() = default; void append(const std::string& key) { m_keys.push_back(key); } void complete() { std::sort(m_keys.begin(), m_keys.end()); m_keys.erase(std::unique(m_keys.begin(), m_keys.end()), m_keys.end()); } const std::vector& get() const { return m_keys; } }; } // namespace namespace py = pybind11; template void define_Trie(py::module_& m, const std::string& suffix) { const std::string name = "Trie" + suffix; py::class_(m, name.c_str(), "A class of trie dictionary") // .def(py::init([](const Keyset& keyset) { // return std::make_unique(keyset.get()); }), "Build the trie dictionary from the keyset.", py::arg("keyset")) .def(py::init([](const std::string& filepath) { // return std::make_unique(xcdat::load(filepath)); }), "Load the trie dictionary from the filepath.", py::arg("filepath")) .def( "save", [](const Trie& self, const std::string& filepath) { xcdat::save(self, filepath); }, "Save the trie dictionary to the filepath", py::arg("filepath")) .def( "memory_in_bytes", [](const Trie& self) { return xcdat::memory_in_bytes(self); }, "Get the memory usage of the trie dictionary") .def("bin_mode", &Trie::bin_mode, "Check if the binary mode.") .def("num_keys", &Trie::num_keys, "Get the number of stored keywords.") .def("alphabet_size", &Trie::alphabet_size, "Get the alphabet size.") .def("max_length", &Trie::max_length, "Get the maximum length of keywords.") .def("num_nodes", &Trie::num_nodes, "Get the number of trie nodes.") .def("num_units", &Trie::num_units, "Get the number of DA units.") .def("num_free_units", &Trie::num_free_units, "Get the number of unused DA units.") .def("tail_length", &Trie::tail_length, "Get the length of TAIL vector.") .def("lookup", &Trie::lookup, "Lookup the ID of the keyword.", py::arg("key")) .def( "decode", [](const Trie& self, std::uint64_t id) { return self.decode(id); }, "Decode the keyword associated with the ID.", py::arg("id")) .def( "make_prefix_iterator", [](const Trie& self, const std::string& key) { return std::make_unique(self.make_prefix_iterator(key)); }, "Make the common prefix searcher for the given keyword.", py::arg("key")) .def( "make_predictive_iterator", [](const Trie& self, const std::string& key) { return std::make_unique(self.make_predictive_iterator(key)); }, "Make the predictive searcher for the keyword.", py::arg("key")) .def( "make_enumerative_iterator", [](const Trie& self) { return std::make_unique(self.make_enumerative_iterator()); }, "Make the enumerator."); } template void define_PrefixIterator(py::module_& m, const std::string& suffix) { const std::string name = "PrefixIterator" + suffix; py::class_( m, name.c_str(), "An iterator class for common prefix search.\n" "It enumerates all the keywords contained as prefixes of a given string.\n" "It should be instantiated via the function 'make_prefix_iterator'.") .def("next", &Trie::prefix_iterator::next, "Increment the iterator. Return false if the iteration is terminated.") .def("id", &Trie::prefix_iterator::id, "Get the result ID.") .def("decoded", &Trie::prefix_iterator::decoded, "Get the result keyword."); } template void define_PredictiveIterator(py::module_& m, const std::string& suffix) { const std::string name = "PredictiveIterator" + suffix; py::class_( m, name.c_str(), "An iterator class for predictive search or enumeration.\n" "It enumerates all the keywords starting with a given string (including an empty string).\n" "It should be instantiated via the function 'make_prefix_iterator' or 'make_enumerative_iterator'.") // .def("next", &Trie::predictive_iterator::next, "Increment the iterator. Return false if the iteration is terminated.") .def("id", &Trie::predictive_iterator::id, "Get the result ID.") .def("decoded", &Trie::predictive_iterator::decoded, "Get the result keyword."); } PYBIND11_MODULE(xcdat, m) { m.doc() = "Xcdat: Fast compressed trie dictionary"; py::class_(m, "Keyset", "A class to store keywords for building xcdat.Trie.") // .def(py::init<>()) .def("append", &Keyset::append, "Append the given key.", py::arg("key")) .def("complete", &Keyset::complete, "Complete the keyset to build a xcdat.Trie."); define_Trie(m, "8"); define_PrefixIterator(m, "8"); define_PredictiveIterator(m, "8"); define_Trie(m, "16"); define_PrefixIterator(m, "16"); define_PredictiveIterator(m, "16"); define_Trie(m, "7"); define_PrefixIterator(m, "7"); define_PredictiveIterator(m, "7"); define_Trie(m, "15"); define_PrefixIterator(m, "15"); define_PredictiveIterator(m, "15"); }