/src/brpc/src/bvar/vector.h
Line | Count | Source |
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 | 0 | Vector() { |
37 | 0 | for (size_t i = 0; i < N; ++i) { |
38 | 0 | _data[i] = T(); |
39 | 0 | } |
40 | 0 | } Unexecuted instantiation: bvar::Vector<unsigned int, 2ul>::Vector() Unexecuted instantiation: bvar::Vector<long, 4ul>::Vector() |
41 | | |
42 | 0 | Vector(const T& initial_value) { |
43 | 0 | for (size_t i = 0; i < N; ++i) { |
44 | 0 | _data[i] = initial_value; |
45 | 0 | } |
46 | 0 | } Unexecuted instantiation: bvar::Vector<unsigned int, 2ul>::Vector(unsigned int const&) Unexecuted instantiation: bvar::Vector<long, 4ul>::Vector(long const&) |
47 | | |
48 | 0 | void operator+=(const Vector& rhs) { |
49 | 0 | for (size_t i = 0; i < N; ++i) { |
50 | 0 | _data[i] += rhs._data[i]; |
51 | 0 | } |
52 | 0 | } Unexecuted instantiation: bvar::Vector<long, 4ul>::operator+=(bvar::Vector<long, 4ul> const&) Unexecuted instantiation: bvar::Vector<unsigned int, 2ul>::operator+=(bvar::Vector<unsigned int, 2ul> const&) |
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 | 0 | bool operator==(const Vector& rhs) const { |
75 | 0 | for (size_t i = 0; i < N; ++i) { |
76 | 0 | if (!(_data[i] == rhs._data[i])) { |
77 | 0 | return false; |
78 | 0 | } |
79 | 0 | } |
80 | 0 | return true; |
81 | 0 | } Unexecuted instantiation: bvar::Vector<long, 4ul>::operator==(bvar::Vector<long, 4ul> const&) const Unexecuted instantiation: bvar::Vector<unsigned int, 2ul>::operator==(bvar::Vector<unsigned int, 2ul> const&) const |
82 | | |
83 | | bool operator!=(const Vector& rhs) const { |
84 | | return !operator==(rhs); |
85 | | } |
86 | | |
87 | 0 | T& operator[](int index) { return _data[index]; }Unexecuted instantiation: bvar::Vector<long, 4ul>::operator[](int) Unexecuted instantiation: bvar::Vector<unsigned int, 2ul>::operator[](int) |
88 | 0 | const T& operator[](int index) const { return _data[index]; }Unexecuted instantiation: bvar::Vector<long, 4ul>::operator[](int) const Unexecuted instantiation: bvar::Vector<unsigned int, 2ul>::operator[](int) const |
89 | | |
90 | | private: |
91 | | T _data[N]; |
92 | | }; |
93 | | |
94 | | template <typename T, size_t N> |
95 | 0 | std::ostream& operator<<(std::ostream& os, const Vector<T, N>& vec) { |
96 | 0 | if (FLAGS_quote_vector) { |
97 | 0 | os << '"'; |
98 | 0 | } |
99 | 0 | os << '['; |
100 | 0 | if (N != 0) { |
101 | 0 | os << vec[0]; |
102 | 0 | for (size_t i = 1; i < N; ++i) { |
103 | 0 | os << ',' << vec[i]; |
104 | 0 | } |
105 | 0 | } |
106 | 0 | os << ']'; |
107 | 0 | if (FLAGS_quote_vector) { |
108 | 0 | os << '"'; |
109 | 0 | } |
110 | 0 | return os; |
111 | 0 | } Unexecuted instantiation: std::basic_ostream<char, std::char_traits<char> >& bvar::operator<< <long, 4ul>(std::basic_ostream<char, std::char_traits<char> >&, bvar::Vector<long, 4ul> const&) Unexecuted instantiation: std::basic_ostream<char, std::char_traits<char> >& bvar::operator<< <unsigned int, 2ul>(std::basic_ostream<char, std::char_traits<char> >&, bvar::Vector<unsigned int, 2ul> const&) |
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 |