Created by the British Broadcasting Corporation.
00001 /* ***** BEGIN LICENSE BLOCK ***** 00002 * 00003 * $Id: arrays.h,v 1.16 2006/06/05 14:52:09 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 } 00263 } 00264 00265 template <class T> 00266 void OneDArray<T>::FreePtr() 00267 { 00268 if ( m_length>0 ) 00269 delete[] m_ptr; 00270 } 00271 00272 00274 //Two-Dimensional Array type// 00276 00278 00286 template <class T> class TwoDArray 00287 { 00288 typedef T* element_type; 00289 00290 public: 00291 00293 00296 TwoDArray(){ Init(0,0); } 00297 00299 00302 TwoDArray( const int height , const int width ){Init(height , width);} 00303 00305 00309 TwoDArray( const int height , const int width , T val); 00310 00312 00315 virtual ~TwoDArray(){ 00316 FreeData(); 00317 } 00318 00320 00323 TwoDArray(const TwoDArray<T>& Cpy); 00324 00326 00329 TwoDArray<T>& operator=(const TwoDArray<T>& rhs); 00330 00332 void Resize(const int height, const int width); 00333 00335 00339 inline element_type& operator[](const int pos){return m_array_of_rows[pos];} 00340 00342 00346 inline const element_type& operator[](const int pos) const {return m_array_of_rows[pos];} 00347 00349 const int LengthX() const { return m_length_x; } 00350 00352 const int LengthY() const { return m_length_y; } 00353 00355 const int FirstX() const { return m_first_x; } 00356 00358 const int FirstY() const { return m_first_y; } 00359 00361 const int LastX() const { return m_last_x; } 00362 00364 const int LastY() const { return m_last_y; } 00365 00366 private: 00368 void Init(const int height,const int width); 00369 00371 void FreeData(); 00372 00373 int m_first_x; 00374 int m_first_y; 00375 00376 int m_last_x; 00377 int m_last_y; 00378 00379 int m_length_x; 00380 int m_length_y; 00381 00382 element_type* m_array_of_rows; 00383 }; 00384 00385 //public member functions// 00387 00388 template <class T> 00389 TwoDArray<T>::TwoDArray( const int height , const int width , const T val) 00390 { 00391 Init( height , width ); 00392 std::fill_n( m_array_of_rows[0], m_length_x*m_length_y, val); 00393 } 00394 00395 template <class T> 00396 TwoDArray<T>::TwoDArray(const TwoDArray<T>& Cpy) 00397 { 00398 m_first_x = Cpy.m_first_x; 00399 m_first_y = Cpy.m_first_y; 00400 m_last_x = Cpy.m_last_x; 00401 m_last_y = Cpy.m_last_y; 00402 00403 m_length_x = m_last_x - m_first_x + 1; 00404 m_length_y = m_last_y - m_first_y + 1; 00405 00406 if (m_first_x == 0 && m_first_y == 0) 00407 Init(m_length_y , m_length_x); 00408 else{ 00409 //based 2D arrays not yet supported 00410 } 00411 00412 memcpy( m_array_of_rows[0] , (Cpy.m_array_of_rows)[0] , m_length_x * m_length_y * sizeof( T ) ); 00413 00414 } 00415 00416 template <class T> 00417 TwoDArray<T>& TwoDArray<T>::operator=(const TwoDArray<T>& rhs) 00418 { 00419 if (&rhs != this) 00420 { 00421 FreeData(); 00422 00423 m_first_x = rhs.m_first_x; 00424 m_first_y = rhs.m_first_y; 00425 00426 m_last_x = rhs.m_last_x; 00427 m_last_y = rhs.m_last_y; 00428 00429 m_length_x = m_last_x - m_first_x + 1; 00430 m_length_y = m_last_y - m_first_y + 1; 00431 00432 if (m_first_x == 0 && m_first_y == 0) 00433 Init(m_length_y , m_length_x); 00434 else 00435 { 00436 //based 2D arrays not yet supported 00437 } 00438 00439 memcpy( m_array_of_rows[0], (rhs.m_array_of_rows)[0], m_length_x * m_length_y * sizeof( T ) ); 00440 00441 } 00442 00443 return *this; 00444 00445 } 00446 00447 template <class T> 00448 void TwoDArray<T>::Resize(const int height, const int width) 00449 { 00450 if (height != m_length_y || width != m_length_x) 00451 { 00452 FreeData(); 00453 Init(height , width); 00454 } 00455 } 00456 00457 //private member functions// 00459 00460 template <class T> 00461 void TwoDArray<T>::Init(const int height , const int width) 00462 { 00463 m_length_x = width; 00464 m_length_y = height; 00465 m_first_x = 0; 00466 m_first_y = 0; 00467 00468 m_last_x = m_length_x-1; 00469 m_last_y = m_length_y-1; 00470 00471 if (m_length_y>0) 00472 { 00473 // allocate the array containing ptrs to all the rows 00474 m_array_of_rows = new element_type[ m_length_y ]; 00475 00476 if ( m_length_x>0 ) 00477 { 00478 // Allocate the whole thing as a single big block 00479 m_array_of_rows[0] = new T[ m_length_x * m_length_y ]; 00480 00481 // Point the pointers 00482 for (int j=1 ; j<m_length_y ; ++j) 00483 m_array_of_rows[j] = m_array_of_rows[0] + j * m_length_x; 00484 } 00485 else 00486 { 00487 m_length_x = 0; 00488 m_first_x = 0; 00489 m_last_x = -1; 00490 } 00491 } 00492 else 00493 { 00494 m_length_x = 0; 00495 m_length_y = 0; 00496 m_first_x = 0; 00497 m_first_y = 0; 00498 m_last_x = -1; 00499 m_last_y = -1; 00500 } 00501 } 00502 00503 template <class T> 00504 void TwoDArray<T>::FreeData() 00505 { 00506 if (m_length_y>0) 00507 { 00508 if (m_length_x>0) 00509 { 00510 delete[] m_array_of_rows[0]; 00511 } 00512 00513 m_length_y = m_length_x = 0; 00514 // deallocate the array of rows 00515 delete[] m_array_of_rows; 00516 } 00517 } 00518 00519 // Related functions 00520 00522 template <class T > 00523 std::ostream & operator<< (std::ostream & stream, TwoDArray<T> & array) 00524 { 00525 for (int j=0 ; j<array.LengthY() ; ++j) 00526 { 00527 for (int i=0 ; i<array.LengthX() ; ++i) 00528 { 00529 stream << array[j][i] << " "; 00530 }// i 00531 stream << std::endl; 00532 }// j 00533 00534 return stream; 00535 } 00536 00538 template <class T > 00539 std::istream & operator>> (std::istream & stream, TwoDArray<T> & array) 00540 { 00541 for (int j=0 ; j<array.LengthY() ; ++j) 00542 { 00543 for (int i=0 ; i<array.LengthX() ; ++i) 00544 { 00545 stream >> array[j][i]; 00546 }// i 00547 }// j 00548 00549 return stream; 00550 } 00551 00552 } //namespace dirac 00553 #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.