os/ossrv/ossrv_pub/boost_apis/boost/multi_array/view.hpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright 2002 The Trustees of Indiana University.
sl@0
     2
sl@0
     3
// Use, modification and distribution is subject to the Boost Software 
sl@0
     4
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
sl@0
     5
// http://www.boost.org/LICENSE_1_0.txt)
sl@0
     6
sl@0
     7
//  Boost.MultiArray Library
sl@0
     8
//  Authors: Ronald Garcia
sl@0
     9
//           Jeremy Siek
sl@0
    10
//           Andrew Lumsdaine
sl@0
    11
//  See http://www.boost.org/libs/multi_array for documentation.
sl@0
    12
sl@0
    13
#ifndef BOOST_MULTI_ARRAY_VIEW_RG071301_HPP
sl@0
    14
#define BOOST_MULTI_ARRAY_VIEW_RG071301_HPP
sl@0
    15
sl@0
    16
//
sl@0
    17
// view.hpp - code for creating "views" of array data.
sl@0
    18
//
sl@0
    19
sl@0
    20
#include "boost/multi_array/base.hpp"
sl@0
    21
#include "boost/multi_array/concept_checks.hpp"
sl@0
    22
#include "boost/multi_array/iterator.hpp"
sl@0
    23
#include "boost/multi_array/storage_order.hpp"
sl@0
    24
#include "boost/multi_array/subarray.hpp"
sl@0
    25
#include "boost/multi_array/algorithm.hpp"
sl@0
    26
#include "boost/type_traits/is_integral.hpp"
sl@0
    27
#include "boost/array.hpp"
sl@0
    28
#include "boost/limits.hpp"
sl@0
    29
#include <algorithm>
sl@0
    30
#include <cstddef>
sl@0
    31
#include <functional>
sl@0
    32
#include <numeric>
sl@0
    33
sl@0
    34
