fixed_size_array.hpp

00001 #ifndef CORE_UBLAS_FIXEDSIZEARRAY_H
00002 #define CORE_UBLAS_FIXEDSIZEARRAY_H
00003 
00004 #include <boost/numeric/ublas/storage.hpp>
00005 
00006 namespace boost { namespace numeric { namespace ublas {
00007 
00008     // Fixed size array 
00009     template<class T, std::size_t N> 
00010     class fixed_size_array: 
00011         public storage_array<fixed_size_array<T, N> > { 
00012 
00013         typedef fixed_size_array<T, N> self_type; 
00014     public: 
00015         typedef std::size_t size_type; 
00016         typedef std::ptrdiff_t difference_type; 
00017         typedef T value_type; 
00018         typedef const T &const_reference; 
00019         typedef T &reference; 
00020         typedef const T *const_pointer; 
00021         typedef T *pointer; 
00022         typedef const_pointer const_iterator; 
00023         typedef pointer iterator; 
00024 
00025         // Construction and destruction 
00026         BOOST_UBLAS_INLINE 
00027         fixed_size_array () { 
00028         } 
00029         explicit BOOST_UBLAS_INLINE 
00030         fixed_size_array (size_type size) { 
00031             BOOST_UBLAS_CHECK (size == N, bad_size ()); 
00032         }
00033         BOOST_UBLAS_INLINE 
00034         fixed_size_array (size_type size, const value_type &init) { 
00035             BOOST_UBLAS_CHECK (size == N, bad_size ()); 
00036             std::fill (data_, data_ + N, init) ; 
00037         } 
00038         BOOST_UBLAS_INLINE 
00039         fixed_size_array (const fixed_size_array &c) : storage_array<fixed_size_array<T, N> >() { 
00040             std::copy (c.data_, c.data_ + N, data_); 
00041         } 
00042 
00043         // Resizing 
00044         BOOST_UBLAS_INLINE 
00045         void resize (size_type size) { 
00046             BOOST_UBLAS_CHECK (size == N, bad_size ()); 
00047         } 
00048         BOOST_UBLAS_INLINE 
00049         void resize (size_type size, value_type init) { 
00050             BOOST_UBLAS_CHECK (size == N, bad_size ()); 
00051             std::fill (data_, data_ + N, init); 
00052         }
00053 
00054         // Random Access Container 
00055         BOOST_UBLAS_INLINE 
00056         size_type max_size () const { 
00057             return N; 
00058         } 
00059 
00060         BOOST_UBLAS_INLINE 
00061         bool empty () const { 
00062             return N == 0; 
00063         } 
00064 
00065         BOOST_UBLAS_INLINE 
00066         size_type size () const { 
00067             return N; 
00068         } 
00069 
00070         // Element access 
00071         BOOST_UBLAS_INLINE 
00072         const_reference operator [] (size_type i) const { 
00073             BOOST_UBLAS_CHECK (i < N, bad_index ()); 
00074             return data_ [i]; 
00075         } 
00076         BOOST_UBLAS_INLINE 
00077         reference operator [] (size_type i) { 
00078             BOOST_UBLAS_CHECK (i < N, bad_index ()); 
00079             return data_ [i]; 
00080         } 
00081 
00082         // Assignment 
00083         BOOST_UBLAS_INLINE 
00084         fixed_size_array &operator = (const fixed_size_array &a) { 
00085             if (this != &a) { 
00086                 std::copy (a.data_, a.data_ + N, data_); 
00087             } 
00088             return *this; 
00089         } 
00090         BOOST_UBLAS_INLINE 
00091         fixed_size_array &assign_temporary (fixed_size_array &a) { 
00092             *this = a; 
00093             return *this; 
00094         } 
00095 
00096         // Swapping 
00097         BOOST_UBLAS_INLINE 
00098         void swap (fixed_size_array &a) { 
00099             if (this != &a) { 
00100                 std::swap_ranges (data_, data_ + N, a.data_); 
00101             } 
00102         } 
00103         BOOST_UBLAS_INLINE 
00104         friend void swap (fixed_size_array &a1, fixed_size_array &a2) { 
00105             a1.swap (a2); 
00106         } 
00107 
00108         BOOST_UBLAS_INLINE 
00109         const_iterator begin () const { 
00110             return data_; 
00111         } 
00112         BOOST_UBLAS_INLINE 
00113         const_iterator end () const { 
00114             return data_ + N; 
00115         } 
00116 
00117         BOOST_UBLAS_INLINE 
00118         iterator begin () { 
00119             return data_; 
00120         } 
00121         BOOST_UBLAS_INLINE 
00122         iterator end () { 
00123             return data_ + N; 
00124         } 
00125 
00126         // Reverse iterators 
00127         typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 
00128         typedef std::reverse_iterator<iterator> reverse_iterator; 
00129 
00130         BOOST_UBLAS_INLINE 
00131         const_reverse_iterator rbegin () const { 
00132             return const_reverse_iterator (end ()); 
00133         } 
00134         BOOST_UBLAS_INLINE 
00135         const_reverse_iterator rend () const { 
00136             return const_reverse_iterator (begin ()); 
00137         } 
00138         BOOST_UBLAS_INLINE 
00139         reverse_iterator rbegin () { 
00140             return reverse_iterator (end ()); 
00141         } 
00142         BOOST_UBLAS_INLINE 
00143         reverse_iterator rend () { 
00144             return reverse_iterator (begin ()); 
00145         } 
00146 
00147     private: 
00148         value_type data_ [N]; 
00149     };
00150 
00151     // Fixed size array of lenth zero
00152     template<class T> 
00153     class fixed_size_array<T, 0> : 
00154         public storage_array<fixed_size_array<T, 0> > { 
00155 
00156         typedef fixed_size_array<T, 0> self_type; 
00157     public: 
00158         typedef std::size_t size_type; 
00159         typedef std::ptrdiff_t difference_type; 
00160         typedef T value_type; 
00161         typedef const T &const_reference; 
00162         typedef T &reference; 
00163         typedef const T *const_pointer; 
00164         typedef T *pointer; 
00165         typedef const_pointer const_iterator; 
00166         typedef pointer iterator; 
00167 
00168         // Construction and destruction 
00169         BOOST_UBLAS_INLINE 
00170         fixed_size_array () { 
00171         } 
00172         explicit BOOST_UBLAS_INLINE 
00173         fixed_size_array (size_type size) { 
00174             BOOST_UBLAS_CHECK (size == 0, bad_size ()); 
00175         } 
00176         BOOST_UBLAS_INLINE 
00177         fixed_size_array (size_type size, const value_type &init) { 
00178             BOOST_UBLAS_CHECK (size == 0, bad_size ()); 
00179         } 
00180         BOOST_UBLAS_INLINE 
00181         fixed_size_array (const fixed_size_array &c) { 
00182         } 
00183 
00184         // Resizing 
00185         BOOST_UBLAS_INLINE 
00186         void resize (size_type size) { 
00187             BOOST_UBLAS_CHECK (size == 0, bad_size ()); 
00188         } 
00189         BOOST_UBLAS_INLINE 
00190         void resize (size_type size, value_type init) { 
00191             BOOST_UBLAS_CHECK (size == 0, bad_size ()); 
00192         } 
00193 
00194         // Random Access Container 
00195         BOOST_UBLAS_INLINE 
00196         size_type max_size () const { 
00197             return 0; 
00198         } 
00199 
00200         BOOST_UBLAS_INLINE 
00201         bool empty () const { 
00202             return 0 == 0; 
00203         } 
00204 
00205         BOOST_UBLAS_INLINE 
00206         size_type size () const { 
00207             return 0; 
00208         } 
00209 
00210         // Assignment 
00211         BOOST_UBLAS_INLINE 
00212         fixed_size_array &operator = (const fixed_size_array &a) { 
00213             return *this; 
00214         } 
00215         BOOST_UBLAS_INLINE 
00216         fixed_size_array &assign_temporary (fixed_size_array &a) { 
00217             return *this; 
00218         } 
00219 
00220         // Swapping 
00221         BOOST_UBLAS_INLINE 
00222         void swap (fixed_size_array &a) { 
00223         } 
00224         BOOST_UBLAS_INLINE 
00225         friend void swap (fixed_size_array &a1, fixed_size_array &a2) { 
00226         } 
00227 
00228         BOOST_UBLAS_INLINE 
00229         const_iterator begin () const { 
00230             return 0; 
00231         } 
00232         BOOST_UBLAS_INLINE 
00233         const_iterator end () const { 
00234             return 0; 
00235         } 
00236 
00237         BOOST_UBLAS_INLINE 
00238         iterator begin () { 
00239             return 0; 
00240         } 
00241         BOOST_UBLAS_INLINE 
00242         iterator end () { 
00243             return 0; 
00244         } 
00245 
00246         // Reverse iterators 
00247         typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 
00248         typedef std::reverse_iterator<iterator> reverse_iterator; 
00249 
00250         BOOST_UBLAS_INLINE 
00251         const_reverse_iterator rbegin () const { 
00252             return const_reverse_iterator (end ()); 
00253         } 
00254         BOOST_UBLAS_INLINE 
00255         const_reverse_iterator rend () const { 
00256             return const_reverse_iterator (begin ()); 
00257         } 
00258         BOOST_UBLAS_INLINE 
00259         reverse_iterator rbegin () { 
00260             return reverse_iterator (end ()); 
00261         } 
00262         BOOST_UBLAS_INLINE 
00263         reverse_iterator rend () { 
00264             return reverse_iterator (begin ()); 
00265         } 
00266     }; 
00267 }}}
00268 
00269 #endif

Generated on Tue Feb 10 10:01:29 2009 for imaging2 by  doxygen 1.5.5