1 | // Licensed to the Apache Software Foundation (ASF) under one |
2 | // or more contributor license agreements. See the NOTICE file |
3 | // distributed with this work for additional information |
4 | // regarding copyright ownership. The ASF licenses this file |
5 | // to you under the Apache License, Version 2.0 (the |
6 | // "License"); you may not use this file except in compliance |
7 | // with the License. You may obtain a copy of the License at |
8 | // |
9 | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | // |
11 | // Unless required by applicable law or agreed to in writing, |
12 | // software distributed under the License is distributed on an |
13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | // KIND, either express or implied. See the License for the |
15 | // specific language governing permissions and limitations |
16 | // under the License. |
17 | |
18 | // Date: Sun Sep 20 12:25:11 CST 2015 |
19 | |
20 | #ifndef BVAR_VECTOR_H |
21 | #define BVAR_VECTOR_H |
22 | |
23 | #include <ostream> |
24 | #include <gflags/gflags_declare.h> |
25 | |
26 | namespace bvar { |
27 | |
28 | DECLARE_bool(quote_vector); |
29 | |
30 | // Data inside a Vector will be plotted in a same graph. |
31 | template <typename T, size_t N> |
32 | class Vector { |
33 | public: |
34 | static const size_t WIDTH = N; |
35 | |
36 | Vector() { |
37 | for (size_t i = 0; i < N; ++i) { |
38 | _data[i] = T(); |
39 | } |
40 | } |
41 | |
42 | Vector(const T& initial_value) { |
43 | for (size_t i = 0; i < N; ++i) { |
44 | _data[i] = initial_value; |
45 | } |
46 | } |
47 | |
48 | void operator+=(const Vector& rhs) { |
49 | for (size_t i = 0; i < N; ++i) { |
50 | _data[i] += rhs._data[i]; |
51 | } |
52 | } |
53 | |
54 | void operator-=(const Vector& rhs) { |
55 | for (size_t i = 0; i < N; ++i) { |
56 | _data[i] -= rhs._data[i]; |
57 | } |
58 | } |
59 | |
60 | template <typename S> |
61 | void operator*=(const S& scalar) { |
62 | for (size_t i = 0; i < N; ++i) { |
63 | _data[i] *= scalar; |
64 | } |
65 | } |
66 | |
67 | template <typename S> |
68 | void operator/=(const S& scalar) { |
69 | for (size_t i = 0; i < N; ++i) { |
70 | _data[i] /= scalar; |
71 | } |
72 | } |
73 | |
74 | bool operator==(const Vector& rhs) const { |
75 | for (size_t i = 0; i < N; ++i) { |
76 | if (!(_data[i] == rhs._data[i])) { |
77 | return false; |
78 | } |
79 | } |
80 | return true; |
81 | } |
82 | |
83 | bool operator!=(const Vector& rhs) const { |
84 | return !operator==(rhs); |
85 | } |
86 | |
87 | T& operator[](int index) { return _data[index]; } |
88 | const T& operator[](int index) const { return _data[index]; } |
89 | |
90 | private: |
91 | T _data[N]; |
92 | }; |
93 | |
94 | template <typename T, size_t N> |
95 | std::ostream& operator<<(std::ostream& os, const Vector<T, N>& vec) { |
96 | if (FLAGS_quote_vector) { |
97 | os << '"'; |
98 | } |
99 | os << '['; |
100 | if (N != 0) { |
101 | os << vec[0]; |
102 | for (size_t i = 1; i < N; ++i) { |
103 | os << ',' << vec[i]; |
104 | } |
105 | } |
106 | os << ']'; |
107 | if (FLAGS_quote_vector) { |
108 | os << '"'; |
109 | } |
110 | return os; |
111 | } |
112 | |
113 | template <typename T> |
114 | struct is_vector : public butil::false_type {}; |
115 | |
116 | template <typename T, size_t N> |
117 | struct is_vector<Vector<T,N> > : public butil::true_type {}; |
118 | |
119 | } // namespace bvar |
120 | |
121 | #endif //BVAR_VECTOR_H |
122 | |