Created by the British Broadcasting Corporation.
00001 /* ***** BEGIN LICENSE BLOCK ***** 00002 * 00003 * $Id: arrays.h,v 1.8 2004/08/11 14:40:01 asuraparaju Exp $ $Name: Dirac_0_4_3 $ 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 * 00025 * Alternatively, the contents of this file may be used under the terms of 00026 * the GNU General Public License Version 2 (the "GPL"), or the GNU Lesser 00027 * Public License Version 2.1 (the "LGPL"), in which case the provisions of 00028 * the GPL or the LGPL are applicable instead of those above. If you wish to 00029 * allow use of your version of this file only under the terms of the either 00030 * the GPL or LGPL and not to allow others to use your version of this file 00031 * under the MPL, indicate your decision by deleting the provisions above 00032 * and replace them with the notice and other provisions required by the GPL 00033 * or LGPL. If you do not delete the provisions above, a recipient may use 00034 * your version of this file under the terms of any one of the MPL, the GPL 00035 * or the LGPL. 00036 * ***** END LICENSE BLOCK ***** */ 00037 00038 #ifndef _ARRAYS_H_ 00039 #define _ARRAYS_H_ 00040 00041 //basic array types used for pictures etc 00042 00043 #include <memory> 00044 #include <cstddef> 00045 #include <stdexcept> 00046 #include <iostream> 00047 00048 typedef short ValueType; 00049 typedef int CalcValueType; 00050 00052 00055 class Range 00056 { 00057 public: 00059 00062 Range(int s, int e): m_fst(s), m_lst(e){} 00063 00065 const int First() const {return m_fst;} 00066 00068 const int Last() const {return m_lst;} 00069 00070 private: 00071 int m_fst ,m_lst; 00072 }; 00073 00075 //One-Dimensional Array type// 00077 00079 00084 template <class T> class OneDArray 00085 { 00086 public: 00088 00091 OneDArray(); 00092 00094 00097 OneDArray(const int len); 00098 00100 00104 OneDArray(const Range& r); 00105 00107 00110 ~OneDArray() 00111 { 00112 FreePtr(); 00113 } 00114 00116 00119 OneDArray(const OneDArray<T>& cpy); 00120 00122 00125 OneDArray<T>& operator=(const OneDArray<T>& rhs); 00126 00128 void Resize(int l); 00129 00131 T& operator[](const int pos){return m_ptr[pos-m_first];} 00132 00134 const T& operator[](const int pos) const {return m_ptr[pos-m_first];} 00135 00137 int Length() const {return m_length;} 00138 00140 int First() const {return m_first;} 00141 00143 int Last() const {return m_last;} 00144 00145 private: 00146 void Init(const int len); 00147 00148 void Init(const Range& r); 00149 00150 void FreePtr(); 00151 00152 int m_first, m_last; 00153 int m_length; 00154 T* m_ptr; 00155 }; 00156 00157 //public member functions// 00159 00160 template <class T> 00161 OneDArray<T>::OneDArray() 00162 { 00163 Init(0); 00164 } 00165 00166 template <class T> 00167 OneDArray<T>::OneDArray(const int len) 00168 { 00169 Init(len); 00170 } 00171 00172 template <class T> 00173 OneDArray<T>::OneDArray(const Range& r) 00174 { 00175 Init(r); 00176 } 00177 00178 template <class T> 00179 OneDArray<T>::OneDArray(const OneDArray<T>& cpy) 00180 { 00181 m_first = cpy.m_first; 00182 m_last = cpy.m_last; 00183 m_length = m_last - m_first + 1; 00184 00185 if (m_first==0) 00186 Init(m_length); 00187 else 00188 Init(Range(m_first , m_last)); 00189 00190 for (int i=0 ; i<m_length ; ++i) 00191 *(m_ptr+i) = *(cpy.m_ptr+i); 00192 } 00193 00194 template <class T> 00195 OneDArray<T>& OneDArray<T>::operator=(const OneDArray<T>& rhs) 00196 { 00197 if (&rhs != this) 00198 { 00199 FreePtr(); 00200 m_first = rhs.m_first; 00201 m_last = rhs.m_last; 00202 m_length = rhs.m_length; 00203 00204 if (m_first == 0) 00205 Init(m_length); 00206 else 00207 Init(Range(m_first , m_last)); 00208 00209 for (int i=0 ; i<m_length ; ++i) 00210 *(m_ptr+i) = *(rhs.m_ptr+i); 00211 00212 } 00213 return *this; 00214 } 00215 00216 template <class T> 00217 void OneDArray<T>::Resize(int l) 00218 { 00219 FreePtr(); 00220 Init(l); 00221 } 00222 00223 //private member functions// 00225 00226 template <class T> 00227 void OneDArray<T>::Init(const int len) 00228 { 00229 Range r(0 , len-1); 00230 00231 Init(r); 00232 00233 } 00234 00235 template <class T> 00236 void OneDArray<T>::Init(const Range& r) 00237 { 00238 00239 m_first = r.First(); 00240 m_last = r.Last(); 00241 m_length = m_last - m_first + 1; 00242 00243 if ( m_length>0 ) 00244 { 00245 m_ptr = new T[ m_length ]; 00246 } 00247 else 00248 { 00249 m_length = 0; 00250 m_first = 0; 00251 m_last = -1; 00252 } 00253 } 00254 00255 template <class T> 00256 void OneDArray<T>::FreePtr() 00257 { 00258 if ( m_length>0 ) 00259 delete[] m_ptr; 00260 } 00261 00262 00264 //Two-Dimensional Array type// 00266 00268 00274 template <class T> class TwoDArray 00275 { 00276 typedef T* element_type; 00277 00278 public: 00279 00281 00284 TwoDArray(){ Init(0,0); } 00285 00287 00290 TwoDArray( const int height , const int width ){Init(height , width);} 00291 00293 00296 virtual ~TwoDArray(){ 00297 FreeData(); 00298 } 00299 00301 00304 TwoDArray(const TwoDArray<T>& Cpy); 00305 00307 00310 TwoDArray<T>& operator=(const TwoDArray<T>& rhs); 00311 00313 void Resize(const int height, const int width); 00314 00316 00320 element_type& operator[](const int pos){return m_array_of_rows[pos];} 00321 00323 00327 const element_type& operator[](const int pos) const {return m_array_of_rows[pos];} 00328 00330 const int LengthX() const { return m_length_x; } 00331 00333 const int LengthY() const { return m_length_y; } 00334 00336 const int FirstX() const { return m_first_x; } 00337 00339 const int FirstY() const { return m_first_y; } 00340 00342 const int LastX() const { return m_last_x; } 00343 00345 const int LastY() const { return m_last_y; } 00346 00347 private: 00349 void Init(const int height,const int width); 00350 00352 void FreeData(); 00353 00354 int m_first_x; 00355 int m_first_y; 00356 00357 int m_last_x; 00358 int m_last_y; 00359 00360 int m_length_x; 00361 int m_length_y; 00362 00363 element_type* m_array_of_rows; 00364 }; 00365 00366 //public member functions// 00368 00370 template <class T > 00371 std::ostream & operator<< (std::ostream & stream, TwoDArray<T> & array) 00372 { 00373 for (int j=0 ; j<array.LengthY() ; ++j) 00374 { 00375 for (int i=0 ; i<array.LengthX() ; ++i) 00376 { 00377 stream << array[j][i] << " "; 00378 }// i 00379 stream << std::endl; 00380 }// j 00381 00382 return stream; 00383 } 00384 00386 template <class T > 00387 std::istream & operator>> (std::istream & stream, TwoDArray<T> & array) 00388 { 00389 for (int j=0 ; j<array.LengthY() ; ++j) 00390 { 00391 for (int i=0 ; i<array.LengthX() ; ++i) 00392 { 00393 stream >> array[j][i]; 00394 }// i 00395 }// j 00396 00397 return stream; 00398 } 00399 00400 template <class T> 00401 TwoDArray<T>::TwoDArray(const TwoDArray<T>& Cpy) 00402 { 00403 m_first_x = Cpy.m_first_x; 00404 m_first_y = Cpy.m_first_y; 00405 m_last_x = Cpy.m_last_x; 00406 m_last_y = Cpy.m_last_y; 00407 00408 m_length_x = m_last_x - m_first_x + 1; 00409 m_length_y = m_last_y - m_first_y + 1; 00410 00411 if (m_first_x == 0 && m_first_y == 0) 00412 Init(m_length_y , m_length_x); 00413 else{ 00414 //based 2D arrays not yet supported 00415 } 00416 for (int j=0 ; j<m_length_y ; ++j) 00417 { 00418 for (int i=0 ; i<m_length_x ; ++i) 00419 { 00420 *(m_array_of_rows[j] + i) = *( (Cpy.m_array_of_rows)[j] + i ); 00421 }// i 00422 }// j 00423 00424 } 00425 00426 template <class T> 00427 TwoDArray<T>& TwoDArray<T>::operator=(const TwoDArray<T>& rhs){ 00428 if (&rhs != this) 00429 { 00430 FreeData(); 00431 00432 m_first_x = rhs.m_first_x; 00433 m_first_y = rhs.m_first_y; 00434 00435 m_last_x = rhs.m_last_x; 00436 m_last_y = rhs.m_last_y; 00437 00438 m_length_x = m_last_x - m_first_x + 1; 00439 m_length_y = m_last_y - m_first_y + 1; 00440 00441 if (m_first_x == 0 && m_first_y == 0) 00442 Init(m_length_y , m_length_x); 00443 else 00444 { 00445 //based 2D arrays not yet supported 00446 } 00447 00448 for ( int j=0 ; j<m_length_y; ++j) 00449 { 00450 for ( int i=0; i<m_length_x ; ++i) 00451 { 00452 *(m_array_of_rows[j] + i ) = *( (rhs.m_array_of_rows)[j] + i ); 00453 } 00454 } 00455 } 00456 00457 return *this; 00458 00459 } 00460 00461 template <class T> 00462 void TwoDArray<T>::Resize(const int height, const int width) 00463 { 00464 FreeData(); 00465 Init(height , width); 00466 } 00467 00468 //private member functions// 00470 00471 template <class T> 00472 void TwoDArray<T>::Init(const int height , const int width) 00473 { 00474 m_length_x = width; 00475 m_length_y = height; 00476 m_first_x = 0; 00477 m_first_y = 0; 00478 00479 m_last_x = m_length_x-1; 00480 m_last_y = m_length_y-1; 00481 00482 if (m_length_y>0) 00483 { 00484 // allocate the array containing ptrs to all the rows 00485 m_array_of_rows = new element_type[ m_length_y ]; 00486 00487 if ( m_length_x>0 ) 00488 { 00489 // next, allocate all the rows 00490 for (int j=0 ; j<m_length_y ; ++j) 00491 { 00492 m_array_of_rows[j] = new T[ m_length_x ]; 00493 }// j 00494 } 00495 else 00496 { 00497 m_length_x = 0; 00498 m_first_x = 0; 00499 m_last_x = -1; 00500 } 00501 } 00502 else 00503 { 00504 m_length_x = 0; 00505 m_length_y = 0; 00506 m_first_x = 0; 00507 m_first_y = 0; 00508 m_last_x = -1; 00509 m_last_y = -1; 00510 } 00511 } 00512 00513 template <class T> 00514 void TwoDArray<T>::FreeData() 00515 { 00516 if (m_length_y>0) 00517 { 00518 if (m_length_x>0) 00519 { 00520 // deallocate each row 00521 for (int j=0 ; j<m_length_y ; ++j) 00522 { 00523 delete[] m_array_of_rows[j]; 00524 }// j 00525 } 00526 00527 // deallocate the array of rows 00528 delete[] m_array_of_rows; 00529 } 00530 } 00531 00532 #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.