mav.h 13.9 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
#define _MSC_VER
Martin Reinecke's avatar
Martin Reinecke committed
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

Martin Reinecke's avatar
Martin Reinecke committed
102
103
104
105
    membuf(const T *d_, membuf &other)
      : ptr(other.ptr), d(d_), rw(other.rw) {}
    membuf(const T *d_, const membuf &other)
      : ptr(other.ptr), d(d_), rw(false) {}
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
106

Martin Reinecke's avatar
Martin Reinecke committed
107
  public:
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
108
109
110
111
112
113
    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
114
115
    membuf(const membuf &other)
      : ptr(other.ptr), d(other.d), rw(false) {}
Martin Reinecke's avatar
Martin Reinecke committed
116
#if defined(_MSC_VER)
Martin Reinecke's avatar
tweak    
Martin Reinecke committed
117
118
119
120
    membuf(membuf &other)
      : ptr(other.ptr), d(other.d), rw(other.rw) {}
    membuf(membuf &&other)
      : ptr(move(other.ptr)), d(move(other.d)), rw(move(other.rw)) {}
Martin Reinecke's avatar
Martin Reinecke committed
121
#else
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
122
    membuf(membuf &other) = default;
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
123
    membuf(membuf &&other) = default;
Martin Reinecke's avatar
Martin Reinecke committed
124
#endif
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
125

Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
126
    template<typename I> T &vraw(I i)
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)[i];
      }
Martin Reinecke's avatar
Martin Reinecke committed
131
132
133
134
    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
135
    T *vdata()
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
136
      {
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
137
      MR_assert(rw, "array is not writable");
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
138
139
      return const_cast<T *>(d);
      }
140
    bool writable() const { return rw; }
Martin Reinecke's avatar
Martin Reinecke committed
141
  };
142

Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
143
144
// "mav" stands for "multidimensional array view"
template<typename T> class fmav: public fmav_info, public membuf<T>
Martin Reinecke's avatar
Martin Reinecke committed
145
146
  {
  public:
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
147
148
149
150
151
152
153
154
    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_) {}
155
    fmav(const shape_t &shp_)
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
156
157
158
159
160
      : 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
tweak    
Martin Reinecke committed
161
    fmav(const fmav &other) = default;
Martin Reinecke's avatar
Martin Reinecke committed
162
#if defined(_MSC_VER)
Martin Reinecke's avatar
tweak    
Martin Reinecke committed
163
164
    fmav(fmav &other) : fmav_info(other), membuf<T>(other) {}
    fmav(fmav &&other) : fmav_info(other), membuf<T>(other) {}
Martin Reinecke's avatar
Martin Reinecke committed
165
#else
Martin Reinecke's avatar
Martin Reinecke committed
166
    fmav(fmav &other) = default;
Martin Reinecke's avatar
tweak    
Martin Reinecke committed
167
    fmav(fmav &&other) = default;
