commit 5780b622eea2d8fa2e43c8080ebcba8dceaf33d3
parent 218b1a43ab670820786fdec12b6c13edf34867a6
Author: d.levin256@gmail.com <d.levin256@gmail.com>
Date: Wed, 24 Aug 2016 22:09:15 +0300
Add new function: rebind
Diffstat:
5 files changed, 33 insertions(+), 3 deletions(-)
diff --git a/include/kfr/base/expression.hpp b/include/kfr/base/expression.hpp
@@ -186,6 +186,11 @@ struct expression_function : expression<arg<Args>...>
fn(std::forward<Fn>(fn))
{
}
+ expression_function(const Fn& fn, arg<Args>&&... args) noexcept
+ : expression<arg<Args>...>(std::forward<arg<Args>>(args)...),
+ fn(fn)
+ {
+ }
template <typename T, size_t N>
CMT_INLINE vec<T, N> operator()(cinput_t, size_t index, vec_t<T, N> x) const
{
@@ -195,6 +200,8 @@ struct expression_function : expression<arg<Args>...>
return this->call(fn, index, x);
}
+ const Fn& get_fn() const noexcept { return fn; }
+
protected:
Fn fn;
};
@@ -236,6 +243,17 @@ CMT_INLINE internal::expression_function<decay<Fn>, internal::arg<Args>...> bind
return internal::expression_function<decay<Fn>, internal::arg<Args>...>(std::forward<Fn>(fn),
std::forward<Args>(args)...);
}
+/**
+ * @brief Construct a new expression using the same function as in @c e and new arguments
+ * @param e an expression
+ * @param args new arguments for the function
+ */
+template <typename Fn, typename... OldArgs, typename... NewArgs>
+CMT_INLINE internal::expression_function<Fn, NewArgs...> rebind(
+ const internal::expression_function<Fn, OldArgs...>& e, NewArgs&&... args)
+{
+ return internal::expression_function<Fn, NewArgs...>(e.get_fn(), std::forward<NewArgs>(args)...);
+}
template <typename Tout, cpu_t c = cpu_t::native, size_t width = 0, typename OutFn, typename Fn>
CMT_INLINE void process(OutFn&& outfn, const Fn& fn, size_t size)
diff --git a/include/kfr/base/pointer.hpp b/include/kfr/base/pointer.hpp
@@ -25,8 +25,8 @@
*/
#pragma once
-#include "vec.hpp"
#include "basic_expressions.hpp"
+#include "vec.hpp"
#include <memory>
namespace kfr
diff --git a/include/kfr/base/reduce.hpp b/include/kfr/base/reduce.hpp
@@ -25,11 +25,11 @@
*/
#pragma once
+#include "basic_expressions.hpp"
#include "function.hpp"
#include "min_max.hpp"
#include "operators.hpp"
#include "vec.hpp"
-#include "basic_expressions.hpp"
namespace kfr
{
diff --git a/include/kfr/cometa.hpp b/include/kfr/cometa.hpp
@@ -175,7 +175,7 @@ template <typename T>
using deep_subtype = typename compound_type_traits<T>::deep_subtype;
template <typename T, typename SubType>
-using rebind = typename compound_type_traits<T>::template rebind<SubType>;
+using rebind_subtype = typename compound_type_traits<T>::template rebind<SubType>;
template <typename T, typename SubType>
using deep_rebind = typename compound_type_traits<T>::template deep_rebind<SubType>;
diff --git a/tests/vec_test.cpp b/tests/vec_test.cpp
@@ -302,4 +302,16 @@ TEST(test_adjacent)
CHECK(v1[19] == 342);
}
+TEST(test_rebind)
+{
+ auto c_minus_two = counter() - 2;
+ auto four_minus_c = rebind(c_minus_two, 4, counter());
+ univector<int, 5> v1 = c_minus_two;
+ univector<int, 5> v2 = four_minus_c;
+ CHECK(v1[0] == -2);
+ CHECK(v1[1] == -1);
+ CHECK(v2[0] == 4);
+ CHECK(v2[1] == 3);
+}
+
int main(int argc, char** argv) { return testo::run_all("", true); }