ivl 679
|
00001 /* This file is part of the ivl C++ library <http://image.ntua.gr/ivl>. 00002 A C++ template library extending syntax towards mathematical notation. 00003 00004 Copyright (C) 2012 Yannis Avrithis <iavr@image.ntua.gr> 00005 Copyright (C) 2012 Kimon Kontosis <kimonas@image.ntua.gr> 00006 00007 ivl is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU Lesser General Public License 00009 version 3 as published by the Free Software Foundation. 00010 00011 Alternatively, you can redistribute it and/or modify it under the terms 00012 of the GNU General Public License version 2 as published by the Free 00013 Software Foundation. 00014 00015 ivl is distributed in the hope that it will be useful, 00016 but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00018 See the GNU General Public License for more details. 00019 00020 You should have received a copy of the GNU General Public License 00021 and a copy of the GNU Lesser General Public License along 00022 with ivl. If not, see <http://www.gnu.org/licenses/>. */ 00023 00024 00030 template <class T, 00031 class DERIVED_INFO, 00032 int N, 00033 class NAMING 00034 > 00035 class array<T, data::fixed<N, NAMING, DERIVED_INFO> > 00036 : 00037 public array_common_base< 00038 array<T, data::fixed<N, NAMING, DERIVED_INFO> > >, 00039 00040 public array_details::fixed_storage<T, N, NAMING> 00041 { 00042 00043 private: 00044 typedef array_common_base<array<T, 00045 data::fixed<N, NAMING, DERIVED_INFO> > > common_base_class; 00046 00047 void try_size(size_t count) { if(count != N) throw erange(); } 00048 00049 using array_details::fixed_storage<T, N, NAMING>::base_ptr; 00050 00051 public: 00052 typedef array this_type; 00053 00054 typedef this_type this_array_type; 00055 00056 typedef this_type array_type; 00057 00058 typedef T elem_type; 00059 00060 typedef T& reference; 00061 typedef const T& const_reference; 00062 typedef reference best_reference; 00063 00064 typedef typename common_base_class::base_class base_class; 00065 00067 typedef size_t size_type; 00068 00070 typedef ptrdiff_t diff_type; 00071 00072 using base_class::derived; 00073 00074 // iterators 00075 typedef data::ptr_iterator<T, false> iterator; 00076 typedef data::ptr_iterator<T, true> const_iterator; 00077 typedef std::reverse_iterator<iterator> reverse_iterator; 00078 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 00079 00080 typedef iterator best_iterator; 00081 00082 typedef ptrdiff_t iter_border_walker; 00083 00084 iterator begin() { return iterator(base_ptr); } 00085 iterator end() { return iterator(base_ptr + N); } 00086 const_iterator begin() const { return const_iterator(base_ptr); } 00087 const_iterator end() const { return const_iterator(base_ptr + N); } 00088 reverse_iterator rbegin() 00089 { return reverse_iterator(iterator(base_ptr + N)); } 00090 reverse_iterator rend() 00091 { return reverse_iterator(iterator(base_ptr)); } 00092 const_reverse_iterator rbegin() const 00093 { return const_reverse_iterator(const_iterator(base_ptr + N)); } 00094 const_reverse_iterator rend() const 00095 { return const_reverse_iterator(const_iterator(base_ptr)); } 00096 00097 T* c_ptr() { return base_ptr; } 00098 const T* c_ptr() const { return base_ptr; } 00099 00102 //TODO: explain this better. 00104 size_t length() const { return N; } 00106 size_type size() const { return length(); } 00108 size_t numel() const { return length(); } 00113 00114 void resize(size_t len) { CHECK(len == length(), ecomp); } 00116 void resize(size_t len, const T& s) { CHECK(len == length(), ecomp); } 00118 void reshape(size_t len) { resize(len); } 00119 void reshape(size_t len, const T& s) { resize(len, s); } 00122 iter_border_walker first_to_last() { return this->length() - 1; } 00123 iter_border_walker begin_to_end() { return this->length(); } 00124 00130 using common_base_class::operator[]; 00131 00133 const T& operator[](size_t offset) const { 00134 CHECK(offset >= 0 && offset < length(), erange); 00135 return base_ptr[offset]; 00136 } 00138 T& operator[](size_t offset) { 00139 CHECK(offset >= 0 && offset < length(), erange); 00140 return base_ptr[offset]; 00141 } 00147 00148 array() { } 00149 00151 explicit array(size_t count) { try_size(count); } 00152 00154 explicit array(int count) { try_size(count); } 00155 00157 explicit array(long int count) { try_size(count); } 00158 00165 array(size_t count, const T& s) 00166 { try_size(count); ivl::copy(*this, s, types::term()); } 00167 00174 array(size_t count, const T *ptr) { try_size(count); ivl::copy(*this, ptr); } 00175 00177 template <class J> 00178 array(const internal::tuple_rvalue<J>& r) 00179 { r.tuple_output(internal::reftpl(*this)); } 00180 00182 array(const this_type& a) { ivl::copy(*this, a); } 00183 00192 template <class J, class S> 00193 array(const array<J, S>& a, size_t n) 00194 { 00195 try_size(n); 00196 ivl::copy_out(*this, a); 00197 } 00198 00201 template <class J, class S> 00202 array(const array<J, S>& a) 00203 { 00204 try_size(a.length()); 00205 ivl::copy(*this, a); 00206 } 00207 00217 template <class J, class S> 00218 array(size_t count, const array<J, S>& a) 00219 { 00220 try_size(count); 00221 init_size_with(a); 00222 // 00223 } 00224 00225 00226 #ifdef IVL_MATLAB 00227 00233 explicit array(const mxArray* mx); 00234 #endif 00235 00238 00239 ~array() { } 00240 00241 00254 using base_class::operator=; 00255 00256 this_type& operator=(const this_type& a) 00257 { 00258 common_base_class::operator=(a); 00259 return *this; 00260 } 00261 /* 00262 this_type& operator=(const this_type& a) // LEFT IN THE CONST MODE FOR A COPY CONSTRUCTOR! 00263 { 00264 // if(this != &a) TODO: make this a rule: make clear that ... 00265 // Note: the data class handles the check: if(this != &a), 00266 // and only IF needed, so there is no case of cpu waste 00267 if(this == &a) return *this; 00268 ivl::copy(*this, a); 00269 return *this; 00270 } 00271 00272 template<class S, class K> 00273 derived_type& operator=(const ivl::array<T, S, K>& a) 00274 { 00275 try_size(a.length()); 00276 ivl::copy(*this, a); 00277 return derived(); 00278 } 00279 */ 00280 /* operator =(const T& s) unsupported */ 00281 00282 }; 00283