Created by the British Broadcasting Corporation.
00001 /* ***** BEGIN LICENSE BLOCK ***** 00002 * 00003 * $Id: arrays.h,v 1.14 2005/08/01 16:14:03 asuraparaju Exp $ $Name: Dirac_0_5_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 * 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 // For clipping purposes, set the maximum value at 1020 = 4*255 00056 // (we have two accuracy bits throughout) 00057 const int PIXEL_VALUE_MAX = 1020; 00058 const int PIXEL_VALUE_MIN = 0; 00059 00060 typedef int CalcValueType; 00061 00063 00067 class Range 00068 { 00069 public: 00071 00074 Range(int s, int e): m_fst(s), m_lst(e){} 00075 00077 const int First() const {return m_fst;} 00078 00080 const int Last() const {return m_lst;} 00081 00082 private: 00083 int m_fst ,m_lst; 00084 }; 00085 00087 //One-Dimensional Array type// 00089 00091 00096 template <class T> class OneDArray 00097 { 00098 public: 00100 00103 OneDArray(); 00104 00106 00109 OneDArray(const int len); 00110 00112 00117 OneDArray(const Range& r); 00118 00120 00123 ~OneDArray() 00124 { 00125 FreePtr(); 00126 } 00127 00129 00132 OneDArray(const OneDArray<T>& cpy); 00133 00135 00138 OneDArray<T>& operator=(const OneDArray<T>& rhs); 00139 00141 void Resize(int l); 00142 00144 T& operator[](const int pos){return m_ptr[pos-m_first];} 00145 00147 const T& operator[](const int pos) const {return m_ptr[pos-m_first];} 00148 00150 int Length() const {return m_length;} 00151 00153 int First() const {return m_first;} 00154 00156 int Last() const {return m_last;} 00157 00158 private: 00159 void Init(const int len); 00160 00161 void Init(const Range& r); 00162 00163 void FreePtr(); 00164 00165 int m_first, m_last; 00166 int m_length; 00167 T* m_ptr; 00168 }; 00169 00170 //public member functions// 00172 00173 template <class T> 00174 OneDArray<T>::OneDArray() 00175 { 00176 Init(0); 00177 } 00178 00179 template <class T> 00180 OneDArray<T>::OneDArray(const int len) 00181 { 00182 Init(len); 00183 } 00184 00185 template <class T> 00186 OneDArray<T>::OneDArray(const Range& r) 00187 { 00188 Init(r); 00189 } 00190 00191 template <class T> 00192 OneDArray<T>::OneDArray(const OneDArray<T>& cpy) 00193 { 00194 m_first = cpy.m_first; 00195 m_last = cpy.m_last; 00196 m_length = m_last - m_first + 1; 00197 00198 if (m_first==0) 00199 Init(m_length); 00200 else 00201 Init(Range(m_first , m_last)); 00202 00203 memcpy( m_ptr , cpy.m_ptr , m_length * sizeof( T ) ); 00204 } 00205 00206 template <class T> 00207 OneDArray<T>& OneDArray<T>::operator=(const OneDArray<T>& rhs) 00208 { 00209 if (&rhs != this) 00210 { 00211 FreePtr(); 00212 m_first = rhs.m_first; 00213 m_last = rhs.m_last; 00214 m_length = rhs.m_length; 00215 00216 if (m_first == 0) 00217 Init(m_length); 00218 else 00219 Init(Range(m_first , m_last)); 00220 00221 memcpy( m_ptr , rhs.m_ptr , m_length * sizeof( T ) ); 00222 00223 } 00224 return *this; 00225 } 00226 00227 template <class T> 00228 void OneDArray<T>::Resize(int l) 00229 { 00230 FreePtr(); 00231 Init(l); 00232 } 00233 00234 //private member functions// 00236 00237 template <class T> 00238 void OneDArray<T>::Init(const int len) 00239 { 00240 Range r(0 , len-1); 00241 00242 Init(r); 00243 00244 } 00245 00246 template <class T> 00247 void OneDArray<T>::Init(const Range& r) 00248 { 00249 00250 m_first = r.First(); 00251 m_last = r.Last(); 00252 m_length = m_last - m_first + 1; 00253 00254 if ( m_length>0 ) 00255 { 00256 m_ptr = new T[ m_length ]; 00257 } 00258 else 00259 { 00260 m_length = 0; 00261 m_first = 0; 00262 m_last = -1; 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 } 00502 } 00503 00504 template <class T> 00505 void TwoDArray<T>::FreeData() 00506 { 00507 if (m_length_y>0) 00508 { 00509 if (m_length_x>0) 00510 { 00511 delete[] m_array_of_rows[0]; 00512 } 00513 00514 m_length_y = m_length_x = 0; 00515 // deallocate the array of rows 00516 delete[] m_array_of_rows; 00517 } 00518 } 00519 00520 // Related functions 00521 00523 template <class T > 00524 std::ostream & operator<< (std::ostream & stream, TwoDArray<T> & array) 00525 { 00526 for (int j=0 ; j<array.LengthY() ; ++j) 00527 { 00528 for (int i=0 ; i<array.LengthX() ; ++i) 00529 { 00530 stream << array[j][i] << " "; 00531 }// i 00532 stream << std::endl; 00533 }// j 00534 00535 return stream; 00536 } 00537 00539 template <class T > 00540 std::istream & operator>> (std::istream & stream, TwoDArray<T> & array) 00541 { 00542 for (int j=0 ; j<array.LengthY() ; ++j) 00543 { 00544 for (int i=0 ; i<array.LengthX() ; ++i) 00545 { 00546 stream >> array[j][i]; 00547 }// i 00548 }// j 00549 00550 return stream; 00551 } 00552 00553 } //namespace dirac 00554 #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.