os/ossrv/ossrv_pub/boost_apis/boost/multi_array/base.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 BASE_RG071801_HPP
sl@0
    14
#define BASE_RG071801_HPP
sl@0
    15
sl@0
    16
//
sl@0
    17
// base.hpp - some implementation base classes for from which
sl@0
    18
// functionality is acquired
sl@0
    19
//
sl@0
    20
sl@0
    21
#include "boost/multi_array/extent_range.hpp"
sl@0
    22
#include "boost/multi_array/extent_gen.hpp"
sl@0
    23
#include "boost/multi_array/index_range.hpp"
sl@0
    24
#include "boost/multi_array/index_gen.hpp"
sl@0
    25
#include "boost/multi_array/storage_order.hpp"
sl@0
    26
#include "boost/multi_array/types.hpp"
sl@0
    27
#include "boost/config.hpp"
sl@0
    28
#include "boost/multi_array/concept_checks.hpp" //for ignore_unused_...
sl@0
    29
#include "boost/mpl/eval_if.hpp"
sl@0
    30
#include "boost/mpl/if.hpp"
sl@0
    31
#include "boost/mpl/size_t.hpp"
sl@0
    32
#include "boost/mpl/aux_/msvc_eti_base.hpp"
sl@0
    33
#include "boost/iterator/reverse_iterator.hpp"
sl@0
    34
#include "boost/static_assert.hpp"
sl@0
    35
#include "boost/type.hpp"
sl@0
    36
#include "boost/assert.hpp"
sl@0
    37
#include <cstddef>
sl@0
    38
#include <memory>
sl@0
    39
sl@0
    40
