Skip to content

Commit 424d084

Browse files
committed
refactor source files to split the simple algorithm implementations and their respective pipeline adapters into separate files
1 parent 39744f8 commit 424d084

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+1153
-817
lines changed

CMakeLists.txt

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,49 @@ set(SOURCES )
1414
add_executable(unit_tests ${SOURCES}
1515
test/main.cpp
1616
test/meta/test_result.cpp
17-
test/test_all.cpp
18-
test/test_any.cpp
19-
test/test_catenate.cpp
20-
test/test_drop.cpp
21-
test/test_filter.cpp
17+
18+
test/pipeline/test_all.cpp
19+
test/pipeline/test_any.cpp
20+
test/pipeline/test_catenate.cpp
21+
test/pipeline/test_drop.cpp
22+
test/pipeline/test_filter.cpp
23+
test/pipeline/test_gather.cpp
24+
test/pipeline/test_grade.cpp
25+
test/pipeline/test_iota.cpp
26+
test/pipeline/test_map.cpp
27+
test/pipeline/test_none.cpp
28+
test/pipeline/test_reduce.cpp
29+
test/pipeline/test_repeat.cpp
30+
test/pipeline/test_reverse.cpp
31+
test/pipeline/test_rank.cpp
32+
test/pipeline/test_rotate.cpp
33+
test/pipeline/test_scatter.cpp
34+
test/pipeline/test_size.cpp
35+
test/pipeline/test_take.cpp
36+
test/pipeline/test_xpr_pipe.cpp
37+
38+
test/simple/test_all.cpp
39+
test/simple/test_any.cpp
40+
test/simple/test_catenate.cpp
41+
test/simple/test_drop.cpp
42+
test/simple/test_filter.cpp
43+
test/simple/test_gather.cpp
44+
test/simple/test_grade.cpp
45+
test/simple/test_iota.cpp
46+
test/simple/test_map.cpp
47+
test/simple/test_none.cpp
48+
test/simple/test_reduce.cpp
49+
test/simple/test_repeat.cpp
50+
test/simple/test_reverse.cpp
51+
test/simple/test_rank.cpp
52+
test/simple/test_rotate.cpp
53+
test/simple/test_scatter.cpp
54+
test/simple/test_shuffle.cpp
55+
test/simple/test_size.cpp
56+
test/simple/test_take.cpp
57+
test/simple/test_zip.cpp
58+
2259
test/test_functional.cpp
23-
test/test_gather.cpp
24-
test/test_grade.cpp
25-
test/test_iota.cpp
26-
test/test_map.cpp
27-
test/test_none.cpp
28-
test/test_reduce.cpp
29-
test/test_repeat.cpp
30-
test/test_reverse.cpp
31-
test/test_rank.cpp
32-
test/test_rotate.cpp
33-
test/test_scatter.cpp
34-
test/test_shuffle.cpp
35-
test/test_size.cpp
36-
test/test_take.cpp
37-
test/test_xpr_pipe.cpp
38-
test/test_zip.cpp
3960
)
4061
target_link_libraries(unit_tests ${GTEST_LIBRARIES} pthread)
4162

src/_main.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
#include "io/vector_stream.hpp"
44
#include "meta/bind.hpp"
55
#include "meta/global_datatypes.hpp"
6-
#include "catenate.hpp"
7-
#include "drop.hpp"
6+
#include "pipeline/catenate.hpp"
7+
#include "pipeline/drop.hpp"
8+
#include "pipeline/iota.hpp"
9+
#include "pipeline/map.hpp"
10+
#include "pipeline/reverse.hpp"
11+
#include "pipeline/take.hpp"
812
#include "functional.hpp"
9-
#include "iota.hpp"
10-
#include "map.hpp"
11-
#include "reverse.hpp"
12-
#include "take.hpp"
1313

