os/ossrv/ossrv_pub/boost_apis/boost/multi_array/subarray.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 SUBARRAY_RG071801_HPP
sl@0
    14
#define SUBARRAY_RG071801_HPP
sl@0
    15
sl@0
    16
//
sl@0
    17
// subarray.hpp - used to implement standard operator[] on
sl@0
    18
// multi_arrays
sl@0
    19
//
sl@0
    20
sl@0
    21
#include "boost/multi_array/base.hpp"
sl@0
    22
#include "boost/multi_array/concept_checks.hpp"
sl@0
    23
#include "boost/limits.hpp"
sl@0
    24
#include "boost/type.hpp"
sl@0
    25
#include <algorithm>
sl@0
    26
#include <cstddef>
sl@0
    27
#include <functional>
sl@0
    28
sl@0
    29
namespace boost {
sl@0
    30
namespace detail {
sl@0
    31
namespace multi_array {
sl@0
    32
sl@0
    33
//
sl@0
    34
// const_sub_array
sl@0
    35
//    multi_array's proxy class to allow multiple overloads of
sl@0
    36
//    operator[] in order to provide a clean multi-dimensional array 
sl@0
    37
//    interface.
sl@0
    38
template <typename T, std::size_t NumDims, typename TPtr>
sl@0
    39
class const_sub_array :
sl@0
    40
  public boost::detail::multi_array::multi_array_impl_base<T,NumDims>
sl@0
    41
{
sl@0
    42
  typedef boost::detail::multi_array::multi_array_impl_base<T,NumDims> super_type;
sl@0
    43
public: 
sl@0
    44
  typedef typename super_type::value_type value_type;
sl@0
    45
  typedef typename super_type::const_reference const_reference;
sl@0
    46
  typedef typename super_type::const_iterator const_iterator;
sl@0
    47
  typedef typename super_type::const_reverse_iterator const_reverse_iterator;
sl@0
    48
  typedef typename super_type::element element;
sl@0
    49
  typedef typename super_type::size_type size_type;
sl@0
    50
  typedef typename super_type::difference_type difference_type;
sl@0
    51
  typedef typename super_type::index index;
sl@0
    52
  typedef typename super_type::extent_range extent_range;
sl@0
    53
sl@0
    54
  // template typedefs
sl@0
    55
  template <std::size_t NDims>
sl@0
    56
  struct const_array_view {
sl@0
    57
    typedef boost::detail::multi_array::const_multi_array_view<T,NDims> type;
sl@0
    58
  };
sl@0
    59
sl@0
    60
  template <std::size_t NDims>
sl@0
    61
  struct array_view {
sl@0
    62
    typedef boost::detail::multi_array::multi_array_view<T,NDims> type;
sl@0
    63
  };
sl@0
    64
sl@0
    65
  // Allow default copy constructor as well.
sl@0
    66
sl@0
    67
  template <typename OPtr>
sl@0
    68
  const_sub_array (const const_sub_array<T,NumDims,OPtr>& rhs) :
sl@0
    69
    base_(rhs.base_), extents_(rhs.extents_), strides_(rhs.strides_),
sl@0
    70
    index_base_(rhs.index_base_) {
sl@0
    71
  }
sl@0
    72
sl@0
    73
  // const_sub_array always returns const types, regardless of its own
sl@0
    74
  // constness.
sl@0
    75
  const_reference operator[](index idx) const {
sl@0
    76
    return super_type::access(boost::type<const_reference>(),
sl@0
    77
                              idx,base_,shape(),strides(),index_bases());
sl@0
    78
  }
sl@0
    79
  
sl@0
    80
  template <typename IndexList>
sl@0
    81
  const element& operator()(const IndexList& indices) const {
sl@0
    82
    boost::function_requires<
sl@0
    83
      detail::multi_array::CollectionConcept<IndexList> >();
sl@0
    84
    return super_type::access_element(boost::type<const element&>(),
sl@0
    85
                                      indices,origin(),
sl@0
    86
                                      shape(),strides(),index_bases());
sl@0
    87
  }
sl@0
    88
sl@0
    89
  // see generate_array_view in base.hpp
sl@0
    90
#if !defined(BOOST_MSVC) || BOOST_MSVC > 1300
sl@0
    91
  template <int NDims>
sl@0
    92
#else
sl@0
    93
  template <int NumDims, int NDims> // else ICE
sl@0
    94
#endif // BOOST_MSVC
sl@0
    95
  typename const_array_view<NDims>::type 
sl@0
    96
  operator[](const boost::detail::multi_array::
sl@0
    97
             index_gen<NumDims,NDims>& indices)
sl@0
    98
    const {
sl@0
    99
    typedef typename const_array_view<NDims>::type return_type;
sl@0
   100
    return
sl@0
   101
      super_type::generate_array_view(boost::type<return_type>(),
sl@0
   102
                                      indices,
sl@0
   103
                                      shape(),
sl@0
   104
                                      strides(),
sl@0
   105
                                      index_bases(),
sl@0
   106
                                      base_);
sl@0
   107
  }
sl@0
   108
sl@0
   109
  template <typename OPtr>
sl@0
   110
  bool operator<(const const_sub_array<T,NumDims,OPtr>& rhs) const {
sl@0
   111
    return std::lexicographical_compare(begin(),end(),rhs.begin(),rhs.end());
sl@0
   112
  }
sl@0
   113
sl@0
   114
  template <typename OPtr>
sl@0
   115
  bool operator==(const const_sub_array<T,NumDims,OPtr>& rhs) const {
sl@0
   116
    if(std::equal(shape(),shape()+num_dimensions(),rhs.shape()))
sl@0
   117
      return std::equal(begin(),end(),rhs.begin());
sl@0
   118
    else return false;
sl@0
   119
  }
sl@0
   120
sl@0
   121
  template <typename OPtr>
sl@0
   122
  bool operator!=(const const_sub_array<T,NumDims,OPtr>& rhs) const {
sl@0
   123
    return !(*this == rhs);
sl@0
   124
  }
sl@0
   125
sl@0
   126
  template <typename OPtr>
sl@0
   127
  bool operator>(const const_sub_array<T,NumDims,OPtr>& rhs) const {
sl@0
   128
    return rhs < *this;
sl@0
   129
  }
sl@0
   130
sl@0
   131
  template <typename OPtr>
sl@0
   132
  bool operator<=(const const_sub_array<T,NumDims,OPtr>& rhs) const {
sl@0
   133
    return !(*this > rhs);
sl@0
   134
  }
sl@0
   135
sl@0
   136
  template <typename OPtr>
sl@0
   137
  bool operator>=(const const_sub_array<T,NumDims,OPtr>& rhs) const {
sl@0
   138
    return !(*this < rhs);
sl@0
   139
  }
sl@0
   140
sl@0
   141
  const_iterator begin() const {
sl@0
   142
    return const_iterator(*index_bases(),origin(),
sl@0
   143
                          shape(),strides(),index_bases());
sl@0
   144
  }
sl@0
   145
sl@0
   146
  const_iterator end() const {
sl@0
   147
    return const_iterator(*index_bases()+(index)*shape(),origin(),
sl@0
   148
                          shape(),strides(),index_bases());
sl@0
   149
  }
sl@0
   150
sl@0
   151
  const_reverse_iterator rbegin() const {
sl@0
   152
    return const_reverse_iterator(end());
sl@0
   153
  }
sl@0
   154
sl@0
   155
  const_reverse_iterator rend() const {
sl@0
   156
    return const_reverse_iterator(begin());
sl@0
   157
  }
sl@0
   158
sl@0
   159
  TPtr origin() const { return base_; }
sl@0
   160
  size_type size() const { return extents_[0]; }
sl@0
   161
  size_type max_size() const { return num_elements(); }
sl@0
   162
  bool empty() const { return size() == 0; }
sl@0
   163
  size_type num_dimensions() const { return NumDims; }
sl@0
   164
  const size_type*  shape() const { return extents_; }
sl@0
   165
  const index* strides() const { return strides_; }
sl@0
   166
  const index* index_bases() const { return index_base_; }
sl@0
   167
sl@0
   168
  size_type num_elements() const { 
sl@0
   169
    return std::accumulate(shape(),shape() + num_dimensions(),
sl@0
   170
                           size_type(1), std::multiplies<size_type>());
sl@0
   171
  }
sl@0
   172
sl@0
   173
sl@0
   174
#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
sl@0
   175
protected:
sl@0
   176
  template <typename,std::size_t> friend class value_accessor_n;  
sl@0
   177
  template <typename,std::size_t,typename> friend class const_sub_array;
sl@0
   178
#else    
sl@0
   179
public:  // Should be protected
sl@0
   180
#endif
sl@0
   181
sl@0
   182
  const_sub_array (TPtr base,
sl@0
   183
                 const size_type* extents,
sl@0
   184
                 const index* strides,
sl@0
   185
                 const index* index_base) :
sl@0
   186
    base_(base), extents_(extents), strides_(strides),
sl@0
   187
    index_base_(index_base) {
sl@0
   188
  }
sl@0
   189
sl@0
   190
  TPtr base_;
sl@0
   191
  const size_type* extents_;
sl@0
   192
  const index* strides_;
sl@0
   193
  const index* index_base_;
sl@0
   194
private:
sl@0
   195
  // const_sub_array cannot be assigned to (no deep copies!)
sl@0
   196
  const_sub_array& operator=(const const_sub_array&);
sl@0
   197
};
sl@0
   198
sl@0
   199
sl@0
   200
//
sl@0
   201
// sub_array
sl@0
   202
//    multi_array's proxy class to allow multiple overloads of
sl@0
   203
//    operator[] in order to provide a clean multi-dimensional array 
sl@0
   204
//    interface.
sl@0
   205
template <typename T, std::size_t NumDims>
sl@0
   206
class sub_array : public const_sub_array<T,NumDims,T*>
sl@0
   207
{
sl@0
   208
  typedef const_sub_array<T,NumDims,T*> super_type;
sl@0
   209
public: 
sl@0
   210
  typedef typename super_type::element element;
sl@0
   211
  typedef typename super_type::reference reference;
sl@0
   212
  typedef typename super_type::index index;
sl@0
   213
  typedef typename super_type::size_type size_type;
sl@0
   214
  typedef typename super_type::iterator iterator;
sl@0
   215
  typedef typename super_type::reverse_iterator reverse_iterator;
sl@0
   216
  typedef typename super_type::const_reference const_reference;
sl@0
   217
  typedef typename super_type::const_iterator const_iterator;
sl@0
   218
  typedef typename super_type::const_reverse_iterator const_reverse_iterator;
sl@0
   219
sl@0
   220
  // template typedefs
sl@0
   221
  template <std::size_t NDims>
sl@0
   222
  struct const_array_view {
sl@0
   223
    typedef boost::detail::multi_array::const_multi_array_view<T,NDims> type;
sl@0
   224
  };
sl@0
   225
sl@0
   226
  template <std::size_t NDims>
sl@0
   227
  struct array_view {
sl@0
   228
    typedef boost::detail::multi_array::multi_array_view<T,NDims> type;
sl@0
   229
  };
sl@0
   230
sl@0
   231
  // Assignment from other ConstMultiArray types.
sl@0
   232
  template <typename ConstMultiArray>
sl@0
   233
  sub_array& operator=(const ConstMultiArray& other) {
sl@0
   234
    function_requires< boost::detail::multi_array::ConstMultiArrayConcept< 
sl@0
   235
        ConstMultiArray, NumDims> >();
sl@0
   236
sl@0
   237
    // make sure the dimensions agree
sl@0
   238
    assert(other.num_dimensions() == this->num_dimensions());
sl@0
   239
    assert(std::equal(other.shape(),other.shape()+this->num_dimensions(),
sl@0
   240
                      this->shape()));
sl@0
   241
    // iterator-based copy
sl@0
   242
    std::copy(other.begin(),other.end(),begin());
sl@0
   243
    return *this;
sl@0
   244
  }
sl@0
   245
sl@0
   246
sl@0
   247
  sub_array& operator=(const sub_array& other) {
sl@0
   248
    if (&other != this) {
sl@0
   249
      // make sure the dimensions agree
sl@0
   250
      assert(other.num_dimensions() == this->num_dimensions());
sl@0
   251
      assert(std::equal(other.shape(),other.shape()+this->num_dimensions(),
sl@0
   252
                        this->shape()));
sl@0
   253
      // iterator-based copy
sl@0
   254
      std::copy(other.begin(),other.end(),begin());
sl@0
   255
    }
sl@0
   256
    return *this;
sl@0
   257
  }
sl@0
   258
sl@0
   259
  T* origin() { return this->base_; }
sl@0
   260
  const T* origin() const { return this->base_; }
sl@0
   261
sl@0
   262
  reference operator[](index idx) {
sl@0
   263
    return super_type::access(boost::type<reference>(),
sl@0
   264
                              idx,this->base_,this->shape(),this->strides(),
sl@0
   265
                              this->index_bases());
sl@0
   266
  }
sl@0
   267
sl@0
   268
  // see generate_array_view in base.hpp
sl@0
   269
#if !defined(BOOST_MSVC) || BOOST_MSVC > 1300
sl@0
   270
  template <int NDims>
sl@0
   271
#else
sl@0
   272
  template <int NumDims, int NDims> // else ICE
sl@0
   273
#endif // BOOST_MSVC
sl@0
   274
  typename array_view<NDims>::type 
sl@0
   275
  operator[](const boost::detail::multi_array::
sl@0
   276
             index_gen<NumDims,NDims>& indices) {
sl@0
   277
    typedef typename array_view<NDims>::type return_type;
sl@0
   278
    return
sl@0
   279
      super_type::generate_array_view(boost::type<return_type>(),
sl@0
   280
                                      indices,
sl@0
   281
                                      this->shape(),
sl@0
   282
                                      this->strides(),
sl@0
   283
                                      this->index_bases(),
sl@0
   284
                                      origin());
sl@0
   285
  }
sl@0
   286
sl@0
   287
  template <class IndexList>
sl@0
   288
  element& operator()(const IndexList& indices) {
sl@0
   289
    boost::function_requires<
sl@0
   290
      detail::multi_array::CollectionConcept<IndexList> >();
sl@0
   291
    return super_type::access_element(boost::type<element&>(),
sl@0
   292
                                      indices,origin(),
sl@0
   293
                                      this->shape(),this->strides(),
sl@0
   294
                                      this->index_bases());
sl@0
   295
  }
sl@0
   296
sl@0
   297
  iterator begin() {
sl@0
   298
    return iterator(*this->index_bases(),origin(),
sl@0
   299
                    this->shape(),this->strides(),this->index_bases());
sl@0
   300
  }
sl@0
   301
sl@0
   302
  iterator end() {
sl@0
   303
    return iterator(*this->index_bases()+(index)*this->shape(),origin(),
sl@0
   304
                    this->shape(),this->strides(),this->index_bases());
sl@0
   305
  }
sl@0
   306
sl@0
   307
  // RG - rbegin() and rend() written naively to thwart MSVC ICE.
sl@0
   308
  reverse_iterator rbegin() {
sl@0
   309
    reverse_iterator ri(end());
sl@0
   310
    return ri;
sl@0
   311
  }
sl@0
   312
sl@0
   313
  reverse_iterator rend() {
sl@0
   314
    reverse_iterator ri(begin());
sl@0
   315
    return ri;
sl@0
   316
  }
sl@0
   317
sl@0
   318
  //
sl@0
   319
  // proxies
sl@0
   320
  //
sl@0
   321
sl@0
   322
  template <class IndexList>
sl@0
   323
  const element& operator()(const IndexList& indices) const {
sl@0
   324
    boost::function_requires<
sl@0
   325
      detail::multi_array::CollectionConcept<IndexList> >();
sl@0
   326
    return super_type::operator()(indices);
sl@0
   327
  }
sl@0
   328
sl@0
   329
  const_reference operator[](index idx) const {
sl@0
   330
    return super_type::operator[](idx);
sl@0
   331
  }
sl@0
   332
sl@0
   333
  // see generate_array_view in base.hpp
sl@0
   334
#if !defined(BOOST_MSVC) || BOOST_MSVC > 1300
sl@0
   335
  template <int NDims>
sl@0
   336
#else
sl@0
   337
  template <int NumDims, int NDims> // else ICE
sl@0
   338
#endif // BOOST_MSVC
sl@0
   339
  typename const_array_view<NDims>::type 
sl@0
   340
  operator[](const boost::detail::multi_array::
sl@0
   341
             index_gen<NumDims,NDims>& indices)
sl@0
   342
    const {
sl@0
   343
    return super_type::operator[](indices);
sl@0
   344
  }
sl@0
   345
sl@0
   346
  const_iterator begin() const {
sl@0
   347
    return super_type::begin();
sl@0
   348
  }
sl@0
   349
  
sl@0
   350
  const_iterator end() const {
sl@0
   351
    return super_type::end();
sl@0
   352
  }
sl@0
   353
sl@0
   354
  const_reverse_iterator rbegin() const {
sl@0
   355
    return super_type::rbegin();
sl@0
   356
  }
sl@0
   357
sl@0
   358
  const_reverse_iterator rend() const {
sl@0
   359
    return super_type::rend();
sl@0
   360
  }
sl@0
   361
sl@0
   362
#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
sl@0
   363
private:
sl@0
   364
  template <typename,std::size_t> friend class value_accessor_n;
sl@0
   365
#else
sl@0
   366
public: // should be private
sl@0
   367
#endif
sl@0
   368
sl@0
   369
  sub_array (T* base,
sl@0
   370
            const size_type* extents,
sl@0
   371
            const index* strides,
sl@0
   372
            const index* index_base) :
sl@0
   373
    super_type(base,extents,strides,index_base) {
sl@0
   374
  }
sl@0
   375
sl@0
   376
};
sl@0
   377
sl@0
   378
} // namespace multi_array
sl@0
   379
} // namespace detail
sl@0
   380
//
sl@0
   381
// traits classes to get sub_array types
sl@0
   382
//
sl@0
   383
template <typename Array, int N>
sl@0
   384
class subarray_gen {
sl@0
   385
  typedef typename Array::element element;
sl@0
   386
public:
sl@0
   387
  typedef boost::detail::multi_array::sub_array<element,N> type;
sl@0
   388
};
sl@0
   389
sl@0
   390
template <typename Array, int N>
sl@0
   391
class const_subarray_gen {
sl@0
   392
  typedef typename Array::element element;
sl@0
   393
public:
sl@0
   394
  typedef boost::detail::multi_array::const_sub_array<element,N> type;  
sl@0
   395
};
sl@0
   396
} // namespace boost
sl@0
   397
  
sl@0
   398
#endif // SUBARRAY_RG071801_HPP