namespace boost {
sl@0
    41
sl@0
    42
/////////////////////////////////////////////////////////////////////////
sl@0
    43
// class declarations
sl@0
    44
/////////////////////////////////////////////////////////////////////////
sl@0
    45
sl@0
    46
template<typename T, std::size_t NumDims,
sl@0
    47
  typename Allocator = std::allocator<T> >
sl@0
    48
class multi_array;
sl@0
    49
sl@0
    50
// This is a public interface for use by end users!
sl@0
    51
namespace multi_array_types {
sl@0
    52
  typedef boost::detail::multi_array::size_type size_type;
sl@0
    53
  typedef std::ptrdiff_t difference_type;
sl@0
    54
  typedef boost::detail::multi_array::index index;
sl@0
    55
  typedef detail::multi_array::index_range<index,size_type> index_range;
sl@0
    56
  typedef detail::multi_array::extent_range<index,size_type> extent_range;
sl@0
    57
  typedef detail::multi_array::index_gen<0,0> index_gen;
sl@0
    58
  typedef detail::multi_array::extent_gen<0> extent_gen;
sl@0
    59
}
sl@0
    60
sl@0
    61
sl@0
    62
// boost::extents and boost::indices are now a part of the public
sl@0
    63
// interface.  That way users don't necessarily have to create their 
sl@0
    64
// own objects.  On the other hand, one may not want the overhead of 
sl@0
    65
// object creation in small-memory environments.  Thus, the objects
sl@0
    66
// can be left undefined by defining BOOST_MULTI_ARRAY_NO_GENERATORS 
sl@0
    67
// before loading multi_array.hpp.
sl@0
    68
#if !BOOST_MULTI_ARRAY_NO_GENERATORS
sl@0
    69
namespace {
sl@0
    70
  multi_array_types::extent_gen extents;
sl@0
    71
  multi_array_types::index_gen indices;
sl@0
    72
}
sl@0
    73
#endif // BOOST_MULTI_ARRAY_NO_GENERATORS
sl@0
    74
sl@0
    75
namespace detail {
sl@0
    76
namespace multi_array {
sl@0
    77
sl@0
    78
template <typename T, std::size_t NumDims>
sl@0
    79
class sub_array;
sl@0
    80
sl@0
    81
template <typename T, std::size_t NumDims, typename TPtr = const T*>
sl@0
    82
class const_sub_array;
sl@0
    83
sl@0
    84
template <typename T, typename TPtr, typename NumDims, typename Reference>
sl@0
    85
class array_iterator;
sl@0
    86
sl@0
    87
template <typename T, std::size_t NumDims, typename TPtr = const T*>
sl@0
    88
class const_multi_array_view;
sl@0
    89
sl@0
    90
template <typename T, std::size_t NumDims>
sl@0
    91
class multi_array_view;
sl@0
    92
sl@0
    93
/////////////////////////////////////////////////////////////////////////
sl@0
    94
// class interfaces
sl@0
    95
/////////////////////////////////////////////////////////////////////////
sl@0
    96
sl@0
    97
class multi_array_base {
sl@0
    98
public:
sl@0
    99
  typedef multi_array_types::size_type size_type;
sl@0
   100
  typedef multi_array_types::difference_type difference_type;
sl@0
   101
  typedef multi_array_types::index index;
sl@0
   102
  typedef multi_array_types::index_range index_range;
sl@0
   103
  typedef multi_array_types::extent_range extent_range;
sl@0
   104
  typedef multi_array_types::index_gen index_gen;
sl@0
   105
  typedef multi_array_types::extent_gen extent_gen;
sl@0
   106
};
sl@0
   107
sl@0
   108
//
sl@0
   109
// value_accessor_n
sl@0
   110
//  contains the routines for accessing elements from
sl@0
   111
//  N-dimensional views.
sl@0
   112
//
sl@0
   113
template<typename T, std::size_t NumDims>
sl@0
   114
class value_accessor_n : public multi_array_base {
sl@0
   115
  typedef multi_array_base super_type;
sl@0
   116
public:
sl@0
   117
  typedef typename super_type::index index;
sl@0
   118
sl@0
   119
  // 
sl@0
   120
  // public typedefs used by classes that inherit from this base
sl@0
   121
  //
sl@0
   122
  typedef T element;
sl@0
   123
  typedef boost::multi_array<T,NumDims-1> value_type;
sl@0
   124
  typedef sub_array<T,NumDims-1> reference;
sl@0
   125
  typedef const_sub_array<T,NumDims-1> const_reference;
sl@0
   126
sl@0
   127
protected:
sl@0
   128
  // used by array operator[] and iterators to get reference types.
sl@0
   129
  template <typename Reference, typename TPtr>
sl@0
   130
  Reference access(boost::type<Reference>,index idx,TPtr base,
sl@0
   131
                   const size_type* extents,
sl@0
   132
                   const index* strides,
sl@0
   133
                   const index* index_bases) const {
sl@0
   134
sl@0
   135
    BOOST_ASSERT(idx - index_bases[0] >= 0);
sl@0
   136
    BOOST_ASSERT(size_type(idx - index_bases[0]) < extents[0]);
sl@0
   137
    // return a sub_array<T,NDims-1> proxy object
sl@0
   138
    TPtr newbase = base + idx * strides[0];
sl@0
   139
    return Reference(newbase,extents+1,strides+1,index_bases+1);
sl@0
   140
sl@0
   141
  }
sl@0
   142
sl@0
   143
  value_accessor_n() { }
sl@0
   144
  ~value_accessor_n() { }
sl@0
   145
};
sl@0
   146
sl@0
   147
sl@0
   148
sl@0
   149
//
sl@0
   150
// value_accessor_one
sl@0
   151
//  contains the routines for accessing reference elements from
sl@0
   152
//  1-dimensional views.
sl@0
   153
//
sl@0
   154
template<typename T>
sl@0
   155
class value_accessor_one : public multi_array_base {
sl@0
   156
  typedef multi_array_base super_type;
sl@0
   157
public:
sl@0
   158
  typedef typename super_type::index index;
sl@0
   159
  //
sl@0
   160
  // public typedefs for use by classes that inherit it.
sl@0
   161
  //
sl@0
   162
  typedef T element;
sl@0
   163
  typedef T value_type;
sl@0
   164
  typedef T& reference;
sl@0
   165
  typedef T const& const_reference;
sl@0
   166
sl@0
   167
protected:
sl@0
   168
  // used by array operator[] and iterators to get reference types.
sl@0
   169
  template <typename Reference, typename TPtr>
sl@0
   170
  Reference access(boost::type<Reference>,index idx,TPtr base,
sl@0
   171
                   const size_type* extents,
sl@0
   172
                   const index* strides,
sl@0
   173
                   const index* index_bases) const {
sl@0
   174
sl@0
   175
    ignore_unused_variable_warning(index_bases);
sl@0
   176
    ignore_unused_variable_warning(extents);
sl@0
   177
    BOOST_ASSERT(idx - index_bases[0] >= 0);
sl@0
   178
    BOOST_ASSERT(size_type(idx - index_bases[0]) < extents[0]);
sl@0
   179
    return *(base + idx * strides[0]);
sl@0
   180
  }
sl@0
   181
sl@0
   182
  value_accessor_one() { }
sl@0
   183
  ~value_accessor_one() { }
sl@0
   184
};
sl@0
   185
sl@0
   186
sl@0
   187
/////////////////////////////////////////////////////////////////////////
sl@0
   188
// choose value accessor begins
sl@0
   189
//
sl@0
   190
sl@0
   191
template <typename T, std::size_t NumDims>
sl@0
   192
struct choose_value_accessor_n {
sl@0
   193
  typedef value_accessor_n<T,NumDims> type;
sl@0
   194
};
sl@0
   195
sl@0
   196
template <typename T>
sl@0
   197
struct choose_value_accessor_one {
sl@0
   198
  typedef value_accessor_one<T> type;
sl@0
   199
};
sl@0
   200
sl@0
   201
template <typename T, typename NumDims>
sl@0
   202
struct value_accessor_generator {
sl@0
   203
    BOOST_STATIC_CONSTANT(std::size_t, dimensionality = NumDims::value);
sl@0
   204
    
sl@0
   205
  typedef typename
sl@0
   206
  mpl::eval_if_c<(dimensionality == 1),
sl@0
   207
                  choose_value_accessor_one<T>,
sl@0
   208
                  choose_value_accessor_n<T,dimensionality>
sl@0
   209
  >::type type;
sl@0
   210
};
sl@0
   211
sl@0
   212
#if BOOST_WORKAROUND(BOOST_MSVC, < 1300)
sl@0
   213
sl@0
   214
struct eti_value_accessor
sl@0
   215
{
sl@0
   216
  typedef int index;
sl@0
   217
  typedef int size_type;
sl@0
   218
  typedef int element;
sl@0
   219
  typedef int index_range;
sl@0
   220
  typedef int value_type;
sl@0
   221
  typedef int reference;
sl@0
   222
  typedef int const_reference;
sl@0
   223
};
sl@0
   224
    
sl@0
   225
template <>
sl@0
   226
struct value_accessor_generator<int,int>
sl@0
   227
{
sl@0
   228
  typedef eti_value_accessor type;
sl@0
   229
};
sl@0
   230
sl@0
   231
template <class T, class NumDims>
sl@0
   232
struct associated_types
sl@0
   233
  : mpl::aux::msvc_eti_base<
sl@0
   234
        typename value_accessor_generator<T,NumDims>::type
sl@0
   235
    >::type
sl@0
   236
{};
sl@0
   237
sl@0
   238
template <>
sl@0
   239
struct associated_types<int,int> : eti_value_accessor {};
sl@0
   240
sl@0
   241
#else
sl@0
   242
sl@0
   243
template <class T, class NumDims>
sl@0
   244
struct associated_types
sl@0
   245
  : value_accessor_generator<T,NumDims>::type
sl@0
   246
{};
sl@0
   247
sl@0
   248
#endif
sl@0
   249
sl@0
   250
//
sl@0
   251
// choose value accessor ends
sl@0
   252
/////////////////////////////////////////////////////////////////////////
sl@0
   253
sl@0
   254
sl@0
   255
sl@0
   256
////////////////////////////////////////////////////////////////////////
sl@0
   257
// multi_array_base
sl@0
   258
////////////////////////////////////////////////////////////////////////
sl@0
   259
template <typename T, std::size_t NumDims>
sl@0
   260
class multi_array_impl_base
sl@0
   261
  :
sl@0
   262
#if BOOST_WORKAROUND(BOOST_MSVC, < 1300)
sl@0
   263
      public mpl::aux::msvc_eti_base<
sl@0
   264
          typename value_accessor_generator<T,mpl::size_t<NumDims> >::type
sl@0
   265
       >::type
sl@0
   266
#else
sl@0
   267
      public value_accessor_generator<T,mpl::size_t<NumDims> >::type
sl@0
   268
#endif 
sl@0
   269
{
sl@0
   270
  typedef associated_types<T,mpl::size_t<NumDims> > types;
sl@0
   271
public:
sl@0
   272
  typedef typename types::index index;
sl@0
   273
  typedef typename types::size_type size_type;
sl@0
   274
  typedef typename types::element element;
sl@0
   275
  typedef typename types::index_range index_range;
sl@0
   276
  typedef typename types::value_type value_type;
sl@0
   277
  typedef typename types::reference reference;
sl@0
   278
  typedef typename types::const_reference const_reference;
sl@0
   279
sl@0
   280
  template <std::size_t NDims>
sl@0
   281
  struct subarray {
sl@0
   282
    typedef boost::detail::multi_array::sub_array<T,NDims> type;
sl@0
   283
  };
sl@0
   284
sl@0
   285
  template <std::size_t NDims>
sl@0
   286
  struct const_subarray {
sl@0
   287
    typedef boost::detail::multi_array::const_sub_array<T,NDims> type;
sl@0
   288
  };
sl@0
   289
sl@0
   290
  template <std::size_t NDims>
sl@0
   291
  struct array_view {
sl@0
   292
    typedef boost::detail::multi_array::multi_array_view<T,NDims> type;
sl@0
   293
  };
sl@0
   294
sl@0
   295
  template <std::size_t NDims>
sl@0
   296
  struct const_array_view {
sl@0
   297
  public:
sl@0
   298
    typedef boost::detail::multi_array::const_multi_array_view<T,NDims> type;
sl@0
   299
  };
sl@0
   300
sl@0
   301
  //
sl@0
   302
  // iterator support
sl@0
   303
  //
sl@0
   304
  typedef array_iterator<T,T*,mpl::size_t<NumDims>,reference> iterator;
sl@0
   305
  typedef array_iterator<T,T const*,mpl::size_t<NumDims>,const_reference> const_iterator;
sl@0
   306
sl@0
   307
  typedef ::boost::reverse_iterator<iterator> reverse_iterator;
sl@0
   308
  typedef ::boost::reverse_iterator<const_iterator> const_reverse_iterator;
sl@0
   309
sl@0
   310
  BOOST_STATIC_CONSTANT(std::size_t, dimensionality = NumDims);
sl@0
   311
protected:
sl@0
   312
sl@0
   313
  multi_array_impl_base() { }
sl@0
   314
  ~multi_array_impl_base() { }
sl@0
   315
sl@0
   316
  // Used by operator() in our array classes
sl@0
   317
  template <typename Reference, typename IndexList, typename TPtr>
sl@0
   318
  Reference access_element(boost::type<Reference>,
sl@0
   319
                           const IndexList& indices,
sl@0
   320
                           TPtr base,
sl@0
   321
                           const size_type* extents,
sl@0
   322
                           const index* strides,
sl@0
   323
                           const index* index_bases) const {
sl@0
   324
sl@0
   325
    ignore_unused_variable_warning(index_bases);
sl@0
   326
    ignore_unused_variable_warning(extents);
sl@0
   327
#if !defined(NDEBUG) && !defined(BOOST_DISABLE_ASSERTS)
sl@0
   328
    for (size_type i = 0; i != NumDims; ++i) {
sl@0
   329
      BOOST_ASSERT(indices[i] - index_bases[i] >= 0);
sl@0
   330
      BOOST_ASSERT(size_type(indices[i] - index_bases[i]) < extents[i]);
sl@0
   331
    }
sl@0
   332
#endif
sl@0
   333
sl@0
   334
    index offset = 0;
sl@0
   335
    for (size_type n = 0; n != NumDims; ++n) 
sl@0
   336
      offset += indices[n] * strides[n];
sl@0
   337
    
sl@0
   338
    return base[offset];
sl@0
   339
  }
sl@0
   340
sl@0
   341
  template <typename StrideList, typename ExtentList>
sl@0
   342
  void compute_strides(StrideList& stride_list, ExtentList& extent_list,
sl@0
   343
                       const general_storage_order<NumDims>& storage)
sl@0
   344
  {
sl@0
   345
    // invariant: stride = the stride for dimension n
sl@0
   346
    index stride = 1;
sl@0
   347
    for (size_type n = 0; n != NumDims; ++n) {
sl@0
   348
      index stride_sign = +1;
sl@0
   349
      
sl@0
   350
      if (!storage.ascending(storage.ordering(n)))
sl@0
   351
        stride_sign = -1;
sl@0
   352
      
sl@0
   353
      // The stride for this dimension is the product of the
sl@0
   354
      // lengths of the ranks minor to it.
sl@0
   355
      stride_list[storage.ordering(n)] = stride * stride_sign;
sl@0
   356
      
sl@0
   357
      stride *= extent_list[storage.ordering(n)];
sl@0
   358
    } 
sl@0
   359
  }
sl@0
   360
sl@0
   361
  // This calculates the offset to the array base pointer due to:
sl@0
   362
  // 1. dimensions stored in descending order
sl@0
   363
  // 2. non-zero dimension index bases
sl@0
   364
  template <typename StrideList, typename ExtentList, typename BaseList>
sl@0
   365
  index
sl@0
   366
  calculate_origin_offset(const StrideList& stride_list,
sl@0
   367
                          const ExtentList& extent_list,
sl@0
   368
                          const general_storage_order<NumDims>& storage,
sl@0
   369
                          const BaseList& index_base_list)
sl@0
   370
  {
sl@0
   371
    return
sl@0
   372
      calculate_descending_dimension_offset(stride_list,extent_list,
sl@0
   373
                                            storage) +
sl@0
   374
      calculate_indexing_offset(stride_list,index_base_list);
sl@0
   375
  }
sl@0
   376
sl@0
   377
  // This calculates the offset added to the base pointer that are
sl@0
   378
  // caused by descending dimensions
sl@0
   379
  template <typename StrideList, typename ExtentList>
sl@0
   380
  index
sl@0
   381
  calculate_descending_dimension_offset(const StrideList& stride_list,
sl@0
   382
                                const ExtentList& extent_list,
sl@0
   383
                                const general_storage_order<NumDims>& storage)
sl@0
   384
  {
sl@0
   385
    index offset = 0;
sl@0
   386
    if (!storage.all_dims_ascending()) 
sl@0
   387
      for (size_type n = 0; n != NumDims; ++n)
sl@0
   388
        if (!storage.ascending(n))
sl@0
   389
          offset -= (extent_list[n] - 1) * stride_list[n];
sl@0
   390
sl@0
   391
    return offset;
sl@0
   392
  }
sl@0
   393
sl@0
   394
  // This is used to reindex array_views, which are no longer
sl@0
   395
  // concerned about storage order (specifically, whether dimensions
sl@0
   396
  // are ascending or descending) since the viewed array handled it.
sl@0
   397
sl@0
   398
  template <typename StrideList, typename BaseList>
sl@0
   399
  index
sl@0
   400
  calculate_indexing_offset(const StrideList& stride_list,
sl@0
   401
                          const BaseList& index_base_list)
sl@0
   402
  {
sl@0
   403
    index offset = 0;
sl@0
   404
    for (size_type n = 0; n != NumDims; ++n)
sl@0
   405
        offset -= stride_list[n] * index_base_list[n];
sl@0
   406
    return offset;
sl@0
   407
  }
sl@0
   408
sl@0
   409
  // Slicing using an index_gen.
sl@0
   410
  // Note that populating an index_gen creates a type that encodes
sl@0
   411
  // both the number of dimensions in the current Array (NumDims), and 
sl@0
   412
  // the Number of dimensions for the resulting view.  This allows the 
sl@0
   413
  // compiler to fail if the dimensions aren't completely accounted
sl@0
   414
  // for.  For reasons unbeknownst to me, a BOOST_STATIC_ASSERT
sl@0
   415
  // within the member function template does not work. I should add a 
sl@0
   416
  // note to the documentation specifying that you get a damn ugly
sl@0
   417
  // error message if you screw up in your slicing code.
sl@0
   418
  template <typename ArrayRef, int NDims, typename TPtr>
sl@0
   419
  ArrayRef
sl@0
   420
  generate_array_view(boost::type<ArrayRef>,
sl@0
   421
                      const boost::detail::multi_array::
sl@0
   422
                      index_gen<NumDims,NDims>& indices,
sl@0
   423
                      const size_type* extents,
sl@0
   424
                      const index* strides,
sl@0
   425
                      const index* index_bases,
sl@0
   426
                      TPtr base) const {
sl@0
   427
sl@0
   428
    boost::array<index,NDims> new_strides;
sl@0
   429
    boost::array<index,NDims> new_extents;
sl@0
   430
sl@0
   431
    index offset = 0;
sl@0
   432
    size_type dim = 0;
sl@0
   433
    for (size_type n = 0; n != NumDims; ++n) {
sl@0
   434
      const index default_start = index_bases[n];
sl@0
   435
      const index default_finish = default_start+extents[n];
sl@0
   436
      const index_range& current_range = indices.ranges_[n];
sl@0
   437
      index start = current_range.get_start(default_start);
sl@0
   438
      index finish = current_range.get_finish(default_finish);
sl@0
   439
      index index_factor = current_range.stride();
sl@0
   440
      index len = (finish - start + (index_factor - 1)) / index_factor;
sl@0
   441
sl@0
   442
      BOOST_ASSERT(index_bases[n] <= start &&
sl@0
   443
                   start <= index_bases[n]+index(extents[n]));
sl@0
   444
      BOOST_ASSERT(index_bases[n] <= finish &&
sl@0
   445
                   finish <= index_bases[n]+index(extents[n]));
sl@0
   446
      BOOST_ASSERT(index_factor > 0);
sl@0
   447
sl@0
   448
      // the array data pointer is modified to account for non-zero
sl@0
   449
      // bases during slicing (see [Garcia] for the math involved)
sl@0
   450
      offset += start * strides[n];
sl@0
   451
sl@0
   452
      if (!current_range.is_degenerate()) {
sl@0
   453
sl@0
   454
        // The index_factor for each dimension is included into the
sl@0
   455
        // strides for the array_view (see [Garcia] for the math involved).
sl@0
   456
        new_strides[dim] = index_factor * strides[n];
sl@0
   457
        
sl@0
   458
        // calculate new extents
sl@0
   459
        new_extents[dim] = len;
sl@0
   460
        ++dim;
sl@0
   461
      }
sl@0
   462
    }
sl@0
   463
    BOOST_ASSERT(dim == NDims);
sl@0
   464
sl@0
   465
    return
sl@0
   466
      ArrayRef(base+offset,
sl@0
   467
               new_extents,
sl@0
   468
               new_strides);
sl@0
   469
  }
sl@0
   470
                     
sl@0
   471
sl@0
   472
};
sl@0
   473
sl@0
   474
} // namespace multi_array
sl@0
   475
} // namespace detail
sl@0
   476
sl@0
   477
} // namespace boost
sl@0
   478
sl@0
   479
#endif // BASE_RG071801_HPP