Skip to content

Commit

Permalink
Add fliplr & flipud (#280)
Browse files Browse the repository at this point in the history
* add flipr & flipud views

* add fliplr & flipud functions

* add fliplr & flipud eager eval

* add fliplr & flipud tests

* fix sycl opencl ci
  • Loading branch information
alifahrri committed May 26, 2024
1 parent d0def64 commit 2fef926
Show file tree
Hide file tree
Showing 10 changed files with 1,053 additions and 117 deletions.
26 changes: 26 additions & 0 deletions include/nmtools/array/array/flip.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,32 @@ namespace nmtools::array
,resolver
);
} // flip

template <typename output_t=none_t, typename context_t=none_t, typename resolver_t=eval_result_t<>,
typename array_t>
constexpr auto fliplr(const array_t& array,
context_t&& context=context_t{}, output_t&& output=output_t{},meta::as_value<resolver_t> resolver=meta::as_value_v<resolver_t>)
{
auto flipped = view::fliplr(array);
return eval(flipped
,nmtools::forward<context_t>(context)
,nmtools::forward<output_t>(output)
,resolver
);
} // fliplr

template <typename output_t=none_t, typename context_t=none_t, typename resolver_t=eval_result_t<>,
typename array_t>
constexpr auto flipud(const array_t& array,
context_t&& context=context_t{}, output_t&& output=output_t{},meta::as_value<resolver_t> resolver=meta::as_value_v<resolver_t>)
{
auto flipped = view::flipud(array);
return eval(flipped
,nmtools::forward<context_t>(context)
,nmtools::forward<output_t>(output)
,resolver
);
} // flipud
} // namespace nmtools::array

#endif // NMTOOLS_ARRAY_ARRAY_FLIP_HPP
22 changes: 21 additions & 1 deletion include/nmtools/array/functional/flip.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,29 @@ namespace nmtools::functional
return view::flip(args...);
}
};

struct fliplr_t
{
template <typename...args_t>
constexpr auto operator()(const args_t&...args) const
{
return view::fliplr(args...);
}
};

struct flipud_t
{
template <typename...args_t>
constexpr auto operator()(const args_t&...args) const
{
return view::flipud(args...);
}
};
}

constexpr inline auto flip = functor_t{unary_fmap_t<fun::flip_t>{}};
constexpr inline auto flip = functor_t{unary_fmap_t<fun::flip_t>{}};
constexpr inline auto fliplr = functor_t{unary_fmap_t<fun::fliplr_t>{}};
constexpr inline auto flipud = functor_t{unary_fmap_t<fun::flipud_t>{}};
} // namespace nmtools::functional


Expand Down
14 changes: 12 additions & 2 deletions include/nmtools/array/view/flip.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#include "nmtools/array/index/flip.hpp"
#include "nmtools/array/view/slice.hpp"
#include "nmtools/array/shape.hpp"
#include "nmtools/array/utility/at.hpp"
#include "nmtools/array/utility/apply_at.hpp"

namespace nmtools::view
{
Expand All @@ -29,6 +27,18 @@ namespace nmtools::view
auto slices = index::flip_slices(a_dim,axis);
return apply_slice(array,slices);
} // flip

template <typename array_t>
constexpr inline auto flipud(const array_t& array)
{
return view::flip(array,meta::ct_v<0>);
} // flipud

template <typename array_t>
constexpr inline auto fliplr(const array_t& array)
{
return view::flip(array,meta::ct_v<1>);
} // fliplr
} // namespace nmtools::view

#endif // NMTOOLS_ARRAY_VIEW_FLIP_HPP
97 changes: 97 additions & 0 deletions include/nmtools/testing/data/array/fliplr.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#ifndef NMTOOLS_TESTING_DATA_ARRAY_FLIPLR_HPP
#define NMTOOLS_TESTING_DATA_ARRAY_FLIPLR_HPP

#include "nmtools/testing/testing.hpp"

