Skip to content

Commit 8121800

Browse files
committed
simplify the functional header to contain the function objects only
1 parent 3d542ea commit 8121800

File tree

7 files changed

+9
-121
lines changed

7 files changed

+9
-121
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ set(CMAKE_CXX_STANDARD 17)
88
find_package(GTest REQUIRED)
99
include_directories(${GTEST_INCLUDE_DIRS})
1010

11-
set(SOURCES src/functional.cpp)
11+
set(SOURCES )
1212

1313
# Link unit_tests with what we want to test and the GTest and pthread library
1414
add_executable(unit_tests ${SOURCES}

src/_main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ int main()
1818
vi32 v = vi32{1, 2, 4, 8, 16} | take(3) | drop(1) | reverse();
1919
std::cout << " The answer: " << v << std::endl; // ( 4 2 )
2020

21-
vi32 squared_values = iota(5) | map(power() <<= 2);
21+
vi32 squared_values = iota(5) | map(nv::Power{} <<= 2);
2222
std::cout << "Squared values: " << squared_values << std::endl; // ( 0 1 4 9 16 )
2323

2424
// create a palindrome from those squared numbers

src/functional.cpp

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/functional.hpp

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -41,64 +41,3 @@ struct GreatestCommonDivisor { template <typename T> T operator()(T lhs, T rhs)
4141
// note that OR is a specialization of GCD for booleans
4242

4343
}
44-
45-
namespace xpr {
46-
47-
// functions which retrive the function object, see cpp file for implementation
48-
49-
// unary
50-
nv::LogicalNot logical_not();
51-
nv::Identity identity();
52-
nv::Negate negate();
53-
54-
// binary
55-
nv::LogicalAnd logical_and();
56-
nv::LogicalOr logical_or();
57-
nv::Plus plus();
58-
nv::Minus minus();
59-
nv::Multiply multipy();
60-
nv::Divide divide();
61-
nv::Modulo modulo();
62-
nv::Power power();
63-
nv::Minimum minimum();
64-
nv::Maximum maximum();
65-
nv::Equal equal() ;
66-
nv::NotEqual not_equal();
67-
nv::Less less();
68-
nv::LessEqual less_equal();
69-
nv::Greater greater();
70-
nv::GreaterEqual greater_equal();
71-
nv::LeastCommonMultiple least_common_multiple();
72-
nv::GreatestCommonDivisor greatest_common_divisor();
73-
74-
// functions which retrieve and execute the function object
75-
76-
// unary
77-
template <typename T> auto logical_not(T const& t) { return nv::LogicalNot{}(t); }
78-
template <typename T> auto identity(T const& t) { return nv::Identity{}(t); }
79-
template <typename T> auto negate(T const& t) { return nv::Negate{}(t); }
80-
81-
// binary
82-
template <typename T> auto logical_and(T const& lhs, T const& rhs) { return nv::LogicalAnd{}(lhs, rhs); }
83-
template <typename T> auto logical_or(T const& lhs, T const& rhs) { return nv::LogicalOr{}(lhs, rhs); }
84-
template <typename T> auto plus(T const& lhs, T const& rhs) { return nv::Plus{}(lhs, rhs); }
85-
template <typename T> auto minus(T const& lhs, T const& rhs) { return nv::Minus{}(lhs, rhs); }
86-
template <typename T> auto multipy(T const& lhs, T const& rhs) { return nv::Multiply{}(lhs, rhs); }
87-
template <typename T> auto divide(T const& lhs, T const& rhs) { return nv::Divide{}(lhs, rhs); }
88-
template <typename T> auto modulo(T const& lhs, T const& rhs) { return nv::Modulo{}(lhs, rhs); }
89-
template <typename T> auto power(T const& lhs, T const& rhs) { return nv::Power{}(lhs, rhs); }
90-
template <typename T> auto minimum(T const& lhs, T const& rhs) { return nv::Minimum{}(lhs, rhs); }
91-
template <typename T> auto maximum(T const& lhs, T const& rhs) { return nv::Maximum{}(lhs, rhs); }
92-
template <typename T> auto equal(T const& lhs, T const& rhs) { return nv::Equal{}(lhs, rhs); }
93-
template <typename T> auto not_equal(T const& lhs, T const& rhs) { return nv::NotEqual{}(lhs, rhs); }
94-
template <typename T> auto less(T const& lhs, T const& rhs) { return nv::Less{}(lhs, rhs); }
95-
template <typename T> auto less_equal(T const& lhs, T const& rhs) { return nv::LessEqual{}(lhs, rhs); }
96-
template <typename T> auto greater(T const& lhs, T const& rhs) { return nv::Greater{}(lhs, rhs); }
97-
template <typename T> auto greater_equal(T const& lhs, T const& rhs) { return nv::GreaterEqual{}(lhs, rhs); }
98-
template <typename T> auto least_common_multiple(T const& lhs, T const& rhs) { return nv::LeastCommonMultiple{}(lhs, rhs); }
99-
template <typename T> auto greatest_common_divisor(T const& lhs, T const& rhs) { return nv::GreatestCommonDivisor{}(lhs, rhs); }
100-
101-
}
102-
103-
// Unfortunately 'not' 'and' 'or' are reserved keywords in C++, so I cannot create functions with that name.
104-
// This led me to introduce the postfix '_l' to all of the names.

test/test_functional.cpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,154 +7,132 @@ TEST(functional, logical_not)
77
{
88
const auto f = nv::LogicalNot{};
99
EXPECT_EQ(f(false), true);
10-
EXPECT_EQ(xpr::logical_not(false), true);
1110
}
1211

1312
TEST(functional, logical_and)
1413
{
1514
const auto f = nv::LogicalAnd{};
1615
EXPECT_EQ(f(false, true), false);
17-
EXPECT_EQ(xpr::logical_and(false, true), false);
1816
}
1917

2018
TEST(functional, logical_or)
2119
{
2220
const auto f = nv::LogicalOr{};
2321
EXPECT_EQ(f(false, true), true);
24-
EXPECT_EQ(xpr::logical_or(false, true), true);
2522
}
2623

2724
TEST(functional, identity)
2825
{
2926
const auto f = nv::Identity{};
3027
EXPECT_EQ(f(5), 5);
31-
EXPECT_EQ(xpr::identity(5), 5);
3228
}
3329

3430
TEST(functional, negate)
3531
{
3632
const auto f = nv::Negate{};
3733
EXPECT_EQ(f(5), -5);
38-
EXPECT_EQ(xpr::negate(5), -5);
3934
}
4035

4136
TEST(functional, plus)
4237
{
4338
const auto f = nv::Plus{};
4439
EXPECT_EQ(f(4, 6), 10);
45-
EXPECT_EQ(xpr::plus(4, 6), 10);
4640
}
4741

4842
TEST(functional, minus)
4943
{
5044
const auto f = nv::Minus{};
5145
EXPECT_EQ(f(4, 6), -2);
52-
EXPECT_EQ(xpr::minus(4, 6), -2);
5346
}
5447

5548
TEST(functional, times)
5649
{
5750
const auto f = nv::Multiply{};
5851
EXPECT_EQ(f(4, 6), 24);
59-
EXPECT_EQ(xpr::multipy(4, 6), 24);
6052
}
6153

6254
TEST(functional, divided)
6355
{
6456
const auto f = nv::Divide{};
6557
EXPECT_EQ(f(12, 6), 2);
66-
EXPECT_EQ(xpr::divide(12, 6), 2);
6758
}
6859

6960
TEST(functional, modulo)
7061
{
7162
const auto f = nv::Modulo{};
7263
EXPECT_EQ(f(12, 5), 2);
73-
EXPECT_EQ(xpr::modulo(12, 5), 2);
7464
}
7565

7666
TEST(functional, power)
7767
{
7868
const auto f = nv::Power{};
7969
EXPECT_EQ(f(2, 3), 8);
80-
EXPECT_EQ(xpr::power(2, 3), 8);
8170
}
8271

8372
TEST(functional, minimum)
8473
{
8574
const auto f = nv::Minimum{};
8675
EXPECT_EQ(f(4, 6), 4);
87-
EXPECT_EQ(xpr::minimum(4, 6), 4);
8876
}
8977

9078
TEST(functional, maximum)
9179
{
9280
const auto f = nv::Maximum{};
9381
EXPECT_EQ(f(4, 6), 6);
94-
EXPECT_EQ(xpr::maximum(4, 6), 6);
9582
}
9683

9784
TEST(functional, equal)
9885
{
9986
const auto f = nv::Equal{};
10087
EXPECT_EQ(f(4, 6), false);
101-
EXPECT_EQ(xpr::equal(4, 6), false);
10288
}
10389

10490
TEST(functional, unequal)
10591
{
10692
const auto f = nv::NotEqual{};
10793
EXPECT_EQ(f(4, 6), true);
108-
EXPECT_EQ(xpr::not_equal(4, 6), true);
10994
}
11095

11196
TEST(functional, less)
11297
{
11398
const auto f = nv::Less{};
11499
EXPECT_EQ(f(4, 6), true);
115-
EXPECT_EQ(xpr::less(4, 6), true);
116100
}
117101

118102
TEST(functional, less_equal)
119103
{
120104
const auto f = nv::LessEqual{};
121105
EXPECT_EQ(f(4, 6), true);
122-
EXPECT_EQ(xpr::less_equal(4, 6), true);
123106
}
124107

125108
TEST(functional, greater)
126109
{
127110
const auto f = nv::Greater{};
128111
EXPECT_EQ(f(4, 6), false);
129-
EXPECT_EQ(xpr::greater(4, 6), false);
130112
}
131113

132114
TEST(functional, greater_equal)
133115
{
134116
const auto f = nv::GreaterEqual{};
135117
EXPECT_EQ(f(4, 6), false);
136-
EXPECT_EQ(xpr::greater_equal(4, 6), false);
137118
}
138119

139120
TEST(functional, least_common_multiple)
140121
{
141122
const auto f = nv::LeastCommonMultiple{};
142123
EXPECT_EQ(f(4, 6), 12);
143-
EXPECT_EQ(xpr::least_common_multiple(4, 6), 12);
144124
}
145125

146126
TEST(functional, greatest_common_divisor)
147127
{
148128
const auto f = nv::GreatestCommonDivisor{};
149129
EXPECT_EQ(f(4, 6), 2);
150-
EXPECT_EQ(xpr::greatest_common_divisor(4, 6), 2);
151130
}
152131

153132
// TEST(functional, foooo)
154133
// {
155134
// const auto f = nv::foooo{};
156135
// EXPECT_EQ(f(4, 6), 12);
157-
// EXPECT_EQ(xpr::foooo(4, 6), 12);
158136
// }
159137

160138
}

