1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
|
// Copyright 2002 The Trustees of Indiana University.
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// Boost.MultiArray Library
// Authors: Ronald Garcia
// Jeremy Siek
// Andrew Lumsdaine
// See http://www.boost.org/libs/multi_array for documentation.
#ifndef BOOST_MULTI_ARRAY_REF_RG071801_HPP
#define BOOST_MULTI_ARRAY_REF_RG071801_HPP
//
// multi_array_ref.hpp - code for creating "views" of array data.
//
#include "boost/multi_array/base.hpp"
#include "boost/multi_array/collection_concept.hpp"
#include "boost/multi_array/concept_checks.hpp"
#include "boost/multi_array/iterator.hpp"
#include "boost/multi_array/storage_order.hpp"
#include "boost/multi_array/subarray.hpp"
#include "boost/multi_array/view.hpp"
#include "boost/multi_array/algorithm.hpp"
#include "boost/type_traits/is_integral.hpp"
#include "boost/utility/enable_if.hpp"
#include "boost/array.hpp"
#include "boost/concept_check.hpp"
#include "boost/functional.hpp"
#include "boost/limits.hpp"
#include <algorithm>
#include <cstddef>
#include <functional>
#include <numeric>
namespace boost {
template <typename T, std::size_t NumDims,
typename TPtr = const T*
>
class const_multi_array_ref :
public detail::multi_array::multi_array_impl_base<T,NumDims>
{
typedef detail::multi_array::multi_array_impl_base<T,NumDims> super_type;
public:
typedef typename super_type::value_type value_type;
typedef typename super_type::const_reference const_reference;
typedef typename super_type::const_iterator const_iterator;
typedef typename super_type::const_reverse_iterator const_reverse_iterator;
typedef typename super_type::element element;
typedef typename super_type::size_type size_type;
typedef typename super_type::difference_type difference_type;
typedef typename super_type::index index;
typedef typename super_type::extent_range extent_range;
typedef general_storage_order<NumDims> storage_order_type;
// template typedefs
template <std::size_t NDims>
struct const_array_view {
typedef boost::detail::multi_array::const_multi_array_view<T,NDims> type;
};
template <std::size_t NDims>
struct array_view {
typedef boost::detail::multi_array::multi_array_view<T,NDims> type;
};
#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
// make const_multi_array_ref a friend of itself
template <typename,std::size_t,typename>
friend class const_multi_array_ref;
#endif
// This ensures that const_multi_array_ref types with different TPtr
// types can convert to each other
template <typename OPtr>
const_multi_array_ref(const const_multi_array_ref<T,NumDims,OPtr>& other)
: base_(other.base_), storage_(other.storage_),
extent_list_(other.extent_list_),
stride_list_(other.stride_list_),
index_base_list_(other.index_base_list_),
origin_offset_(other.origin_offset_),
directional_offset_(other.directional_offset_),
num_elements_(other.num_elements_) { }
template <typename ExtentList>
explicit const_multi_array_ref(TPtr base, const ExtentList& extents) :
base_(base), storage_(c_storage_order()) {
boost::function_requires<
CollectionConcept<ExtentList> >();
index_base_list_.assign(0);
init_multi_array_ref(extents.begin());
}
template <typename ExtentList>
explicit const_multi_array_ref(TPtr base, const ExtentList& extents,
const general_storage_order<NumDims>& so) :
base_(base), storage_(so) {
boost::function_requires<
CollectionConcept<ExtentList> >();
index_base_list_.assign(0);
init_multi_array_ref(extents.begin());
}
explicit const_multi_array_ref(TPtr base,
const detail::multi_array::
extent_gen<NumDims>& ranges) :
base_(base), storage_(c_storage_order()) {
init_from_extent_gen(ranges);
}
explicit const_multi_array_ref(TPtr base,
const detail::multi_array::
extent_gen<NumDims>& ranges,
const general_storage_order<NumDims>& so) :
base_(base), storage_(so) {
init_from_extent_gen(ranges);
}
template <class InputIterator>
void assign(InputIterator begin, InputIterator end) {
boost::function_requires<InputIteratorConcept<InputIterator> >();
InputIterator in_iter = begin;
T* out_iter = base_;
std::size_t copy_count=0;
while (in_iter != end && copy_count < num_elements_) {
*out_iter++ = *in_iter++;
copy_count++;
}
}
template <class BaseList>
#ifdef BOOST_NO_SFINAE
void
#else
typename
disable_if<typename boost::is_integral<BaseList>::type,void >::type
#endif // BOOST_NO_SFINAE
reindex(const BaseList& values) {
boost::function_requires<
CollectionConcept<BaseList> >();
boost::detail::multi_array::
copy_n(values.begin(),num_dimensions(),index_base_list_.begin());
origin_offset_ =
this->calculate_origin_offset(stride_list_,extent_list_,
storage_,index_base_list_);
}
void reindex(index value) {
index_base_list_.assign(value);
origin_offset_ =
this->calculate_origin_offset(stride_list_,extent_list_,
storage_,index_base_list_);
}
template <typename SizeList>
void reshape(const SizeList& extents) {
boost::function_requires<
CollectionConcept<SizeList> >();
BOOST_ASSERT(num_elements_ ==
std::accumulate(extents.begin(),extents.end(),
size_type(1),std::multiplies<size_type>()));
std::copy(extents.begin(),extents.end(),extent_list_.begin());
this->compute_strides(stride_list_,extent_list_,storage_);
origin_offset_ =
this->calculate_origin_offset(stride_list_,extent_list_,
storage_,index_base_list_);
}
size_type num_dimensions() const { return NumDims; }
size_type size() const { return extent_list_.front(); }
// given reshaping functionality, this is the max possible size.
size_type max_size() const { return num_elements(); }
bool empty() const { return size() == 0; }
const size_type* shape() const {
return extent_list_.data();
}
const index* strides() const {
return stride_list_.data();
}
const element* origin() const { return base_+origin_offset_; }
const element* data() const { return base_; }
size_type num_elements() const { return num_elements_; }
const index* index_bases() const {
return index_base_list_.data();
}
const storage_order_type& storage_order() const {
return storage_;
}
template <typename IndexList>
const element& operator()(IndexList indices) const {
boost::function_requires<
CollectionConcept<IndexList> >();
return super_type::access_element(boost::type<const element&>(),
indices,origin(),
shape(),strides(),index_bases());
}
// Only allow const element access
const_reference operator[](index idx) const {
return super_type::access(boost::type<const_reference>(),
idx,origin(),
shape(),strides(),index_bases());
}
// see generate_array_view in base.hpp
#if !defined(BOOST_MSVC) || BOOST_MSVC > 1300
template <int NDims>
#else
template <int NumDims, int NDims> // else ICE
#endif // BOOST_MSVC
typename const_array_view<NDims>::type
operator[](const detail::multi_array::
index_gen<NumDims,NDims>& indices)
const {
typedef typename const_array_view<NDims>::type return_type;
return
super_type::generate_array_view(boost::type<return_type>(),
indices,
shape(),
strides(),
index_bases(),
origin());
}
const_iterator begin() const {
return const_iterator(*index_bases(),origin(),
shape(),strides(),index_bases());
}
const_iterator end() const {
return const_iterator(*index_bases()+(index)*shape(),origin(),
shape(),strides(),index_bases());
}
const_reverse_iterator rbegin() const {
return const_reverse_iterator(end());
}
const_reverse_iterator rend() const {
return const_reverse_iterator(begin());
}
template <typename OPtr>
bool operator==(const
const_multi_array_ref<T,NumDims,OPtr>& rhs)
const {
if(std::equal(extent_list_.begin(),
extent_list_.end(),
rhs.extent_list_.begin()))
return std::equal(begin(),end(),rhs.begin());
else return false;
}
template <typename OPtr>
bool operator<(const
const_multi_array_ref<T,NumDims,OPtr>& rhs)
const {
return std::lexicographical_compare(begin(),end(),rhs.begin(),rhs.end());
}
template <typename OPtr>
bool operator!=(const
const_multi_array_ref<T,NumDims,OPtr>& rhs)
const {
return !(*this == rhs);
}
template <typename OPtr>
bool operator>(const
const_multi_array_ref<T,NumDims,OPtr>& rhs)
const {
return rhs < *this;
}
template <typename OPtr>
bool operator<=(const
const_multi_array_ref<T,NumDims,OPtr>& rhs)
const {
return !(*this > rhs);
}
template <typename OPtr>
bool operator>=(const
const_multi_array_ref<T,NumDims,OPtr>& rhs)
const {
return !(*this < rhs);
}
#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
protected:
#else
public:
#endif
typedef boost::array<size_type,NumDims> size_list;
typedef boost::array<index,NumDims> index_list;
// This is used by multi_array, which is a subclass of this
void set_base_ptr(TPtr new_base) { base_ = new_base; }
// This constructor supports multi_array's default constructor
// and constructors from multi_array_ref, subarray, and array_view
explicit
const_multi_array_ref(TPtr base,
const storage_order_type& so,
const index * index_bases,
const size_type* extents) :
base_(base), storage_(so), origin_offset_(0), directional_offset_(0)
{
// If index_bases or extents is null, then initialize the corresponding
// private data to zeroed lists.
if(index_bases) {
boost::detail::multi_array::
copy_n(index_bases,NumDims,index_base_list_.begin());
} else {
std::fill_n(index_base_list_.begin(),NumDims,0);
}
if(extents) {
init_multi_array_ref(extents);
} else {
boost::array<index,NumDims> extent_list;
extent_list.assign(0);
init_multi_array_ref(extent_list.begin());
}
}
TPtr base_;
storage_order_type storage_;
size_list extent_list_;
index_list stride_list_;
index_list index_base_list_;
index origin_offset_;
index directional_offset_;
size_type num_elements_;
private:
// const_multi_array_ref cannot be assigned to (no deep copies!)
const_multi_array_ref& operator=(const const_multi_array_ref& other);
void init_from_extent_gen(const
detail::multi_array::
extent_gen<NumDims>& ranges) {
typedef boost::array<index,NumDims> extent_list;
// get the index_base values
std::transform(ranges.ranges_.begin(),ranges.ranges_.end(),
index_base_list_.begin(),
boost::mem_fun_ref(&extent_range::start));
// calculate the extents
extent_list extents;
std::transform(ranges.ranges_.begin(),ranges.ranges_.end(),
extents.begin(),
boost::mem_fun_ref(&extent_range::size));
init_multi_array_ref(extents.begin());
}
#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
protected:
#else
public:
#endif
// RG - move me!
template <class InputIterator>
void init_multi_array_ref(InputIterator extents_iter) {
boost::function_requires<InputIteratorConcept<InputIterator> >();
boost::detail::multi_array::
copy_n(extents_iter,num_dimensions(),extent_list_.begin());
// Calculate the array size
num_elements_ = std::accumulate(extent_list_.begin(),extent_list_.end(),
size_type(1),std::multiplies<size_type>());
this->compute_strides(stride_list_,extent_list_,storage_);
origin_offset_ =
this->calculate_origin_offset(stride_list_,extent_list_,
storage_,index_base_list_);
directional_offset_ =
this->calculate_descending_dimension_offset(stride_list_,extent_list_,
storage_);
}
};
template <typename T, std::size_t NumDims>
class multi_array_ref :
public const_multi_array_ref<T,NumDims,T*>
{
typedef const_multi_array_ref<T,NumDims,T*> super_type;
public:
typedef typename super_type::value_type value_type;
typedef typename super_type::reference reference;
typedef typename super_type::iterator iterator;
typedef typename super_type::reverse_iterator reverse_iterator;
typedef typename super_type::const_reference const_reference;
typedef typename super_type::const_iterator const_iterator;
typedef typename super_type::const_reverse_iterator const_reverse_iterator;
typedef typename super_type::element element;
typedef typename super_type::size_type size_type;
typedef typename super_type::difference_type difference_type;
typedef typename super_type::index index;
typedef typename super_type::extent_range extent_range;
typedef typename super_type::storage_order_type storage_order_type;
typedef typename super_type::index_list index_list;
typedef typename super_type::size_list size_list;
template <std::size_t NDims>
struct const_array_view {
typedef boost::detail::multi_array::const_multi_array_view<T,NDims> type;
};
template <std::size_t NDims>
struct array_view {
typedef boost::detail::multi_array::multi_array_view<T,NDims> type;
};
template <class ExtentList>
explicit multi_array_ref(T* base, const ExtentList& extents) :
super_type(base,extents) {
boost::function_requires<
CollectionConcept<ExtentList> >();
}
template <class ExtentList>
explicit multi_array_ref(T* base, const ExtentList& extents,
const general_storage_order<NumDims>& so) :
super_type(base,extents,so) {
boost::function_requires<
CollectionConcept<ExtentList> >();
}
explicit multi_array_ref(T* base,
const detail::multi_array::
extent_gen<NumDims>& ranges) :
super_type(base,ranges) { }
explicit multi_array_ref(T* base,
const detail::multi_array::
extent_gen<NumDims>&
ranges,
const general_storage_order<NumDims>& so) :
super_type(base,ranges,so) { }
// Assignment from other ConstMultiArray types.
template <typename ConstMultiArray>
multi_array_ref& operator=(const ConstMultiArray& other) {
function_requires<
multi_array_concepts::
ConstMultiArrayConcept<ConstMultiArray,NumDims> >();
// make sure the dimensions agree
BOOST_ASSERT(other.num_dimensions() == this->num_dimensions());
BOOST_ASSERT(std::equal(other.shape(),other.shape()+this->num_dimensions(),
this->shape()));
// iterator-based copy
std::copy(other.begin(),other.end(),this->begin());
return *this;
}
multi_array_ref& operator=(const multi_array_ref& other) {
if (&other != this) {
// make sure the dimensions agree
BOOST_ASSERT(other.num_dimensions() == this->num_dimensions());
BOOST_ASSERT(std::equal(other.shape(),
other.shape()+this->num_dimensions(),
this->shape()));
// iterator-based copy
std::copy(other.begin(),other.end(),this->begin());
}
return *this;
}
element* origin() { return super_type::base_+super_type::origin_offset_; }
element* data() { return super_type::base_; }
template <class IndexList>
element& operator()(const IndexList& indices) {
boost::function_requires<
CollectionConcept<IndexList> >();
return super_type::access_element(boost::type<element&>(),
indices,origin(),
this->shape(),this->strides(),
this->index_bases());
}
reference operator[](index idx) {
return super_type::access(boost::type<reference>(),
idx,origin(),
this->shape(),this->strides(),
this->index_bases());
}
// See note attached to generate_array_view in base.hpp
#if !defined(BOOST_MSVC) || BOOST_MSVC > 1300
template <int NDims>
#else
template <int NumDims, int NDims> // else ICE
#endif // BOOST_MSVC
typename array_view<NDims>::type
operator[](const detail::multi_array::
index_gen<NumDims,NDims>& indices) {
typedef typename array_view<NDims>::type return_type;
return
super_type::generate_array_view(boost::type<return_type>(),
indices,
this->shape(),
this->strides(),
this->index_bases(),
origin());
}
iterator begin() {
return iterator(*this->index_bases(),origin(),this->shape(),
this->strides(),this->index_bases());
}
iterator end() {
return iterator(*this->index_bases()+(index)*this->shape(),origin(),
this->shape(),this->strides(),
this->index_bases());
}
// rbegin() and rend() written naively to thwart MSVC ICE.
reverse_iterator rbegin() {
reverse_iterator ri(end());
return ri;
}
reverse_iterator rend() {
reverse_iterator ri(begin());
return ri;
}
// Using declarations don't seem to work for g++
// These are the proxies to work around this.
const element* origin() const { return super_type::origin(); }
const element* data() const { return super_type::data(); }
template <class IndexList>
const element& operator()(const IndexList& indices) const {
boost::function_requires<
CollectionConcept<IndexList> >();
return super_type::operator()(indices);
}
const_reference operator[](index idx) const {
return super_type::access(boost::type<const_reference>(),
idx,origin(),
this->shape(),this->strides(),
this->index_bases());
}
// See note attached to generate_array_view in base.hpp
#if !defined(BOOST_MSVC) || BOOST_MSVC > 1300
template <int NDims>
#else
template <int NumDims, int NDims> // else ICE
#endif // BOOST_MSVC
typename const_array_view<NDims>::type
operator[](const detail::multi_array::
index_gen<NumDims,NDims>& indices)
const {
return super_type::operator[](indices);
}
const_iterator begin() const {
return super_type::begin();
}
const_iterator end() const {
return super_type::end();
}
const_reverse_iterator rbegin() const {
return super_type::rbegin();
}
const_reverse_iterator rend() const {
return super_type::rend();
}
protected:
// This is only supplied to support multi_array's default constructor
explicit multi_array_ref(T* base,
const storage_order_type& so,
const index* index_bases,
const size_type* extents) :
super_type(base,so,index_bases,extents) { }
};
} // namespace boost
#endif // BOOST_MULTI_ARRAY_REF_RG071801_HPP
|