• R/O
  • HTTP
  • SSH
  • HTTPS

コミット

よく使われているワード(クリックで追加)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

C++11 Math Library


コミットメタ情報

リビジョン054eee66529b18c62836c36c85bc3dfcdca85385 (tree)
日時2013-07-03 02:03:14
作者Daisuke Yamashta <yamasdais@gmai...>
コミッターDaisuke Yamashta

ログメッセージ

Vector クラス、色々と見直し。
Google Unit Test を組み込んだ。

変更サマリ

差分

--- a/dmath.hh
+++ b/dmath.hh
@@ -8,42 +8,91 @@
88 #include <cstddef>
99 #include <array>
1010 #include <vector>
11+#include <algorithm>
1112
1213 namespace dmath {
1314 typedef std::size_t size_type;
1415
1516 template <typename N_TYPE,
16- size_type LEN=0ul>
17+ size_type LEN = 0ul>
1718 class Vector {
1819 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;
2029
2130 private:
22- typename Traits::node_type nodes;
31+ node_type nodes;
2332
2433 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-
3334 explicit Vector()
3435 : nodes()
3536 {
3637 }
3738
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()
4046 {
47+ auto dst = nodes.begin();
48+ for (auto n : arg) {
49+ if (dst == nodes.end()) {
50+ break;
51+ }
52+ *dst++ = n;
53+ }
4154 }
4255
4356 constexpr size_type size() const noexcept {
44- return Traits::Size::length;
57+ return Size::length;
4558 }
4659
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+
4796 void checker() {
4897 std::cout << "Base Vector" << std::endl;
4998 }
@@ -53,27 +102,27 @@ template <typename N_TYPE>
53102 class Vector<N_TYPE, 0ul> {
54103 public:
55104 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;
56109
57110 private:
58- typename Traits::node_type nodes;
111+ node_type nodes;
59112
60113 public:
61114 struct Traits {
62115 enum Size : size_type {
63116 length = 0,
64117 };
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;
69118 };
70119 void checker() {
71120 std::cout << "Variant Vector" << std::endl;
72121 }
73122
74123 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)
77126 {
78127 }
79128
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -1,9 +1,16 @@
1+GTEST_DIR=/usr/src/gtest
2+USER_DIR = ./
13 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
56 LDFLAGS=
67
8+TESTS = test0 utest_vector0
9+TARGETS=$(TESTS)
10+
11+GTEST_HEADERS = /usr/include/gtest/*.h \
12+ /usr/include/gtest/internal/*.h
13+
714 .SUFFIXES: .cc
815
916 .cc.o:
@@ -18,6 +25,25 @@ test0 : test0.o
1825 $(CPLUSPLUS) $(LDFLAGS) -o $@ test0.o
1926
2027 clean :
21- /bin/rm -f $(TARGETS) *.o *.s
28+ /bin/rm -f $(TARGETS) *.o *.s *.a
2229
2330 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
--- a/tests/test0.cc
+++ b/tests/test0.cc
@@ -1,4 +1,6 @@
11 #include <iostream>
2+#include <algorithm>
3+#include <array>
24
35 #include <dmath.hh>
46
@@ -6,14 +8,23 @@ using namespace std;
68 using namespace dmath;
79
810 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 };
1014 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;
1120
1221 Vector<double> defaultvec{ 3, 23.5, 3.141592 };
1322 defaultvec.checker();
1423
15- Vector<double, 0ul> varvec(4);
16- varvec.checker();
24+// Vector<double, 0ul> varvec(4);
25+// varvec.checker();
26+
27+ return 0;
1728 }
1829
1930 int testfunc() {
@@ -21,6 +32,8 @@ int testfunc() {
2132 CurMatrix mat;
2233
2334 cout << "column size: " << mat.column_size() << endl;
35+
36+ return 0;
2437 }
2538
2639 int main(int argc, char* argv[])
--- /dev/null
+++ b/tests/utest_vector0.cc
@@ -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+}