Martin Reinecke's avatar
tweak    
Martin Reinecke committed
168
#endif
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
169
170
    fmav(membuf<T> &buf, const shape_t &shp_, const stride_t &str_)
      : fmav_info(shp_, str_), membuf<T>(buf) {}
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
171
172
    fmav(const 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
173
174
  };

Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
175
template<size_t ndim> class mav_info
Martin Reinecke's avatar
Martin Reinecke committed
176
  {
Martin Reinecke's avatar
Martin Reinecke committed
177
  protected:
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
178
179
    using shape_t = array<size_t, ndim>;
    using stride_t = array<ptrdiff_t, ndim>;
Martin Reinecke's avatar
Martin Reinecke committed
180

Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
181
182
183
184
185
    shape_t shp;
    stride_t str;
    size_t sz;

    static size_t prod(const shape_t &shape)
186
      {
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
187
188
189
190
      size_t res=1;
      for (auto sz: shape)
        res*=sz;
      return res;
191
      }
Martin Reinecke's avatar
Martin Reinecke committed
192
193
194
195
    template<typename... Ns> ptrdiff_t getIdx(size_t dim, size_t n, Ns... ns) const
      { return str[dim]*n + getIdx(dim+1, ns...); }
    ptrdiff_t getIdx(size_t dim, size_t n) const
      { return str[dim]*n; }
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
196
197
198
199
200
201
202

  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
203
      {
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
204
      MR_assert(ndim>0, "at least 1D required");
Martin Reinecke's avatar
Martin Reinecke committed
205
      str[ndim-1]=1;
Martin Reinecke's avatar
Martin Reinecke committed
206
      for (size_t i=2; i<=ndim; ++i)
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
207
        str[ndim-i] = str[ndim-i+1]*ptrdiff_t(shp[ndim-i+1]);
208
      }
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
209
210
    size_t size() const { return sz; }
    const shape_t &shape() const { return shp; }
Martin Reinecke's avatar
Martin Reinecke committed
211
    size_t shape(size_t i) const { return shp[i]; }
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
212
213
    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
214
    bool last_contiguous() const
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
215
      { return (str.back()==1); }
Martin Reinecke's avatar
Martin Reinecke committed
216
217
218
219
220
221
    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
222
        stride *= ptrdiff_t(shp[ndim-1-i]);
Martin Reinecke's avatar
Martin Reinecke committed
223
224
225
        }
      return true;
      }
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
226
    bool conformable(const mav_info &other) const
Martin Reinecke's avatar
Martin Reinecke committed
227
      { return shp==other.shp; }
Martin Reinecke's avatar
Martin Reinecke committed
228
229
    bool conformable(const shape_t &other) const
      { return shp==other; }
Martin Reinecke's avatar
Martin Reinecke committed
230
    template<typename... Ns> ptrdiff_t idx(Ns... ns) const
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
231
      {
Martin Reinecke's avatar
Martin Reinecke committed
232
233
      static_assert(ndim==sizeof...(ns), "incorrect number of indices");
      return getIdx(0, ns...);
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
234
      }
Martin Reinecke's avatar
Martin Reinecke committed
235
  };
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
236
237

