Ticket #6889: multi_array_accept_initial_value.diff

File multi_array_accept_initial_value.diff, 8.8 KB (added by Andrew Morris <andy@…>, 10 years ago)

Proposed change

  • boost/multi_array.hpp

     
    3333#include <functional>
    3434#include <numeric>
    3535#include <vector>
     36#include <memory>
    3637
    3738
    38 
    3939namespace boost {
    4040  namespace detail {
    4141    namespace multi_array {
     
    140140  explicit multi_array() :
    141141    super_type((T*)initial_base_,c_storage_order(),
    142142               /*index_bases=*/0, /*extents=*/0) {
    143     allocate_space();
     143    default_initialize();
    144144  }
    145145
    146146  template <class ExtentList>
     
    155155    super_type((T*)initial_base_,extents) {
    156156    boost::function_requires<
    157157      detail::multi_array::CollectionConcept<ExtentList> >();
    158     allocate_space();
     158    default_initialize();
    159159  }
    160160
     161  template <class ExtentList>
     162  explicit multi_array(
     163      ExtentList const& extents,
     164      const T& value
     165      ) :
     166    super_type((T*)initial_base_,extents) {
     167    boost::function_requires<
     168      detail::multi_array::CollectionConcept<ExtentList> >();
     169    default_initialize(value);
     170  }
    161171   
    162172  template <class ExtentList>
    163173  explicit multi_array(ExtentList const& extents,
     
    165175    super_type((T*)initial_base_,extents,so) {
    166176    boost::function_requires<
    167177      detail::multi_array::CollectionConcept<ExtentList> >();
    168     allocate_space();
     178    default_initialize();
    169179  }
    170180
    171181  template <class ExtentList>
     
    175185    super_type((T*)initial_base_,extents,so), allocator_(alloc) {
    176186    boost::function_requires<
    177187      detail::multi_array::CollectionConcept<ExtentList> >();
    178     allocate_space();
     188    default_initialize();
    179189  }
    180190
    181 
    182191  explicit multi_array(const detail::multi_array
    183192                       ::extent_gen<NumDims>& ranges) :
    184193    super_type((T*)initial_base_,ranges) {
    185194
    186     allocate_space();
     195    default_initialize();
    187196  }
    188197
     198  explicit multi_array(const detail::multi_array
     199                       ::extent_gen<NumDims>& ranges,
     200                       const T& value) :
     201    super_type((T*)initial_base_,ranges) {
    189202
     203    default_initialize(value);
     204  }
     205
     206
    190207  explicit multi_array(const detail::multi_array
    191208                       ::extent_gen<NumDims>& ranges,
    192209                       const general_storage_order<NumDims>& so) :
    193210    super_type((T*)initial_base_,ranges,so) {
    194211
    195     allocate_space();
     212    default_initialize();
    196213  }
    197214
    198215
     
    202219                       Allocator const& alloc) :
    203220    super_type((T*)initial_base_,ranges,so), allocator_(alloc) {
    204221
    205     allocate_space();
     222    default_initialize();
    206223  }
    207224
    208225  multi_array(const multi_array& rhs) :
    209226  super_type(rhs), allocator_(rhs.allocator_) {
    210     allocate_space();
    211     boost::detail::multi_array::copy_n(rhs.base_,rhs.num_elements(),base_);
     227    copy_initialize(rhs);
    212228  }
    213229
    214230
     
    226242              const general_storage_order<NumDims>& so = c_storage_order())
    227243    : super_type(0,so,rhs.index_bases(),rhs.shape())
    228244  {
    229     allocate_space();
     245    default_initialize();
    230246    // Warning! storage order may change, hence the following copy technique.
    231247    std::copy(rhs.begin(),rhs.end(),this->begin());
    232248  }
     
    237253              const general_storage_order<NumDims>& so = c_storage_order())
    238254    : super_type(0,so,rhs.index_bases(),rhs.shape())
    239255  {
    240     allocate_space();
     256    default_initialize();
    241257    std::copy(rhs.begin(),rhs.end(),this->begin());
    242258  }
    243259
     
    248264              const general_storage_order<NumDims>& so = c_storage_order())
    249265    : super_type(0,so,rhs.index_bases(),rhs.shape())
    250266  {
    251     allocate_space();
     267    default_initialize();
    252268    std::copy(rhs.begin(),rhs.end(),this->begin());
    253269  }
    254270
     
    259275  multi_array(const const_multi_array_ref<T,NumDims>& rhs)
    260276    : super_type(0,c_storage_order(),rhs.index_bases(),rhs.shape())
    261277  {
    262     allocate_space();
     278    default_initialize();
    263279    // Warning! storage order may change, hence the following copy technique.
    264280    std::copy(rhs.begin(),rhs.end(),this->begin());
    265281  }
     
    268284              const general_storage_order<NumDims>& so)
    269285    : super_type(0,so,rhs.index_bases(),rhs.shape())
    270286  {
    271     allocate_space();
     287    default_initialize();
    272288    // Warning! storage order may change, hence the following copy technique.
    273289    std::copy(rhs.begin(),rhs.end(),this->begin());
    274290  }
     
    277293              const_sub_array<T,NumDims>& rhs)
    278294    : super_type(0,c_storage_order(),rhs.index_bases(),rhs.shape())
    279295  {
    280     allocate_space();
     296    default_initialize();
    281297    std::copy(rhs.begin(),rhs.end(),this->begin());
    282298  }
    283299
     
    286302              const general_storage_order<NumDims>& so)
    287303    : super_type(0,so,rhs.index_bases(),rhs.shape())
    288304  {
    289     allocate_space();
     305    default_initialize();
    290306    std::copy(rhs.begin(),rhs.end(),this->begin());
    291307  }
    292308
     
    295311              const_multi_array_view<T,NumDims>& rhs)
    296312    : super_type(0,c_storage_order(),rhs.index_bases(),rhs.shape())
    297313  {
    298     allocate_space();
     314    default_initialize();
    299315    std::copy(rhs.begin(),rhs.end(),this->begin());
    300316  }
    301317
     
    304320              const general_storage_order<NumDims>& so)
    305321    : super_type(0,so,rhs.index_bases(),rhs.shape())
    306322  {
    307     allocate_space();
     323    default_initialize();
    308324    std::copy(rhs.begin(),rhs.end(),this->begin());
    309325  }
    310326
     
    314330  multi_array(const multi_array_ref<T,NumDims>& rhs)
    315331    : super_type(0,c_storage_order(),rhs.index_bases(),rhs.shape())
    316332  {
    317     allocate_space();
     333    default_initialize();
    318334    // Warning! storage order may change, hence the following copy technique.
    319335    std::copy(rhs.begin(),rhs.end(),this->begin());
    320336  }
     
    323339              const general_storage_order<NumDims>& so)
    324340    : super_type(0,so,rhs.index_bases(),rhs.shape())
    325341  {
    326     allocate_space();
     342    default_initialize();
    327343    // Warning! storage order may change, hence the following copy technique.
    328344    std::copy(rhs.begin(),rhs.end(),this->begin());
    329345  }
     
    333349              sub_array<T,NumDims>& rhs)
    334350    : super_type(0,c_storage_order(),rhs.index_bases(),rhs.shape())
    335351  {
    336     allocate_space();
     352    default_initialize();
    337353    std::copy(rhs.begin(),rhs.end(),this->begin());
    338354  }
    339355
     
    342358              const general_storage_order<NumDims>& so)
    343359    : super_type(0,so,rhs.index_bases(),rhs.shape())
    344360  {
    345     allocate_space();
     361    default_initialize();
    346362    std::copy(rhs.begin(),rhs.end(),this->begin());
    347363  }
    348364
     
    351367              multi_array_view<T,NumDims>& rhs)
    352368    : super_type(0,c_storage_order(),rhs.index_bases(),rhs.shape())
    353369  {
    354     allocate_space();
     370    default_initialize();
    355371    std::copy(rhs.begin(),rhs.end(),this->begin());
    356372  }
    357373   
     
    360376              const general_storage_order<NumDims>& so)
    361377    : super_type(0,so,rhs.index_bases(),rhs.shape())
    362378  {
    363     allocate_space();
     379    default_initialize();
    364380    std::copy(rhs.begin(),rhs.end(),this->begin());
    365381  }
    366382   
     
    469485  }
    470486
    471487private:
     488
    472489  void allocate_space() {
    473490    typename Allocator::const_pointer no_hint=0;
    474491    base_ = allocator_.allocate(this->num_elements(),no_hint);
    475492    this->set_base_ptr(base_);
    476493    allocated_elements_ = this->num_elements();
    477     std::uninitialized_fill_n(base_,allocated_elements_,T());
    478494  }
    479495
     496  void default_initialize(const T& value = T()) {
     497    this->allocate_space();
     498    std::uninitialized_fill_n(base_,allocated_elements_,value);
     499  }
     500
     501  void copy_initialize(const multi_array<T,NumDims,Allocator>& rhs) {
     502    this->allocate_space();
     503    std::uninitialized_copy(rhs.base_,rhs.base_ + rhs.num_elements(),base_);
     504  }
     505
    480506  void deallocate_space() {
    481507    if(base_) {
    482508      for(T* i = base_; i != base_+allocated_elements_; ++i)
  • libs/multi_array/test/constructors.cpp

     
    2121#include <algorithm>
    2222#include <list>
    2323
     24
     25struct no_default
     26{
     27    explicit no_default(int i) {}
     28};
     29
    2430void check_shape(const double&, std::size_t*, int*, unsigned int)
    2531{}
    2632
     33void check_shape(const no_default&, std::size_t*, int*, unsigned int)
     34{}
     35
    2736template <class Array>
    2837void check_shape(const Array& A,
    2938                 std::size_t* sizes,
     
    3746  check_shape(A[0], ++sizes, ++strides, num_elements / A.size());
    3847}
    3948
    40 
    4149bool equal(const double& a, const double& b)
    4250{
    4351  return a == b;
     
    8290    check_shape(C, &sizes[0], strides, num_elements);
    8391  }
    8492
     93  // Constructor 1, initialise with default value
     94  {
     95      boost::multi_array<no_default, 3> A(sizes, no_default(10));
     96      check_shape(A, &sizes[0], strides, num_elements);
     97  }
     98
    8599  // Constructor 1, fortran storage order and user-supplied allocator
    86100  {
    87101    typedef boost::multi_array<double, 3,