-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
279 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <tuple> | ||
#include <utility> | ||
|
||
#include <pybind11/pybind11.h> | ||
|
||
#include <sdsl/rmq_support_sparse_table.hpp> | ||
#include <sdsl/rmq_succinct_sada.hpp> | ||
|
||
#include "operations/sizes.hpp" | ||
#include "operations/iteration.hpp" | ||
#include "util/tupletricks.hpp" | ||
#include "docstrings.hpp" | ||
#include "io.hpp" | ||
#include "calc.hpp" | ||
|
||
|
||
|
||
namespace detail { | ||
// adds constructors of t_rac... containers | ||
template <typename PybindClass, typename... t_rac> | ||
typename std::enable_if<sizeof...(t_rac) == 0>::type add_rac_constructor(const PybindClass&) {} | ||
|
||
template <typename PybindClass, typename t_rac_head, typename... t_rac_tail> | ||
void add_rac_constructor(PybindClass& cls) { | ||
cls.def(py::init([](const t_rac_head* rac) { | ||
return typename PybindClass::type(rac); | ||
})); | ||
add_rac_constructor<PybindClass, t_rac_tail...>(cls); | ||
} | ||
} | ||
|
||
// containers names | ||
namespace RAC_names { | ||
const char INT_VECTOR_NAME[] = "IntVector", | ||
INT16_VECTOR_NAME[] = "Int16Vector"; | ||
} | ||
|
||
|
||
struct add_rmq_sparse_table_functor { | ||
py::module& m; | ||
const char* doc; | ||
|
||
constexpr add_rmq_sparse_table_functor(py::module& m, const char* doc = nullptr) | ||
: m(m), doc(doc) {} | ||
|
||
template <typename t_rac, const char* rac_name, bool t_min> | ||
decltype(auto) operator()(std::tuple<t_rac, | ||
std::integral_constant<const char*, rac_name>, | ||
std::integral_constant<bool, t_min>>) { | ||
using T = sdsl::rmq_support_sparse_table<t_rac, t_min>; | ||
|
||
std::string name = | ||
std::string("Range") + (t_min ? "Min" : "Max") + "QuerySparseTable_for_" + rac_name; | ||
|
||
auto cls = py::class_<T>(m, name.c_str()) | ||
.def_property_readonly("size", (typename T::size_type(T::*)(void) const)& T::size) | ||
.def(py::init([](const t_rac* rac) {return T(rac);})) | ||
.def("set_vector", &T::set_vector) | ||
.def("__call__", | ||
(typename T::size_type | ||
(T::*)(typename T::size_type, typename T::size_type) const)& T::operator()); | ||
|
||
add_sizes(cls); | ||
add_description(cls); | ||
|
||
// load takes two params | ||
// add_serialization(cls); | ||
|
||
return cls; | ||
} | ||
}; | ||
|
||
|
||
struct add_rmq_sada_functor { | ||
py::module& m; | ||
const char* doc; | ||
|
||
constexpr add_rmq_sada_functor(py::module& m, const char* doc = nullptr) | ||
: m(m), doc(doc) {} | ||
|
||
|
||
template <typename... t_rac, bool t_min> | ||
decltype(auto) operator()(std::tuple<std::tuple<t_rac...>, | ||
std::integral_constant<bool, t_min>>) { | ||
using T = typename std::conditional<t_min, sdsl::rmq_succinct_sada<>, | ||
typename sdsl::range_maximum_support_sada<>::type>::type; | ||
|
||
std::string name = | ||
std::string("Range") + (t_min ? "Min" : "Max") + "QuerySuccintSada"; | ||
|
||
auto cls = py::class_<T>(m, name.c_str()) | ||
.def_property_readonly("size", (typename T::size_type(T::*)(void) const)& T::size) | ||
.def(py::init()) | ||
.def("__call__", | ||
(typename T::size_type | ||
(T::*)(typename T::size_type, typename T::size_type) const)& T::operator()); | ||
|
||
detail::add_rac_constructor<decltype(cls), t_rac...>(cls); | ||
|
||
add_sizes(cls); | ||
add_description(cls); | ||
add_serialization(cls); | ||
|
||
return cls; | ||
} | ||
}; | ||
|
||
|
||
struct add_rmq_sct_functor { | ||
py::module& m; | ||
const char* doc; | ||
|
||
constexpr add_rmq_sct_functor(py::module& m, const char* doc = nullptr) | ||
: m(m), doc(doc) {} | ||
|
||
|
||
template <typename... t_rac, bool t_min> | ||
decltype(auto) operator()(std::tuple<std::tuple<t_rac...>, | ||
std::integral_constant<bool, t_min>>) { | ||
using T = typename std::conditional<t_min, sdsl::rmq_succinct_sct<>, | ||
typename sdsl::range_maximum_sct<>::type>::type; | ||
|
||
std::string name = | ||
std::string("Range") + (t_min ? "Min" : "Max") + "QuerySuccintSct"; | ||
|
||
auto cls = py::class_<T>(m, name.c_str()) | ||
.def_property_readonly("size", (typename T::size_type(T::*)(void) const)& T::size) | ||
.def(py::init()) | ||
.def("__call__", | ||
(typename T::size_type | ||
(T::*)(typename T::size_type, typename T::size_type) const)& T::operator()); | ||
|
||
detail::add_rac_constructor<decltype(cls), t_rac...>(cls); | ||
|
||
add_sizes(cls); | ||
add_description(cls); | ||
add_serialization(cls); | ||
|
||
return cls; | ||
} | ||
}; | ||
|
||
|
||
// generalized (constants -> typenames) template for usage with GeneralSubsetFunctor | ||
template <typename t_rac, typename t_min_ic> | ||
using general_rmq_sparse_table = py::class_<sdsl::rmq_support_sparse_table<t_rac, t_min_ic::value>>; | ||
|
||
template <typename t_min_ic> | ||
using general_rmq_sada = py::class_< | ||
typename std::conditional<t_min_ic::value, | ||
sdsl::rmq_succinct_sada<>, | ||
typename sdsl::range_maximum_support_sada<>::type>::type>; | ||
|
||
template <typename t_min_ic> | ||
using general_rmq_sct = py::class_< | ||
typename std::conditional<t_min_ic::value, | ||
sdsl::rmq_succinct_sct<>, | ||
typename sdsl::range_maximum_sct<>::type>::type>; | ||
|
||
|
||
inline auto add_rmq_classes(py::module& m) { | ||
m.attr("rmq_sparse_table") = py::dict(); | ||
|
||
|
||
using rmq_support_sparse_table_params = std::tuple< | ||
std::tuple<sdsl::int_vector<>, | ||
std::integral_constant<const char*, RAC_names::INT_VECTOR_NAME>, | ||
std::integral_constant<bool, true>>, | ||
std::tuple<sdsl::int_vector<>, | ||
std::integral_constant<const char*, RAC_names::INT_VECTOR_NAME>, | ||
std::integral_constant<bool, false>>, | ||
std::tuple<sdsl::int_vector<16>, | ||
std::integral_constant<const char*, RAC_names::INT16_VECTOR_NAME>, | ||
std::integral_constant<bool, true>>, | ||
std::tuple<sdsl::int_vector<16>, | ||
std::integral_constant<const char*, RAC_names::INT16_VECTOR_NAME>, | ||
std::integral_constant<bool, false>> | ||
>; | ||
|
||
using rmq_sada_params = std::tuple< | ||
std::tuple<std::tuple<sdsl::int_vector<>, sdsl::int_vector<16>>, | ||
std::integral_constant<bool, true>>, | ||
std::tuple<std::tuple<sdsl::int_vector<>, sdsl::int_vector<16>>, | ||
std::integral_constant<bool, false>> | ||
>; | ||
|
||
using rmq_sct_params = std::tuple< | ||
std::tuple<std::tuple<sdsl::int_vector<>, sdsl::int_vector<16>>, | ||
std::integral_constant<bool, true>>, | ||
std::tuple<std::tuple<sdsl::int_vector<>, sdsl::int_vector<16>>, | ||
std::integral_constant<bool, false>> | ||
>; | ||
|
||
auto rmq_sparse_tables = for_each_in_tuple(rmq_support_sparse_table_params(), | ||
add_rmq_sparse_table_functor(m, doc_rmq_sparse_table)); | ||
auto rmq_sada_classes = for_each_in_tuple(rmq_sada_params(), | ||
add_rmq_sada_functor(m, doc_rmq_sada)); | ||
auto rmq_sct_classes = for_each_in_tuple(rmq_sct_params(), | ||
add_rmq_sct_functor(m, doc_rmq_sada)); | ||
|
||
////////////// as params ////////////////////// | ||
using rmq_support_sparse_table_as_params = std::tuple< | ||
std::tuple<sdsl::int_vector<>, std::integral_constant<bool, true>>, | ||
std::tuple<sdsl::int_vector<>, std::integral_constant<bool, false>> | ||
>; | ||
|
||
auto rmq_sparse_tables_as_params = for_each_in_tuple(rmq_support_sparse_table_as_params(), | ||
make_general_sybset_functor<general_rmq_sparse_table>(rmq_sparse_tables)); | ||
/////////////////////////////////////////////// | ||
|
||
return std::make_tuple(rmq_sparse_tables, rmq_sada_classes, rmq_sct_classes); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters