00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef BOOST_UBLAS_STORAGE_ADAPTORS_H
00018 #define BOOST_UBLAS_STORAGE_ADAPTORS_H
00019
00020 #include <algorithm>
00021
00022 #include <boost/numeric/ublas/exception.hpp>
00023 #include <boost/numeric/ublas/vector.hpp>
00024 #include <boost/numeric/ublas/matrix.hpp>
00025 #include <boost/numeric/ublas/detail/iterator.hpp>
00026
00027
00028 namespace boost
00029 {
00030 namespace numeric
00031 {
00032 namespace ublas
00033 {
00034
00035
00053 template<class T>
00054 class readonly_array_adaptor:
00055 public storage_array<readonly_array_adaptor<T> >
00056 {
00057
00058 typedef readonly_array_adaptor<T> self_type;
00059 public:
00060 typedef std::size_t size_type;
00061 typedef std::ptrdiff_t difference_type;
00062 typedef T value_type;
00063 typedef const T &const_reference;
00064 typedef const T *const_pointer;
00065 public:
00066
00067
00068 BOOST_UBLAS_INLINE
00069 readonly_array_adaptor ():
00070 size_ (0), data_ (0)
00071 {
00072 }
00073 BOOST_UBLAS_INLINE
00074 readonly_array_adaptor (size_type size, const_pointer data):
00075 size_ (size), data_ (data)
00076 {
00077 }
00078 BOOST_UBLAS_INLINE
00079 ~readonly_array_adaptor ()
00080 {
00081 }
00082
00083 readonly_array_adaptor (const readonly_array_adaptor& rhs)
00084 : size_(rhs.size_), data_(rhs.data_)
00085 { }
00086
00087
00088 BOOST_UBLAS_INLINE
00089 void resize (size_type size)
00090 {
00091 size_ = size;
00092 }
00093 BOOST_UBLAS_INLINE
00094 void resize (size_type size, const_pointer data)
00095 {
00096 size_ = size;
00097 data_ = data;
00098 }
00099
00100
00101 BOOST_UBLAS_INLINE
00102 size_type max_size () const
00103 {
00104 return std::numeric_limits<size_type>::max ();
00105 }
00106
00107 BOOST_UBLAS_INLINE
00108 bool empty () const
00109 {
00110 return size_ == 0;
00111 }
00112
00113 BOOST_UBLAS_INLINE
00114 size_type size () const
00115 {
00116 return size_;
00117 }
00118
00119
00120 BOOST_UBLAS_INLINE
00121 const_reference operator [] (size_type i) const
00122 {
00123 BOOST_UBLAS_CHECK (i < size_, bad_index ());
00124 return data_ [i];
00125 }
00126
00127
00128 typedef const_pointer const_iterator;
00129
00130 BOOST_UBLAS_INLINE
00131 const_iterator begin () const
00132 {
00133 return data_;
00134 }
00135 BOOST_UBLAS_INLINE
00136 const_iterator end () const
00137 {
00138 return data_ + size_;
00139 }
00140
00141
00142 typedef const_pointer iterator;
00143
00144
00145 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
00146 typedef std::reverse_iterator<iterator> reverse_iterator;
00147
00148 BOOST_UBLAS_INLINE
00149 const_reverse_iterator rbegin () const
00150 {
00151 return const_reverse_iterator (end ());
00152 }
00153 BOOST_UBLAS_INLINE
00154 const_reverse_iterator rend () const
00155 {
00156 return const_reverse_iterator (begin ());
00157 }
00158
00159 private:
00160 size_type size_;
00161 const_pointer data_;
00162 };
00163
00173 template <class T>
00174 vector<const T, readonly_array_adaptor<T> >
00175 make_vector_from_pointer(const size_t size, const T * data)
00176 {
00177 typedef readonly_array_adaptor<T> a_t;
00178 typedef vector<const T, a_t> v_t;
00179 return v_t(size, a_t(size, data));
00180 }
00181
00192 template <class LAYOUT, class T>
00193 matrix<const T, LAYOUT, readonly_array_adaptor<T> >
00194 make_matrix_from_pointer(const size_t size1, const size_t size2, const T * data)
00195 {
00196 typedef readonly_array_adaptor<T> a_t;
00197 typedef matrix<const T, LAYOUT, a_t> m_t;
00198 return m_t(size1, size2, a_t(size1*size2, data));
00199 }
00200
00201 template <class T>
00202 matrix<const T, row_major, readonly_array_adaptor<T> >
00203 make_matrix_from_pointer(const size_t size1, const size_t size2, const T * data)
00204 {
00205 return make_matrix_from_pointer<row_major>(size1, size2, data);
00206 }
00207
00218 template <class T, size_t M, size_t N>
00219 matrix<const T, row_major, readonly_array_adaptor<T> >
00220 make_matrix_from_pointer(const T (&array)[M][N])
00221 {
00222 typedef readonly_array_adaptor<T> a_t;
00223 typedef matrix<const T, row_major, a_t> m_t;
00224 return m_t(M, N, a_t(M*N, array[0]));
00225 }
00226 template <class T, size_t M, size_t N>
00227 matrix<const T, row_major, readonly_array_adaptor<T> >
00228 make_matrix_from_pointer(const T (*array)[M][N])
00229 {
00230 typedef readonly_array_adaptor<T> a_t;
00231 typedef matrix<const T, row_major, a_t> m_t;
00232 return m_t(M, N, a_t(M*N, (*array)[0]));
00233 }
00234
00235 }
00236 }
00237 }
00238
00239 #endif