From d090bdc7cfc73c015d943f46d08c99d5d967e109 Mon Sep 17 00:00:00 2001 From: Dmitrii Kamaldinov Date: Sat, 23 Mar 2019 15:49:53 +0300 Subject: [PATCH 01/10] functor for dac --- pysdsl/types/encodedvector.hpp | 115 +++++++++++++++++++++++---------- 1 file changed, 82 insertions(+), 33 deletions(-) diff --git a/pysdsl/types/encodedvector.hpp b/pysdsl/types/encodedvector.hpp index 11dd297..7664db8 100644 --- a/pysdsl/types/encodedvector.hpp +++ b/pysdsl/types/encodedvector.hpp @@ -1,8 +1,10 @@ #pragma once #include +#include #include #include +#include #include @@ -148,33 +150,84 @@ template inline std::string key_to_string(KEY_T key) { return std::to_string(key); } -template -inline -auto add_dac_vector(py::module& m, KEY_T key, - const char* doc = nullptr) +namespace { + +char dprrr[] = "DPRRR"; +char dp[] = "DP"; + +template > +struct get_vector_type {}; + +template +struct get_vector_type : sdsl::dac_vector; + +template <> +struct get_vector_type : sdsl::dac_vector_dp<>; + +template <> +struct get_vector_type : sdsl::dac_vector_dp>; + +} // namespace + + +class add_dac_vector_functor { - auto name = "DirectAccessibleCodesVector" + key_to_string(key); +public: + constexpr add_dac_vector_functor(py::module& m, const char* doc = nullptr, + const char* doc_dp = nullptr): + m(m), doc(doc), doc_dp(doc_dp) {} - auto cls = py::class_(m, name.c_str()).def(py::init()); - add_sizes(cls); - add_description(cls); - add_serialization(cls); - add_to_string(cls); + template + inline + decltype(auto) get_vector(std::integral_constant) { + using type = get_vector_type; + auto name = "DirectAccessibleCodesVector" + key_to_string(key); - add_read_access(cls); - add_std_algo(cls); + auto cls = py::class_(m, name.c_str()).def(py::init()); - if (doc) { - cls.doc() = doc; } + add_sizes(cls); + add_description(cls); + add_serialization(cls); + add_to_string(cls); - cls.def_property_readonly("levels", &Sequence::levels); + add_read_access(cls); + add_std_algo(cls); - m.attr("direct_accessible_codes_vector").attr("__setitem__")(key, cls); - m.attr("all_compressed_integer_vectors").attr("append")(cls); + if (doc && std::is_integral_v) + cls.doc() = doc; + else if (doc_dp && !std::is_integral_v) + cls.doc() = doc_dp; - return cls; -} + + cls.def_property_readonly("levels", &type::levels); + + m.attr("direct_accessible_codes_vector").attr("__setitem__")(key, cls); + m.attr("all_compressed_integer_vectors").attr("append")(cls); + + return cls; + } + + template >* dummy = nullptr> + inline + decltype(auto) operator()(std::integral_constant t) + { + return get_vector(t); + + } + template >* dummy = nullptr> + inline + decltype(auto) operator()(std::integral_constant t) { + return get_vector(t).def("cost", &get_vector_type::cost, + py::arg("n"), py::arg("m")); + } + +private: + py::module& m; + const char* doc; + const char* doc_dp; +}; auto add_encoded_vectors(py::module& m) @@ -186,20 +239,16 @@ auto add_encoded_vectors(py::module& m) auto enc_classes = for_each_in_tuple(coders, add_enc_coders_functor(m)); auto vlc_classes = for_each_in_tuple(coders, add_vlc_coders_functor(m)); - auto dac_classes = std::make_tuple( - add_dac_vector>(m, 4, doc_dac_vector), - add_dac_vector>(m, 8, doc_dac_vector), - add_dac_vector>(m, 16, doc_dac_vector), - add_dac_vector>(m, 63, doc_dac_vector), - add_dac_vector>(m, "DP", doc_dac_vector_dp) - .def("cost", &sdsl::dac_vector_dp<>::cost, - py::arg("n"), py::arg("m")), - add_dac_vector< - sdsl::dac_vector_dp< - sdsl::rrr_vector<>>>(m, "DPRRR", doc_dac_vector_dp) - .def("cost", &sdsl::dac_vector_dp>::cost, - py::arg("n"), py::arg("m")) - ); + + using dac_params = std::tuple< + std::integral_constant, + std::integral_constant, + std::integral_constant, + std::integral_constant, + std::integral_constant, + std::integral_constant + >; + auto dac_classes = for_each_in_tuple(dac_params(), add_dac_vector_functor(m, doc_dac_vector, doc_dac_vector_dp)); m.attr("DACVector") = m.attr("DirectAccessibleCodesVector4"); m.attr("DirectAccessibleCodesVector") = m.attr( From 5397109170a8c9bffac2d43d928094e1366c17bf Mon Sep 17 00:00:00 2001 From: Dmitrii Kamaldinov Date: Sat, 23 Mar 2019 16:56:28 +0300 Subject: [PATCH 02/10] replaced is_same_v with is_same::value (c++14) --- pysdsl/types/encodedvector.hpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pysdsl/types/encodedvector.hpp b/pysdsl/types/encodedvector.hpp index 7664db8..b147727 100644 --- a/pysdsl/types/encodedvector.hpp +++ b/pysdsl/types/encodedvector.hpp @@ -155,17 +155,17 @@ namespace { char dprrr[] = "DPRRR"; char dp[] = "DP"; -template > +template ::value> struct get_vector_type {}; template -struct get_vector_type : sdsl::dac_vector; +struct get_vector_type : sdsl::dac_vector {}; template <> -struct get_vector_type : sdsl::dac_vector_dp<>; +struct get_vector_type : sdsl::dac_vector_dp<> {}; template <> -struct get_vector_type : sdsl::dac_vector_dp>; +struct get_vector_type : sdsl::dac_vector_dp> {}; } // namespace @@ -194,9 +194,9 @@ class add_dac_vector_functor add_read_access(cls); add_std_algo(cls); - if (doc && std::is_integral_v) + if (doc && std::is_integral::value) cls.doc() = doc; - else if (doc_dp && !std::is_integral_v) + else if (doc_dp && !std::is_integral::value) cls.doc() = doc_dp; @@ -208,7 +208,7 @@ class add_dac_vector_functor return cls; } - template >* dummy = nullptr> + template ::value>* dummy = nullptr> inline decltype(auto) operator()(std::integral_constant t) { @@ -216,7 +216,7 @@ class add_dac_vector_functor } template >* dummy = nullptr> + typename std::enable_if::value>::type* dummy = nullptr> inline decltype(auto) operator()(std::integral_constant t) { return get_vector(t).def("cost", &get_vector_type::cost, From 41c896deebe4f73a2d7a7c2399cc0ea0b34e0d19 Mon Sep 17 00:00:00 2001 From: Dmitrii Kamaldinov Date: Sat, 23 Mar 2019 18:09:38 +0300 Subject: [PATCH 03/10] string.h removed; enable_if_t -> enable_if<>::type --- pysdsl/types/encodedvector.hpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pysdsl/types/encodedvector.hpp b/pysdsl/types/encodedvector.hpp index b147727..1515e10 100644 --- a/pysdsl/types/encodedvector.hpp +++ b/pysdsl/types/encodedvector.hpp @@ -1,10 +1,8 @@ #pragma once #include -#include #include #include -#include #include @@ -208,7 +206,9 @@ class add_dac_vector_functor return cls; } - template ::value>* dummy = nullptr> + template ::value>::type* dummy = nullptr> inline decltype(auto) operator()(std::integral_constant t) { @@ -216,7 +216,8 @@ class add_dac_vector_functor } template ::value>::type* dummy = nullptr> + typename std::enable_if< + std::is_same::value>::type* dummy = nullptr> inline decltype(auto) operator()(std::integral_constant t) { return get_vector(t).def("cost", &get_vector_type::cost, @@ -248,7 +249,8 @@ auto add_encoded_vectors(py::module& m) std::integral_constant, std::integral_constant >; - auto dac_classes = for_each_in_tuple(dac_params(), add_dac_vector_functor(m, doc_dac_vector, doc_dac_vector_dp)); + auto dac_classes = for_each_in_tuple(dac_params(), + add_dac_vector_functor(m, doc_dac_vector, doc_dac_vector_dp)); m.attr("DACVector") = m.attr("DirectAccessibleCodesVector4"); m.attr("DirectAccessibleCodesVector") = m.attr( From 5b92c9f4c2490e350d6d41411120422564df60bf Mon Sep 17 00:00:00 2001 From: Dmitrii Kamaldinov Date: Sat, 23 Mar 2019 21:44:32 +0300 Subject: [PATCH 04/10] droneci test --- pysdsl/types/encodedvector.hpp | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/pysdsl/types/encodedvector.hpp b/pysdsl/types/encodedvector.hpp index 1515e10..e666a4f 100644 --- a/pysdsl/types/encodedvector.hpp +++ b/pysdsl/types/encodedvector.hpp @@ -39,6 +39,14 @@ auto constexpr coders = std::make_tuple( std::make_tuple("Comma4", dens<128>{}, width<0>{}, (sdsl::coder::comma<4>*) nullptr)); +using dac_params = std::tuple< + std::integral_constant, + std::integral_constant, + std::integral_constant, + std::integral_constant, + std::integral_constant, + std::integral_constant +>; class add_enc_coders_functor { @@ -210,10 +218,8 @@ class add_dac_vector_functor typename std::enable_if< std::is_integral::value>::type* dummy = nullptr> inline - decltype(auto) operator()(std::integral_constant t) - { + decltype(auto) operator()(std::integral_constant t) { return get_vector(t); - } template , - std::integral_constant, - std::integral_constant, - std::integral_constant, - std::integral_constant, - std::integral_constant - >; + auto dac_classes = for_each_in_tuple(dac_params(), add_dac_vector_functor(m, doc_dac_vector, doc_dac_vector_dp)); From 9261f1c798d12e46b0447bd421bd8d172de9e760 Mon Sep 17 00:00:00 2001 From: Dmitrii Kamaldinov Date: Sat, 23 Mar 2019 21:50:15 +0300 Subject: [PATCH 05/10] droneci test --- pysdsl/types/encodedvector.hpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/pysdsl/types/encodedvector.hpp b/pysdsl/types/encodedvector.hpp index e666a4f..dd1ce0a 100644 --- a/pysdsl/types/encodedvector.hpp +++ b/pysdsl/types/encodedvector.hpp @@ -39,14 +39,6 @@ auto constexpr coders = std::make_tuple( std::make_tuple("Comma4", dens<128>{}, width<0>{}, (sdsl::coder::comma<4>*) nullptr)); -using dac_params = std::tuple< - std::integral_constant, - std::integral_constant, - std::integral_constant, - std::integral_constant, - std::integral_constant, - std::integral_constant ->; class add_enc_coders_functor { @@ -247,7 +239,14 @@ auto add_encoded_vectors(py::module& m) auto enc_classes = for_each_in_tuple(coders, add_enc_coders_functor(m)); auto vlc_classes = for_each_in_tuple(coders, add_vlc_coders_functor(m)); - + using dac_params = std::tuple< + std::integral_constant, + std::integral_constant, + std::integral_constant, + std::integral_constant, + std::integral_constant, + std::integral_constant + >; auto dac_classes = for_each_in_tuple(dac_params(), add_dac_vector_functor(m, doc_dac_vector, doc_dac_vector_dp)); From e11b365c041bdabae14a0984f8686b7e9efd1333 Mon Sep 17 00:00:00 2001 From: Dmitrii Kamaldinov Date: Sat, 23 Mar 2019 22:19:37 +0300 Subject: [PATCH 06/10] add const --- pysdsl/types/encodedvector.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pysdsl/types/encodedvector.hpp b/pysdsl/types/encodedvector.hpp index dd1ce0a..b5fbc9a 100644 --- a/pysdsl/types/encodedvector.hpp +++ b/pysdsl/types/encodedvector.hpp @@ -150,8 +150,8 @@ inline std::string key_to_string(KEY_T key) { return std::to_string(key); } namespace { -char dprrr[] = "DPRRR"; -char dp[] = "DP"; +const char dprrr[] = "DPRRR"; +const char dp[] = "DP"; template ::value> struct get_vector_type {}; From bbb8d6425319344e1ed8a3af0c1c4469362de846 Mon Sep 17 00:00:00 2001 From: Dmitrii Kamaldinov Date: Sun, 24 Mar 2019 02:52:18 +0300 Subject: [PATCH 07/10] inheritance -> using type --- pysdsl/types/encodedvector.hpp | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/pysdsl/types/encodedvector.hpp b/pysdsl/types/encodedvector.hpp index b5fbc9a..bdf26a6 100644 --- a/pysdsl/types/encodedvector.hpp +++ b/pysdsl/types/encodedvector.hpp @@ -157,13 +157,22 @@ template ::value> struct get_vector_type {}; template -struct get_vector_type : sdsl::dac_vector {}; +struct get_vector_type { + using type = sdsl::dac_vector; +}; template <> -struct get_vector_type : sdsl::dac_vector_dp<> {}; +struct get_vector_type { + using type = sdsl::dac_vector_dp<>; +}; template <> -struct get_vector_type : sdsl::dac_vector_dp> {}; +struct get_vector_type { + using type = sdsl::dac_vector_dp>; +}; + +template +using get_vector_type_t = typename get_vector_type::type; } // namespace @@ -179,7 +188,7 @@ class add_dac_vector_functor template inline decltype(auto) get_vector(std::integral_constant) { - using type = get_vector_type; + using type = get_vector_type_t; auto name = "DirectAccessibleCodesVector" + key_to_string(key); auto cls = py::class_(m, name.c_str()).def(py::init()); @@ -218,7 +227,7 @@ class add_dac_vector_functor std::is_same::value>::type* dummy = nullptr> inline decltype(auto) operator()(std::integral_constant t) { - return get_vector(t).def("cost", &get_vector_type::cost, + return get_vector(t).def("cost", &get_vector_type_t::cost, py::arg("n"), py::arg("m")); } From 8453620469d2dcb8ab273e7306e68f1e084e8c6d Mon Sep 17 00:00:00 2001 From: Dmitrii Kamaldinov Date: Sat, 13 Apr 2019 03:06:29 +0300 Subject: [PATCH 08/10] rmq first commit --- pysdsl/types/rmq.hpp | 74 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 pysdsl/types/rmq.hpp diff --git a/pysdsl/types/rmq.hpp b/pysdsl/types/rmq.hpp new file mode 100644 index 0000000..e6a8c3c --- /dev/null +++ b/pysdsl/types/rmq.hpp @@ -0,0 +1,74 @@ +#pragma once + +#include +#include +#include + +#include + +#include + +#include "operations/sizes.hpp" +#include "operations/iteration.hpp" +#include "util/tupletricks.hpp" +#include "docstrings.hpp" +#include "io.hpp" +#include "calc.hpp" + + + +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 + decltype(auto) operator()(std::tuple, + std::integral_constant>) { + using T = sdsl::rmq_support_sparse_table; + + std::string name = + std::string("Range") + (t_min ? "Min" : "Max") + "QuerySparseTable_for_" + rac_name; + + auto cls = py::class_(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("__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; + } +}; + + +namespace RAC_names { + const char INT_VECTOR_NAME[] = "IntVector"; +} + + +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, + std::integral_constant, + std::integral_constant>, + std::tuple, + std::integral_constant, + std::integral_constant> + >; + + auto rmq_sparse_tables = for_each_in_tuple(rmq_support_sparse_table_params(), + add_rmq_sparse_table_functor(m, doc_rmq_sparse_table)); + + return std::tuple_cat(rmq_sparse_tables); +} From 88c1da69656be7f8c38508a5260d69566b0381d7 Mon Sep 17 00:00:00 2001 From: Dmitrii Kamaldinov Date: Sat, 13 Apr 2019 03:25:17 +0300 Subject: [PATCH 09/10] rmq as params --- pysdsl/types/rmq.hpp | 13 ++++++++ pysdsl/util/tupletricks.hpp | 64 ++++++++++++++++++++++++++++++++++++- 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/pysdsl/types/rmq.hpp b/pysdsl/types/rmq.hpp index e6a8c3c..24bc322 100644 --- a/pysdsl/types/rmq.hpp +++ b/pysdsl/types/rmq.hpp @@ -53,6 +53,9 @@ namespace RAC_names { const char INT_VECTOR_NAME[] = "IntVector"; } +template +using general_rmq_sparse_table = py::class_>; + inline auto add_rmq_classes(py::module& m) { m.attr("rmq_sparse_table") = py::dict(); @@ -69,6 +72,16 @@ inline auto add_rmq_classes(py::module& m) { auto rmq_sparse_tables = for_each_in_tuple(rmq_support_sparse_table_params(), add_rmq_sparse_table_functor(m, doc_rmq_sparse_table)); + + ////////////// as params ////////////////////// + using rmq_support_sparse_table_as_params = std::tuple< + std::tuple, std::integral_constant>, + std::tuple, std::integral_constant> + >; + + auto rmq_sparse_tables_as_params = for_each_in_tuple(rmq_support_sparse_table_as_params(), + make_general_sybset_functor(rmq_sparse_tables)); + /////////////////////////////////////////////// return std::tuple_cat(rmq_sparse_tables); } diff --git a/pysdsl/util/tupletricks.hpp b/pysdsl/util/tupletricks.hpp index 2070080..98d7471 100644 --- a/pysdsl/util/tupletricks.hpp +++ b/pysdsl/util/tupletricks.hpp @@ -14,7 +14,7 @@ decltype(auto) for_each_impl(P&& t, Function&& f, std::index_sequence) { template constexpr -decltype(auto) for_each_impl(P& t, Function&& f, std::index_sequence) { +decltype(auto) for_each_impl(const P& t, Function&& f, std::index_sequence) { return std::make_tuple(f(std::get(t))...); } template @@ -27,6 +27,26 @@ constexpr decltype(auto) for_each(std::tuple& t, Function&& f) { return for_each_impl(t, f, std::index_sequence_for{}); } +template +constexpr +decltype(auto) forward_each_impl(P&& t, Function&& f, std::index_sequence) { + return std::forward_as_tuple(f(std::get(t))...); } + +template +constexpr +decltype(auto) forward_each_impl(const P& t, Function&& f, std::index_sequence) { + return std::forward_as_tuple(f(std::get(t))...); } + +template +constexpr +decltype(auto) forward_each(const std::tuple& t, Function&& f) { + return forward_each_impl(t, f, std::index_sequence_for{}); } + +template +constexpr +decltype(auto) forward_each(std::tuple& t, Function&& f) { + return forward_each_impl(t, f, std::index_sequence_for{}); } + } // namespace detail @@ -40,3 +60,45 @@ template constexpr decltype(auto) for_each_in_tuple(std::tuple &t, F f) { return detail::for_each(t, f); } + +template +constexpr +decltype(auto) forward_each_in_tuple(const std::tuple &t, F f) { + return detail::forward_each(t, f); } + + +template +constexpr +decltype(auto) forward_each_in_tuple(std::tuple &t, F f) { + return detail::forward_each(t, f); } + + +// subset functor +template +struct get_template_arg { + using arg = T; +}; + +template +struct get_template_arg> { + const static T arg = N; +}; + + +template