$search
00001 // HOG-Man - Hierarchical Optimization for Pose Graphs on Manifolds 00002 // Copyright (C) 2010 G. Grisetti, R. Kümmerle, C. Stachniss 00003 // 00004 // HOG-Man is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU Lesser General Public License as published 00006 // by the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // HOG-Man is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU Lesser General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU Lesser General Public License 00015 // along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 00017 #ifndef _ARRAY_ALLOCATOR_HH_ 00018 #define _ARRAY_ALLOCATOR_HH_ 00019 #include <assert.h> 00020 #include <string.h> 00021 #include "macros.h" 00022 00025 template <int N, typename Base> 00026 struct _ArrayAllocator{ 00027 typedef Base BaseType; 00028 static const int TemplateSize=N; 00030 _ArrayAllocator(int n=N) { 00031 /* if (n!=N) */ 00032 /* std::cerr << "Fatal n= " << n << " N=" << N << std::endl; */ 00033 assert (n==N); (void) n; 00034 } 00036 int size() const {return N;} 00037 00039 Base* ptr() {return _svalues;} 00040 00042 const Base* ptr() const {return _svalues;} 00043 00045 inline Base& operator [] (int i) { return _svalues[i]; } 00046 00048 inline const Base& operator [] (int i) const { return _svalues[i]; } 00049 protected: 00050 Base _svalues[N]; 00051 }; 00052 00053 00058 template <> 00059 template <typename Base> 00060 struct _ArrayAllocator <0, Base>{ 00061 typedef Base BaseType; 00062 static const int TemplateSize=0; 00063 00065 _ArrayAllocator(int n=0) { 00066 allocate(n); 00067 } 00068 00070 ~_ArrayAllocator(){ 00071 deallocate(); 00072 } 00073 00075 _ArrayAllocator<0,Base>& operator = (const _ArrayAllocator <0, Base>& src){ 00076 resize(src.size()); 00077 copy(src._dvalues); 00078 return *this; 00079 } 00080 00082 _ArrayAllocator(const _ArrayAllocator <0, Base>& src) { 00083 allocate(src.size()); 00084 copy(src._dvalues); 00085 } 00086 00088 int size() const { return _size;} 00089 00091 Base* ptr() {return _dvalues;} 00092 00094 const Base* ptr() const {return _dvalues;} 00095 00097 inline Base& operator [] (int i) { return _dvalues[i]; } 00098 00100 inline const Base& operator [] (int i) const { return _dvalues[i]; } 00101 00103 void resize(int s) { 00104 if (s==_size) 00105 return; 00106 if (! s) 00107 deallocate(); 00108 allocate(s); 00109 } 00110 00111 protected: 00112 void copy(const Base* src){ 00113 if ( AISNAV_IS_POD(Base) ) 00114 memcpy(_dvalues, src, _size*sizeof(Base)); 00115 else 00116 for (int i=0; i<_size; i++) 00117 _dvalues[i]=src[i]; 00118 } 00119 00120 void deallocate() { 00121 if (_size) 00122 delete [] _dvalues; 00123 _dvalues=0; 00124 _size=0; 00125 } 00126 00127 void allocate(int n) { 00128 _dvalues = n ? new Base[n] : 0; 00129 _size=n; 00130 } 00131 int _size; 00132 Base* _dvalues; 00133 }; 00134 00135 00137 template <int N, typename Base> 00138 void st2dyn (_ArrayAllocator<0,Base>& dest, const _ArrayAllocator<N,Base>& src) { 00139 dest.resize(src.size()); 00140 for (int i=0; i<src.size(); i++) 00141 dest[i]=src[i]; 00142 } 00143 00145 template <int N, typename Base> 00146 void dyn2st(_ArrayAllocator<N,Base>& dest, const _ArrayAllocator<0,Base>& src){ 00147 assert (dest.size()==src.size()); 00148 for (int i=0; i<src.size(); i++) 00149 dest[i]=src[i]; 00150 } 00151 00154 template <int N, int M, typename Base> 00155 struct _Array2DAllocator{ 00156 typedef Base BaseType; 00157 static const int TemplateRows = N; 00158 static const int TemplateCols = M; 00159 typedef _Array2DAllocator<M,N,Base> TransposedType; 00160 00162 _Array2DAllocator(int rows_=N, int cols_=M) { 00163 assert (rows_==N); (void) rows_; 00164 assert (cols_==M); (void) cols_; 00165 } 00166 00168 inline Base* operator[] (int i) {return _svalues[i];} 00169 00171 inline const Base* operator [] (int i) const {return _svalues[i];} 00172 00174 int rows() const {return TemplateRows;} 00175 00177 int cols() const {return TemplateCols;} 00178 protected: 00179 Base _svalues[N][M]; 00180 }; 00181 00184 template <> 00185 template <typename Base> 00186 struct _Array2DAllocator<0,0,Base>{ 00187 typedef Base BaseType; 00188 static const int TemplateRows = 0; 00189 static const int TemplateCols = 0; 00190 typedef _Array2DAllocator<0,0,Base> TransposedType; 00191 00193 inline Base* operator[] (int i) {return _dvalues[i];} 00194 00196 inline const Base* operator [] (int i) const {return _dvalues[i];} 00197 00199 ~_Array2DAllocator(){ 00200 deallocate(); 00201 } 00202 00204 _Array2DAllocator(int rows_=0, int cols_=0) { 00205 allocate (rows_, cols_); 00206 } 00207 00209 _Array2DAllocator(const _Array2DAllocator<0,0,Base>& src){ 00210 allocate (src.rows(), src.cols()); 00211 copy(src._dvalues); 00212 } 00213 00215 _Array2DAllocator<0,0,Base>& operator = (const _Array2DAllocator<0,0,Base>& src){ 00216 resize( src._rows, src._cols); 00217 copy(src._dvalues); 00218 return *this; 00219 } 00220 00222 int rows() const {return _rows;} 00223 00225 int cols() const {return _cols;} 00226 00228 void resize (int n, int m) { 00229 if (n==cols() && m==rows()) 00230 return; 00231 deallocate(); 00232 allocate(n,m); 00233 } 00234 00235 protected: 00236 Base ** _dvalues; 00237 void deallocate() { 00238 if (rows()&&cols()){ 00239 for (int r=0; r<rows(); r++) 00240 delete [] _dvalues[r]; 00241 delete [] _dvalues; 00242 } 00243 _rows=0; 00244 _cols=0; 00245 _dvalues=0; 00246 } 00247 00248 void allocate(int n, int m){ 00249 if (m==0 || n==0){ 00250 _rows=0; 00251 _cols=0; 00252 _dvalues=0; 00253 return; 00254 } 00255 _rows=n; 00256 _cols=m; 00257 _dvalues = new Base*[_rows]; 00258 for (int r=0; r<_rows; r++) 00259 _dvalues[r]=new Base[_cols]; 00260 } 00261 00262 void copy(Base** const src){ 00263 for (int r=0; r<rows(); r++) { 00264 if ( AISNAV_IS_POD(Base) ) 00265 memcpy(_dvalues[r], src[r], _cols*sizeof(Base)); 00266 else 00267 for (int c=0; c<_cols; c++) 00268 _dvalues[r][c]=src[r][c]; 00269 } 00270 } 00271 int _cols; 00272 int _rows; 00273 }; 00274 00276 template <int M, int N, typename Base> 00277 void st2dyn (_Array2DAllocator<0,0,Base>& dest, const _Array2DAllocator<M,N,Base>& src) { 00278 dest.resize(src.rows(), src.cols()); 00279 for (int r=0; r<src.rows(); r++) 00280 for (int c=0; c<src.cols(); c++) 00281 dest[r][c]=src[r][c]; 00282 } 00283 00285 template <int M, int N, typename Base> 00286 void dyn2st(_Array2DAllocator<M,N,Base>& dest, const _Array2DAllocator<0,0,Base>& src){ 00287 assert (dest.rows()==src.rows()); 00288 assert (dest.cols()==src.cols()); 00289 for (int r=0; r<src.rows(); r++) 00290 for (int c=0; c<src.cols(); c++) 00291 dest[r][c]=src[r][c]; 00292 } 00293 00294 00295 #endif 00296