ztsdb
zts.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 ZTS_HPP
20 #define ZTS_HPP
21 
22 
23 #include <string>
24 #include <vector>
25 #include <memory>
26 #include "globals.hpp"
27 #include "array.hpp"
28 #include "timezone/ztime_vector.hpp"
29 
30 
31 // namespace zcore { }
32 
33 namespace arr {
34 
35  struct zts {
36 
37  zts(rsv_t,
38  const Vector<idx_type>& dim_p,
39  const std::vector<Vector<zstring>> cnames=std::vector<Vector<zstring>>());
40 
41  zts(const Vector<idx_type>& dim_p,
42  const Vector<Global::dtime>& tidx_p,
43  const Vector<double>& v_p,
44  const std::vector<Vector<zstring>> cnames=std::vector<Vector<zstring>>(),
45  std::unique_ptr<AllocFactory>&& allocf_a=std::make_unique<FlexAllocFactory>(),
46  std::unique_ptr<AllocFactory>&& allocf_idx=std::make_unique<FlexAllocFactory>());
47 
48  zts(Array<Global::dtime> tidx_p,
49  Array<double> a_p,
50  std::unique_ptr<AllocFactory>&& allocf_a=std::make_unique<FlexAllocFactory>(),
51  std::unique_ptr<AllocFactory>&& allocf_idx=std::make_unique<FlexAllocFactory>()); // right ref here? LLL
52 
53  zts(std::unique_ptr<AllocFactory>&& allocf_a,
54  std::unique_ptr<AllocFactory>&& allocf_idx);
55 
56  zts(const zts& z,
57  std::unique_ptr<AllocFactory>&& allocf_a=std::make_unique<FlexAllocFactory>(),
58  std::unique_ptr<AllocFactory>&& allocf_idx=std::make_unique<FlexAllocFactory>());
59 
60  zts(zts&& z);
61 
62  const Array<double>& getArray() const { return *a; }
63  const Array<Global::dtime>& getIndex() const { return *idx; }
64 
68  std::shared_ptr<Array<double>>& getArrayPtr() const { return a; }
72  std::shared_ptr<Array<Global::dtime>>& getIndexPtr() const { return idx; }
73 
74  inline bool operator==(const zts& o) const {
75  return *idx == *o.idx && *a == *o.a;
76  }
77 
78  inline const Vector<idx_type>& getdim() const { return a->getdim(); }
79  inline const idx_type getdim(idx_type d) const { return a->getdim(d); }
80  inline const idx_type size() const { return a->size(); }
81  inline const Vector<double>& getcol(idx_type i) const { return a->getcol(i); }
82  inline fsys::path getAllocfDirname() const { return a->allocf->getDirname(); }
83  inline void msync(bool async) const { a->msync(async); idx->msync(async); }
84 
85  zts& append(const char* buf, size_t buflen, size_t& offset);
86  zts& appendVector(const char* buf, size_t buflen);
87 
88  Global::buflen_pair to_buffer(size_t offset=0) const;
89 
90  inline zts& addprefix(const string& prefix, idx_type d) { a->addprefix(prefix, d); return *this; }
91 
92  zts& resize(idx_type d, idx_type sz, idx_type from=0);
93 
94  // Indexing operations (subsetting, subassign) ------------------
95 
97  template<typename INDEX>
98  zts operator()(const vector<INDEX>& i, bool drop=true) const {
99  if (i.size() != a->dim.size()) {
100  throw range_error("incorrect number of dimensions");
101  }
102  // the following is a bit more complicated than using
103  // 'idx(vector<INDEX>{i[0]})' but in compensation it avoids the
104  // copy of 'idx[0]':
106  i[0].subset(sidx, idx->getcol(0));
107  arr::idx_type dropfirst = 2; // protect against vectorification
108  return zts(arr::Array<Global::dtime>({sidx.size()}, sidx), (*a)(i, drop, dropfirst));
109  }
110 
112  template<typename INDEX, typename U>
113  zts& operator()(const vector<INDEX>& i, const Array<U>& u) {
114  (*a)(i, u);
115  return *this;
116  }
117 
119  template<typename INDEX, typename U>
120  zts& operator()(const vector<INDEX>& i, U u) {
121  (*a)(i, u);
122  return *this;
123  }
124 
127  zts subsetRows(idx_type from, idx_type to, bool dummy=false) const;
128 
130  template<class F>
131  zts& applyf(F f) {
132  a->applyf(f);
133  return *this;
134  }
135 
136  // bind functions ---------
137  zts& abind(const zts& z, idx_type d, const string& prefix="");
138  zts& abind(const Array<double>& u, idx_type d, const string& prefix="");
139 
140  inline bool isPersistent() const { return a->isPersistent(); }
141 
142  private:
143  // can we please get rid of the mutable here? LLL
144  mutable std::shared_ptr<Array<double>> a;
145  mutable std::shared_ptr<Array<Global::dtime>> idx;
146  };
147 
148 
150  template <typename DS, typename DE>
151  zts align_closest(const zts& ts,
152  const Array<Global::dtime>& y,
153  const DS& start,
154  const DE& end)
155  {
156  auto dim = ts.getdim();
157  setv(dim, 0, y.size());
158  Array<double> a(arr::rsv, dim);
159 
160  for (size_t i=0; i<a.ncols(); ++i) {
161  arr::align_closest<double, Global::NANF, DS, DE>
162  (ts.getIndex().getcol(0),
163  y.getcol(0),
164  ts.getArray().getcol(i),
165  a.getcol(i),
166  start,
167  end);
168  }
169 
170  return arr::zts(y, std::move(a)); // LLL verify no copy
171  }
172 
174 
175  template <typename F, typename DS, typename DE>
176  zts align_func(const zts& ts,
177  const Array<Global::dtime>& y,
178  const DS& start,
179  const DE& end)
180  {
181  auto dim = ts.getArray().getdim();
182  setv(dim, 0, y.size());
183  Array<double> a(arr::rsv, dim);
184 
185  for (size_t i=0; i<a.ncols(); ++i) {
186  arr::align_func<double, F, DS, DE>
187  (ts.getIndex().getcol(0),
188  y.getcol(0),
189  ts.getArray().getcol(i),
190  a.getcol(i),
191  start,
192  end);
193  }
194 
195  // avoid the copy of y/a here! LLL
196  return arr::zts(y, std::move(a));
197  }
198 
199  template <typename F>
200  void op(const zts& x, zts& y)
201  {
202  for (size_t i=0; i<y.getArray().ncols(); ++i) {
203  arr::op_zts<double, F>(x.getIndex().getcol(0), y.getIndex().getcol(0),
204  x.getArray().getcol(0), y.getArrayPtr()->getcol(i));
205  }
206  }
207 
208  struct LengthMismatch : std::invalid_argument {
209  LengthMismatch(const std::string& s) : std::invalid_argument(s) { }
210  };
211  struct UnorderedIndex : std::invalid_argument {
212  UnorderedIndex(const std::string& s) : std::invalid_argument(s) { }
213  };
214 
215 } // end namespace zts
216 
217 
218 #endif
arr::zts::getIndexPtr
std::shared_ptr< Array< Global::dtime > > & getIndexPtr() const
Definition: zts.hpp:72
arr::align_closest
zts align_closest(const zts &ts, const Array< Global::dtime > &y, const DS &start, const DE &end)
alignment functions:
Definition: zts.hpp:151
arr::Array::size
idx_type size() const
Get the total number of elements in the array.
Definition: array.hpp:833
arr::LengthMismatch
Definition: zts.hpp:208
arr::Vector< idx_type >
arr::zts::appendVector
zts & appendVector(const char *buf, size_t buflen)
Definition: zts.cpp:139
arr::zts::operator()
zts operator()(const vector< INDEX > &i, bool drop=true) const
Complex subsetting.
Definition: zts.hpp:98
arr::UnorderedIndex
Definition: zts.hpp:211
arr::zts::operator()
zts & operator()(const vector< INDEX > &i, const Array< U > &u)
subassign an array.
Definition: zts.hpp:113
arr::rsv_t
Definition: vector_base.hpp:59
arr::zts
Definition: zts.hpp:35
arr
Contains the classes and functions that implement a multidimentional array type.
Definition: allocator.hpp:29
arr::zts::subsetRows
zts subsetRows(idx_type from, idx_type to, bool dummy=false) const
Definition: zts.cpp:197
arr::align_func
zts align_func(const zts &ts, const Array< Global::dtime > &y, const DS &start, const DE &end)
align_func
Definition: zts.hpp:176
arr::zts::operator()
zts & operator()(const vector< INDEX > &i, U u)
subassign a scalar.
Definition: zts.hpp:120
arr::Array
Definition: array.hpp:109
arr::zts::applyf
zts & applyf(F f)
Apply a function on every element of the array.
Definition: zts.hpp:131
arr::zts::getArrayPtr
std::shared_ptr< Array< double > > & getArrayPtr() const
Definition: zts.hpp:68