Created by the British Broadcasting Corporation.
00001 /* ***** BEGIN LICENSE BLOCK ***** 00002 * 00003 * $Id: arrays.h,v 1.17 2007/03/19 16:18:59 asuraparaju Exp $ $Name: $ 00004 * 00005 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 00006 * 00007 * The contents of this file are subject to the Mozilla Public License 00008 * Version 1.1 (the "License"); you may not use this file except in compliance 00009 * with the License. You may obtain a copy of the License at 00010 * http://www.mozilla.org/MPL/ 00011 * 00012 * Software distributed under the License is distributed on an "AS IS" basis, 00013 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for 00014 * the specific language governing rights and limitations under the License. 00015 * 00016 * The Original Code is BBC Research and Development code. 00017 * 00018 * The Initial Developer of the Original Code is the British Broadcasting 00019 * Corporation. 00020 * Portions created by the Initial Developer are Copyright (C) 2004. 00021 * All Rights Reserved. 00022 * 00023 * Contributor(s): Thomas Davies (Original Author), 00024 * Peter Meerwald (pmeerw@users.sourceforge.net) 00025 * Mike Ferenduros (mike_ferenzduros@users.sourceforge.net) 00026 * Anuradha Suraparaju 00027 * 00028 * Alternatively, the contents of this file may be used under the terms of 00029 * the GNU General Public License Version 2 (the "GPL"), or the GNU Lesser 00030 * Public License Version 2.1 (the "LGPL"), in which case the provisions of 00031 * the GPL or the LGPL are applicable instead of those above. If you wish to 00032 * allow use of your version of this file only under the terms of the either 00033 * the GPL or LGPL and not to allow others to use your version of this file 00034 * under the MPL, indicate your decision by deleting the provisions above 00035 * and replace them with the notice and other provisions required by the GPL 00036 * or LGPL. If you do not delete the provisions above, a recipient may use 00037 * your version of this file under the terms of any one of the MPL, the GPL 00038 * or the LGPL. 00039 * ***** END LICENSE BLOCK ***** */ 00040 00041 #ifndef _ARRAYS_H_ 00042 #define _ARRAYS_H_ 00043 00044 //basic array types used for pictures etc 00045 00046 #include <memory> 00047 #include <cstddef> 00048 #include <stdexcept> 00049 #include <iostream> 00050 #include <algorithm> 00051 00052 namespace dirac 00053 { 00054 typedef short ValueType; 00055 00056 typedef int CalcValueType; 00057 00059 00063 class Range 00064 { 00065 public: 00067 00070 Range(int s, int e): m_fst(s), m_lst(e){} 00071 00073 const int First() const {return m_fst;} 00074 00076 const int Last() const {return m_lst;} 00077 00078 private: 00079 int m_fst ,m_lst; 00080 }; 00081 00083 //One-Dimensional Array type// 00085 00087 00092 template <class T> class OneDArray 00093 { 00094 public: 00096 00099 OneDArray(); 00100 00102 00105 OneDArray(const int len); 00106 00108 00113 OneDArray(const Range& r); 00114 00116 00119 ~OneDArray() 00120 { 00121 FreePtr(); 00122 } 00123 00125 00128 OneDArray(const OneDArray<T>& cpy); 00129 00131 00134 OneDArray<T>& operator=(const OneDArray<T>& rhs); 00135 00137 void Resize(int l); 00138 00140 T& operator[](const int pos){return m_ptr[pos-m_first];} 00141 00143 const T& operator[](const int pos) const {return m_ptr[pos-m_first];} 00144 00146 int Length() const {return m_length;} 00147 00149 int First() const {return m_first;} 00150 00152 int Last() const {return m_last;} 00153 00154 private: 00155 void Init(const int len); 00156 00157 void Init(const Range& r); 00158 00159 void FreePtr(); 00160 00161 int m_first, m_last; 00162 int m_length; 00163 T* m_ptr; 00164 }; 00165 00166 //public member functions// 00168 00169 template <class T> 00170 OneDArray<T>::OneDArray() 00171 { 00172 Init(0); 00173 } 00174 00175 template <class T> 00176 OneDArray<T>::OneDArray(const int len) 00177 { 00178 Init(len); 00179 } 00180 00181 template <class T> 00182 OneDArray<T>::OneDArray(const Range& r) 00183 { 00184 Init(r); 00185 } 00186 00187 template <class T> 00188 OneDArray<T>::OneDArray(const OneDArray<T>& cpy) 00189 { 00190 m_first = cpy.m_first; 00191 m_last = cpy.m_last; 00192 m_length = m_last - m_first + 1; 00193 00194 if (m_first==0) 00195 Init(m_length); 00196 else 00197 Init(Range(m_first , m_last)); 00198 00199 memcpy( m_ptr , cpy.m_ptr , m_length * sizeof( T ) ); 00200 } 00201 00202 template <class T> 00203 OneDArray<T>& OneDArray<T>::operator=(const OneDArray<T>& rhs) 00204 { 00205 if (&rhs != this) 00206 { 00207 FreePtr(); 00208 m_first = rhs.m_first; 00209 m_last = rhs.m_last; 00210 m_length = rhs.m_length; 00211 00212 if (m_first == 0) 00213 Init(m_length); 00214 else 00215 Init(Range(m_first , m_last)); 00216 00217 memcpy( m_ptr , rhs.m_ptr , m_length * sizeof( T ) ); 00218 00219 } 00220 return *this; 00221 } 00222 00223 template <class T> 00224 void OneDArray<T>::Resize(int l) 00225 { 00226 if (l != m_length) 00227 { 00228 FreePtr(); 00229 Init(l); 00230 } 00231 } 00232 00233 //private member functions// 00235 00236 template <class T> 00237 void OneDArray<T>::Init(const int len) 00238 { 00239 Range r(0 , len-1); 00240 00241 Init(r); 00242 00243 } 00244 00245 template <class T> 00246 void OneDArray<T>::Init(const Range& r) 00247 { 00248 00249 m_first = r.First(); 00250 m_last = r.Last(); 00251 m_length = m_last - m_first + 1; 00252 00253 if ( m_length>0 ) 00254 { 00255 m_ptr = new T[ m_length ]; 00256 } 00257 else 00258 { 00259 m_length = 0; 00260 m_first = 0; 00261 m_last = -1; 00262 m_ptr = NULL; 00263 } 00264 } 00265 00266 template <class T> 00267 void OneDArray<T>::FreePtr() 00268 { 00269 if ( m_length>0 ) 00270 delete[] m_ptr; 00271 } 00272 00273 00275 //Two-Dimensional Array type// 00277 00279 00287 template <class T> class TwoDArray 00288 { 00289 typedef T* element_type; 00290 00291 public: 00292 00294 00297 TwoDArray(){ Init(0,0); } 00298 00300 00303 TwoDArray( const int height , const int width ){Init(height , width);} 00304 00306 00310 TwoDArray( const int height , const int width , T val); 00311 00313 00316 virtual ~TwoDArray(){ 00317 FreeData(); 00318 } 00319 00321 00324 TwoDArray(const TwoDArray<T>& Cpy); 00325 00327 00330 TwoDArray<T>& operator=(const TwoDArray<T>& rhs); 00331 00333 void Resize(const int height, const int width); 00334 00336 00340 inline element_type& operator[](const int pos){return m_array_of_rows[pos];} 00341 00343 00347 inline const element_type& operator[](const int pos) const {return m_array_of_rows[pos];} 00348 00350 const int LengthX() const { return m_length_x; } 00351 00353 const int LengthY() const { return m_length_y; } 00354 00356 const int FirstX() const { return m_first_x; } 00357 00359 const int FirstY() const { return m_first_y; } 00360 00362 const int LastX() const { return m_last_x; } 00363 00365 const int LastY() const { return m_last_y; } 00366 00367 private: 00369 void Init(const int height,const int width); 00370 00372 void FreeData(); 00373 00374 int m_first_x; 00375 int m_first_y; 00376 00377 int m_last_x; 00378 int m_last_y; 00379 00380 int m_length_x; 00381 int m_length_y; 00382 00383 element_type* m_array_of_rows; 00384 }; 00385 00386 //public member functions// 00388 00389 template <class T> 00390 TwoDArray<T>::TwoDArray( const int height , const int width , const T val) 00391 { 00392 Init( height , width ); 00393 std::fill_n( m_array_of_rows[0], m_length_x*m_length_y, val); 00394 } 00395 00396 template <class T> 00397 TwoDArray<T>::TwoDArray(const TwoDArray<T>& Cpy) 00398 { 00399 m_first_x = Cpy.m_first_x; 00400 m_first_y = Cpy.m_first_y; 00401 m_last_x = Cpy.m_last_x; 00402 m_last_y = Cpy.m_last_y; 00403 00404 m_length_x = m_last_x - m_first_x + 1; 00405 m_length_y = m_last_y - m_first_y + 1; 00406 00407 if (m_first_x == 0 && m_first_y == 0) 00408 Init(m_length_y , m_length_x); 00409 else{ 00410 //based 2D arrays not yet supported 00411 } 00412 00413 memcpy( m_array_of_rows[0] , (Cpy.m_array_of_rows)[0] , m_length_x * m_length_y * sizeof( T ) ); 00414 00415 } 00416 00417 template <class T> 00418 TwoDArray<T>& TwoDArray<T>::operator=(const TwoDArray<T>& rhs) 00419 { 00420 if (&rhs != this) 00421 { 00422 FreeData(); 00423 00424 m_first_x = rhs.m_first_x; 00425 m_first_y = rhs.m_first_y; 00426 00427 m_last_x = rhs.m_last_x; 00428 m_last_y = rhs.m_last_y; 00429 00430 m_length_x = m_last_x - m_first_x + 1; 00431 m_length_y = m_last_y - m_first_y + 1; 00432 00433 if (m_first_x == 0 && m_first_y == 0) 00434 Init(m_length_y , m_length_x); 00435 else 00436 { 00437 //based 2D arrays not yet supported 00438 } 00439 00440 memcpy( m_array_of_rows[0], (rhs.m_array_of_rows)[0], m_length_x * m_length_y * sizeof( T ) ); 00441 00442 } 00443 00444 return *this; 00445 00446 } 00447 00448 template <class T> 00449 void TwoDArray<T>::Resize(const int height, const int width) 00450 { 00451 if (height != m_length_y || width != m_length_x) 00452 { 00453 FreeData(); 00454 Init(height , width); 00455 } 00456 } 00457 00458 //private member functions// 00460 00461 template <class T> 00462 void TwoDArray<T>::Init(const int height , const int width) 00463 { 00464 m_length_x = width; 00465 m_length_y = height; 00466 m_first_x = 0; 00467 m_first_y = 0; 00468 00469 m_last_x = m_length_x-1; 00470 m_last_y = m_length_y-1; 00471 00472 if (m_length_y>0) 00473 { 00474 // allocate the array containing ptrs to all the rows 00475 m_array_of_rows = new element_type[ m_length_y ]; 00476 00477 if ( m_length_x>0 ) 00478 { 00479 // Allocate the whole thing as a single big block 00480 m_array_of_rows[0] = new T[ m_length_x * m_length_y ]; 00481 00482 // Point the pointers 00483 for (int j=1 ; j<m_length_y ; ++j) 00484 m_array_of_rows[j] = m_array_of_rows[0] + j * m_length_x; 00485 } 00486 else 00487 { 00488 m_length_x = 0; 00489 m_first_x = 0; 00490 m_last_x = -1; 00491 } 00492 } 00493 else 00494 { 00495 m_length_x = 0; 00496 m_length_y = 0; 00497 m_first_x = 0; 00498 m_first_y = 0; 00499 m_last_x = -1; 00500 m_last_y = -1; 00501 m_array_of_rows = NULL; 00502 } 00503 } 00504 00505 template <class T> 00506 void TwoDArray<T>::FreeData() 00507 { 00508 if (m_length_y>0) 00509 { 00510 if (m_length_x>0) 00511 { 00512 delete[] m_array_of_rows[0]; 00513 } 00514 00515 m_length_y = m_length_x = 0; 00516 // deallocate the array of rows 00517 delete[] m_array_of_rows; 00518 } 00519 } 00520 00521 // Related functions 00522 00524 template <class T > 00525 std::ostream & operator<< (std::ostream & stream, TwoDArray<T> & array) 00526 { 00527 for (int j=0 ; j<array.LengthY() ; ++j) 00528 { 00529 for (int i=0 ; i<array.LengthX() ; ++i) 00530 { 00531 stream << array[j][i] << " "; 00532 }// i 00533 stream << std::endl; 00534 }// j 00535 00536 return stream; 00537 } 00538 00540 template <class T > 00541 std::istream & operator>> (std::istream & stream, TwoDArray<T> & array) 00542 { 00543 for (int j=0 ; j<array.LengthY() ; ++j) 00544 { 00545 for (int i=0 ; i<array.LengthX() ; ++i) 00546 { 00547 stream >> array[j][i]; 00548 }// i 00549 }// j 00550 00551 return stream; 00552 } 00553 00554 } //namespace dirac 00555 #endif
© 2004 British Broadcasting Corporation.
Dirac code licensed under the Mozilla Public License (MPL) Version 1.1.
HTML documentation generated by Dimitri van Heesch's
excellent Doxygen tool.