C++11 Math Library
リビジョン | 054eee66529b18c62836c36c85bc3dfcdca85385 (tree) |
---|---|
日時 | 2013-07-03 02:03:14 |
作者 | Daisuke Yamashta <yamasdais@gmai...> |
コミッター | Daisuke Yamashta |
Vector クラス、色々と見直し。
Google Unit Test を組み込んだ。
@@ -8,42 +8,91 @@ | ||
8 | 8 | #include <cstddef> |
9 | 9 | #include <array> |
10 | 10 | #include <vector> |
11 | +#include <algorithm> | |
11 | 12 | |
12 | 13 | namespace dmath { |
13 | 14 | typedef std::size_t size_type; |
14 | 15 | |
15 | 16 | template <typename N_TYPE, |
16 | - size_type LEN=0ul> | |
17 | + size_type LEN = 0ul> | |
17 | 18 | class Vector { |
18 | 19 | public: |
19 | - struct Traits; | |
20 | + enum Size : size_type { | |
21 | + length = LEN, | |
22 | + }; | |
23 | + typedef N_TYPE value_type; | |
24 | + typedef std::array<N_TYPE, Size::length> node_type; | |
25 | + typedef typename node_type::reference reference; | |
26 | + typedef typename node_type::const_reference const_reference; | |
27 | + typedef typename node_type::iterator iterator; | |
28 | + typedef typename node_type::const_iterator const_iterator; | |
20 | 29 | |
21 | 30 | private: |
22 | - typename Traits::node_type nodes; | |
31 | + node_type nodes; | |
23 | 32 | |
24 | 33 | public: |
25 | - struct Traits { | |
26 | - enum Size : size_type { | |
27 | - length = LEN, | |
28 | - }; | |
29 | - typedef N_TYPE element_type; | |
30 | - typedef std::array<N_TYPE, Size::length> node_type; | |
31 | - }; | |
32 | - | |
33 | 34 | explicit Vector() |
34 | 35 | : nodes() |
35 | 36 | { |
36 | 37 | } |
37 | 38 | |
38 | - explicit Vector(std::initializer_list<N_TYPE> arg) | |
39 | - : nodes(arg) | |
39 | + Vector(const node_type& ary) | |
40 | + : nodes(ary) | |
41 | + { | |
42 | + } | |
43 | + | |
44 | + Vector(std::initializer_list<N_TYPE> arg) | |
45 | + : nodes() | |
40 | 46 | { |
47 | + auto dst = nodes.begin(); | |
48 | + for (auto n : arg) { | |
49 | + if (dst == nodes.end()) { | |
50 | + break; | |
51 | + } | |
52 | + *dst++ = n; | |
53 | + } | |
41 | 54 | } |
42 | 55 | |
43 | 56 | constexpr size_type size() const noexcept { |
44 | - return Traits::Size::length; | |
57 | + return Size::length; | |
45 | 58 | } |
46 | 59 | |
60 | + value_type | |
61 | + at(size_type i) | |
62 | + noexcept(noexcept(nodes.at(i))) { | |
63 | + return nodes.at(i); | |
64 | + } | |
65 | + | |
66 | + reference | |
67 | + operator[](size_type i) { | |
68 | + return nodes[i]; | |
69 | + } | |
70 | + const_reference | |
71 | + operator[](size_type i) | |
72 | + const noexcept { | |
73 | + return nodes[i]; | |
74 | + } | |
75 | + | |
76 | + inline iterator | |
77 | + begin() noexcept(noexcept(iterator(nodes.begin()))) { | |
78 | + return iterator(nodes.begin()); | |
79 | + } | |
80 | + | |
81 | + inline const_iterator | |
82 | + begin() const noexcept(noexcept(const_iterator(nodes.begin()))) { | |
83 | + return const_iterator(nodes.begin()); | |
84 | + } | |
85 | + | |
86 | + inline iterator | |
87 | + end() noexcept(noexcept(iterator(nodes.end()))) { | |
88 | + return iterator(nodes.end()); | |
89 | + } | |
90 | + | |
91 | + inline const_iterator | |
92 | + end() const noexcept(noexcept(const_iterator(nodes.end()))) { | |
93 | + return const_iterator(nodes.end()); | |
94 | + } | |
95 | + | |
47 | 96 | void checker() { |
48 | 97 | std::cout << "Base Vector" << std::endl; |
49 | 98 | } |
@@ -53,27 +102,27 @@ template <typename N_TYPE> | ||
53 | 102 | class Vector<N_TYPE, 0ul> { |
54 | 103 | public: |
55 | 104 | struct Traits; |
105 | + typedef N_TYPE value_type; | |
106 | + typedef std::vector<N_TYPE> node_type; | |
107 | + typedef value_type& reference; | |
108 | + typedef const value_type& const_reference; | |
56 | 109 | |
57 | 110 | private: |
58 | - typename Traits::node_type nodes; | |
111 | + node_type nodes; | |
59 | 112 | |
60 | 113 | public: |
61 | 114 | struct Traits { |
62 | 115 | enum Size : size_type { |
63 | 116 | length = 0, |
64 | 117 | }; |
65 | - typedef N_TYPE element_type; | |
66 | - typedef element_type& reference; | |
67 | - typedef const element_type& const_reference; | |
68 | - typedef typename std::vector<N_TYPE> node_type; | |
69 | 118 | }; |
70 | 119 | void checker() { |
71 | 120 | std::cout << "Variant Vector" << std::endl; |
72 | 121 | } |
73 | 122 | |
74 | 123 | explicit Vector(size_type size, |
75 | - typename Traits::const_reference val = N_TYPE()) | |
76 | - : nodes(size) | |
124 | + const_reference val = N_TYPE()) | |
125 | + : nodes(size, val) | |
77 | 126 | { |
78 | 127 | } |
79 | 128 |
@@ -1,9 +1,16 @@ | ||
1 | +GTEST_DIR=/usr/src/gtest | |
2 | +USER_DIR = ./ | |
1 | 3 | CPLUSPLUS=g++ |
2 | -TARGETS=test0 | |
3 | -CPLUSPLUSFLAGS=-O -std=c++11 | |
4 | -INCLUDES=-I../ | |
4 | +CPLUSPLUSFLAGS=-O -std=c++11 -Wall -Wextra -g | |
5 | +INCLUDES=-I../ -I$/usr/include/gtest | |
5 | 6 | LDFLAGS= |
6 | 7 | |
8 | +TESTS = test0 utest_vector0 | |
9 | +TARGETS=$(TESTS) | |
10 | + | |
11 | +GTEST_HEADERS = /usr/include/gtest/*.h \ | |
12 | + /usr/include/gtest/internal/*.h | |
13 | + | |
7 | 14 | .SUFFIXES: .cc |
8 | 15 | |
9 | 16 | .cc.o: |
@@ -18,6 +25,25 @@ test0 : test0.o | ||
18 | 25 | $(CPLUSPLUS) $(LDFLAGS) -o $@ test0.o |
19 | 26 | |
20 | 27 | clean : |
21 | - /bin/rm -f $(TARGETS) *.o *.s | |
28 | + /bin/rm -f $(TARGETS) *.o *.s *.a | |
22 | 29 | |
23 | 30 | test0.o : ../dmath.hh |
31 | + | |
32 | +gtest-all.o : $(GTEST_SRCS_) | |
33 | + $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \ | |
34 | + $(GTEST_DIR)/src/gtest-all.cc | |
35 | + | |
36 | +gtest_main.o : $(GTEST_SRCS_) | |
37 | + $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \ | |
38 | + $(GTEST_DIR)/src/gtest_main.cc | |
39 | + | |
40 | +gtest.a : gtest-all.o | |
41 | + $(AR) $(ARFLAGS) $@ $^ | |
42 | + | |
43 | +gtest_main.a : gtest-all.o gtest_main.o | |
44 | + $(AR) $(ARFLAGS) $@ $^ | |
45 | + | |
46 | +utest_vector0 : utest_vector0.o gtest_main.a | |
47 | + $(CPLUSPLUS) $(CPLUSPLUSFLAGS) $^ -o $@ -lpthread | |
48 | + | |
49 | +utest_vector0.cc : ../dmath.hh |
@@ -1,4 +1,6 @@ | ||
1 | 1 | #include <iostream> |
2 | +#include <algorithm> | |
3 | +#include <array> | |
2 | 4 | |
3 | 5 | #include <dmath.hh> |
4 | 6 |
@@ -6,14 +8,23 @@ using namespace std; | ||
6 | 8 | using namespace dmath; |
7 | 9 | |
8 | 10 | int test_vector() { |
9 | - Vector<double, 3ul> basevec; | |
11 | + std::array<double, 3> ary{ {4.24, 35.5 } }; | |
12 | + Vector<double, 3> vec0{ary}; | |
13 | + Vector<double, 3> basevec{3.24, 35.5, 34.5, 23456.345 }; | |
10 | 14 | basevec.checker(); |
15 | + cout << "at(0): " << vec0.at(0) << endl; | |
16 | + for_each(basevec.begin(), basevec.end(), [](double val) { | |
17 | + cout << val << ","; | |
18 | + }); | |
19 | + cout << endl; | |
11 | 20 | |
12 | 21 | Vector<double> defaultvec{ 3, 23.5, 3.141592 }; |
13 | 22 | defaultvec.checker(); |
14 | 23 | |
15 | - Vector<double, 0ul> varvec(4); | |
16 | - varvec.checker(); | |
24 | +// Vector<double, 0ul> varvec(4); | |
25 | +// varvec.checker(); | |
26 | + | |
27 | + return 0; | |
17 | 28 | } |
18 | 29 | |
19 | 30 | int testfunc() { |
@@ -21,6 +32,8 @@ int testfunc() { | ||
21 | 32 | CurMatrix mat; |
22 | 33 | |
23 | 34 | cout << "column size: " << mat.column_size() << endl; |
35 | + | |
36 | + return 0; | |
24 | 37 | } |
25 | 38 | |
26 | 39 | int main(int argc, char* argv[]) |
@@ -0,0 +1,42 @@ | ||
1 | +#include <climits> | |
2 | +#include <iostream> | |
3 | +#include <algorithm> | |
4 | +#include <array> | |
5 | + | |
6 | +#include <dmath.hh> | |
7 | + | |
8 | +#include <gtest/gtest.h> | |
9 | + | |
10 | +using namespace std; | |
11 | +using namespace dmath; | |
12 | + | |
13 | +TEST(VectorTest, Base_NodeOperation) { | |
14 | + using VecType = Vector<int, 24>; | |
15 | + VecType v0; | |
16 | + for_each(begin(v0), end(v0), [](int v) { | |
17 | + EXPECT_EQ(0, v); | |
18 | + }); | |
19 | + array<int, 24> ary0{ | |
20 | + { 3, 4, 5, 6, 7, 8, 9, 10, | |
21 | + -24, -100, -124, 34, 4, 235, 795, 34, | |
22 | + 426, 94, 86, -3, 8, 8264, -82934, 234 } | |
23 | + }; | |
24 | + auto ary_iter = ary0.begin(); | |
25 | + for_each(begin(v0), end(v0), [&ary_iter](int& v) { | |
26 | + v = *ary_iter++; | |
27 | + }); | |
28 | + EXPECT_EQ(24, v0.size()); | |
29 | + EXPECT_EQ(24, VecType::Size::length); | |
30 | + for (size_t i = 0; i < v0.size(); i++) { | |
31 | + EXPECT_EQ(ary0[i], v0[i]); | |
32 | + EXPECT_EQ(ary0[i], v0.at(i)); | |
33 | + } | |
34 | +} | |
35 | + | |
36 | +TEST(VectorTest, Variant_NodeOperation) { | |
37 | + using VecType = Vector<int>; | |
38 | + VecType v0(24ul); | |
39 | +// for_each(begin(v0), end(v0), [](int v) { | |
40 | +// EXPECT_EQ(0, v); | |
41 | +// }); | |
42 | +} |