antioch-0.4.0
vector_utils.h
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // Antioch - A Gas Dynamics Thermochemistry Library
5 //
6 // Copyright (C) 2014-2016 Paul T. Bauman, Benjamin S. Kirk,
7 // Sylvain Plessis, Roy H. Stonger
8 //
9 // Copyright (C) 2013 The PECOS Development Team
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the Version 2.1 GNU Lesser General
13 // Public License as published by the Free Software Foundation.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
23 // Boston, MA 02110-1301 USA
24 //
25 //-----------------------------------------------------------------------el-
26 
27 #ifndef ANTIOCH_VECTOR_UTILS_H
28 #define ANTIOCH_VECTOR_UTILS_H
29 
30 #ifdef ANTIOCH_METAPROGRAMMING_H
31 # ifndef ANTIOCH_VECTOR_UTILS_DECL_H
32 # error vector_utils_decl.h must be included before metaprogramming.h
33 # endif
34 #endif
35 
36 // Antioch
38 
39 // C++
40 #include <cmath>
41 #include <iostream>
42 #include <vector>
43 
44 // Add some overloads
45 
46 namespace std
47 {
48 
49 template <typename T>
50 inline
51 std::ostream&
52 operator<< (std::ostream& output, const std::vector<T>& a)
53 {
54  output << '{';
55  const std::size_t size = a.size();
56  if (size)
57  output << a[0];
58  for (std::size_t i=1; i<size; ++i)
59  output << ',' << a[i];
60  output << '}';
61  return output;
62 }
63 
64 template <typename T>
65 inline
66 std::vector<T>
67 operator* (const std::vector<T>& src, const T & mul)
68 {
69  std::vector<T> output(src);
70  const std::size_t size = src.size();
71  for (std::size_t i=1; i<size; ++i)
72  output[i] = src[i] * mul;
73 
74  return output;
75 }
76 
77 template <typename T>
78 inline
79 std::vector<T>
80 operator/ (const std::vector<T>& src, const T & mul)
81 {
82  std::vector<T> output(src);
83  const std::size_t size = src.size();
84  for (std::size_t i=1; i<size; ++i)
85  output[i] = src[i] / mul;
86 
87  return output;
88 }
89 
90 } // end namespace std
91 
92 namespace Antioch
93 {
94 
95 template <typename T, typename NewScalar>
96 struct rebind<std::vector<T>, NewScalar>
97 {
98  typedef std::vector<NewScalar> type;
99 };
100 
101 template <typename T>
102 struct has_size<std::vector<T> >
103 {
104  static const bool value = true;
105 };
106 
107 template <typename T>
108 struct return_auto<std::vector<T> >
109 {
110  static const bool value = true;
111 };
112 
113 template <typename T>
114 struct size_type<std::vector<T> >
115 {
116  typedef typename std::vector<T>::size_type type;
117 };
118 
119 template <typename T>
120 struct value_type<std::vector<T> >
121 {
122  typedef T type;
123 };
124 
125 template <typename T>
126 struct raw_value_type<std::vector<T> >
127 {
128  typedef typename raw_value_type<T>::type type;
129 };
130 
131 template <typename T>
132 inline
133 std::vector<T>
134 zero_clone(const std::vector<T>& example)
135 {
136  if (example.size())
137  return std::vector<T>(example.size(),zero_clone(example[0]));
138 
139  return std::vector<T>();
140 }
141 
142 template <typename T1, typename T2>
143 inline
144 void
145 zero_clone(std::vector<T1>& output, const std::vector<T2>& example)
146 {
147  const std::size_t sz = example.size();
148  output.resize(sz);
149  for (std::size_t i=0; i != sz; ++i)
150  Antioch::zero_clone(output[i], example[i]);
151 }
152 
153 template <typename T, typename Scalar>
154 inline
155 std::vector<T>
156 zero_clone(const std::vector<T>& example, const Scalar& value)
157 {
158  if (example.size())
159  return std::vector<T>(example.size(),value);
160 
161  return std::vector<T>();
162 }
163 
164 // A function for zero-setting vectorized numeric types
165 template <typename T>
166 inline
167 void set_zero(std::vector<T>& a)
168 {
169  if (a.size())
170  std::fill(a.begin(), a.end(), zero_clone(a[0]));
171 }
172 
173 template <typename T, typename VectorScalar>
174 inline
175 std::vector<T> custom_clone(const std::vector<T> & /*vec*/, const VectorScalar & vecsrc, std::vector<unsigned int> & indexes)
176 {
177  std::vector<T> returnval;
178  returnval.resize(indexes.size());
179  for (std::size_t i=0; i != indexes.size(); ++i)
180  returnval[i] = vecsrc[indexes[i]];
181  return returnval;
182 }
183 
184 } // end namespace Antioch
185 
186 
187 #endif //ANTIOCH_VECTOR_UTILS_H
std::vector< T > operator/(const std::vector< T > &src, const T &mul)
Definition: vector_utils.h:80
T custom_clone(const T &, const VectorScalar &values, unsigned int indexes)
std::vector< T > operator*(const std::vector< T > &src, const T &mul)
Definition: vector_utils.h:67
void set_zero(_Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &a)
Definition: eigen_utils.h:217
std::vector< T >::size_type type
Definition: vector_utils.h:116
The parameters are reduced parameters.
_Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > zero_clone(const _Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &ex)
Definition: eigen_utils.h:145

Generated on Thu Jul 7 2016 11:09:46 for antioch-0.4.0 by  doxygen 1.8.8