namespace boost {
sl@0
    35
namespace detail {
sl@0
    36
namespace multi_array {
sl@0
    37
sl@0
    38
// TPtr = const T* defaulted in base.hpp
sl@0
    39
template <typename T, std::size_t NumDims, typename TPtr>
sl@0
    40
class const_multi_array_view :
sl@0
    41
    public boost::detail::multi_array::multi_array_impl_base<T,NumDims>
sl@0
    42
{
sl@0
    43
  typedef boost::detail::multi_array::multi_array_impl_base<T,NumDims> super_type;
sl@0
    44
public: 
sl@0
    45
  typedef typename super_type::value_type value_type;
sl@0
    46
  typedef typename super_type::const_reference const_reference;
sl@0
    47
  typedef typename super_type::const_iterator const_iterator;
sl@0
    48
  typedef typename super_type::const_reverse_iterator const_reverse_iterator;
sl@0
    49
  typedef typename super_type::element element;
sl@0
    50
  typedef typename super_type::size_type size_type;
sl@0
    51
  typedef typename super_type::difference_type difference_type;
sl@0
    52
  typedef typename super_type::index index;
sl@0
    53
  typedef typename super_type::extent_range extent_range;
sl@0
    54
sl@0
    55
  // template typedefs
sl@0
    56
  template <std::size_t NDims>
sl@0
    57
  struct const_array_view {
sl@0
    58
    typedef boost::detail::multi_array::const_multi_array_view<T,NDims> type;
sl@0
    59
  };
sl@0
    60
sl@0
    61
  template <std::size_t NDims>
sl@0
    62
  struct array_view {
sl@0
    63
    typedef boost::detail::multi_array::multi_array_view<T,NDims> type;
sl@0
    64
  };
sl@0
    65
sl@0
    66
  template <typename OPtr>
sl@0
    67
  const_multi_array_view(const 
sl@0
    68
                         const_multi_array_view<T,NumDims,OPtr>& other) :
sl@0
    69
    base_(other.base_), origin_offset_(other.origin_offset_),
sl@0
    70
    num_elements_(other.num_elements_), extent_list_(other.extent_list_),
sl@0
    71
    stride_list_(other.stride_list_), index_base_list_(other.index_base_list_)
sl@0
    72
  { }
sl@0
    73
sl@0
    74
sl@0
    75
  template <class BaseList>
sl@0
    76
#ifdef BOOST_NO_SFINAE
sl@0
    77
  void
sl@0
    78
#else
sl@0
    79
  typename
sl@0
    80
  disable_if<typename boost::is_integral<BaseList>::type,void >::type
sl@0
    81
#endif
sl@0
    82
  reindex(const BaseList& values) {
sl@0
    83
    boost::function_requires<
sl@0
    84
      detail::multi_array::CollectionConcept<BaseList> >();
sl@0
    85
    boost::detail::multi_array::
sl@0
    86
      copy_n(values.begin(),num_dimensions(),index_base_list_.begin());
sl@0
    87
    origin_offset_ =
sl@0
    88
      this->calculate_indexing_offset(stride_list_,index_base_list_);
sl@0
    89
  }
sl@0
    90
sl@0
    91
  void reindex(index value) {
sl@0
    92
    index_base_list_.assign(value);
sl@0
    93
    origin_offset_ =
sl@0
    94
      this->calculate_indexing_offset(stride_list_,index_base_list_);
sl@0
    95
  }
sl@0
    96
sl@0
    97
  size_type num_dimensions() const { return NumDims; }
sl@0
    98
sl@0
    99
  size_type size() const { return extent_list_.front(); }
sl@0
   100
  size_type max_size() const { return num_elements(); }
sl@0
   101
  bool empty() const { return size() == 0; }
sl@0
   102
sl@0
   103
  const size_type* shape() const {
sl@0
   104
    return extent_list_.data();
sl@0
   105
  }
sl@0
   106
sl@0
   107
  const index* strides() const {
sl@0
   108
    return stride_list_.data();
sl@0
   109
  }
sl@0
   110
sl@0
   111
  const T* origin() const { return base_+origin_offset_; }
sl@0
   112
sl@0
   113
  size_type num_elements() const { return num_elements_; }
sl@0
   114
sl@0
   115
  const index* index_bases() const {
sl@0
   116
    return index_base_list_.data();
sl@0
   117
  }
sl@0
   118
sl@0
   119
  template <typename IndexList>
sl@0
   120
  const element& operator()(IndexList indices) const {
sl@0
   121
    boost::function_requires<
sl@0
   122
      detail::multi_array::CollectionConcept<IndexList> >();
sl@0
   123
    return super_type::access_element(boost::type<const element&>(),
sl@0
   124
                                      indices,origin(),
sl@0
   125
                                      shape(),strides(),index_bases());
sl@0
   126
  }
sl@0
   127
sl@0
   128
  // Only allow const element access
sl@0
   129
  const_reference operator[](index idx) const {
sl@0
   130
    return super_type::access(boost::type<const_reference>(),
sl@0
   131
                              idx,origin(),
sl@0
   132
                              shape(),strides(),
sl@0
   133
                              index_bases());
sl@0
   134
  }
sl@0
   135
sl@0
   136
  // see generate_array_view in base.hpp
sl@0
   137
#if !defined(BOOST_MSVC) || BOOST_MSVC > 1300
sl@0
   138
  template <int NDims>
sl@0
   139
#else
sl@0
   140
  template <int NumDims, int NDims> // else ICE
sl@0
   141
#endif // BOOST_MSVC
sl@0
   142
  typename const_array_view<NDims>::type 
sl@0
   143
  operator[](const boost::detail::multi_array::
sl@0
   144
             index_gen<NumDims,NDims>& indices)
sl@0
   145
    const {
sl@0
   146
    typedef typename const_array_view<NDims>::type return_type;
sl@0
   147
    return
sl@0
   148
      super_type::generate_array_view(boost::type<return_type>(),
sl@0
   149
                                      indices,
sl@0
   150
                                      shape(),
sl@0
   151
                                      strides(),
sl@0
   152
                                      index_bases(),
sl@0
   153
                                      origin());
sl@0
   154
  }
sl@0
   155
  const_iterator begin() const {
sl@0
   156
    return const_iterator(*index_bases(),origin(),
sl@0
   157
                          shape(),strides(),index_bases());
sl@0
   158
  }
sl@0
   159
sl@0
   160
  const_iterator end() const {
sl@0
   161
    return const_iterator(*index_bases()+(index)*shape(),origin(),
sl@0
   162
                          shape(),strides(),index_bases());
sl@0
   163
  }
sl@0
   164
  
sl@0
   165
  const_reverse_iterator rbegin() const {
sl@0
   166
    return const_reverse_iterator(end());
sl@0
   167
  }
sl@0
   168
sl@0
   169
  const_reverse_iterator rend() const {
sl@0
   170
    return const_reverse_iterator(begin());
sl@0
   171
  }
sl@0
   172
sl@0
   173
sl@0
   174
  template <typename OPtr>
sl@0
   175
  bool operator==(const
sl@0
   176
                  const_multi_array_view<T,NumDims,OPtr>& rhs)
sl@0
   177
    const {
sl@0
   178
    if(std::equal(extent_list_.begin(),
sl@0
   179
                  extent_list_.end(),
sl@0
   180
                  rhs.extent_list_.begin()))
sl@0
   181
      return std::equal(begin(),end(),rhs.begin());
sl@0
   182
    else return false;
sl@0
   183
  }
sl@0
   184
sl@0
   185
  template <typename OPtr>
sl@0
   186
  bool operator<(const
sl@0
   187
                 const_multi_array_view<T,NumDims,OPtr>& rhs)
sl@0
   188
    const {
sl@0
   189
    return std::lexicographical_compare(begin(),end(),rhs.begin(),rhs.end());
sl@0
   190
  }
sl@0
   191
sl@0
   192
  template <typename OPtr>
sl@0
   193
  bool operator!=(const
sl@0
   194
                  const_multi_array_view<T,NumDims,OPtr>& rhs)
sl@0
   195
    const {
sl@0
   196
    return !(*this == rhs);
sl@0
   197
  }
sl@0
   198
sl@0
   199
  template <typename OPtr>
sl@0
   200
  bool operator>(const
sl@0
   201
                 const_multi_array_view<T,NumDims,OPtr>& rhs)
sl@0
   202
    const {
sl@0
   203
    return rhs < *this;
sl@0
   204
  }
sl@0
   205
sl@0
   206
  template <typename OPtr>
sl@0
   207
  bool operator<=(const
sl@0
   208
                 const_multi_array_view<T,NumDims,OPtr>& rhs)
sl@0
   209
    const {
sl@0
   210
    return !(*this > rhs);
sl@0
   211
  }
sl@0
   212
sl@0
   213
  template <typename OPtr>
sl@0
   214
  bool operator>=(const
sl@0
   215
                 const_multi_array_view<T,NumDims,OPtr>& rhs)
sl@0
   216
    const {
sl@0
   217
    return !(*this < rhs);
sl@0
   218
  }
sl@0
   219
sl@0
   220
sl@0
   221
#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
sl@0
   222
protected:
sl@0
   223
  template <typename,std::size_t> friend class multi_array_impl_base;
sl@0
   224
  template <typename,std::size_t,typename> friend class const_multi_array_view;
sl@0
   225
#else
sl@0
   226
public: // should be protected
sl@0
   227
#endif
sl@0
   228
sl@0
   229
  // This constructor is used by multi_array_impl_base::generate_array_view
sl@0
   230
  // to create strides  
sl@0
   231
  template <typename ExtentList, typename Index>
sl@0
   232
  explicit const_multi_array_view(TPtr base,
sl@0
   233
                           const ExtentList& extents,
sl@0
   234
                           const boost::array<Index,NumDims>& strides): 
sl@0
   235
    base_(base), origin_offset_(0) {
sl@0
   236
sl@0
   237
    index_base_list_.assign(0);
sl@0
   238
sl@0
   239
    // Get the extents and strides
sl@0
   240
    boost::detail::multi_array::
sl@0
   241
      copy_n(extents.begin(),NumDims,extent_list_.begin());
sl@0
   242
    boost::detail::multi_array::
sl@0
   243
      copy_n(strides.begin(),NumDims,stride_list_.begin());
sl@0
   244
sl@0
   245
    // Calculate the array size
sl@0
   246
    num_elements_ = std::accumulate(extent_list_.begin(),extent_list_.end(),
sl@0
   247
                            size_type(1),std::multiplies<size_type>());
sl@0
   248
#if 0
sl@0
   249
    assert(num_elements_ != 0);
sl@0
   250
#endif
sl@0
   251
  }
sl@0
   252
sl@0
   253
  typedef boost::array<size_type,NumDims> size_list;
sl@0
   254
  typedef boost::array<index,NumDims> index_list;
sl@0
   255
sl@0
   256
  TPtr base_;
sl@0
   257
  index origin_offset_;
sl@0
   258
  size_type num_elements_;
sl@0
   259
  size_list extent_list_;
sl@0
   260
  index_list stride_list_;
sl@0
   261
  index_list index_base_list_;
sl@0
   262
sl@0
   263
private:
sl@0
   264
  // const_multi_array_view cannot be assigned to (no deep copies!)
sl@0
   265
  const_multi_array_view& operator=(const const_multi_array_view& other);
sl@0
   266
};
sl@0
   267
sl@0
   268
sl@0
   269
template <typename T, std::size_t NumDims>
sl@0
   270
class multi_array_view :
sl@0
   271
  public const_multi_array_view<T,NumDims,T*>
sl@0
   272
{
sl@0
   273
  typedef const_multi_array_view<T,NumDims,T*> super_type;
sl@0
   274
public: 
sl@0
   275
  typedef typename super_type::value_type value_type;
sl@0
   276
  typedef typename super_type::reference reference;
sl@0
   277
  typedef typename super_type::iterator iterator;
sl@0
   278
  typedef typename super_type::reverse_iterator reverse_iterator;
sl@0
   279
  typedef typename super_type::const_reference const_reference;
sl@0
   280
  typedef typename super_type::const_iterator const_iterator;
sl@0
   281
  typedef typename super_type::const_reverse_iterator const_reverse_iterator;
sl@0
   282
  typedef typename super_type::element element;
sl@0
   283
  typedef typename super_type::size_type size_type;
sl@0
   284
  typedef typename super_type::difference_type difference_type;
sl@0
   285
  typedef typename super_type::index index;
sl@0
   286
  typedef typename super_type::extent_range extent_range;
sl@0
   287
sl@0
   288
  // template typedefs
sl@0
   289
  template <std::size_t NDims>
sl@0
   290
  struct const_array_view {
sl@0
   291
    typedef boost::detail::multi_array::const_multi_array_view<T,NDims> type;
sl@0
   292
  };
sl@0
   293
sl@0
   294
  template <std::size_t NDims>
sl@0
   295
  struct array_view {
sl@0
   296
    typedef boost::detail::multi_array::multi_array_view<T,NDims> type;
sl@0
   297
  };
sl@0
   298
sl@0
   299
  // Assignment from other ConstMultiArray types.
sl@0
   300
  template <typename ConstMultiArray>
sl@0
   301
  multi_array_view& operator=(const ConstMultiArray& other) {
sl@0
   302
    function_requires< 
sl@0
   303
      boost::detail::multi_array::
sl@0
   304
      ConstMultiArrayConcept<ConstMultiArray,NumDims> >();
sl@0
   305
sl@0
   306
    // make sure the dimensions agree
sl@0
   307
    assert(other.num_dimensions() == this->num_dimensions());
sl@0
   308
    assert(std::equal(other.shape(),other.shape()+this->num_dimensions(),
sl@0
   309
                      this->shape()));
sl@0
   310
    // iterator-based copy
sl@0
   311
    std::copy(other.begin(),other.end(),begin());
sl@0
   312
    return *this;
sl@0
   313
  }
sl@0
   314
sl@0
   315
sl@0
   316
  multi_array_view& operator=(const multi_array_view& other) {
sl@0
   317
    if (&other != this) {
sl@0
   318
      // make sure the dimensions agree
sl@0
   319
      assert(other.num_dimensions() == this->num_dimensions());
sl@0
   320
      assert(std::equal(other.shape(),other.shape()+this->num_dimensions(),
sl@0
   321
                        this->shape()));
sl@0
   322
      // iterator-based copy
sl@0
   323
      std::copy(other.begin(),other.end(),begin());
sl@0
   324
    }
sl@0
   325
    return *this;
sl@0
   326
  }
sl@0
   327
sl@0
   328
  element* origin() { return this->base_+this->origin_offset_; }
sl@0
   329
sl@0
   330
  template <class IndexList>
sl@0
   331
  element& operator()(const IndexList& indices) {
sl@0
   332
    boost::function_requires<
sl@0
   333
      detail::multi_array::CollectionConcept<IndexList> >();
sl@0
   334
    return super_type::access_element(boost::type<element&>(),
sl@0
   335
                                      indices,origin(),
sl@0
   336
                                      this->shape(),this->strides(),
sl@0
   337
                                      this->index_bases());
sl@0
   338
  }
sl@0
   339
sl@0
   340
sl@0
   341
  reference operator[](index idx) {
sl@0
   342
    return super_type::access(boost::type<reference>(),
sl@0
   343
                              idx,origin(),
sl@0
   344
                              this->shape(),this->strides(),
sl@0
   345
                              this->index_bases());
sl@0
   346
  }
sl@0
   347
sl@0
   348
sl@0
   349
  // see generate_array_view in base.hpp
sl@0
   350
#if !defined(BOOST_MSVC) || BOOST_MSVC > 1300
sl@0
   351
  template <int NDims>
sl@0
   352
#else
sl@0
   353
  template <int NumDims, int NDims> // else ICE
sl@0
   354
#endif // BOOST_MSVC
sl@0
   355
  typename array_view<NDims>::type 
sl@0
   356
  operator[](const boost::detail::multi_array::
sl@0
   357
             index_gen<NumDims,NDims>& indices) {
sl@0
   358
    typedef typename array_view<NDims>::type return_type;
sl@0
   359
    return
sl@0
   360
      super_type::generate_array_view(boost::type<return_type>(),
sl@0
   361
                                      indices,
sl@0
   362
                                      this->shape(),
sl@0
   363
                                      this->strides(),
sl@0
   364
                                      this->index_bases(),
sl@0
   365
                                      origin());
sl@0
   366
  }
sl@0
   367
  
sl@0
   368
  
sl@0
   369
  iterator begin() {
sl@0
   370
    return iterator(*this->index_bases(),origin(),
sl@0
   371
                    this->shape(),this->strides(),
sl@0
   372
                    this->index_bases());
sl@0
   373
  }
sl@0
   374
sl@0
   375
  iterator end() {
sl@0
   376
    return iterator(*this->index_bases()+(index)*this->shape(),origin(),
sl@0
   377
                    this->shape(),this->strides(),
sl@0
   378
                    this->index_bases());
sl@0
   379
  }
sl@0
   380
sl@0
   381
  reverse_iterator rbegin() {
sl@0
   382
    return reverse_iterator(end());
sl@0
   383
  }
sl@0
   384
sl@0
   385
  reverse_iterator rend() {
sl@0
   386
    return reverse_iterator(begin());
sl@0
   387
  }
sl@0
   388
sl@0
   389
  // Using declarations don't seem to work for g++
sl@0
   390
  // These are the proxies to work around this.
sl@0
   391
sl@0
   392
  const element* origin() const { return super_type::origin(); }
sl@0
   393
sl@0
   394
  template <class IndexList>
sl@0
   395
  const element& operator()(const IndexList& indices) const {
sl@0
   396
    boost::function_requires<
sl@0
   397
      detail::multi_array::CollectionConcept<IndexList> >();
sl@0
   398
    return super_type::operator()(indices);
sl@0
   399
  }
sl@0
   400
sl@0
   401
  const_reference operator[](index idx) const {
sl@0
   402
    return super_type::operator[](idx);
sl@0
   403
  }
sl@0
   404
sl@0
   405
  // see generate_array_view in base.hpp
sl@0
   406
#if !defined(BOOST_MSVC) || BOOST_MSVC > 1300
sl@0
   407
  template <int NDims>
sl@0
   408
#else
sl@0
   409
  template <int NumDims, int NDims> // else ICE
sl@0
   410
#endif // BOOST_MSVC
sl@0
   411
  typename const_array_view<NDims>::type 
sl@0
   412
  operator[](const boost::detail::multi_array::
sl@0
   413
             index_gen<NumDims,NDims>& indices)
sl@0
   414
    const {
sl@0
   415
    return super_type::operator[](indices);
sl@0
   416
  }
sl@0
   417
  
sl@0
   418
  const_iterator begin() const {
sl@0
   419
    return super_type::begin();
sl@0
   420
  }
sl@0
   421
sl@0
   422
  const_iterator end() const {
sl@0
   423
    return super_type::end();
sl@0
   424
  }
sl@0
   425
sl@0
   426
  const_reverse_iterator rbegin() const {
sl@0
   427
    return super_type::rbegin();
sl@0
   428
  }
sl@0
   429
sl@0
   430
  const_reverse_iterator rend() const {
sl@0
   431
    return super_type::rend();
sl@0
   432
  }
sl@0
   433
sl@0
   434
#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
sl@0
   435
private:
sl@0
   436
  template <typename,std::size_t> friend class multi_array_impl_base;
sl@0
   437
#else
sl@0
   438
public: // should be private
sl@0
   439
#endif
sl@0
   440
sl@0
   441
  // constructor used by multi_array_impl_base::generate_array_view to
sl@0
   442
  // generate array views
sl@0
   443
  template <typename ExtentList, typename Index>
sl@0
   444
  explicit multi_array_view(T* base,
sl@0
   445
                            const ExtentList& extents,
sl@0
   446
                            const boost::array<Index,NumDims>& strides) :
sl@0
   447
    super_type(base,extents,strides) { }
sl@0
   448
sl@0
   449
};
sl@0
   450
sl@0
   451
} // namespace multi_array
sl@0
   452
} // namespace detail
sl@0
   453
sl@0
   454
//
sl@0
   455
// traits classes to get array_view types
sl@0
   456
//
sl@0
   457
template <typename Array, int N>
sl@0
   458
class array_view_gen {
sl@0
   459
  typedef typename Array::element element;
sl@0
   460
public:
sl@0
   461
  typedef boost::detail::multi_array::multi_array_view<element,N> type;
sl@0
   462
};
sl@0
   463
sl@0
   464
template <typename Array, int N>
sl@0
   465
class const_array_view_gen {
sl@0
   466
  typedef typename Array::element element;
sl@0
   467
public:
sl@0
   468
  typedef boost::detail::multi_array::const_multi_array_view<element,N> type;  
sl@0
   469
};
sl@0
   470
sl@0
   471
} // namespace boost
sl@0
   472
sl@0
   473
#endif // BOOST_MULTI_ARRAY_VIEW_RG071301_HPP
sl@0
   474