1414
int main()
1515
{

src/pipeline/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Pipeline adapters
2+
3+
This folder contains a set of function objects.
4+
They serve as adapters which allow the simple functions to be used in a pipeline using the bash-style `|` operator.
5+
Due to the various function signatures and function overloads, these adapters are non-trivial and have to be written by hand to achieve a clean syntax for the pipelines.

src/all.hpp renamed to src/pipeline/all.hpp

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,8 @@
33
#include <algorithm>
44
#include <functional>
55
#include <vector>
6-
#include "meta/expression.hpp"
7-
#include "size.hpp"
8-
9-
namespace nv {
10-
11-
template<typename L, typename UnaryPred>
12-
bool all(std::vector<L> const& left, UnaryPred pred)
13-
{
14-
return std::all_of(left.cbegin(), left.cend(), pred);
15-
}
16-
17-
template<typename L, typename BinaryPred, typename R>
18-
bool all(std::vector<L> const& left, BinaryPred pred, std::vector<R> const& right)
19-
{
20-
return std::equal(left.cbegin(), left.cend(), right.cbegin(), right.cend(), pred);
21-
}
22-
23-
}
6+
#include "../meta/expression.hpp"
7+
#include "../simple/all.hpp"
248

259
namespace xpr {
2610

src/any.hpp renamed to src/pipeline/any.hpp

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,9 @@
44
#include <cstdint>
55
#include <functional>
66
#include <vector>
7-
#include "meta/expression.hpp"
7+
#include "../meta/expression.hpp"
88
#include "size.hpp"
9-
10-
namespace nv {
11-
12-
template<typename L, typename UnaryPred>
13-
bool any(std::vector<L> const& left, UnaryPred pred)
14-
{
15-
return std::any_of(left.cbegin(), left.cend(), pred);
16-
}
17-
18-
template<typename L, typename BinaryPred, typename R>
19-
bool any(std::vector<L> const& left, BinaryPred pred, std::vector<R> const& right)
20-
{
21-
int32_t count = std::min(size(left), size(right));
22-
return !std::equal(left.cbegin(), left.cbegin()+count, right.cbegin(), right.cbegin()+count, [pred](auto l, auto r){ return !pred(l, r); });
23-
}
24-
25-
}
9+
#include "../simple/any.hpp"
2610

2711
namespace xpr {
2812

src/pipeline/catenate.hpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#pragma once
2+
3+
#include <algorithm>
4+
#include <vector>
5+
#include "../meta/expression.hpp"
6+
#include "size.hpp"
7+
#include "../simple/catenate.hpp"
8+
9+
namespace xpr {
10+
11+
template <typename T>
12+
struct catenate : Expression<catenate<T>>
13+
{
14+
catenate(T const& right) : _right{right} {}
15+
16+
std::vector<T> operator()(std::vector<T> const& left) const { return nv::catenate(left, _right); }
17+
std::vector<T> operator()(T const& left) const { return nv::catenate(left, _right); }
18+
19+
const T _right;
20+
};
21+
22+
// template specialization for std::vector
23+
template <typename T>
24+
struct catenate<std::vector<T>> : Expression<catenate<std::vector<T>>>
25+
{
26+
catenate(std::vector<T> const& right) : _right{right} {}
27+
28+
std::vector<T> operator()(std::vector<T> const& left) const { return nv::catenate(left, _right); }
29+
std::vector<T> operator()(T const& left) const { return nv::catenate(left, _right); }
30+
31+
const std::vector<T> _right;
32+
};
33+
34+
}

src/pipeline/drop.hpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#pragma once
2+
3+
#include <algorithm>
4+
#include <cassert>
5+
#include <vector>
6+
#include "../meta/expression.hpp"
7+
#include "size.hpp"
8+
#include "../simple/drop.hpp"
9+
10+
namespace xpr {
11+
12+
struct drop : Expression<drop>
13+
{
14+
drop(int count) : _count{count} {}
15+
16+
template<typename T>
17+
std::vector<T> operator()(std::vector<T> const& v) const { return nv::drop(v, _count); }
18+
19+
const int _count;
20+
};
21+
22+
struct drop_last : Expression<drop_last>
23+
{
24+
drop_last(int count) : _count{count} {}
25+
26+
template<typename T>
27+
std::vector<T> operator()(std::vector<T> const& v) const { return nv::drop_last(v, _count); }
28+
29+
const int _count;
30+
31+
};
32+
33+
}

src/filter.hpp renamed to src/pipeline/filter.hpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,9 @@
33
#include <algorithm>
44
#include <iterator>
55
#include <vector>
6-
#include "meta/expression.hpp"
6+
#include "../meta/expression.hpp"
77
#include "size.hpp"
8-
9-
namespace nv {
10-
11-
template<typename T, typename UnaryPred>
12-
std::vector<T> filter(std::vector<T> const& v, UnaryPred op)
13-
{
14-
std::vector<T> result{};
15-
std::copy_if(v.cbegin(), v.cend(), std::back_inserter(result), op);
16-
return result;
17-
}
18-
19-
}
8+
#include "../simple/filter.hpp"
209

2110
namespace xpr {
2211

src/pipeline/gather.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// naive implementation of 'gather' from nvidia thrust library
2+
// https://nvidia.github.io/cccl/thrust/api/groups/group__gathering.html
3+
4+
#pragma once
5+
6+
#include <cassert>
7+
#include <vector>
8+
#include "../meta/expression.hpp"
9+
#include "size.hpp"
10+
#include "../simple/gather.hpp"
11+
12+
namespace xpr {
13+
14+
struct gather : Expression<gather>
15+
{
16+
gather(std::vector<int> const& order) : _order{order} {}
17+
18+
template<typename T>
19+
std::vector<T> operator()(std::vector<T> const& v) const { return nv::gather(v, _order); }
20+
21+
std::vector<int> const _order;
22+
};
23+
24+
}

src/pipeline/grade.hpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// implementation of APL algorithm 'grade'
2+
3+
#pragma once
4+
5+
#include <algorithm>
6+
#include <vector>
7+
#include "../meta/expression.hpp"
8+
#include "iota.hpp"
9+
#include "size.hpp"
10+
#include "../simple/grade.hpp"
11+
12+
namespace xpr {
13+
14+
template <typename BinaryOp>
15+
struct grade : Expression<grade<BinaryOp>>
16+
{
17+
grade(BinaryOp op) : _op{op} {}
18+
19+
template<typename T>
20+
std::vector<int> operator()(std::vector<T> const& v) const { return nv::grade(v, _op); }
21+
22+
BinaryOp const _op;
23+
};
24+
25+
template <typename BinaryOp>
26+
struct stable_grade : Expression<stable_grade<BinaryOp>>
27+
{
28+
stable_grade(BinaryOp op) : _op{op} {}
29+
30+
template<typename T>
31+
std::vector<int> operator()(std::vector<T> const& v) const { return nv::stable_grade(v, _op); }
32+
33+
BinaryOp const _op;
34+
};
35+
36+
}

0 commit comments

Comments
 (0)