template<typename T, size_t ndim> class mav: public mav_info<ndim>, public membuf<T>
Martin Reinecke's avatar
Martin Reinecke committed
238
  {
239
//  static_assert((ndim>0) && (ndim<4), "only supports 1D, 2D, and 3D arrays");
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
240

Martin Reinecke's avatar
Martin Reinecke committed
241
  protected:
242
243
    using typename mav_info<ndim>::shape_t;
    using typename mav_info<ndim>::stride_t;
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
244
245
246
247
248
    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
249
    using membuf<T>::vraw;
Martin Reinecke's avatar
Martin Reinecke committed
250

251
252
253
254
255
256
257
258
259
260
261
262
    template<size_t idim, typename Func> void applyHelper(ptrdiff_t idx, Func func)
      {
      if constexpr (idim+1<ndim)
        for (size_t i=0; i<shp[idim]; ++i)
          applyHelper<idim+1, Func>(idx+i*str[idim], func);
      else
        {
        T *d2 = vdata();
        for (size_t i=0; i<shp[idim]; ++i)
          func(d2[idx+i*str[idim]]);
        }
      }
Martin Reinecke's avatar
Martin Reinecke committed
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
    template<size_t idim, typename Func> void applyHelper(ptrdiff_t idx, Func func) const
      {
      if constexpr (idim+1<ndim)
        for (size_t i=0; i<shp[idim]; ++i)
          applyHelper<idim+1, Func>(idx+i*str[idim], func);
      else
        {
        const T *d2 = data();
        for (size_t i=0; i<shp[idim]; ++i)
          func(d2[idx+i*str[idim]]);
        }
      }
    template<size_t idim, typename T2, typename Func>
      void applyHelper(ptrdiff_t idx, ptrdiff_t idx2,
                       const mav<T2,ndim> &other, Func func)
      {
      if constexpr (idim==0)
        MR_assert(conformable(other), "dimension mismatch");
      if constexpr (idim+1<ndim)
        for (size_t i=0; i<shp[idim]; ++i)
          applyHelper<idim+1, T2, Func>(idx+i*str[idim],
                                        idx2+i*other.str[idim], other, func);
      else
        {
        T *d2 = vdata();
        const T2 *d3 = other.data();
        for (size_t i=0; i<shp[idim]; ++i)
          func(d2[idx+i*str[idim]],d3[idx2+i*other.str[idim]]);
        }
      }
Martin Reinecke's avatar
Martin Reinecke committed
293

Martin Reinecke's avatar
Martin Reinecke committed
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
    template<size_t nd2> void subdata(const shape_t &i0, const shape_t &extent,
      array<size_t, nd2> &nshp, array<ptrdiff_t, nd2> &nstr, ptrdiff_t &nofs) const
      {
      size_t n0=0;
      for (auto x:extent) if (x==0)++n0;
      MR_assert(n0+nd2==ndim, "bad extent");
      nofs=0;
      for (size_t i=0, i2=0; i<ndim; ++i)
        {
        MR_assert(i0[i]<shp[i], "bad subset");
        nofs+=i0[i]*str[i];
        if (extent[i]!=0)
          {
          MR_assert(i0[i]+extent[i2]<=shp[i], "bad subset");
          nshp[i2] = extent[i]; nstr[i2]=str[i];
          ++i2;
          }
        }
      }

Martin Reinecke's avatar
Martin Reinecke committed
314
  public:
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
315
316
317
    using membuf<T>::operator[];
    using membuf<T>::vdata;
    using membuf<T>::data;
Martin Reinecke's avatar
Martin Reinecke committed
318
    using mav_info<ndim>::contiguous;
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
319
320
    using mav_info<ndim>::size;
    using mav_info<ndim>::idx;
Martin Reinecke's avatar
Martin Reinecke committed
321
    using mav_info<ndim>::conformable;
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
322

Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
323
324
325
326
327
328
329
330
    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_) {}
331
    mav(const array<size_t,ndim> &shp_)
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
332
      : mav_info<ndim>(shp_), membuf<T>(size()) {}
Martin Reinecke's avatar
tweak    
Martin Reinecke committed
333
    mav(const mav &other) = default;
Martin Reinecke's avatar
Martin Reinecke committed
334
#if defined(_MSC_VER)
Martin Reinecke's avatar
tweak    
Martin Reinecke committed
335
336
    mav(mav &other): mav_info<ndim>(other), membuf<T>(other) {}
    mav(mav &&other): mav_info<ndim>(other), membuf<T>(other) {}
Martin Reinecke's avatar
Martin Reinecke committed
337
#else
Martin Reinecke's avatar
Martin Reinecke committed
338
    mav(mav &other) = default;
Martin Reinecke's avatar
tweak    
Martin Reinecke committed
339
    mav(mav &&other) = default;
Martin Reinecke's avatar
tweak    
Martin Reinecke committed
340
#endif
Martin Reinecke's avatar
Martin Reinecke committed
341
342
343
344
    mav(const shape_t &shp_, const stride_t &str_, const T *d_, membuf<T> &mb)
      : mav_info<ndim>(shp_, str_), membuf<T>(d_, mb) {}
    mav(const shape_t &shp_, const stride_t &str_, const T *d_, const membuf<T> &mb)
      : mav_info<ndim>(shp_, str_), membuf<T>(d_, mb) {}
Martin Reinecke's avatar
Martin Reinecke committed
345
346
    operator fmav<T>() const
      {
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
347
      return fmav<T>(*this, {shp.begin(), shp.end()}, {str.begin(), str.end()});
Martin Reinecke's avatar
Martin Reinecke committed
348
      }
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
349
    operator fmav<T>()
Martin Reinecke's avatar
Martin Reinecke committed
350
      {
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
351
      return fmav<T>(*this, {shp.begin(), shp.end()}, {str.begin(), str.end()});
Martin Reinecke's avatar
Martin Reinecke committed
352
      }
