mav.h 11 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
 *  This file is part of the MR utility library.
 *
 *  This code is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This code is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this code; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
19
/* Copyright (C) 2019-2020 Max-Planck-Society
Martin Reinecke's avatar
Martin Reinecke committed
20
21
   Author: Martin Reinecke */

Martin Reinecke's avatar
Martin Reinecke committed
22
23
24
25
26
#ifndef MRUTIL_MAV_H
#define MRUTIL_MAV_H

#include <cstdlib>
#include <array>
Martin Reinecke's avatar
Martin Reinecke committed
27
#include <vector>
28
#include <memory>
29
#include "mr_util/infra/error_handling.h"
Martin Reinecke's avatar
Martin Reinecke committed
30
31
32

namespace mr {

Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
33
namespace detail_mav {
Martin Reinecke's avatar
Martin Reinecke committed
34
35
36

using namespace std;

Martin Reinecke's avatar
Martin Reinecke committed
37
38
39
40
41
42
43
44
using shape_t = vector<size_t>;
using stride_t = vector<ptrdiff_t>;

class fmav_info
  {
  protected:
    shape_t shp;
    stride_t str;
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
45
    size_t sz;
Martin Reinecke's avatar
Martin Reinecke committed
46
47
48
49
50
51
52
53
54
55
56

    static size_t prod(const shape_t &shape)
      {
      size_t res=1;
      for (auto sz: shape)
        res*=sz;
      return res;
      }

