Ticket #6889: multi_array_accept_initial_value.diff
File multi_array_accept_initial_value.diff, 8.8 KB (added by , 10 years ago) |
---|
-
boost/multi_array.hpp
33 33 #include <functional> 34 34 #include <numeric> 35 35 #include <vector> 36 #include <memory> 36 37 37 38 38 39 39 namespace boost { 40 40 namespace detail { 41 41 namespace multi_array { … … 140 140 explicit multi_array() : 141 141 super_type((T*)initial_base_,c_storage_order(), 142 142 /*index_bases=*/0, /*extents=*/0) { 143 allocate_space();143 default_initialize(); 144 144 } 145 145 146 146 template <class ExtentList> … … 155 155 super_type((T*)initial_base_,extents) { 156 156 boost::function_requires< 157 157 detail::multi_array::CollectionConcept<ExtentList> >(); 158 allocate_space();158 default_initialize(); 159 159 } 160 160 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 } 161 171 162 172 template <class ExtentList> 163 173 explicit multi_array(ExtentList const& extents, … … 165 175 super_type((T*)initial_base_,extents,so) { 166 176 boost::function_requires< 167 177 detail::multi_array::CollectionConcept<ExtentList> >(); 168 allocate_space();178 default_initialize(); 169 179 } 170 180 171 181 template <class ExtentList> … … 175 185 super_type((T*)initial_base_,extents,so), allocator_(alloc) { 176 186 boost::function_requires< 177 187 detail::multi_array::CollectionConcept<ExtentList> >(); 178 allocate_space();188 default_initialize(); 179 189 } 180 190 181 182 191 explicit multi_array(const detail::multi_array 183 192 ::extent_gen<NumDims>& ranges) : 184 193 super_type((T*)initial_base_,ranges) { 185 194 186 allocate_space();195 default_initialize(); 187 196 } 188 197 198 explicit multi_array(const detail::multi_array 199 ::extent_gen<NumDims>& ranges, 200 const T& value) : 201 super_type((T*)initial_base_,ranges) { 189 202 203 default_initialize(value); 204 } 205 206 190 207 explicit multi_array(const detail::multi_array 191 208 ::extent_gen<NumDims>& ranges, 192 209 const general_storage_order<NumDims>& so) : 193 210 super_type((T*)initial_base_,ranges,so) { 194 211 195 allocate_space();212 default_initialize(); 196 213 } 197 214 198 215 … … 202 219 Allocator const& alloc) : 203 220 super_type((T*)initial_base_,ranges,so), allocator_(alloc) { 204 221 205 allocate_space();222 default_initialize(); 206 223 } 207 224 208 225 multi_array(const multi_array& rhs) : 209 226 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); 212 228 } 213 229 214 230 … … 226 242 const general_storage_order<NumDims>& so = c_storage_order()) 227 243 : super_type(0,so,rhs.index_bases(),rhs.shape()) 228 244 { 229 allocate_space();245 default_initialize(); 230 246 // Warning! storage order may change, hence the following copy technique. 231 247 std::copy(rhs.begin(),rhs.end(),this->begin()); 232 248 } … … 237 253 const general_storage_order<NumDims>& so = c_storage_order()) 238 254 : super_type(0,so,rhs.index_bases(),rhs.shape()) 239 255 { 240 allocate_space();256 default_initialize(); 241 257 std::copy(rhs.begin(),rhs.end(),this->begin()); 242 258 } 243 259 … … 248 264 const general_storage_order<NumDims>& so = c_storage_order()) 249 265 : super_type(0,so,rhs.index_bases(),rhs.shape()) 250 266 { 251 allocate_space();267 default_initialize(); 252 268 std::copy(rhs.begin(),rhs.end(),this->begin()); 253 269 } 254 270 … … 259 275 multi_array(const const_multi_array_ref<T,NumDims>& rhs) 260 276 : super_type(0,c_storage_order(),rhs.index_bases(),rhs.shape()) 261 277 { 262 allocate_space();278 default_initialize(); 263 279 // Warning! storage order may change, hence the following copy technique. 264 280 std::copy(rhs.begin(),rhs.end(),this->begin()); 265 281 } … … 268 284 const general_storage_order<NumDims>& so) 269 285 : super_type(0,so,rhs.index_bases(),rhs.shape()) 270 286 { 271 allocate_space();287 default_initialize(); 272 288 // Warning! storage order may change, hence the following copy technique. 273 289 std::copy(rhs.begin(),rhs.end(),this->begin()); 274 290 } … … 277 293 const_sub_array<T,NumDims>& rhs) 278 294 : super_type(0,c_storage_order(),rhs.index_bases(),rhs.shape()) 279 295 { 280 allocate_space();296 default_initialize(); 281 297 std::copy(rhs.begin(),rhs.end(),this->begin()); 282 298 } 283 299 … … 286 302 const general_storage_order<NumDims>& so) 287 303 : super_type(0,so,rhs.index_bases(),rhs.shape()) 288 304 { 289 allocate_space();305 default_initialize(); 290 306 std::copy(rhs.begin(),rhs.end(),this->begin()); 291 307 } 292 308 … … 295 311 const_multi_array_view<T,NumDims>& rhs) 296 312 : super_type(0,c_storage_order(),rhs.index_bases(),rhs.shape()) 297 313 { 298 allocate_space();314 default_initialize(); 299 315 std::copy(rhs.begin(),rhs.end(),this->begin()); 300 316 } 301 317 … … 304 320 const general_storage_order<NumDims>& so) 305 321 : super_type(0,so,rhs.index_bases(),rhs.shape()) 306 322 { 307 allocate_space();323 default_initialize(); 308 324 std::copy(rhs.begin(),rhs.end(),this->begin()); 309 325 } 310 326 … … 314 330 multi_array(const multi_array_ref<T,NumDims>& rhs) 315 331 : super_type(0,c_storage_order(),rhs.index_bases(),rhs.shape()) 316 332 { 317 allocate_space();333 default_initialize(); 318 334 // Warning! storage order may change, hence the following copy technique. 319 335 std::copy(rhs.begin(),rhs.end(),this->begin()); 320 336 } … … 323 339 const general_storage_order<NumDims>& so) 324 340 : super_type(0,so,rhs.index_bases(),rhs.shape()) 325 341 { 326 allocate_space();342 default_initialize(); 327 343 // Warning! storage order may change, hence the following copy technique. 328 344 std::copy(rhs.begin(),rhs.end(),this->begin()); 329 345 } … … 333 349 sub_array<T,NumDims>& rhs) 334 350 : super_type(0,c_storage_order(),rhs.index_bases(),rhs.shape()) 335 351 { 336 allocate_space();352 default_initialize(); 337 353 std::copy(rhs.begin(),rhs.end(),this->begin()); 338 354 } 339 355 … … 342 358 const general_storage_order<NumDims>& so) 343 359 : super_type(0,so,rhs.index_bases(),rhs.shape()) 344 360 { 345 allocate_space();361 default_initialize(); 346 362 std::copy(rhs.begin(),rhs.end(),this->begin()); 347 363 } 348 364 … … 351 367 multi_array_view<T,NumDims>& rhs) 352 368 : super_type(0,c_storage_order(),rhs.index_bases(),rhs.shape()) 353 369 { 354 allocate_space();370 default_initialize(); 355 371 std::copy(rhs.begin(),rhs.end(),this->begin()); 356 372 } 357 373 … … 360 376 const general_storage_order<NumDims>& so) 361 377 : super_type(0,so,rhs.index_bases(),rhs.shape()) 362 378 { 363 allocate_space();379 default_initialize(); 364 380 std::copy(rhs.begin(),rhs.end(),this->begin()); 365 381 } 366 382 … … 469 485 } 470 486 471 487 private: 488 472 489 void allocate_space() { 473 490 typename Allocator::const_pointer no_hint=0; 474 491 base_ = allocator_.allocate(this->num_elements(),no_hint); 475 492 this->set_base_ptr(base_); 476 493 allocated_elements_ = this->num_elements(); 477 std::uninitialized_fill_n(base_,allocated_elements_,T());478 494 } 479 495 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 480 506 void deallocate_space() { 481 507 if(base_) { 482 508 for(T* i = base_; i != base_+allocated_elements_; ++i) -
libs/multi_array/test/constructors.cpp
21 21 #include <algorithm> 22 22 #include <list> 23 23 24 25 struct no_default 26 { 27 explicit no_default(int i) {} 28 }; 29 24 30 void check_shape(const double&, std::size_t*, int*, unsigned int) 25 31 {} 26 32 33 void check_shape(const no_default&, std::size_t*, int*, unsigned int) 34 {} 35 27 36 template <class Array> 28 37 void check_shape(const Array& A, 29 38 std::size_t* sizes, … … 37 46 check_shape(A[0], ++sizes, ++strides, num_elements / A.size()); 38 47 } 39 48 40 41 49 bool equal(const double& a, const double& b) 42 50 { 43 51 return a == b; … … 82 90 check_shape(C, &sizes[0], strides, num_elements); 83 91 } 84 92 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 85 99 // Constructor 1, fortran storage order and user-supplied allocator 86 100 { 87 101 typedef boost::multi_array<double, 3,