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 if (! detail::has_trivial_constructor<T>::value) {
70 for (pointer d = data_; d != data_ + size_; ++d)
71 alloc_.construct(d, value_type());
77 // No value initialised, but still be default constructed
79 unbounded_array (size_type size, const value_type &init, const ALLOC &a = ALLOC()):
80 alloc_ (a), size_ (size) {
82 data_ = alloc_.allocate (size_);
83 std::uninitialized_fill (begin(), end(), init);
89 unbounded_array (const unbounded_array &c):
90 storage_array<unbounded_array<T, ALLOC> >(),
91 alloc_ (c.alloc_), size_ (c.size_) {
93 data_ = alloc_.allocate (size_);
94 std::uninitialized_copy (c.begin(), c.end(), begin());
100 ~unbounded_array () {
102 if (! detail::has_trivial_destructor<T>::value) {
103 // std::_Destroy (begin(), end(), alloc_);
104 const iterator i_end = end();
105 for (iterator i = begin (); i != i_end; ++i) {
106 iterator_destroy (i);
109 alloc_.deallocate (data_, size_);
116 void resize_internal (const size_type size, const value_type init, const bool preserve) {
118 pointer p_data = data_;
120 data_ = alloc_.allocate (size);
125 for (; di != data_ + size; ++di) {
126 alloc_.construct (di, *si);
131 for (; si != p_data + size_; ++si) {
132 alloc_.construct (di, *si);
135 for (; di != data_ + size; ++di) {
136 alloc_.construct (di, init);
141 if (! detail::has_trivial_constructor<T>::value) {
142 for (pointer di = data_; di != data_ + size; ++di)
143 alloc_.construct (di, value_type());
149 if (! detail::has_trivial_destructor<T>::value) {
150 for (pointer si = p_data; si != p_data + size_; ++si)
153 alloc_.deallocate (p_data, size_);
163 void resize (size_type size) {
164 resize_internal (size, value_type (), false);
167 void resize (size_type size, value_type init) {
168 resize_internal (size, init, true);
171 // Random Access Container
173 size_type max_size () const {
174 return ALLOC ().max_size();
178 bool empty () const {
183 size_type size () const {
189 const_reference operator [] (size_type i) const {
190 BOOST_UBLAS_CHECK (i < size_, bad_index ());
194 reference operator [] (size_type i) {
195 BOOST_UBLAS_CHECK (i < size_, bad_index ());
201 unbounded_array &operator = (const unbounded_array &a) {
204 std::copy (a.data_, a.data_ + a.size_, data_);
209 unbounded_array &assign_temporary (unbounded_array &a) {
216 void swap (unbounded_array &a) {
218 std::swap (size_, a.size_);
219 std::swap (data_, a.data_);
223 friend void swap (unbounded_array &a1, unbounded_array &a2) {
228 const_iterator begin () const {
232 const_iterator cbegin () const {
236 const_iterator end () const {
237 return data_ + size_;
240 const_iterator cend () const {
250 return data_ + size_;
254 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
255 typedef std::reverse_iterator<iterator> reverse_iterator;
258 const_reverse_iterator rbegin () const {
259 return const_reverse_iterator (end ());
262 const_reverse_iterator crbegin () const {
266 const_reverse_iterator rend () const {
267 return const_reverse_iterator (begin ());
270 const_reverse_iterator crend () const {
274 reverse_iterator rbegin () {
275 return reverse_iterator (end ());
278 reverse_iterator rend () {
279 return reverse_iterator (begin ());
283 allocator_type get_allocator () {
288 friend class boost::serialization::access;
291 template<class Archive>
292 void serialize(Archive & ar, const unsigned int /*version*/)
294 serialization::collection_size_type s(size_);
295 ar & serialization::make_nvp("size",s);
296 if ( Archive::is_loading::value ) {
299 ar & serialization::make_array(data_, s);
303 // Handle explict destroy on a (possibly indexed) iterator
305 static void iterator_destroy (iterator &i) {
306 (&(*i)) -> ~value_type ();
313 // Bounded array - with allocator for size_type and difference_type
314 template<class T, std::size_t N, class ALLOC>
316 public storage_array<bounded_array<T, N, ALLOC> > {
318 typedef bounded_array<T, N, ALLOC> self_type;
320 // No allocator_type as ALLOC is not used for allocation
321 typedef typename ALLOC::size_type size_type;
322 typedef typename ALLOC::difference_type difference_type;
323 typedef T value_type;
324 typedef const T &const_reference;
325 typedef T &reference;
326 typedef const T *const_pointer;
328 typedef const_pointer const_iterator;
329 typedef pointer iterator;
331 // Construction and destruction
334 size_ (0) /*, data_ ()*/ { // size 0 - use bounded_vector to default construct with size N
336 explicit BOOST_UBLAS_INLINE
337 bounded_array (size_type size):
338 size_ (size) /*, data_ ()*/ {
339 BOOST_UBLAS_CHECK (size_ <= N, bad_size ());
340 // data_ (an array) elements are already default constructed
343 bounded_array (size_type size, const value_type &init):
344 size_ (size) /*, data_ ()*/ {
345 BOOST_UBLAS_CHECK (size_ <= N, bad_size ());
346 // ISSUE elements should be value constructed here, but we must fill instead as already default constructed
347 std::fill (begin(), end(), init) ;
350 bounded_array (const bounded_array &c):
352 // ISSUE elements should be copy constructed here, but we must copy instead as already default constructed
353 std::copy (c.begin(), c.end(), begin());
358 void resize (size_type size) {
359 BOOST_UBLAS_CHECK (size <= N, bad_size ());
363 void resize (size_type size, value_type init) {
364 BOOST_UBLAS_CHECK (size <= N, bad_size ());
366 std::fill (data_ + size_, data_ + size, init);
370 // Random Access Container
372 size_type max_size () const {
377 bool empty () const {
382 size_type size () const {
388 const_reference operator [] (size_type i) const {
389 BOOST_UBLAS_CHECK (i < size_, bad_index ());
393 reference operator [] (size_type i) {
394 BOOST_UBLAS_CHECK (i < size_, bad_index ());
400 bounded_array &operator = (const bounded_array &a) {
403 std::copy (a.data_, a.data_ + a.size_, data_);
408 bounded_array &assign_temporary (bounded_array &a) {
415 void swap (bounded_array &a) {
417 std::swap (size_, a.size_);
418 std::swap_ranges (data_, data_ + (std::max) (size_, a.size_), a.data_);
422 friend void swap (bounded_array &a1, bounded_array &a2) {
427 const_iterator begin () const {
431 const_iterator cbegin () const {
435 const_iterator end () const {
436 return data_ + size_;
439 const_iterator cend () const {
449 return data_ + size_;
453 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
454 typedef std::reverse_iterator<iterator> reverse_iterator;
457 const_reverse_iterator rbegin () const {
458 return const_reverse_iterator (end ());
461 const_reverse_iterator crbegin () const {
465 const_reverse_iterator rend () const {
466 return const_reverse_iterator (begin ());
469 const_reverse_iterator crend () const {
473 reverse_iterator rbegin () {
474 return reverse_iterator (end ());
477 reverse_iterator rend () {
478 return reverse_iterator (begin ());
483 friend class boost::serialization::access;
485 template<class Archive>
486 void serialize(Archive & ar, const unsigned int /*version*/)
488 serialization::collection_size_type s(size_);
489 ar & serialization::make_nvp("size", s);
490 if ( Archive::is_loading::value ) {
491 if (s > N) bad_size("too large size in bounded_array::load()\n").raise();
494 ar & serialization::make_array(data_, s);
499 // MSVC does not like arrays of size 0 in base classes. Hence, this conditionally changes the size to 1
501 BOOST_UBLAS_BOUNDED_ARRAY_ALIGN value_type data_ [(N>0)?N:1];
503 BOOST_UBLAS_BOUNDED_ARRAY_ALIGN value_type data_ [N];
508 // Array adaptor with normal deep copy semantics of elements
511 public storage_array<array_adaptor<T> > {
513 typedef array_adaptor<T> self_type;
515 typedef std::size_t size_type;
516 typedef std::ptrdiff_t difference_type;
517 typedef T value_type;
518 typedef const T &const_reference;
519 typedef T &reference;
520 typedef const T *const_pointer;
523 // Construction and destruction
526 size_ (0), own_ (true), data_ (new value_type [0]) {
528 explicit BOOST_UBLAS_INLINE
529 array_adaptor (size_type size):
530 size_ (size), own_ (true), data_ (new value_type [size]) {
533 array_adaptor (size_type size, const value_type &init):
534 size_ (size), own_ (true), data_ (new value_type [size]) {
535 std::fill (data_, data_ + size_, init);
538 array_adaptor (size_type size, pointer data):
539 size_ (size), own_ (false), data_ (data) {}
542 BOOST_UBLAS_INLINE array_adaptor (T (&data)[N]):
543 size_ (N), own_ (false), data_ (data) {}
545 array_adaptor (const array_adaptor &a):
546 storage_array<self_type> (),
547 size_ (a.size_), own_ (true), data_ (new value_type [a.size_]) {
560 void resize_internal (size_type size, value_type init, bool preserve = true) {
562 pointer data = new value_type [size];
564 std::copy (data_, data_ + (std::min) (size, size_), data);
565 std::fill (data + (std::min) (size, size_), data + size, init);
575 void resize_internal (size_type size, pointer data, value_type init, bool preserve = true) {
578 std::copy (data_, data_ + (std::min) (size, size_), data);
579 std::fill (data + (std::min) (size, size_), data + size, init);
587 std::fill (data + (std::min) (size, size_), data + size, init);
593 void resize (size_type size) {
594 resize_internal (size, value_type (), false);
597 void resize (size_type size, value_type init) {
598 resize_internal (size, init, true);
601 void resize (size_type size, pointer data) {
602 resize_internal (size, data, value_type (), false);
605 void resize (size_type size, pointer data, value_type init) {
606 resize_internal (size, data, init, true);
610 BOOST_UBLAS_INLINE void resize (T (&data)[N]) {
611 resize_internal (N, data, value_type (), false);
615 BOOST_UBLAS_INLINE void resize (T (&data)[N], value_type init) {
616 resize_internal (N, data, init, true);
620 size_type size () const {
626 const_reference operator [] (size_type i) const {
627 BOOST_UBLAS_CHECK (i < size_, bad_index ());
631 reference operator [] (size_type i) {
632 BOOST_UBLAS_CHECK (i < size_, bad_index ());
638 array_adaptor &operator = (const array_adaptor &a) {
641 std::copy (a.data_, a.data_ + a.size_, data_);
646 array_adaptor &assign_temporary (array_adaptor &a) {
656 void swap (array_adaptor &a) {
658 std::swap (size_, a.size_);
659 std::swap (own_, a.own_);
660 std::swap (data_, a.data_);
664 friend void swap (array_adaptor &a1, array_adaptor &a2) {
668 // Iterators simply are pointers.
670 typedef const_pointer const_iterator;
673 const_iterator begin () const {
677 const_iterator cbegin () const {
681 const_iterator end () const {
682 return data_ + size_;
685 const_iterator cend () const {
689 typedef pointer iterator;
697 return data_ + size_;
701 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
702 typedef std::reverse_iterator<iterator> reverse_iterator;
705 const_reverse_iterator rbegin () const {
706 return const_reverse_iterator (end ());
709 const_reverse_iterator crbegin () const {
713 const_reverse_iterator rend () const {
714 return const_reverse_iterator (begin ());
717 const_reverse_iterator crend () const {
721 reverse_iterator rbegin () {
722 return reverse_iterator (end ());
725 reverse_iterator rend () {
726 return reverse_iterator (begin ());
735 #ifdef BOOST_UBLAS_SHALLOW_ARRAY_ADAPTOR
736 // Array adaptor with shallow (reference) copy semantics of elements.
737 // shared_array is used to maintain reference counts.
738 // This class breaks the normal copy semantics for a storage container and is very dangerous!
740 class shallow_array_adaptor:
741 public storage_array<shallow_array_adaptor<T> > {
743 typedef shallow_array_adaptor<T> self_type;
747 typedef void result_type;
748 typedef TT *argument_type;
751 result_type operator () (argument_type /* x */) {}
755 typedef std::size_t size_type;
756 typedef std::ptrdiff_t difference_type;
757 typedef T value_type;
758 typedef const T &const_reference;
759 typedef T &reference;
760 typedef const T *const_pointer;
763 // Construction and destruction
765 shallow_array_adaptor ():
766 size_ (0), own_ (true), data_ (new value_type [0]) {
768 explicit BOOST_UBLAS_INLINE
769 shallow_array_adaptor (size_type size):
770 size_ (size), own_ (true), data_ (new value_type [size]) {
773 shallow_array_adaptor (size_type size, const value_type &init):
774 size_ (size), own_ (true), data_ (new value_type [size]) {
775 std::fill (data_.get (), data_.get () + size_, init);
778 shallow_array_adaptor (size_type size, pointer data):
779 size_ (size), own_ (false), data_ (data, leaker<value_type> ()) {}
782 shallow_array_adaptor (T (&data)[N]):
783 size_ (N), own_ (false), data_ (data, leaker<value_type> ()) {}
786 shallow_array_adaptor (const shallow_array_adaptor &a):
787 storage_array<self_type> (),
788 size_ (a.size_), own_ (a.own_), data_ (a.data_) {}
791 ~shallow_array_adaptor () {
797 void resize_internal (size_type size, value_type init, bool preserve = true) {
799 shared_array<value_type> data (new value_type [size]);
801 std::copy (data_.get (), data_.get () + (std::min) (size, size_), data.get ());
802 std::fill (data.get () + (std::min) (size, size_), data.get () + size, init);
810 void resize_internal (size_type size, pointer data, value_type init, bool preserve = true) {
812 std::copy (data_.get (), data_.get () + (std::min) (size, size_), data);
813 std::fill (data + (std::min) (size, size_), data + size, init);
817 data_.reset(data, leaker<value_type> ());
821 void resize (size_type size) {
822 resize_internal (size, value_type (), false);
825 void resize (size_type size, value_type init) {
826 resize_internal (size, init, true);
829 void resize (size_type size, pointer data) {
830 resize_internal (size, data, value_type (), false);
833 void resize (size_type size, pointer data, value_type init) {
834 resize_internal (size, data, init, true);
838 void resize (T (&data)[N]) {
839 resize_internal (N, data, value_type (), false);
843 void resize (T (&data)[N], value_type init) {
844 resize_internal (N, data, init, true);
848 size_type size () const {
854 const_reference operator [] (size_type i) const {
855 BOOST_UBLAS_CHECK (i < size_, bad_index ());
859 reference operator [] (size_type i) {
860 BOOST_UBLAS_CHECK (i < size_, bad_index ());
866 shallow_array_adaptor &operator = (const shallow_array_adaptor &a) {
869 std::copy (a.data_.get (), a.data_.get () + a.size_, data_.get ());
874 shallow_array_adaptor &assign_temporary (shallow_array_adaptor &a) {
884 void swap (shallow_array_adaptor &a) {
886 std::swap (size_, a.size_);
887 std::swap (own_, a.own_);
888 std::swap (data_, a.data_);
892 friend void swap (shallow_array_adaptor &a1, shallow_array_adaptor &a2) {
896 // Iterators simply are pointers.
898 typedef const_pointer const_iterator;
901 const_iterator begin () const {
905 const_iterator cbegin () const {
909 const_iterator end () const {
910 return data_.get () + size_;
913 const_iterator cend () const {
917 typedef pointer iterator;
925 return data_.get () + size_;
929 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
930 typedef std::reverse_iterator<iterator> reverse_iterator;
933 const_reverse_iterator rbegin () const {
934 return const_reverse_iterator (end ());
937 const_reverse_iterator crbegin () const {
941 const_reverse_iterator rend () const {
942 return const_reverse_iterator (begin ());
945 const_reverse_iterator crend () const {
949 reverse_iterator rbegin () {
950 return reverse_iterator (end ());
953 reverse_iterator rend () {
954 return reverse_iterator (begin ());
960 shared_array<value_type> data_;
967 template <class Z, class D>
969 typedef basic_range<Z, D> self_type;
972 typedef D difference_type;
973 typedef size_type value_type;
974 typedef value_type const_reference;
975 typedef const_reference reference;
976 typedef const value_type *const_pointer;
977 typedef value_type *pointer;
979 // Construction and destruction
982 start_ (0), size_ (0) {}
984 basic_range (size_type start, size_type stop):
985 start_ (start), size_ (stop - start) {
986 BOOST_UBLAS_CHECK (start_ <= stop, bad_index ());
990 size_type start () const {
994 size_type size () const {
998 // Random Access Container
1000 size_type max_size () const {
1005 bool empty () const {
1011 const_reference operator () (size_type i) const {
1012 BOOST_UBLAS_CHECK (i < size_, bad_index ());
1018 basic_range compose (const basic_range &r) const {
1019 return basic_range (start_ + r.start_, start_ + r.start_ + r.size_);
1024 bool operator == (const basic_range &r) const {
1025 return start_ == r.start_ && size_ == r.size_;
1028 bool operator != (const basic_range &r) const {
1029 return ! (*this == r);
1035 typedef size_type const_subiterator_type;
1038 #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
1039 typedef indexed_const_iterator<self_type, std::random_access_iterator_tag> const_iterator;
1041 class const_iterator:
1042 public container_const_reference<basic_range>,
1043 public random_access_iterator_base<std::random_access_iterator_tag,
1044 const_iterator, value_type> {
1046 typedef typename basic_range::value_type value_type;
1047 typedef typename basic_range::difference_type difference_type;
1048 typedef typename basic_range::const_reference reference;
1049 typedef typename basic_range::const_pointer pointer;
1051 // Construction and destruction
1054 container_const_reference<basic_range> (), it_ () {}
1056 const_iterator (const basic_range &r, const const_subiterator_type &it):
1057 container_const_reference<basic_range> (r), it_ (it) {}
1061 const_iterator &operator ++ () {
1066 const_iterator &operator -- () {
1067 BOOST_UBLAS_CHECK (it_ > 0, bad_index ());
1072 const_iterator &operator += (difference_type n) {
1073 BOOST_UBLAS_CHECK (n >= 0 || it_ >= size_type(-n), bad_index ());
1078 const_iterator &operator -= (difference_type n) {
1079 BOOST_UBLAS_CHECK (n <= 0 || it_ >= size_type(n), bad_index ());
1084 difference_type operator - (const const_iterator &it) const {
1085 return it_ - it.it_;
1090 const_reference operator * () const {
1091 BOOST_UBLAS_CHECK ((*this) ().start () <= it_, bad_index ());
1092 BOOST_UBLAS_CHECK (it_ < (*this) ().start () + (*this) ().size (), bad_index ());
1097 const_reference operator [] (difference_type n) const {
1098 return *(*this + n);
1103 size_type index () const {
1104 BOOST_UBLAS_CHECK ((*this) ().start () <= it_, bad_index ());
1105 BOOST_UBLAS_CHECK (it_ < (*this) ().start () + (*this) ().size (), bad_index ());
1106 return it_ - (*this) ().start ();
1111 const_iterator &operator = (const const_iterator &it) {
1112 // Comeau recommends...
1113 this->assign (&it ());
1120 bool operator == (const const_iterator &it) const {
1121 BOOST_UBLAS_CHECK ((*this) () == it (), external_logic ());
1122 return it_ == it.it_;
1125 bool operator < (const const_iterator &it) const {
1126 BOOST_UBLAS_CHECK ((*this) () == it (), external_logic ());
1127 return it_ < it.it_;
1131 const_subiterator_type it_;
1136 const_iterator begin () const {
1137 return const_iterator (*this, start_);
1140 const_iterator cbegin () const {
1144 const_iterator end () const {
1145 return const_iterator (*this, start_ + size_);
1148 const_iterator cend () const {
1153 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
1156 const_reverse_iterator rbegin () const {
1157 return const_reverse_iterator (end ());
1160 const_reverse_iterator crbegin () const {
1164 const_reverse_iterator rend () const {
1165 return const_reverse_iterator (begin ());
1168 const_reverse_iterator crend () const {
1173 basic_range preprocess (size_type size) const {
1176 return basic_range (0, size);
1180 const basic_range &all () {
1187 static const basic_range all_;
1190 template <class Z, class D>
1191 const basic_range<Z,D> basic_range<Z,D>::all_ (0, size_type (-1));
1195 template <class Z, class D>
1197 typedef basic_slice<Z, D> self_type;
1199 typedef Z size_type;
1200 typedef D difference_type;
1201 typedef size_type value_type;
1202 typedef value_type const_reference;
1203 typedef const_reference reference;
1204 typedef const value_type *const_pointer;
1205 typedef value_type *pointer;
1207 // Construction and destruction
1210 start_ (0), stride_ (0), size_ (0) {}
1212 basic_slice (size_type start, difference_type stride, size_type size):
1213 start_ (start), stride_ (stride), size_ (size) {}
1216 size_type start () const {
1220 difference_type stride () const {
1224 size_type size () const {
1228 // Random Access Container
1230 size_type max_size () const {
1235 bool empty () const {
1241 const_reference operator () (size_type i) const {
1242 BOOST_UBLAS_CHECK (i < size_, bad_index ());
1243 BOOST_UBLAS_CHECK (stride_ >= 0 || start_ >= i * -stride_, bad_index ());
1244 return start_ + i * stride_;
1249 basic_slice compose (const basic_range<size_type, difference_type> &r) const {
1250 BOOST_UBLAS_CHECK (stride_ >=0 || start_ >= -stride_ * r.start(), bad_index ());
1251 return basic_slice (start_ + stride_ * r.start (), stride_, r.size ());
1254 basic_slice compose (const basic_slice &s) const {
1255 BOOST_UBLAS_CHECK (stride_ >=0 || start_ >= -stride_ * s.start_, bad_index ());
1256 return basic_slice (start_ + stride_ * s.start_, stride_ * s.stride_, s.size_);
1261 bool operator == (const basic_slice &s) const {
1262 return start_ == s.start_ && stride_ == s.stride_ && size_ == s.size_;
1265 bool operator != (const basic_slice &s) const {
1266 return ! (*this == s);
1272 typedef size_type const_subiterator_type;
1275 #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
1276 typedef indexed_const_iterator<self_type, std::random_access_iterator_tag> const_iterator;
1278 class const_iterator:
1279 public container_const_reference<basic_slice>,
1280 public random_access_iterator_base<std::random_access_iterator_tag,
1281 const_iterator, value_type> {
1283 typedef typename basic_slice::value_type value_type;
1284 typedef typename basic_slice::difference_type difference_type;
1285 typedef typename basic_slice::const_reference reference;
1286 typedef typename basic_slice::const_pointer pointer;
1288 // Construction and destruction
1291 container_const_reference<basic_slice> (), it_ () {}
1293 const_iterator (const basic_slice &s, const const_subiterator_type &it):
1294 container_const_reference<basic_slice> (s), it_ (it) {}
1298 const_iterator &operator ++ () {
1303 const_iterator &operator -- () {
1304 BOOST_UBLAS_CHECK (it_ > 0, bad_index ());
1309 const_iterator &operator += (difference_type n) {
1310 BOOST_UBLAS_CHECK (n >= 0 || it_ >= size_type(-n), bad_index ());
1315 const_iterator &operator -= (difference_type n) {
1316 BOOST_UBLAS_CHECK (n <= 0 || it_ >= size_type(n), bad_index ());
1321 difference_type operator - (const const_iterator &it) const {
1322 return it_ - it.it_;
1327 const_reference operator * () const {
1328 BOOST_UBLAS_CHECK (it_ < (*this) ().size (), bad_index ());
1329 return (*this) ().start () + it_* (*this) ().stride ();
1333 const_reference operator [] (difference_type n) const {
1334 return *(*this + n);
1339 size_type index () const {
1340 BOOST_UBLAS_CHECK (it_ < (*this) ().size (), bad_index ());
1346 const_iterator &operator = (const const_iterator &it) {
1347 // Comeau recommends...
1348 this->assign (&it ());
1355 bool operator == (const const_iterator &it) const {
1356 BOOST_UBLAS_CHECK ((*this) () == it (), external_logic ());
1357 return it_ == it.it_;
1360 bool operator < (const const_iterator &it) const {
1361 BOOST_UBLAS_CHECK ((*this) () == it (), external_logic ());
1362 return it_ < it.it_;
1366 const_subiterator_type it_;
1371 const_iterator begin () const {
1372 return const_iterator (*this, 0);
1375 const_iterator cbegin () const {
1379 const_iterator end () const {
1380 return const_iterator (*this, size_);
1383 const_iterator cend () const {
1388 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
1391 const_reverse_iterator rbegin () const {
1392 return const_reverse_iterator (end ());
1395 const_reverse_iterator crbegin () const {
1399 const_reverse_iterator rend () const {
1400 return const_reverse_iterator (begin ());
1403 const_reverse_iterator crend () const {
1408 basic_slice preprocess (size_type size) const {
1411 return basic_slice (0, 1, size);
1415 const basic_slice &all () {
1421 difference_type stride_;
1423 static const basic_slice all_;
1426 template <class Z, class D>
1427 const basic_slice<Z,D> basic_slice<Z,D>::all_ (0, 1, size_type (-1));
1430 // Indirect array class
1432 class indirect_array {
1433 typedef indirect_array<A> self_type;
1435 typedef A array_type;
1436 typedef const A const_array_type;
1437 typedef typename A::size_type size_type;
1438 typedef typename A::difference_type difference_type;
1439 typedef typename A::value_type value_type;
1440 typedef typename A::const_reference const_reference;
1441 typedef typename A::reference reference;
1442 typedef typename A::const_pointer const_pointer;
1443 typedef typename A::pointer pointer;
1445 // Construction and destruction
1448 size_ (), data_ () {}
1449 explicit BOOST_UBLAS_INLINE
1450 indirect_array (size_type size):
1451 size_ (size), data_ (size) {}
1453 indirect_array (size_type size, const array_type &data):
1454 size_ (size), data_ (data) {}
1456 indirect_array (pointer start, pointer stop):
1457 size_ (stop - start), data_ (stop - start) {
1458 std::copy (start, stop, data_.begin ());
1462 size_type size () const {
1466 const_array_type data () const {
1470 array_type data () {
1474 // Random Access Container
1476 size_type max_size () const {
1481 bool empty () const {
1482 return data_.size () == 0;
1487 const_reference operator () (size_type i) const {
1488 BOOST_UBLAS_CHECK (i < size_, bad_index ());
1492 reference operator () (size_type i) {
1493 BOOST_UBLAS_CHECK (i < size_, bad_index ());
1498 const_reference operator [] (size_type i) const {
1502 reference operator [] (size_type i) {
1508 indirect_array compose (const basic_range<size_type, difference_type> &r) const {
1509 BOOST_UBLAS_CHECK (r.start () + r.size () <= size_, bad_size ());
1510 array_type data (r.size ());
1511 for (size_type i = 0; i < r.size (); ++ i)
1512 data [i] = data_ [r.start () + i];
1513 return indirect_array (r.size (), data);
1516 indirect_array compose (const basic_slice<size_type, difference_type> &s) const {
1517 BOOST_UBLAS_CHECK (s.start () + s.stride () * (s.size () - (s.size () > 0)) <= size (), bad_size ());
1518 array_type data (s.size ());
1519 for (size_type i = 0; i < s.size (); ++ i)
1520 data [i] = data_ [s.start () + s.stride () * i];
1521 return indirect_array (s.size (), data);
1524 indirect_array compose (const indirect_array &ia) const {
1525 array_type data (ia.size_);
1526 for (size_type i = 0; i < ia.size_; ++ i) {
1527 BOOST_UBLAS_CHECK (ia.data_ [i] <= size_, bad_size ());
1528 data [i] = data_ [ia.data_ [i]];
1530 return indirect_array (ia.size_, data);
1536 bool operator == (const indirect_array<OA> &ia) const {
1537 if (size_ != ia.size_)
1539 for (size_type i = 0; i < BOOST_UBLAS_SAME (size_, ia.size_); ++ i)
1540 if (data_ [i] != ia.data_ [i])
1546 bool operator != (const indirect_array<OA> &ia) const {
1547 return ! (*this == ia);
1552 // Use a index difference
1553 typedef difference_type const_subiterator_type;
1556 #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
1557 typedef indexed_const_iterator<indirect_array, std::random_access_iterator_tag> const_iterator;
1559 class const_iterator:
1560 public container_const_reference<indirect_array>,
1561 public random_access_iterator_base<std::random_access_iterator_tag,
1562 const_iterator, value_type> {
1564 typedef typename indirect_array::value_type value_type;
1565 typedef typename indirect_array::difference_type difference_type;
1566 typedef typename indirect_array::const_reference reference;
1567 typedef typename indirect_array::const_pointer pointer;
1569 // Construction and destruction
1572 container_const_reference<indirect_array> (), it_ () {}
1574 const_iterator (const indirect_array &ia, const const_subiterator_type &it):
1575 container_const_reference<indirect_array> (ia), it_ (it) {}
1579 const_iterator &operator ++ () {
1584 const_iterator &operator -- () {
1589 const_iterator &operator += (difference_type n) {
1594 const_iterator &operator -= (difference_type n) {
1599 difference_type operator - (const const_iterator &it) const {
1600 return it_ - it.it_;
1605 const_reference operator * () const {
1606 return (*this) () (it_);
1610 const_reference operator [] (difference_type n) const {
1611 return *(*this + n);
1616 size_type index () const {
1622 const_iterator &operator = (const const_iterator &it) {
1623 // Comeau recommends...
1624 this->assign (&it ());
1631 bool operator == (const const_iterator &it) const {
1632 BOOST_UBLAS_CHECK ((*this) () == it (), external_logic ());
1633 return it_ == it.it_;
1636 bool operator < (const const_iterator &it) const {
1637 BOOST_UBLAS_CHECK ((*this) () == it (), external_logic ());
1638 return it_ < it.it_;
1642 const_subiterator_type it_;
1647 const_iterator begin () const {
1648 return const_iterator (*this, 0);
1651 const_iterator cbegin () const {
1655 const_iterator end () const {
1656 return const_iterator (*this, size_);
1659 const_iterator cend () const {
1664 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
1667 const_reverse_iterator rbegin () const {
1668 return const_reverse_iterator (end ());
1671 const_reverse_iterator crbegin () const {
1675 const_reverse_iterator rend () const {
1676 return const_reverse_iterator (begin ());
1679 const_reverse_iterator crend () const {
1684 indirect_array preprocess (size_type size) const {
1687 indirect_array ia (size);
1688 for (size_type i = 0; i < size; ++ i)
1694 const indirect_array &all () {
1701 static const indirect_array all_;
1705 const indirect_array<A> indirect_array<A>::all_;
1709 // Gunter Winkler contributed the classes index_pair, index_pair_array,
1710 // index_triple and index_triple_array to enable inplace sort of parallel arrays.
1714 public container_reference<V> {
1716 typedef index_pair<V> self_type;
1718 typedef typename V::size_type size_type;
1721 index_pair(V& v, size_type i) :
1722 container_reference<V>(v), i_(i),
1723 v1_(v.data1_[i]), v2_(v.data2_[i]),
1724 dirty_(false), is_copy_(false) {}
1726 index_pair(const self_type& rhs) :
1727 container_reference<V>(rhs()), i_(0),
1728 v1_(rhs.v1_), v2_(rhs.v2_),
1729 dirty_(false), is_copy_(true) {}
1732 if (dirty_ && (!is_copy_) ) {
1733 (*this)().data1_[i_] = v1_;
1734 (*this)().data2_[i_] = v2_;
1739 self_type& operator=(const self_type& rhs) {
1747 void swap(self_type& rhs) {
1754 friend void swap(self_type& lhs, self_type& rhs) {
1758 friend void swap(self_type lhs, self_type rhs) { // For gcc 4.8 and c++11
1764 bool equal(const self_type& rhs) const {
1765 return (v1_ == rhs.v1_);
1768 bool less(const self_type& rhs) const {
1769 return (v1_ < rhs.v1_);
1772 friend bool operator == (const self_type& lhs, const self_type& rhs) {
1773 return lhs.equal(rhs);
1776 friend bool operator != (const self_type& lhs, const self_type& rhs) {
1777 return !lhs.equal(rhs);
1780 friend bool operator < (const self_type& lhs, const self_type& rhs) {
1781 return lhs.less(rhs);
1784 friend bool operator >= (const self_type& lhs, const self_type& rhs) {
1785 return !lhs.less(rhs);
1788 friend bool operator > (const self_type& lhs, const self_type& rhs) {
1789 return rhs.less(lhs);
1792 friend bool operator <= (const self_type& lhs, const self_type& rhs) {
1793 return !rhs.less(lhs);
1798 typename V::value1_type v1_;
1799 typename V::value2_type v2_;
1804 template <class V1, class V2>
1805 class index_pair_array:
1806 private boost::noncopyable {
1808 typedef index_pair_array<V1, V2> self_type;
1810 typedef typename V1::value_type value1_type;
1811 typedef typename V2::value_type value2_type;
1813 typedef typename V1::size_type size_type;
1814 typedef typename V1::difference_type difference_type;
1815 typedef index_pair<self_type> value_type;
1816 // There is nothing that can be referenced directly. Always return a copy of the index_pair
1817 typedef value_type reference;
1818 typedef const value_type const_reference;
1821 index_pair_array(size_type size, V1& data1, V2& data2) :
1822 size_(size),data1_(data1),data2_(data2) {}
1825 size_type size() const {
1830 const_reference operator () (size_type i) const {
1831 return value_type((*this), i);
1834 reference operator () (size_type i) {
1835 return value_type((*this), i);
1838 typedef indexed_iterator<self_type, std::random_access_iterator_tag> iterator;
1839 typedef indexed_const_iterator<self_type, std::random_access_iterator_tag> const_iterator;
1843 return iterator( (*this), 0);
1847 return iterator( (*this), size());
1851 const_iterator begin() const {
1852 return const_iterator( (*this), 0);
1855 const_iterator cbegin () const {
1859 const_iterator end() const {
1860 return const_iterator( (*this), size());
1863 const_iterator cend () const {
1867 // unnecessary function:
1869 bool equal(size_type i1, size_type i2) const {
1870 return data1_[i1] == data1_[i2];
1873 bool less(size_type i1, size_type i2) const {
1874 return data1_[i1] < data1_[i2];
1877 // gives a large speedup
1879 friend void iter_swap(const iterator& lhs, const iterator& rhs) {
1880 const size_type i1 = lhs.index();
1881 const size_type i2 = rhs.index();
1882 std::swap(lhs().data1_[i1], rhs().data1_[i2]);
1883 std::swap(lhs().data2_[i1], rhs().data2_[i2]);
1891 // friend class value_type;
1892 friend class index_pair<self_type>;
1896 class index_triple :
1897 public container_reference<M> {
1899 typedef index_triple<M> self_type;
1901 typedef typename M::size_type size_type;
1904 index_triple(M& m, size_type i) :
1905 container_reference<M>(m), i_(i),
1906 v1_(m.data1_[i]), v2_(m.data2_[i]), v3_(m.data3_[i]),
1907 dirty_(false), is_copy_(false) {}
1909 index_triple(const self_type& rhs) :
1910 container_reference<M>(rhs()), i_(0),
1911 v1_(rhs.v1_), v2_(rhs.v2_), v3_(rhs.v3_),
1912 dirty_(false), is_copy_(true) {}
1915 if (dirty_ && (!is_copy_) ) {
1916 (*this)().data1_[i_] = v1_;
1917 (*this)().data2_[i_] = v2_;
1918 (*this)().data3_[i_] = v3_;
1923 self_type& operator=(const self_type& rhs) {
1932 void swap(self_type& rhs) {
1939 friend void swap(self_type& lhs, self_type& rhs) {
1943 friend void swap(self_type lhs, self_type rhs) { // For gcc 4.8 and c++11
1948 bool equal(const self_type& rhs) const {
1949 return ((v1_ == rhs.v1_) && (v2_ == rhs.v2_));
1952 bool less(const self_type& rhs) const {
1953 return ((v1_ < rhs.v1_) ||
1954 (v1_ == rhs.v1_ && v2_ < rhs.v2_));
1957 friend bool operator == (const self_type& lhs, const self_type& rhs) {
1958 return lhs.equal(rhs);
1961 friend bool operator != (const self_type& lhs, const self_type& rhs) {
1962 return !lhs.equal(rhs);
1965 friend bool operator < (const self_type& lhs, const self_type& rhs) {
1966 return lhs.less(rhs);
1969 friend bool operator >= (const self_type& lhs, const self_type& rhs) {
1970 return !lhs.less(rhs);
1973 friend bool operator > (const self_type& lhs, const self_type& rhs) {
1974 return rhs.less(lhs);
1977 friend bool operator <= (const self_type& lhs, const self_type& rhs) {
1978 return !rhs.less(lhs);
1983 typename M::value1_type v1_;
1984 typename M::value2_type v2_;
1985 typename M::value3_type v3_;
1990 template <class V1, class V2, class V3>
1991 class index_triple_array:
1992 private boost::noncopyable {
1994 typedef index_triple_array<V1, V2, V3> self_type;
1996 typedef typename V1::value_type value1_type;
1997 typedef typename V2::value_type value2_type;
1998 typedef typename V3::value_type value3_type;
2000 typedef typename V1::size_type size_type;
2001 typedef typename V1::difference_type difference_type;
2002 typedef index_triple<self_type> value_type;
2003 // There is nothing that can be referenced directly. Always return a copy of the index_triple
2004 typedef value_type reference;
2005 typedef const value_type const_reference;
2008 index_triple_array(size_type size, V1& data1, V2& data2, V3& data3) :
2009 size_(size),data1_(data1),data2_(data2),data3_(data3) {}
2012 size_type size() const {
2017 const_reference operator () (size_type i) const {
2018 return value_type((*this), i);
2021 reference operator () (size_type i) {
2022 return value_type((*this), i);
2025 typedef indexed_iterator<self_type, std::random_access_iterator_tag> iterator;
2026 typedef indexed_const_iterator<self_type, std::random_access_iterator_tag> const_iterator;
2030 return iterator( (*this), 0);
2034 return iterator( (*this), size());
2038 const_iterator begin() const {
2039 return const_iterator( (*this), 0);
2042 const_iterator cbegin () const {
2046 const_iterator end() const {
2047 return const_iterator( (*this), size());
2050 const_iterator cend () const {
2054 // unnecessary function:
2056 bool equal(size_type i1, size_type i2) const {
2057 return ((data1_[i1] == data1_[i2]) && (data2_[i1] == data2_[i2]));
2060 bool less(size_type i1, size_type i2) const {
2061 return ((data1_[i1] < data1_[i2]) ||
2062 (data1_[i1] == data1_[i2] && data2_[i1] < data2_[i2]));
2065 // gives a large speedup
2067 friend void iter_swap(const iterator& lhs, const iterator& rhs) {
2068 const size_type i1 = lhs.index();
2069 const size_type i2 = rhs.index();
2070 std::swap(lhs().data1_[i1], rhs().data1_[i2]);
2071 std::swap(lhs().data2_[i1], rhs().data2_[i2]);
2072 std::swap(lhs().data3_[i1], rhs().data3_[i2]);
2081 // friend class value_type;
2082 friend class index_triple<self_type>;