  public:
    fmav_info(const shape_t &shape_, const stride_t &stride_)
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
57
58
59
60
61
      : shp(shape_), str(stride_), sz(prod(shp))
      {
      MR_assert(shp.size()>0, "at least 1D required");
      MR_assert(shp.size()==str.size(), "dimensions mismatch");
      }
Martin Reinecke's avatar
Martin Reinecke committed
62
    fmav_info(const shape_t &shape_)
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
63
      : shp(shape_), str(shape_.size()), sz(prod(shp))
Martin Reinecke's avatar
Martin Reinecke committed
64
65
      {
      auto ndim = shp.size();
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
66
      MR_assert(ndim>0, "at least 1D required");
Martin Reinecke's avatar
Martin Reinecke committed
67
68
69
70
71
      str[ndim-1]=1;
      for (size_t i=2; i<=ndim; ++i)
        str[ndim-i] = str[ndim-i+1]*ptrdiff_t(shp[ndim-i+1]);
      }
    size_t ndim() const { return shp.size(); }
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
72
    size_t size() const { return sz; }
Martin Reinecke's avatar
Martin Reinecke committed
73
74
75
76
77
78
79
80
81
82
83
84
85
    const shape_t &shape() const { return shp; }
    size_t shape(size_t i) const { return shp[i]; }
    const stride_t &stride() const { return str; }
    const ptrdiff_t &stride(size_t i) const { return str[i]; }
    bool last_contiguous() const
      { return (str.back()==1); }
    bool contiguous() const
      {
      auto ndim = shp.size();
      ptrdiff_t stride=1;
      for (size_t i=0; i<ndim; ++i)
        {
        if (str[ndim-1-i]!=stride) return false;
Martin Reinecke's avatar
Martin Reinecke committed
86
        stride *= ptrdiff_t(shp[ndim-1-i]);
Martin Reinecke's avatar
Martin Reinecke committed
87
88
89
90
91
92
93
        }
      return true;
      }
    bool conformable(const fmav_info &other) const
      { return shp==other.shp; }
  };

Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
94
template<typename T> class membuf
Martin Reinecke's avatar
Martin Reinecke committed
95
96
  {
  protected:
97
98
    using Tsp = shared_ptr<vector<T>>;
    Tsp ptr;
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
99
100
    const T *d;
    bool rw;
Martin Reinecke's avatar
Martin Reinecke committed
101
102

  public:
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
103
104
105
106
107
108
    membuf(T *d_, bool rw_=false)
      : d(d_), rw(rw_) {}
    membuf(const T *d_)
      : d(d_), rw(false) {}
    membuf(size_t sz)
      : ptr(make_unique<vector<T>>(sz)), d(ptr->data()), rw(true) {}
Martin Reinecke's avatar
Martin Reinecke committed
109
110
    membuf(const membuf &other)
      : ptr(other.ptr), d(other.d), rw(false) {}
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
111
    membuf(membuf &other) = default;
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
112
    membuf(membuf &&other) = default;
113
// Not for public use!
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
114
115
116
    membuf(T *d_, const Tsp &p, bool rw_)
      : ptr(p), d(d_), rw(rw_) {}

Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
117
    template<typename I> T &vraw(I i)
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
118
      {
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
119
      MR_assert(rw, "array is not writable");
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
120
121
      return const_cast<T *>(d)[i];
      }
Martin Reinecke's avatar
Martin Reinecke committed
122
123
124
125
    template<typename I> const T &operator[](I i) const
      { return d[i]; }
    const T *data() const
      { return d; }
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
126
    T *vdata()
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
127
      {
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
128
      MR_assert(rw, "array is not writable");
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
129
130
      return const_cast<T *>(d);
      }
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
131
    bool writable() { return rw; }
Martin Reinecke's avatar
Martin Reinecke committed
132
  };
133

Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
134
135
// "mav" stands for "multidimensional array view"
template<typename T> class fmav: public fmav_info, public membuf<T>
Martin Reinecke's avatar
Martin Reinecke committed
136
137
  {
  protected:
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
138
    using typename membuf<T>::Tsp;
Martin Reinecke's avatar
Martin Reinecke committed
139
140

  public:
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
141
142
143
144
145
146
147
148
    fmav(const T *d_, const shape_t &shp_, const stride_t &str_)
      : fmav_info(shp_, str_), membuf<T>(d_) {}
    fmav(const T *d_, const shape_t &shp_)
      : fmav_info(shp_), membuf<T>(d_) {}
    fmav(T *d_, const shape_t &shp_, const stride_t &str_, bool rw_)
      : fmav_info(shp_, str_), membuf<T>(d_,rw_) {}
    fmav(T *d_, const shape_t &shp_, bool rw_)
      : fmav_info(shp_), membuf<T>(d_,rw_) {}
149
    fmav(const shape_t &shp_)
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
150
151
152
153
154
      : fmav_info(shp_), membuf<T>(size()) {}
    fmav(T* d_, const fmav_info &info, bool rw_=false)
      : fmav_info(info), membuf<T>(d_, rw_) {}
    fmav(const T* d_, const fmav_info &info)
      : fmav_info(info), membuf<T>(d_) {}
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
155
    fmav(const fmav &other) = default;
Martin Reinecke's avatar
Martin Reinecke committed
156
    fmav(fmav &other) = default;
157
158
    fmav(fmav &&other) = default;
// Not for public use!
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
159
160
    fmav(membuf<T> &buf, const shape_t &shp_, const stride_t &str_)
      : fmav_info(shp_, str_), membuf<T>(buf) {}
Martin Reinecke's avatar
Martin Reinecke committed
161
162
  };

Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
163
template<size_t ndim> class mav_info
Martin Reinecke's avatar
Martin Reinecke committed
164
  {
Martin Reinecke's avatar
Martin Reinecke committed
165
  protected:
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
166
167
    using shape_t = array<size_t, ndim>;
    using stride_t = array<ptrdiff_t, ndim>;
Martin Reinecke's avatar
Martin Reinecke committed
168

Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
169
170
171
172
173
    shape_t shp;
    stride_t str;
    size_t sz;

    static size_t prod(const shape_t &shape)
174
      {
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
175
176
177
178
      size_t res=1;
      for (auto sz: shape)
        res*=sz;
      return res;
179
      }
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
180
181
182
183
184
185
186

  public:
    mav_info(const shape_t &shape_, const stride_t &stride_)
      : shp(shape_), str(stride_), sz(prod(shp))
      { MR_assert(shp.size()==str.size(), "dimensions mismatch"); }
    mav_info(const shape_t &shape_)
      : shp(shape_), sz(prod(shp))
Martin Reinecke's avatar
Martin Reinecke committed
187
      {
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
188
      MR_assert(ndim>0, "at least 1D required");
Martin Reinecke's avatar
Martin Reinecke committed
189
      str[ndim-1]=1;
Martin Reinecke's avatar
Martin Reinecke committed
190
      for (size_t i=2; i<=ndim; ++i)
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
191
        str[ndim-i] = str[ndim-i+1]*ptrdiff_t(shp[ndim-i+1]);
192
      }
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
193
194
    size_t size() const { return sz; }
    const shape_t &shape() const { return shp; }
Martin Reinecke's avatar
Martin Reinecke committed
195
    size_t shape(size_t i) const { return shp[i]; }
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
196
197
    const stride_t &stride() const { return str; }
    const ptrdiff_t &stride(size_t i) const { return str[i]; }
Martin Reinecke's avatar
Martin Reinecke committed
198
    bool last_contiguous() const
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
199
      { return (str.back()==1); }
Martin Reinecke's avatar
Martin Reinecke committed
200
201
202
203
204
205
    bool contiguous() const
      {
      ptrdiff_t stride=1;
      for (size_t i=0; i<ndim; ++i)
        {
        if (str[ndim-1-i]!=stride) return false;
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
206
        stride *= ptrdiff_t(shp[ndim-1-i]);
Martin Reinecke's avatar
Martin Reinecke committed
207
208
209
        }
      return true;
      }
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
210
    bool conformable(const mav_info &other) const
Martin Reinecke's avatar
Martin Reinecke committed
211
      { return shp==other.shp; }
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
    ptrdiff_t idx(size_t i) const
      {
      static_assert(ndim==1, "ndim must be 1");
      return str[0]*i;
      }
    ptrdiff_t idx(size_t i, size_t j) const
      {
      static_assert(ndim==2, "ndim must be 2");
      return str[0]*i + str[1]*j;
      }
    ptrdiff_t idx(size_t i, size_t j, size_t k) const
      {
      static_assert(ndim==3, "ndim must be 3");
      return str[0]*i + str[1]*j + str[2]*k;
      }
Martin Reinecke's avatar
Martin Reinecke committed
227
  };
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
228
229

template<typename T, size_t ndim> class mav: public mav_info<ndim>, public membuf<T>
Martin Reinecke's avatar
Martin Reinecke committed
230
  {
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
231
232
233
234
  static_assert((ndim>0) && (ndim<4), "only supports 1D, 2D, and 3D arrays");
  using typename mav_info<ndim>::shape_t;
  using typename mav_info<ndim>::stride_t;

Martin Reinecke's avatar
Martin Reinecke committed
235
  protected:
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
236
237
238
239
240
    using membuf<T>::d;
    using membuf<T>::ptr;
    using mav_info<ndim>::shp;
    using mav_info<ndim>::str;
    using membuf<T>::rw;
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
241
    using membuf<T>::vraw;
Martin Reinecke's avatar
Martin Reinecke committed
242
243

  public:
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
244
245
246
247
248
249
    using membuf<T>::operator[];
    using membuf<T>::vdata;
    using membuf<T>::data;
    using mav_info<ndim>::size;
    using mav_info<ndim>::idx;

Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
250
251
252
253
254
255
256
257
    mav(const T *d_, const shape_t &shp_, const stride_t &str_)
      : mav_info<ndim>(shp_, str_), membuf<T>(d_) {}
    mav(T *d_, const shape_t &shp_, const stride_t &str_, bool rw_=false)
      : mav_info<ndim>(shp_, str_), membuf<T>(d_, rw_) {}
    mav(const T *d_, const shape_t &shp_)
      : mav_info<ndim>(shp_), membuf<T>(d_) {}
    mav(T *d_, const shape_t &shp_, bool rw_=false)
      : mav_info<ndim>(shp_), membuf<T>(d_, rw_) {}
258
    mav(const array<size_t,ndim> &shp_)
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
259
      : mav_info<ndim>(shp_), membuf<T>(size()) {}
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
260
    mav(const mav &other) = default;
Martin Reinecke's avatar
Martin Reinecke committed
261
    mav(mav &other) = default;
262
    mav(mav &&other) = default;
Martin Reinecke's avatar
Martin Reinecke committed
263
264
    operator fmav<T>() const
      {
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
265
      return fmav<T>(data(), {shp.begin(), shp.end()}, {str.begin(), str.end()});
Martin Reinecke's avatar
Martin Reinecke committed
266
      }
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
267
    operator fmav<T>()
Martin Reinecke's avatar
Martin Reinecke committed
268
      {
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
269
      return fmav<T>(*this, {shp.begin(), shp.end()}, {str.begin(), str.end()});
Martin Reinecke's avatar
Martin Reinecke committed
270
      }
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
271
272
    const T &operator()(size_t i) const
      { return operator[](idx(i)); }
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
273
    const T &operator()(size_t i, size_t j) const
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
274
      { return operator[](idx(i,j)); }
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
275
    const T &operator()(size_t i, size_t j, size_t k) const
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
276
277
278
279
280
281
282
      { return operator[](idx(i,j,k)); }
    T &v(size_t i)
      { return vraw(idx(i)); }
    T &v(size_t i, size_t j)
      { return vraw(idx(i,j)); }
    T &v(size_t i, size_t j, size_t k)
      { return vraw(idx(i,j,k)); }
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
283
    void fill(const T &val)
Martin Reinecke's avatar
Martin Reinecke committed
284
      {
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
285
      T *d2 = vdata();
Martin Reinecke's avatar
Martin Reinecke committed
286
287
288
      // FIXME: special cases for contiguous arrays and/or zeroing?
      if (ndim==1)
        for (size_t i=0; i<shp[0]; ++i)
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
289
          d2[str[0]*i]=val;
Martin Reinecke's avatar
Martin Reinecke committed
290
291
292
      else if (ndim==2)
        for (size_t i=0; i<shp[0]; ++i)
          for (size_t j=0; j<shp[1]; ++j)
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
293
            d2[str[0]*i + str[1]*j] = val;
294
295
296
297
      else if (ndim==3)
        for (size_t i=0; i<shp[0]; ++i)
          for (size_t j=0; j<shp[1]; ++j)
            for (size_t k=0; k<shp[2]; ++k)
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
298
              d2[str[0]*i + str[1]*j + str[2]*k] = val;
Martin Reinecke's avatar
Martin Reinecke committed
299
300
301
      }
  };

Martin Reinecke's avatar
Martin Reinecke committed
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
template<typename T, size_t ndim> class MavIter
  {
  protected:
    fmav<T> mav;
    array<size_t, ndim> shp;
    array<ptrdiff_t, ndim> str;
    shape_t pos;
    ptrdiff_t idx_;
    bool done_;

  public:
    MavIter(const fmav<T> &mav_)
      : mav(mav_), pos(mav.ndim()-ndim,0), idx_(0), done_(false)
      {
      for (size_t i=0; i<ndim; ++i)
        {
        shp[i] = mav.shape(mav.ndim()-ndim+i);
        str[i] = mav.stride(mav.ndim()-ndim+i);
        }
      }
    MavIter(fmav<T> &mav_)
      : mav(mav_), pos(mav.ndim()-ndim,0), idx_(0), done_(false)
      {
      for (size_t i=0; i<ndim; ++i)
        {
        shp[i] = mav.shape(mav.ndim()-ndim+i);
        str[i] = mav.stride(mav.ndim()-ndim+i);
        }
      }
    bool done() const
      { return done_; }
    void inc()
      {
      for (ptrdiff_t i=mav.ndim()-ndim-1; i>=0; --i)
        {
        idx_+=mav.stride(i);
        if (++pos[i]<mav.shape(i)) return;
        pos[i]=0;
        idx_-=mav.shape(i)*mav.stride(i);
        }
      done_=true;
      }
    size_t shape(size_t i) const { return shp[i]; }
    ptrdiff_t idx(size_t i) const
      {
      static_assert(ndim==1, "ndim must be 1");
      return idx_+i*str[0];
      }
    ptrdiff_t idx(size_t i, size_t j) const
      {
      static_assert(ndim==2, "ndim must be 2");
      return idx_+i*str[0]+j*str[1];
      }
    ptrdiff_t idx(size_t i, size_t j, size_t k) const
      {
      static_assert(ndim==3, "ndim must be 3");
      return idx_+i*str[0]+j*str[1]+k*str[2];
      }
    const T &operator()(size_t i) const
      { return mav[idx(i)]; }
    const T &operator()(size_t i, size_t j) const
      { return mav[idx(i,j)]; }
    const T &operator()(size_t i, size_t j, size_t k) const
      { return mav[idx(i,j,k)]; }
    T &v(size_t i)
      { return mav.vraw(idx(i)); }
    T &v(size_t i, size_t j)
      { return mav.vraw(idx(i,j)); }
    T &v(size_t i, size_t j, size_t k)
      { return mav.vraw(idx(i,j,k)); }
  };

Martin Reinecke's avatar
Martin Reinecke committed
374
375
}

Martin Reinecke's avatar
Martin Reinecke committed
376
377
using detail_mav::shape_t;
using detail_mav::stride_t;
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
378
using detail_mav::fmav_info;
Martin Reinecke's avatar
Martin Reinecke committed
379
using detail_mav::fmav;
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
380
using detail_mav::mav;
Martin Reinecke's avatar
Martin Reinecke committed
381
using detail_mav::MavIter;
Martin Reinecke's avatar
Martin Reinecke committed
382

Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
383
}
Martin Reinecke's avatar
Martin Reinecke committed
384
385

#endif