2 // Copyright (c) 2000-2002
3 // Joerg Walter, Mathias Koch
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 // The authors gratefully acknowledge the support of
10 // GeNeSys mbH & Co. KG in producing this work.
13 #ifndef BOOST_UBLAS_STORAGE_H
14 #define BOOST_UBLAS_STORAGE_H
17 #ifdef BOOST_UBLAS_SHALLOW_ARRAY_ADAPTOR
18 #include <boost/shared_array.hpp>
21 #include <boost/serialization/array.hpp>
22 #include <boost/serialization/collection_size_type.hpp>
23 #include <boost/serialization/nvp.hpp>
25 #include <boost/numeric/ublas/exception.hpp>
26 #include <boost/numeric/ublas/traits.hpp>
27 #include <boost/numeric/ublas/detail/iterator.hpp>
30 namespace boost { namespace numeric { namespace ublas {
33 // Base class for Storage Arrays - see the Barton Nackman trick
36 private nonassignable {
40 // Unbounded array - with allocator
41 template<class T, class ALLOC>
42 class unbounded_array:
43 public storage_array<unbounded_array<T, ALLOC> > {
45 typedef unbounded_array<T, ALLOC> self_type;
47 typedef ALLOC allocator_type;
48 typedef typename ALLOC::size_type size_type;
49 typedef typename ALLOC::difference_type difference_type;
51 typedef const T &const_reference;
53 typedef const T *const_pointer;
55 typedef const_pointer const_iterator;
56 typedef pointer iterator;
58 // Construction and destruction
59 explicit BOOST_UBLAS_INLINE
60 unbounded_array (const ALLOC &a = ALLOC()):
61 alloc_ (a), size_ (0) {
64 explicit BOOST_UBLAS_INLINE
65 unbounded_array (size_type size, const ALLOC &a = ALLOC()):
66 alloc_(a), size_ (size) {
68 data_ = alloc_.allocate (size_);
69 //Disabled warning C4127 because the conditional expression is constant
72 #pragma warning(disable: 4127)
74 if (! detail::has_trivial_constructor<T>::value) {
78 for (pointer d = data_; d != data_ + size_; ++d)
79 alloc_.construct(d, value_type());
85 // No value initialised, but still be default constructed
87 unbounded_array (size_type size, const value_type &init, const ALLOC &a = ALLOC()):
88 alloc_ (a), size_ (size) {
90 data_ = alloc_.allocate (size_);
91 std::uninitialized_fill (begin(), end(), init);
97 unbounded_array (const unbounded_array &c):
98 storage_array<unbounded_array<T, ALLOC> >(),
99 alloc_ (c.alloc_), size_ (c.size_) {
101 data_ = alloc_.allocate (size_);
102 std::uninitialized_copy (c.begin(), c.end(), begin());
107 #ifdef BOOST_UBLAS_CPP_GE_2011
109 unbounded_array (unbounded_array &&c) :
110 storage_array<unbounded_array<T, ALLOC> >(),
111 alloc_ (std::move(c.alloc_)), size_ (c.size_), data_(c.data_)
118 ~unbounded_array () {
120 //Disabled warning C4127 because the conditional expression is constant
122 #pragma warning(push)
123 #pragma warning(disable: 4127)
125 if (! detail::has_trivial_destructor<T>::value) {
129 // std::_Destroy (begin(), end(), alloc_);
130 const iterator i_end = end();
131 for (iterator i = begin (); i != i_end; ++i) {
132 iterator_destroy (i);
135 alloc_.deallocate (data_, size_);
142 void resize_internal (const size_type size, const value_type init, const bool preserve) {
144 pointer p_data = data_;
146 data_ = alloc_.allocate (size);
151 for (; di != data_ + size; ++di) {
152 alloc_.construct (di, *si);
157 for (; si != p_data + size_; ++si) {
158 alloc_.construct (di, *si);
161 for (; di != data_ + size; ++di) {
162 alloc_.construct (di, init);
167 //Disabled warning C4127 because the conditional expression is constant
169 #pragma warning(push)
170 #pragma warning(disable: 4127)
172 if (! detail::has_trivial_constructor<T>::value) {
176 for (pointer di = data_; di != data_ + size; ++di)
177 alloc_.construct (di, value_type());
183 //Disabled warning C4127 because the conditional expression is constant
185 #pragma warning(push)
186 #pragma warning(disable: 4127)
188 if (! detail::has_trivial_destructor<T>::value) {
192 for (pointer si = p_data; si != p_data + size_; ++si)
195 alloc_.deallocate (p_data, size_);
205 void resize (size_type size) {
206 resize_internal (size, value_type (), false);
209 void resize (size_type size, value_type init) {
210 resize_internal (size, init, true);
213 // Random Access Container
215 size_type max_size () const {
216 return ALLOC ().max_size();
220 bool empty () const {
225 size_type size () const {
231 const_reference operator [] (size_type i) const {
232 BOOST_UBLAS_CHECK (i < size_, bad_index ());
236 reference operator [] (size_type i) {
237 BOOST_UBLAS_CHECK (i < size_, bad_index ());
243 unbounded_array &operator = (const unbounded_array &a) {
246 std::copy (a.data_, a.data_ + a.size_, data_);
251 unbounded_array &assign_temporary (unbounded_array &a) {
258 void swap (unbounded_array &a) {
260 std::swap (size_, a.size_);
261 std::swap (data_, a.data_);
265 friend void swap (unbounded_array &a1, unbounded_array &a2) {
270 const_iterator begin () const {
274 const_iterator cbegin () const {
278 const_iterator end () const {
279 return data_ + size_;
282 const_iterator cend () const {
292 return data_ + size_;
296 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
297 typedef std::reverse_iterator<iterator> reverse_iterator;
300 const_reverse_iterator rbegin () const {
301 return const_reverse_iterator (end ());
304 const_reverse_iterator crbegin () const {
308 const_reverse_iterator rend () const {
309 return const_reverse_iterator (begin ());
312 const_reverse_iterator crend () const {
316 reverse_iterator rbegin () {
317 return reverse_iterator (end ());
320 reverse_iterator rend () {
321 return reverse_iterator (begin ());
325 allocator_type get_allocator () {
330 friend class boost::serialization::access;
333 template<class Archive>
334 void serialize(Archive & ar, const unsigned int /*version*/)
336 serialization::collection_size_type s(size_);
337 ar & serialization::make_nvp("size",s);
338 if ( Archive::is_loading::value ) {
341 ar & serialization::make_array(data_, s);
345 // Handle explict destroy on a (possibly indexed) iterator
347 static void iterator_destroy (iterator &i) {
349 (&(*i)) -> ~value_type ();
356 // Bounded array - with allocator for size_type and difference_type
357 template<class T, std::size_t N, class ALLOC>
359 public storage_array<bounded_array<T, N, ALLOC> > {
361 typedef bounded_array<T, N, ALLOC> self_type;
363 // No allocator_type as ALLOC is not used for allocation
364 typedef typename ALLOC::size_type size_type;
365 typedef typename ALLOC::difference_type difference_type;
366 typedef T value_type;
367 typedef const T &const_reference;
368 typedef T &reference;
369 typedef const T *const_pointer;
371 typedef const_pointer const_iterator;
372 typedef pointer iterator;
374 // Construction and destruction
377 size_ (0) /*, data_ ()*/ { // size 0 - use bounded_vector to default construct with size N
379 explicit BOOST_UBLAS_INLINE
380 bounded_array (size_type size):
381 size_ (size) /*, data_ ()*/ {
382 BOOST_UBLAS_CHECK (size_ <= N, bad_size ());
383 // data_ (an array) elements are already default constructed
386 bounded_array (size_type size, const value_type &init):
387 size_ (size) /*, data_ ()*/ {
388 BOOST_UBLAS_CHECK (size_ <= N, bad_size ());
389 // ISSUE elements should be value constructed here, but we must fill instead as already default constructed
390 std::fill (begin(), end(), init) ;
393 bounded_array (const bounded_array &c):
395 // ISSUE elements should be copy constructed here, but we must copy instead as already default constructed
396 std::copy (c.begin(), c.end(), begin());
401 void resize (size_type size) {
402 BOOST_UBLAS_CHECK (size <= N, bad_size ());
406 void resize (size_type size, value_type init) {
407 BOOST_UBLAS_CHECK (size <= N, bad_size ());
409 std::fill (data_ + size_, data_ + size, init);
413 // Random Access Container
415 size_type max_size () const {
420 bool empty () const {
425 size_type size () const {
431 const_reference operator [] (size_type i) const {
432 BOOST_UBLAS_CHECK (i < size_, bad_index ());
436 reference operator [] (size_type i) {
437 BOOST_UBLAS_CHECK (i < size_, bad_index ());
443 bounded_array &operator = (const bounded_array &a) {
446 std::copy (a.data_, a.data_ + a.size_, data_);
451 bounded_array &assign_temporary (bounded_array &a) {
458 void swap (bounded_array &a) {
460 std::swap (size_, a.size_);
461 std::swap_ranges (data_, data_ + (std::max) (size_, a.size_), a.data_);
465 friend void swap (bounded_array &a1, bounded_array &a2) {
470 const_iterator begin () const {
474 const_iterator cbegin () const {
478 const_iterator end () const {
479 return data_ + size_;
482 const_iterator cend () const {
492 return data_ + size_;
496 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
497 typedef std::reverse_iterator<iterator> reverse_iterator;
500 const_reverse_iterator rbegin () const {
501 return const_reverse_iterator (end ());
504 const_reverse_iterator crbegin () const {
508 const_reverse_iterator rend () const {
509 return const_reverse_iterator (begin ());
512 const_reverse_iterator crend () const {
516 reverse_iterator rbegin () {
517 return reverse_iterator (end ());
520 reverse_iterator rend () {
521 return reverse_iterator (begin ());
526 friend class boost::serialization::access;
528 template<class Archive>
529 void serialize(Archive & ar, const unsigned int /*version*/)
531 serialization::collection_size_type s(size_);
532 ar & serialization::make_nvp("size", s);
533 if ( Archive::is_loading::value ) {
534 if (s > N) bad_size("too large size in bounded_array::load()\n").raise();
537 ar & serialization::make_array(data_, s);
542 // MSVC does not like arrays of size 0 in base classes. Hence, this conditionally changes the size to 1
544 BOOST_UBLAS_BOUNDED_ARRAY_ALIGN value_type data_ [(N>0)?N:1];
546 BOOST_UBLAS_BOUNDED_ARRAY_ALIGN value_type data_ [N];
551 // Array adaptor with normal deep copy semantics of elements
554 public storage_array<array_adaptor<T> > {
556 typedef array_adaptor<T> self_type;
558 typedef std::size_t size_type;
559 typedef std::ptrdiff_t difference_type;
560 typedef T value_type;
561 typedef const T &const_reference;
562 typedef T &reference;
563 typedef const T *const_pointer;
566 // Construction and destruction
569 size_ (0), own_ (true), data_ (new value_type [0]) {
571 explicit BOOST_UBLAS_INLINE
572 array_adaptor (size_type size):
573 size_ (size), own_ (true), data_ (new value_type [size]) {
576 array_adaptor (size_type size, const value_type &init):
577 size_ (size), own_ (true), data_ (new value_type [size]) {
578 std::fill (data_, data_ + size_, init);
581 array_adaptor (size_type size, pointer data):
582 size_ (size), own_ (false), data_ (data) {}
585 BOOST_UBLAS_INLINE array_adaptor (T (&data)[N]):
586 size_ (N), own_ (false), data_ (data) {}
588 array_adaptor (const array_adaptor &a):
589 storage_array<self_type> (),
590 size_ (a.size_), own_ (true), data_ (new value_type [a.size_]) {
603 void resize_internal (size_type size, value_type init, bool preserve = true) {
605 pointer data = new value_type [size];
607 std::copy (data_, data_ + (std::min) (size, size_), data);
608 std::fill (data + (std::min) (size, size_), data + size, init);
618 void resize_internal (size_type size, pointer data, value_type init, bool preserve = true) {
621 std::copy (data_, data_ + (std::min) (size, size_), data);
622 std::fill (data + (std::min) (size, size_), data + size, init);
630 std::fill (data + (std::min) (size, size_), data + size, init);
636 void resize (size_type size) {
637 resize_internal (size, value_type (), false);
640 void resize (size_type size, value_type init) {
641 resize_internal (size, init, true);
644 void resize (size_type size, pointer data) {
645 resize_internal (size, data, value_type (), false);
648 void resize (size_type size, pointer data, value_type init) {
649 resize_internal (size, data, init, true);
653 BOOST_UBLAS_INLINE void resize (T (&data)[N]) {
654 resize_internal (N, data, value_type (), false);
658 BOOST_UBLAS_INLINE void resize (T (&data)[N], value_type init) {
659 resize_internal (N, data, init, true);
663 size_type size () const {
669 const_reference operator [] (size_type i) const {
670 BOOST_UBLAS_CHECK (i < size_, bad_index ());
674 reference operator [] (size_type i) {
675 BOOST_UBLAS_CHECK (i < size_, bad_index ());
681 array_adaptor &operator = (const array_adaptor &a) {
684 std::copy (a.data_, a.data_ + a.size_, data_);
689 array_adaptor &assign_temporary (array_adaptor &a) {
699 void swap (array_adaptor &a) {
701 std::swap (size_, a.size_);
702 std::swap (own_, a.own_);
703 std::swap (data_, a.data_);
707 friend void swap (array_adaptor &a1, array_adaptor &a2) {
711 // Iterators simply are pointers.
713 typedef const_pointer const_iterator;
716 const_iterator begin () const {
720 const_iterator cbegin () const {
724 const_iterator end () const {
725 return data_ + size_;
728 const_iterator cend () const {
732 typedef pointer iterator;
740 return data_ + size_;
744 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
745 typedef std::reverse_iterator<iterator> reverse_iterator;
748 const_reverse_iterator rbegin () const {
749 return const_reverse_iterator (end ());
752 const_reverse_iterator crbegin () const {
756 const_reverse_iterator rend () const {
757 return const_reverse_iterator (begin ());
760 const_reverse_iterator crend () const {
764 reverse_iterator rbegin () {
765 return reverse_iterator (end ());
768 reverse_iterator rend () {
769 return reverse_iterator (begin ());
778 #ifdef BOOST_UBLAS_SHALLOW_ARRAY_ADAPTOR
779 // Array adaptor with shallow (reference) copy semantics of elements.
780 // shared_array is used to maintain reference counts.
781 // This class breaks the normal copy semantics for a storage container and is very dangerous!
783 class shallow_array_adaptor:
784 public storage_array<shallow_array_adaptor<T> > {
786 typedef shallow_array_adaptor<T> self_type;
790 typedef void result_type;
791 typedef TT *argument_type;
794 result_type operator () (argument_type /* x */) {}
798 typedef std::size_t size_type;
799 typedef std::ptrdiff_t difference_type;
800 typedef T value_type;
801 typedef const T &const_reference;
802 typedef T &reference;
803 typedef const T *const_pointer;
806 // Construction and destruction
808 shallow_array_adaptor ():
809 size_ (0), own_ (true), data_ (new value_type [0]) {
811 explicit BOOST_UBLAS_INLINE
812 shallow_array_adaptor (size_type size):
813 size_ (size), own_ (true), data_ (new value_type [size]) {
816 shallow_array_adaptor (size_type size, const value_type &init):
817 size_ (size), own_ (true), data_ (new value_type [size]) {
818 std::fill (data_.get (), data_.get () + size_, init);
821 shallow_array_adaptor (size_type size, pointer data):
822 size_ (size), own_ (false), data_ (data, leaker<value_type> ()) {}
825 shallow_array_adaptor (T (&data)[N]):
826 size_ (N), own_ (false), data_ (data, leaker<value_type> ()) {}
829 shallow_array_adaptor (const shallow_array_adaptor &a):
830 storage_array<self_type> (),
831 size_ (a.size_), own_ (a.own_), data_ (a.data_) {}
834 ~shallow_array_adaptor () {
840 void resize_internal (size_type size, value_type init, bool preserve = true) {
842 shared_array<value_type> data (new value_type [size]);
844 std::copy (data_.get (), data_.get () + (std::min) (size, size_), data.get ());
845 std::fill (data.get () + (std::min) (size, size_), data.get () + size, init);
853 void resize_internal (size_type size, pointer data, value_type init, bool preserve = true) {
855 std::copy (data_.get (), data_.get () + (std::min) (size, size_), data);
856 std::fill (data + (std::min) (size, size_), data + size, init);
860 data_.reset(data, leaker<value_type> ());
864 void resize (size_type size) {
865 resize_internal (size, value_type (), false);
868 void resize (size_type size, value_type init) {
869 resize_internal (size, init, true);
872 void resize (size_type size, pointer data) {
873 resize_internal (size, data, value_type (), false);
876 void resize (size_type size, pointer data, value_type init) {
877 resize_internal (size, data, init, true);
881 void resize (T (&data)[N]) {
882 resize_internal (N, data, value_type (), false);
886 void resize (T (&data)[N], value_type init) {
887 resize_internal (N, data, init, true);
891 size_type size () const {
897 const_reference operator [] (size_type i) const {
898 BOOST_UBLAS_CHECK (i < size_, bad_index ());
902 reference operator [] (size_type i) {
903 BOOST_UBLAS_CHECK (i < size_, bad_index ());
909 shallow_array_adaptor &operator = (const shallow_array_adaptor &a) {
912 std::copy (a.data_.get (), a.data_.get () + a.size_, data_.get ());
917 shallow_array_adaptor &assign_temporary (shallow_array_adaptor &a) {
927 void swap (shallow_array_adaptor &a) {
929 std::swap (size_, a.size_);
930 std::swap (own_, a.own_);
931 std::swap (data_, a.data_);
935 friend void swap (shallow_array_adaptor &a1, shallow_array_adaptor &a2) {
939 // Iterators simply are pointers.
941 typedef const_pointer const_iterator;
944 const_iterator begin () const {
948 const_iterator cbegin () const {
952 const_iterator end () const {
953 return data_.get () + size_;
956 const_iterator cend () const {
960 typedef pointer iterator;
968 return data_.get () + size_;
972 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
973 typedef std::reverse_iterator<iterator> reverse_iterator;
976 const_reverse_iterator rbegin () const {
977 return const_reverse_iterator (end ());
980 const_reverse_iterator crbegin () const {
984 const_reverse_iterator rend () const {
985 return const_reverse_iterator (begin ());
988 const_reverse_iterator crend () const {
992 reverse_iterator rbegin () {
993 return reverse_iterator (end ());
996 reverse_iterator rend () {
997 return reverse_iterator (begin ());
1003 shared_array<value_type> data_;
1010 template <class Z, class D>
1012 typedef basic_range<Z, D> self_type;
1014 typedef Z size_type;
1015 typedef D difference_type;
1016 typedef size_type value_type;
1017 typedef value_type const_reference;
1018 typedef const_reference reference;
1019 typedef const value_type *const_pointer;
1020 typedef value_type *pointer;
1022 // Construction and destruction
1025 start_ (0), size_ (0) {}
1027 basic_range (size_type start, size_type stop):
1028 start_ (start), size_ (stop - start) {
1029 BOOST_UBLAS_CHECK (start_ <= stop, bad_index ());
1033 size_type start () const {
1037 size_type size () const {
1041 // Random Access Container
1043 size_type max_size () const {
1048 bool empty () const {
1054 const_reference operator () (size_type i) const {
1055 BOOST_UBLAS_CHECK (i < size_, bad_index ());
1061 basic_range compose (const basic_range &r) const {
1062 return basic_range (start_ + r.start_, start_ + r.start_ + r.size_);
1067 bool operator == (const basic_range &r) const {
1068 return start_ == r.start_ && size_ == r.size_;
1071 bool operator != (const basic_range &r) const {
1072 return ! (*this == r);
1078 typedef size_type const_subiterator_type;
1081 #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
1082 typedef indexed_const_iterator<self_type, std::random_access_iterator_tag> const_iterator;
1084 class const_iterator:
1085 public container_const_reference<basic_range>,
1086 public random_access_iterator_base<std::random_access_iterator_tag,
1087 const_iterator, value_type> {
1089 typedef typename basic_range::value_type value_type;
1090 typedef typename basic_range::difference_type difference_type;
1091 typedef typename basic_range::const_reference reference;
1092 typedef typename basic_range::const_pointer pointer;
1094 // Construction and destruction
1097 container_const_reference<basic_range> (), it_ () {}
1099 const_iterator (const basic_range &r, const const_subiterator_type &it):
1100 container_const_reference<basic_range> (r), it_ (it) {}
1104 const_iterator &operator ++ () {
1109 const_iterator &operator -- () {
1110 BOOST_UBLAS_CHECK (it_ > 0, bad_index ());
1115 const_iterator &operator += (difference_type n) {
1116 BOOST_UBLAS_CHECK (n >= 0 || it_ >= size_type(-n), bad_index ());
1121 const_iterator &operator -= (difference_type n) {
1122 BOOST_UBLAS_CHECK (n <= 0 || it_ >= size_type(n), bad_index ());
1127 difference_type operator - (const const_iterator &it) const {
1128 return it_ - it.it_;
1133 const_reference operator * () const {
1134 BOOST_UBLAS_CHECK ((*this) ().start () <= it_, bad_index ());
1135 BOOST_UBLAS_CHECK (it_ < (*this) ().start () + (*this) ().size (), bad_index ());
1140 const_reference operator [] (difference_type n) const {
1141 return *(*this + n);
1146 size_type index () const {
1147 BOOST_UBLAS_CHECK ((*this) ().start () <= it_, bad_index ());
1148 BOOST_UBLAS_CHECK (it_ < (*this) ().start () + (*this) ().size (), bad_index ());
1149 return it_ - (*this) ().start ();
1154 const_iterator &operator = (const const_iterator &it) {
1155 // Comeau recommends...
1156 this->assign (&it ());
1163 bool operator == (const const_iterator &it) const {
1164 BOOST_UBLAS_CHECK ((*this) () == it (), external_logic ());
1165 return it_ == it.it_;
1168 bool operator < (const const_iterator &it) const {
1169 BOOST_UBLAS_CHECK ((*this) () == it (), external_logic ());
1170 return it_ < it.it_;
1174 const_subiterator_type it_;
1179 const_iterator begin () const {
1180 return const_iterator (*this, start_);
1183 const_iterator cbegin () const {
1187 const_iterator end () const {
1188 return const_iterator (*this, start_ + size_);
1191 const_iterator cend () const {
1196 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
1199 const_reverse_iterator rbegin () const {
1200 return const_reverse_iterator (end ());
1203 const_reverse_iterator crbegin () const {
1207 const_reverse_iterator rend () const {
1208 return const_reverse_iterator (begin ());
1211 const_reverse_iterator crend () const {
1216 basic_range preprocess (size_type size) const {
1219 return basic_range (0, size);
1223 const basic_range &all () {
1230 static const basic_range all_;
1233 template <class Z, class D>
1234 const basic_range<Z,D> basic_range<Z,D>::all_ (0, size_type (-1));
1238 template <class Z, class D>
1240 typedef basic_slice<Z, D> self_type;
1242 typedef Z size_type;
1243 typedef D difference_type;
1244 typedef size_type value_type;
1245 typedef value_type const_reference;
1246 typedef const_reference reference;
1247 typedef const value_type *const_pointer;
1248 typedef value_type *pointer;
1250 // Construction and destruction
1253 start_ (0), stride_ (0), size_ (0) {}
1255 basic_slice (size_type start, difference_type stride, size_type size):
1256 start_ (start), stride_ (stride), size_ (size) {}
1259 size_type start () const {
1263 difference_type stride () const {
1267 size_type size () const {
1271 // Random Access Container
1273 size_type max_size () const {
1278 bool empty () const {
1284 const_reference operator () (size_type i) const {
1285 BOOST_UBLAS_CHECK (i < size_, bad_index ());
1286 BOOST_UBLAS_CHECK (stride_ >= 0 || start_ >= i * -stride_, bad_index ());
1287 return start_ + i * stride_;
1292 basic_slice compose (const basic_range<size_type, difference_type> &r) const {
1293 BOOST_UBLAS_CHECK (stride_ >=0 || start_ >= -stride_ * r.start(), bad_index ());
1294 return basic_slice (start_ + stride_ * r.start (), stride_, r.size ());
1297 basic_slice compose (const basic_slice &s) const {
1298 BOOST_UBLAS_CHECK (stride_ >=0 || start_ >= -stride_ * s.start_, bad_index ());
1299 return basic_slice (start_ + stride_ * s.start_, stride_ * s.stride_, s.size_);
1304 bool operator == (const basic_slice &s) const {
1305 return start_ == s.start_ && stride_ == s.stride_ && size_ == s.size_;
1308 bool operator != (const basic_slice &s) const {
1309 return ! (*this == s);
1315 typedef size_type const_subiterator_type;
1318 #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
1319 typedef indexed_const_iterator<self_type, std::random_access_iterator_tag> const_iterator;
1321 class const_iterator:
1322 public container_const_reference<basic_slice>,
1323 public random_access_iterator_base<std::random_access_iterator_tag,
1324 const_iterator, value_type> {
1326 typedef typename basic_slice::value_type value_type;
1327 typedef typename basic_slice::difference_type difference_type;
1328 typedef typename basic_slice::const_reference reference;
1329 typedef typename basic_slice::const_pointer pointer;
1331 // Construction and destruction
1334 container_const_reference<basic_slice> (), it_ () {}
1336 const_iterator (const basic_slice &s, const const_subiterator_type &it):
1337 container_const_reference<basic_slice> (s), it_ (it) {}
1341 const_iterator &operator ++ () {
1346 const_iterator &operator -- () {
1347 BOOST_UBLAS_CHECK (it_ > 0, bad_index ());
1352 const_iterator &operator += (difference_type n) {
1353 BOOST_UBLAS_CHECK (n >= 0 || it_ >= size_type(-n), bad_index ());
1358 const_iterator &operator -= (difference_type n) {
1359 BOOST_UBLAS_CHECK (n <= 0 || it_ >= size_type(n), bad_index ());
1364 difference_type operator - (const const_iterator &it) const {
1365 return it_ - it.it_;
1370 const_reference operator * () const {
1371 BOOST_UBLAS_CHECK (it_ < (*this) ().size (), bad_index ());
1372 return (*this) ().start () + it_* (*this) ().stride ();
1376 const_reference operator [] (difference_type n) const {
1377 return *(*this + n);
1382 size_type index () const {
1383 BOOST_UBLAS_CHECK (it_ < (*this) ().size (), bad_index ());
1389 const_iterator &operator = (const const_iterator &it) {
1390 // Comeau recommends...
1391 this->assign (&it ());
1398 bool operator == (const const_iterator &it) const {
1399 BOOST_UBLAS_CHECK ((*this) () == it (), external_logic ());
1400 return it_ == it.it_;
1403 bool operator < (const const_iterator &it) const {
1404 BOOST_UBLAS_CHECK ((*this) () == it (), external_logic ());
1405 return it_ < it.it_;
1409 const_subiterator_type it_;
1414 const_iterator begin () const {
1415 return const_iterator (*this, 0);
1418 const_iterator cbegin () const {
1422 const_iterator end () const {
1423 return const_iterator (*this, size_);
1426 const_iterator cend () const {
1431 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
1434 const_reverse_iterator rbegin () const {
1435 return const_reverse_iterator (end ());
1438 const_reverse_iterator crbegin () const {
1442 const_reverse_iterator rend () const {
1443 return const_reverse_iterator (begin ());
1446 const_reverse_iterator crend () const {
1451 basic_slice preprocess (size_type size) const {
1454 return basic_slice (0, 1, size);
1458 const basic_slice &all () {
1464 difference_type stride_;
1466 static const basic_slice all_;
1469 template <class Z, class D>
1470 const basic_slice<Z,D> basic_slice<Z,D>::all_ (0, 1, size_type (-1));
1473 // Indirect array class
1475 class indirect_array {
1476 typedef indirect_array<A> self_type;
1478 typedef A array_type;
1479 typedef const A const_array_type;
1480 typedef typename A::size_type size_type;
1481 typedef typename A::difference_type difference_type;
1482 typedef typename A::value_type value_type;
1483 typedef typename A::const_reference const_reference;
1484 typedef typename A::reference reference;
1485 typedef typename A::const_pointer const_pointer;
1486 typedef typename A::pointer pointer;
1488 // Construction and destruction
1491 size_ (), data_ () {}
1492 explicit BOOST_UBLAS_INLINE
1493 indirect_array (size_type size):
1494 size_ (size), data_ (size) {}
1496 indirect_array (size_type size, const array_type &data):
1497 size_ (size), data_ (data) {}
1499 indirect_array (pointer start, pointer stop):
1500 size_ (stop - start), data_ (stop - start) {
1501 std::copy (start, stop, data_.begin ());
1505 size_type size () const {
1509 const_array_type data () const {
1513 array_type data () {
1517 // Random Access Container
1519 size_type max_size () const {
1524 bool empty () const {
1525 return data_.size () == 0;
1530 const_reference operator () (size_type i) const {
1531 BOOST_UBLAS_CHECK (i < size_, bad_index ());
1535 reference operator () (size_type i) {
1536 BOOST_UBLAS_CHECK (i < size_, bad_index ());
1541 const_reference operator [] (size_type i) const {
1545 reference operator [] (size_type i) {
1551 indirect_array compose (const basic_range<size_type, difference_type> &r) const {
1552 BOOST_UBLAS_CHECK (r.start () + r.size () <= size_, bad_size ());
1553 array_type data (r.size ());
1554 for (size_type i = 0; i < r.size (); ++ i)
1555 data [i] = data_ [r.start () + i];
1556 return indirect_array (r.size (), data);
1559 indirect_array compose (const basic_slice<size_type, difference_type> &s) const {
1560 BOOST_UBLAS_CHECK (s.start () + s.stride () * (s.size () - (s.size () > 0)) <= size (), bad_size ());
1561 array_type data (s.size ());
1562 for (size_type i = 0; i < s.size (); ++ i)
1563 data [i] = data_ [s.start () + s.stride () * i];
1564 return indirect_array (s.size (), data);
1567 indirect_array compose (const indirect_array &ia) const {
1568 array_type data (ia.size_);
1569 for (size_type i = 0; i < ia.size_; ++ i) {
1570 BOOST_UBLAS_CHECK (ia.data_ [i] <= size_, bad_size ());
1571 data [i] = data_ [ia.data_ [i]];
1573 return indirect_array (ia.size_, data);
1579 bool operator == (const indirect_array<OA> &ia) const {
1580 if (size_ != ia.size_)
1582 for (size_type i = 0; i < BOOST_UBLAS_SAME (size_, ia.size_); ++ i)
1583 if (data_ [i] != ia.data_ [i])
1589 bool operator != (const indirect_array<OA> &ia) const {
1590 return ! (*this == ia);
1595 // Use a index difference
1596 typedef difference_type const_subiterator_type;
1599 #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
1600 typedef indexed_const_iterator<indirect_array, std::random_access_iterator_tag> const_iterator;
1602 class const_iterator:
1603 public container_const_reference<indirect_array>,
1604 public random_access_iterator_base<std::random_access_iterator_tag,
1605 const_iterator, value_type> {
1607 typedef typename indirect_array::value_type value_type;
1608 typedef typename indirect_array::difference_type difference_type;
1609 typedef typename indirect_array::const_reference reference;
1610 typedef typename indirect_array::const_pointer pointer;
1612 // Construction and destruction
1615 container_const_reference<indirect_array> (), it_ () {}
1617 const_iterator (const indirect_array &ia, const const_subiterator_type &it):
1618 container_const_reference<indirect_array> (ia), it_ (it) {}
1622 const_iterator &operator ++ () {
1627 const_iterator &operator -- () {
1632 const_iterator &operator += (difference_type n) {
1637 const_iterator &operator -= (difference_type n) {
1642 difference_type operator - (const const_iterator &it) const {
1643 return it_ - it.it_;
1648 const_reference operator * () const {
1649 return (*this) () (it_);
1653 const_reference operator [] (difference_type n) const {
1654 return *(*this + n);
1659 size_type index () const {
1665 const_iterator &operator = (const const_iterator &it) {
1666 // Comeau recommends...
1667 this->assign (&it ());
1674 bool operator == (const const_iterator &it) const {
1675 BOOST_UBLAS_CHECK ((*this) () == it (), external_logic ());
1676 return it_ == it.it_;
1679 bool operator < (const const_iterator &it) const {
1680 BOOST_UBLAS_CHECK ((*this) () == it (), external_logic ());
1681 return it_ < it.it_;
1685 const_subiterator_type it_;
1690 const_iterator begin () const {
1691 return const_iterator (*this, 0);
1694 const_iterator cbegin () const {
1698 const_iterator end () const {
1699 return const_iterator (*this, size_);
1702 const_iterator cend () const {
1707 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
1710 const_reverse_iterator rbegin () const {
1711 return const_reverse_iterator (end ());
1714 const_reverse_iterator crbegin () const {
1718 const_reverse_iterator rend () const {
1719 return const_reverse_iterator (begin ());
1722 const_reverse_iterator crend () const {
1727 indirect_array preprocess (size_type size) const {
1730 indirect_array ia (size);
1731 for (size_type i = 0; i < size; ++ i)
1737 const indirect_array &all () {
1744 static const indirect_array all_;
1748 const indirect_array<A> indirect_array<A>::all_;
1752 // Gunter Winkler contributed the classes index_pair, index_pair_array,
1753 // index_triple and index_triple_array to enable inplace sort of parallel arrays.
1757 public container_reference<V> {
1759 typedef index_pair<V> self_type;
1761 typedef typename V::size_type size_type;
1764 index_pair(V& v, size_type i) :
1765 container_reference<V>(v), i_(i),
1766 v1_(v.data1_[i]), v2_(v.data2_[i]),
1767 dirty_(false), is_copy_(false) {}
1769 index_pair(const self_type& rhs) :
1770 container_reference<V>(rhs()), i_(0),
1771 v1_(rhs.v1_), v2_(rhs.v2_),
1772 dirty_(false), is_copy_(true) {}
1775 if (dirty_ && (!is_copy_) ) {
1776 (*this)().data1_[i_] = v1_;
1777 (*this)().data2_[i_] = v2_;
1782 self_type& operator=(const self_type& rhs) {
1790 void swap(self_type& rhs) {
1797 friend void swap(self_type& lhs, self_type& rhs) {
1801 friend void swap(self_type lhs, self_type rhs) { // For gcc 4.8 and c++11
1807 bool equal(const self_type& rhs) const {
1808 return (v1_ == rhs.v1_);
1811 bool less(const self_type& rhs) const {
1812 return (v1_ < rhs.v1_);
1815 friend bool operator == (const self_type& lhs, const self_type& rhs) {
1816 return lhs.equal(rhs);
1819 friend bool operator != (const self_type& lhs, const self_type& rhs) {
1820 return !lhs.equal(rhs);
1823 friend bool operator < (const self_type& lhs, const self_type& rhs) {
1824 return lhs.less(rhs);
1827 friend bool operator >= (const self_type& lhs, const self_type& rhs) {
1828 return !lhs.less(rhs);
1831 friend bool operator > (const self_type& lhs, const self_type& rhs) {
1832 return rhs.less(lhs);
1835 friend bool operator <= (const self_type& lhs, const self_type& rhs) {
1836 return !rhs.less(lhs);
1841 typename V::value1_type v1_;
1842 typename V::value2_type v2_;
1847 template <class V1, class V2>
1848 class index_pair_array:
1849 private boost::noncopyable {
1851 typedef index_pair_array<V1, V2> self_type;
1853 typedef typename V1::value_type value1_type;
1854 typedef typename V2::value_type value2_type;
1856 typedef typename V1::size_type size_type;
1857 typedef typename V1::difference_type difference_type;
1858 typedef index_pair<self_type> value_type;
1859 // There is nothing that can be referenced directly. Always return a copy of the index_pair
1860 typedef value_type reference;
1861 typedef const value_type const_reference;
1864 index_pair_array(size_type size, V1& data1, V2& data2) :
1865 size_(size),data1_(data1),data2_(data2) {}
1868 size_type size() const {
1873 const_reference operator () (size_type i) const {
1874 return value_type((*this), i);
1877 reference operator () (size_type i) {
1878 return value_type((*this), i);
1881 typedef indexed_iterator<self_type, std::random_access_iterator_tag> iterator;
1882 typedef indexed_const_iterator<self_type, std::random_access_iterator_tag> const_iterator;
1886 return iterator( (*this), 0);
1890 return iterator( (*this), size());
1894 const_iterator begin() const {
1895 return const_iterator( (*this), 0);
1898 const_iterator cbegin () const {
1902 const_iterator end() const {
1903 return const_iterator( (*this), size());
1906 const_iterator cend () const {
1910 // unnecessary function:
1912 bool equal(size_type i1, size_type i2) const {
1913 return data1_[i1] == data1_[i2];
1916 bool less(size_type i1, size_type i2) const {
1917 return data1_[i1] < data1_[i2];
1920 // gives a large speedup
1922 friend void iter_swap(const iterator& lhs, const iterator& rhs) {
1923 const size_type i1 = lhs.index();
1924 const size_type i2 = rhs.index();
1925 std::swap(lhs().data1_[i1], rhs().data1_[i2]);
1926 std::swap(lhs().data2_[i1], rhs().data2_[i2]);
1934 // friend class value_type;
1935 friend class index_pair<self_type>;
1939 class index_triple :
1940 public container_reference<M> {
1942 typedef index_triple<M> self_type;
1944 typedef typename M::size_type size_type;
1947 index_triple(M& m, size_type i) :
1948 container_reference<M>(m), i_(i),
1949 v1_(m.data1_[i]), v2_(m.data2_[i]), v3_(m.data3_[i]),
1950 dirty_(false), is_copy_(false) {}
1952 index_triple(const self_type& rhs) :
1953 container_reference<M>(rhs()), i_(0),
1954 v1_(rhs.v1_), v2_(rhs.v2_), v3_(rhs.v3_),
1955 dirty_(false), is_copy_(true) {}
1958 if (dirty_ && (!is_copy_) ) {
1959 (*this)().data1_[i_] = v1_;
1960 (*this)().data2_[i_] = v2_;
1961 (*this)().data3_[i_] = v3_;
1966 self_type& operator=(const self_type& rhs) {
1975 void swap(self_type& rhs) {
1982 friend void swap(self_type& lhs, self_type& rhs) {
1986 friend void swap(self_type lhs, self_type rhs) { // For gcc 4.8 and c++11
1991 bool equal(const self_type& rhs) const {
1992 return ((v1_ == rhs.v1_) && (v2_ == rhs.v2_));
1995 bool less(const self_type& rhs) const {
1996 return ((v1_ < rhs.v1_) ||
1997 (v1_ == rhs.v1_ && v2_ < rhs.v2_));
2000 friend bool operator == (const self_type& lhs, const self_type& rhs) {
2001 return lhs.equal(rhs);
2004 friend bool operator != (const self_type& lhs, const self_type& rhs) {
2005 return !lhs.equal(rhs);
2008 friend bool operator < (const self_type& lhs, const self_type& rhs) {
2009 return lhs.less(rhs);
2012 friend bool operator >= (const self_type& lhs, const self_type& rhs) {
2013 return !lhs.less(rhs);
2016 friend bool operator > (const self_type& lhs, const self_type& rhs) {
2017 return rhs.less(lhs);
2020 friend bool operator <= (const self_type& lhs, const self_type& rhs) {
2021 return !rhs.less(lhs);
2026 typename M::value1_type v1_;
2027 typename M::value2_type v2_;
2028 typename M::value3_type v3_;
2033 template <class V1, class V2, class V3>
2034 class index_triple_array:
2035 private boost::noncopyable {
2037 typedef index_triple_array<V1, V2, V3> self_type;
2039 typedef typename V1::value_type value1_type;
2040 typedef typename V2::value_type value2_type;
2041 typedef typename V3::value_type value3_type;
2043 typedef typename V1::size_type size_type;
2044 typedef typename V1::difference_type difference_type;
2045 typedef index_triple<self_type> value_type;
2046 // There is nothing that can be referenced directly. Always return a copy of the index_triple
2047 typedef value_type reference;
2048 typedef const value_type const_reference;
2051 index_triple_array(size_type size, V1& data1, V2& data2, V3& data3) :
2052 size_(size),data1_(data1),data2_(data2),data3_(data3) {}
2055 size_type size() const {
2060 const_reference operator () (size_type i) const {
2061 return value_type((*this), i);
2064 reference operator () (size_type i) {
2065 return value_type((*this), i);
2068 typedef indexed_iterator<self_type, std::random_access_iterator_tag> iterator;
2069 typedef indexed_const_iterator<self_type, std::random_access_iterator_tag> const_iterator;
2073 return iterator( (*this), 0);
2077 return iterator( (*this), size());
2081 const_iterator begin() const {
2082 return const_iterator( (*this), 0);
2085 const_iterator cbegin () const {
2089 const_iterator end() const {
2090 return const_iterator( (*this), size());
2093 const_iterator cend () const {
2097 // unnecessary function:
2099 bool equal(size_type i1, size_type i2) const {
2100 return ((data1_[i1] == data1_[i2]) && (data2_[i1] == data2_[i2]));
2103 bool less(size_type i1, size_type i2) const {
2104 return ((data1_[i1] < data1_[i2]) ||
2105 (data1_[i1] == data1_[i2] && data2_[i1] < data2_[i2]));
2108 // gives a large speedup
2110 friend void iter_swap(const iterator& lhs, const iterator& rhs) {
2111 const size_type i1 = lhs.index();
2112 const size_type i2 = rhs.index();
2113 std::swap(lhs().data1_[i1], rhs().data1_[i2]);
2114 std::swap(lhs().data2_[i1], rhs().data2_[i2]);
2115 std::swap(lhs().data3_[i1], rhs().data3_[i2]);
2124 // friend class value_type;
2125 friend class index_triple<self_type>;