Martin Reinecke's avatar
Martin Reinecke committed
353
354
355
356
    template<typename... Ns> const T &operator()(Ns... ns) const
      { return operator[](idx(ns...)); }
    template<typename... Ns> T &v(Ns... ns)
      { return vraw(idx(ns...)); }
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
357
    template<typename Func> void apply(Func func)
358
      {
Martin Reinecke's avatar
Martin Reinecke committed
359
360
      if (contiguous())
        {
361
        T *d2 = vdata();
Martin Reinecke's avatar
Martin Reinecke committed
362
        for (auto v=d2; v!=d2+size(); ++v)
Martin Reinecke's avatar
Martin Reinecke committed
363
364
365
          func(*v);
        return;
        }
366
      applyHelper<0,Func>(0,func);
Martin Reinecke's avatar
Martin Reinecke committed
367
      }
Martin Reinecke's avatar
Martin Reinecke committed
368
369
370
    template<typename T2, typename Func> void apply
      (const mav<T2, ndim> &other,Func func)
      { applyHelper<0,T2,Func>(0,0,other,func); }
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
371
372
    void fill(const T &val)
      { apply([val](T &v){v=val;}); }
Martin Reinecke's avatar
Martin Reinecke committed
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
    template<size_t nd2> mav<T,nd2> subarray(const shape_t &i0, const shape_t &extent)
      {
      array<size_t,nd2> nshp;
      array<ptrdiff_t,nd2> nstr;
      ptrdiff_t nofs;
      subdata<nd2> (i0, extent, nshp, nstr, nofs);
      return mav<T,nd2> (nshp, nstr, d+nofs, *this);
      }
    template<size_t nd2> mav<T,nd2> subarray(const shape_t &i0, const shape_t &extent) const
      {
      array<size_t,nd2> nshp;
      array<ptrdiff_t,nd2> nstr;
      ptrdiff_t nofs;
      subdata<nd2> (i0, extent, nshp, nstr, nofs);
      return mav<T,nd2> (nshp, nstr, d+nofs, *this);
      }
Martin Reinecke's avatar
Martin Reinecke committed
389
390
  };

Martin Reinecke's avatar
Martin Reinecke committed
391
392
393
394
395
396
397
398
399
400
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_;

Martin Reinecke's avatar
Martin Reinecke committed
401
402
403
404
405
    template<typename... Ns> ptrdiff_t getIdx(size_t dim, size_t n, Ns... ns) const
      { return str[dim]*n + getIdx(dim+1, ns...); }
    ptrdiff_t getIdx(size_t dim, size_t n) const
      { return str[dim]*n; }

Martin Reinecke's avatar
Martin Reinecke committed
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
  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]; }
Martin Reinecke's avatar
Martin Reinecke committed
439
440
441
442
443
444
    template<typename... Ns> ptrdiff_t idx(Ns... ns) const
      { return idx_ + getIdx(0, ns...); }
    template<typename... Ns> const T &operator()(Ns... ns) const
      { return mav[idx(ns...)]; }
    template<typename... Ns> T &v(Ns... ns)
      { return mav.vraw(idx(ns...)); }
Martin Reinecke's avatar
Martin Reinecke committed
445
446
  };

Martin Reinecke's avatar
Martin Reinecke committed
447
448
}

Martin Reinecke's avatar
Martin Reinecke committed
449
450
using detail_mav::shape_t;
using detail_mav::stride_t;
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
451
using detail_mav::fmav_info;
Martin Reinecke's avatar
Martin Reinecke committed
452
using detail_mav::fmav;
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
453
using detail_mav::mav;
Martin Reinecke's avatar
Martin Reinecke committed
454
using detail_mav::MavIter;
Martin Reinecke's avatar
Martin Reinecke committed
455

Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
456
}
Martin Reinecke's avatar
Martin Reinecke committed
457
458

#endif