test/test_map.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ TEST(map, xpr_vector)
101101

102102
TEST(map, xpr_scalar_scalar)
103103
{
104-
i32 result = 2 | xpr::map(xpr::plus(), 3);
104+
i32 result = 2 | xpr::map(nv::Plus{}, 3);
105105
EXPECT_EQ(result, 5);
106106
}
107107

108108
TEST(map, xpr_scalar_vector)
109109
{
110-
vi32 result = 10 | xpr::map(xpr::plus(), xpr::iota(3));
110+
vi32 result = 10 | xpr::map(nv::Plus{}, xpr::iota(3));
111111

112112
ASSERT_EQ(size(result), 3);
113113
EXPECT_EQ(result[0], 10);
@@ -117,7 +117,7 @@ TEST(map, xpr_scalar_vector)
117117

118118
TEST(map, xpr_vector_scalar)
119119
{
120-
vi32 result = xpr::iota(3) | xpr::map(xpr::plus(), 10);
120+
vi32 result = xpr::iota(3) | xpr::map(nv::Plus{}, 10);
121121

122122
ASSERT_EQ(size(result), 3);
123123
EXPECT_EQ(result[0], 10);
@@ -127,7 +127,7 @@ TEST(map, xpr_vector_scalar)
127127

128128
TEST(map, xpr_vector_vector)
129129
{
130-
vi32 result = xpr::iota(3) | xpr::map(xpr::plus(), nv::reverse(nv::iota(3)));
130+
vi32 result = xpr::iota(3) | xpr::map(nv::Plus{}, nv::reverse(nv::iota(3)));
131131

132132
ASSERT_EQ(size(result), 3);
133133
EXPECT_EQ(result[0], 2);

test/test_reduce.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ TEST(reduce, five)
1818

1919
TEST(reduce, xpr_add)
2020
{
21-
i32 result = vi32{1, 2, 4, 8, 16} | xpr::reduce(xpr::plus(), 0);
21+
i32 result = vi32{1, 2, 4, 8, 16} | xpr::reduce(nv::Plus{}, 0);
2222
EXPECT_EQ(result, 31);
2323
}
2424

2525
TEST(reduce, xpr_mul)
2626
{
27-
i32 result = vi32{1, 2, 4} | xpr::reduce(xpr::multipy(), 1);
27+
i32 result = vi32{1, 2, 4} | xpr::reduce(nv::Multiply{}, 1);
2828
EXPECT_EQ(result, 8);
2929
}
3030

3131
TEST(reduce, xpr_default_init)
3232
{
33-
i32 result = vi32{1, 2, 4} | xpr::reduce(xpr::multipy());
33+
i32 result = vi32{1, 2, 4} | xpr::reduce(nv::Multiply{});
3434
EXPECT_EQ(result, 8);
3535
}
3636

0 commit comments

Comments
 (0)