ztsdb
pseudoarray.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_ARRAY_HPP
20 #define PSEUDO_ARRAY_HPP
21 
22 
23 #include "array.hpp"
24 
25 
26 namespace arr {
27 
28  template <typename T, typename O=typename Array<T>::comparator>
29  struct PseudoArray {
30  PseudoArray(const Array<T,O>& v_p, size_t cols_p=0, size_t sz_p=0) :
31  v(v_p), scalar(v.size() == 1), first_elt(v[0]),
32  cols(cols_p == 0 ? v.size() : cols_p), sz(sz_p == 0 ? v.size() : sz_p), colstruct(v[0]) { }
33 
34  inline const T operator[](size_t i) const { return scalar ? first_elt : v[i]; }
35 
36  inline size_t size() const { return sz; }
37  template <typename C>
38  inline const C& getcol(size_t i) { return scalar ? colstruct : v.getcol(i); }
39 
40  template <typename V>
41  struct ColStruct {
42  ColStruct(const V& v_p) : v(v_p) { }
43  const V& operator[](size_t i) const { return v; }
44  private:
45  const V& v;
46  };
47 
48  private:
49  const Array<T,O>& v;
50  const bool scalar;
51  const T first_elt;
52  const size_t cols;
53  const size_t sz;
54  const ColStruct<T> colstruct;
55  };
56 
57 } // end namespace arr
58 
59 
60 #endif
arr::PseudoArray
Definition: pseudoarray.hpp:29
arr::PseudoArray::ColStruct
Definition: pseudoarray.hpp:41
arr
Contains the classes and functions that implement a multidimentional array type.
Definition: allocator.hpp:29
arr::Array
Definition: array.hpp:109