NMTOOLS_TESTING_DECLARE_CASE(fliplr)
{
NMTOOLS_TESTING_DECLARE_ARGS(case1)
{
inline int array[2][3] = {
{0,1,2},
{3,4,5},
};
NMTOOLS_CAST_ARRAYS(array)
}
NMTOOLS_TESTING_DECLARE_EXPECT(case1)
{
inline int result[2][3] = {
{2,1,0},
{5,4,3},
};
}

NMTOOLS_TESTING_DECLARE_ARGS(case2)
{
inline int array[2][3][2] = {
{
{0,1},
{2,3},
{4,5},
},
{
{ 6, 7},
{ 8, 9},
{10,11},
}
};
NMTOOLS_CAST_ARRAYS(array)
}
NMTOOLS_TESTING_DECLARE_EXPECT(case2)
{
inline int result[2][3][2] = {
{
{4,5},
{2,3},
{0,1},
},
{
{10,11},
{ 8, 9},
{ 6, 7},
}
};
}

NMTOOLS_TESTING_DECLARE_ARGS(case3)
{
inline int array[2][1][3][2] = {
{
{
{0,1},
{2,3},
{4,5},
}
},
{
{
{ 6, 7},
{ 8, 9},
{10,11},
}
}
};
NMTOOLS_CAST_ARRAYS(array)
}
NMTOOLS_TESTING_DECLARE_EXPECT(case3)
{
inline int result[2][1][3][2] = {
{
{
{0,1},
{2,3},
{4,5},
}
},
{
{
{ 6, 7},
{ 8, 9},
{10,11},
}
}
};
}
}

#endif // NMTOOLS_TESTING_DATA_ARRAY_FLIPLR_HPP
107 changes: 107 additions & 0 deletions include/nmtools/testing/data/array/flipud.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#ifndef NMTOOLS_TESTING_DATA_ARRAY_FLIPUD_HPP
#define NMTOOLS_TESTING_DATA_ARRAY_FLIPUD_HPP

#include "nmtools/testing/testing.hpp"

NMTOOLS_TESTING_DECLARE_CASE(flipud)
{
NMTOOLS_TESTING_DECLARE_ARGS(case1)
{
inline int array[6] = {0,1,2,3,4,5};
NMTOOLS_CAST_ARRAYS(array)
}
NMTOOLS_TESTING_DECLARE_EXPECT(case1)
{
inline int result[6] = {5,4,3,2,1,0};
}

NMTOOLS_TESTING_DECLARE_ARGS(case2)
{
inline int array[2][3] = {
{0,1,2},
{3,4,5},
};
NMTOOLS_CAST_ARRAYS(array)
}
NMTOOLS_TESTING_DECLARE_EXPECT(case2)
{
inline int result[2][3] = {
{3,4,5},
{0,1,2},
};
}

NMTOOLS_TESTING_DECLARE_ARGS(case3)
{
inline int array[2][3][2] = {
{
{0,1},
{2,3},
{4,5},
},
{
{ 6, 7},
{ 8, 9},
{10,11},
}
};
NMTOOLS_CAST_ARRAYS(array)
}
NMTOOLS_TESTING_DECLARE_EXPECT(case3)
{
inline int result[2][3][2] = {
{
{ 6, 7},
{ 8, 9},
{10,11},
},
{
{0,1},
{2,3},
{4,5},
},
};
}

NMTOOLS_TESTING_DECLARE_ARGS(case4)
{
inline int array[2][1][3][2] = {
{
{
{0,1},
{2,3},
{4,5},
}
},
{
{
{ 6, 7},
{ 8, 9},
{10,11},
}
}
};
NMTOOLS_CAST_ARRAYS(array)
}
NMTOOLS_TESTING_DECLARE_EXPECT(case4)
{
inline int result[2][1][3][2] = {
{
{
{ 6, 7},
{ 8, 9},
{10,11},
}
},
{
{
{0,1},
{2,3},
{4,5},
}
},
};
}
}

#endif // NMTOOLS_TESTING_DATA_ARRAY_FLIPUD_HPP
Loading

0 comments on commit 2fef926

Please sign in to comment.