ztsdb
pseudovector.hpp
1 // (C) 2016 Leonardo Silvestri
2 //
3 // This file is part of ztsdb.
4 //
5 // ztsdb is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // ztsdb is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with ztsdb. If not, see <http://www.gnu.org/licenses/>.
17 
18 
19 #ifndef PSEUDO_VECTOR_HPP
20 #define PSEUDO_VECTOR_HPP
21 
22 
23 #include "vector.hpp"
24 
25 
26 namespace arr {
27 
28  template <typename T, typename U=T>
29  struct PseudoVector {
30  PseudoVector(const Vector<U>& v_p, size_t sz_p=0) :
31  v(v_p), scalar(v.size() == 1), first_elt(v[0]), sz(sz_p) { }
32  inline const U operator[](size_t i) const { return scalar ? first_elt : v[i]; }
33 
34  inline const T plus(T t, U u) const { return ztsdb::plus<T,U,T>()(t, u); }
35  inline size_t size() const { return scalar ? sz : v.size(); }
36 
37  private:
38  const Vector<U>& v;
39  const bool scalar;
40  const U first_elt;
41  const size_t sz;
42  };
43 
44 } // end namespace arr
45 
46 
47 #endif
arr::Vector< U >
arr
Contains the classes and functions that implement a multidimentional array type.
Definition: allocator.hpp:29
ztsdb::plus
Definition: misc.hpp:152
arr::PseudoVector
Definition: pseudovector.hpp:29