arrays.h

00001 /* ***** BEGIN LICENSE BLOCK *****
00002 *
00003 * $Id: arrays.h,v 1.3 2005/01/30 05:11:40 gabest 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), Peter Meerwald ([email protected])
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 #include <algorithm>
00048 
00049 namespace dirac
00050 {
00051     typedef short ValueType;
00052     typedef int CalcValueType;
00053 
00055 
00059     class Range
00060     {
00061     public:
00063 
00066         Range(int s, int e): m_fst(s), m_lst(e){}
00067 
00069         const int First() const {return m_fst;}
00070 
00072         const int Last() const {return m_lst;}
00073 
00074     private:
00075         int m_fst ,m_lst;
00076     };
00077 
00079     //One-Dimensional Array type//
00081 
00083 
00088     template <class T> class OneDArray
00089     {
00090     public:
00092 
00095         OneDArray();
00096 
00098 
00101         OneDArray(const int len);
00102 
00104 
00109         OneDArray(const Range& r);
00110 
00112 
00115         ~OneDArray()
00116         {
00117             FreePtr();
00118         }
00119 
00121 
00124         OneDArray(const OneDArray<T>& cpy);
00125 
00127 
00130         OneDArray<T>& operator=(const OneDArray<T>& rhs);    
00131 
00133         void Resize(int l);
00134 
00136         T& operator[](const int pos){return m_ptr[pos-m_first];}
00137 
00139         const T& operator[](const int pos) const {return m_ptr[pos-m_first];}
00140 
00142         int Length() const {return m_length;}
00143 
00145         int First() const {return m_first;}
00146 
00148         int Last() const {return m_last;}
00149 
00150     private:
00151         void Init(const int len);
00152 
00153         void Init(const Range& r);
00154 
00155         void FreePtr();    
00156 
00157         int m_first, m_last;
00158         int m_length;
00159         T* m_ptr;
00160     };
00161 
00162     //public member functions//
00164 
00165     template <class T>
00166     OneDArray<T>::OneDArray()
00167     {
00168         Init(0);
00169     }
00170 
00171     template <class T>
00172     OneDArray<T>::OneDArray(const int len)
00173     {
00174         Init(len);
00175     }
00176 
00177     template <class T>
00178     OneDArray<T>::OneDArray(const Range& r)
00179     {
00180         Init(r);
00181     }
00182 
00183     template <class T>
00184     OneDArray<T>::OneDArray(const OneDArray<T>& cpy)
00185     {
00186         m_first = cpy.m_first;
00187         m_last = cpy.m_last;
00188         m_length = m_last - m_first + 1;
00189 
00190         if (m_first==0)
00191             Init(m_length);
00192         else
00193             Init(Range(m_first , m_last));
00194 
00195         memcpy( m_ptr , cpy.m_ptr , m_length * sizeof( T ) );
00196     }
00197 
00198     template <class T>
00199     OneDArray<T>& OneDArray<T>::operator=(const OneDArray<T>& rhs)
00200     {
00201         if (&rhs != this)
00202         {
00203             FreePtr();
00204             m_first = rhs.m_first;
00205             m_last = rhs.m_last;
00206             m_length = rhs.m_length;
00207 
00208             if (m_first == 0)
00209                 Init(m_length);
00210             else
00211                 Init(Range(m_first , m_last));
00212 
00213             memcpy( m_ptr , rhs.m_ptr , m_length * sizeof( T ) );
00214 
00215         }
00216         return *this;
00217     }
00218 
00219     template <class T> 
00220     void OneDArray<T>::Resize(int l)
00221     {    
00222         FreePtr();
00223         Init(l);
00224     }
00225 
00226     //private member functions//
00228 
00229     template <class T>
00230     void OneDArray<T>::Init(const int len)
00231     {
00232         Range r(0 , len-1);
00233 
00234         Init(r);
00235 
00236     }        
00237 
00238     template <class T>
00239     void OneDArray<T>::Init(const Range& r)
00240     {
00241 
00242         m_first = r.First();
00243         m_last = r.Last();
00244         m_length = m_last - m_first + 1; 
00245 
00246         if ( m_length>0 ) 
00247         {
00248             m_ptr = new T[ m_length ];
00249         }
00250         else 
00251         {
00252             m_length = 0;
00253             m_first = 0;
00254             m_last = -1;
00255         }
00256     }
00257 
00258     template <class T>
00259     void OneDArray<T>::FreePtr()
00260     {
00261         if ( m_length>0 )
00262             delete[] m_ptr;
00263     }
00264 
00265 
00267     //Two-Dimensional Array type//
00269 
00271 
00278     template <class T> class TwoDArray
00279     {
00280         typedef T* element_type;
00281 
00282     public:
00283 
00285 
00288         TwoDArray(){ Init(0,0); }
00289 
00291 
00294         TwoDArray( const int height , const int width ){Init(height , width);}
00295 
00297 
00301         TwoDArray( const int height , const int width , T val);
00302 
00304 
00307         virtual ~TwoDArray(){
00308             FreeData();    
00309         }
00310 
00312 
00315         TwoDArray(const TwoDArray<T>& Cpy);
00316 
00318 
00321         TwoDArray<T>& operator=(const TwoDArray<T>& rhs);
00322 
00324         void Resize(const int height, const int width);    
00325 
00327 
00331         inline element_type& operator[](const int pos){return m_array_of_rows[pos];}
00332 
00334 
00338         inline const element_type& operator[](const int pos) const {return m_array_of_rows[pos];}
00339 
00341         const int LengthX() const { return m_length_x; }
00342 
00344         const int LengthY() const { return m_length_y; }
00345 
00347         const int FirstX() const { return m_first_x; } 
00348 
00350         const int FirstY() const { return m_first_y; } 
00351 
00353         const int LastX() const { return m_last_x; } 
00354 
00356         const int LastY() const { return m_last_y; }
00357 
00358     private:
00360         void Init(const int height,const int width);
00361 
00363         void FreeData();    
00364 
00365         int m_first_x;
00366         int m_first_y;
00367 
00368         int m_last_x;
00369         int m_last_y;
00370 
00371         int m_length_x;
00372         int m_length_y;
00373 
00374         element_type* m_array_of_rows;
00375     };
00376 
00377     //public member functions//
00379 
00380     template <class T>
00381     TwoDArray<T>::TwoDArray( const int height , const int width , const T val)
00382     {
00383         Init( height , width );  
00384         for (int j=0 ; j<m_length_y ; ++j)
00385             std::fill_n( m_array_of_rows[j] , m_length_x , val);
00386     }  
00387 
00388     template <class T>
00389     TwoDArray<T>::TwoDArray(const TwoDArray<T>& Cpy)
00390     {
00391         m_first_x = Cpy.m_first_x;
00392         m_first_y = Cpy.m_first_y;        
00393         m_last_x = Cpy.m_last_x;
00394         m_last_y = Cpy.m_last_y;
00395 
00396         m_length_x = m_last_x - m_first_x + 1;
00397         m_length_y = m_last_y - m_first_y + 1;        
00398 
00399         if (m_first_x == 0 && m_first_y == 0)        
00400             Init(m_length_y , m_length_x);
00401         else{
00402                 //based 2D arrays not yet supported    
00403         }
00404         for (int j=0 ; j<m_length_y ; ++j) 
00405             memcpy( m_array_of_rows[j] , (Cpy.m_array_of_rows)[j] , m_length_x * sizeof( T ) ); 
00406 
00407     }
00408 
00409     template <class T>
00410     TwoDArray<T>& TwoDArray<T>::operator=(const TwoDArray<T>& rhs)
00411     {
00412         if (&rhs != this)
00413         {
00414             FreeData();
00415 
00416             m_first_x = rhs.m_first_x;
00417             m_first_y = rhs.m_first_y;            
00418 
00419             m_last_x = rhs.m_last_x;
00420             m_last_y = rhs.m_last_y;
00421 
00422             m_length_x = m_last_x - m_first_x + 1;
00423             m_length_y = m_last_y - m_first_y + 1;        
00424 
00425             if (m_first_x == 0 && m_first_y == 0)
00426                 Init(m_length_y , m_length_x);
00427             else
00428             {
00429                     //based 2D arrays not yet supported
00430             }
00431 
00432             for ( int j=0 ; j<m_length_y; ++j)
00433                 memcpy( m_array_of_rows[j] , (rhs.m_array_of_rows)[j] , m_length_x * sizeof( T ) ); 
00434 
00435         }
00436 
00437         return *this;
00438 
00439     }
00440 
00441     template <class T>
00442     void TwoDArray<T>::Resize(const int height, const int width)
00443     {
00444         FreeData();
00445         Init(height , width);
00446     }
00447 
00448     //private member functions//
00450 
00451     template <class T>
00452     void TwoDArray<T>::Init(const int height , const int width)
00453     {
00454         m_length_x = width; 
00455         m_length_y = height;
00456         m_first_x = 0;
00457         m_first_y = 0;
00458 
00459         m_last_x = m_length_x-1;
00460         m_last_y = m_length_y-1;
00461 
00462         if (m_length_y>0)
00463         {
00464             // allocate the array containing ptrs to all the rows
00465             m_array_of_rows = new element_type[ m_length_y ];
00466 
00467             if ( m_length_x>0 )
00468             {
00469                 // next, allocate all the rows
00470                 for (int j=0 ; j<m_length_y ; ++j)
00471                 {
00472                     m_array_of_rows[j] = new T[ m_length_x ];
00473                 }// j
00474             }
00475             else
00476             {
00477                 m_length_x = 0;
00478                 m_first_x = 0;
00479                 m_last_x = -1;
00480             }
00481         }
00482         else 
00483         {
00484             m_length_x = 0;
00485             m_length_y = 0;
00486             m_first_x = 0;
00487             m_first_y = 0;
00488             m_last_x = -1;
00489             m_last_y = -1;
00490         }
00491     }
00492 
00493     template <class T>
00494     void TwoDArray<T>::FreeData()
00495     {
00496         if (m_length_y>0)
00497         {
00498             if (m_length_x>0) 
00499             {
00500                 // deallocate each row
00501                 for (int j=0 ; j<m_length_y ; ++j)
00502                 {
00503                     delete[]  m_array_of_rows[j];                
00504                 }// j
00505             }
00506 
00507             // deallocate the array of rows
00508             delete[] m_array_of_rows;
00509         }    
00510     }
00511 
00512     // Related functions
00513 
00515     template <class T >
00516     std::ostream & operator<< (std::ostream & stream, TwoDArray<T> & array)
00517     {
00518         for (int j=0 ; j<array.LengthY() ; ++j)
00519         {
00520             for (int i=0 ; i<array.LengthX() ; ++i)
00521             {
00522                 stream << array[j][i] << " ";
00523             }// i
00524             stream << std::endl;
00525         }// j
00526 
00527         return stream;
00528     }
00529 
00531     template <class T >
00532     std::istream & operator>> (std::istream & stream, TwoDArray<T> & array)
00533     {
00534         for (int j=0 ; j<array.LengthY() ; ++j)
00535         {
00536             for (int i=0 ; i<array.LengthX() ; ++i)
00537             {
00538                 stream >> array[j][i];
00539             }// i
00540         }// j
00541 
00542         return stream;
00543     }
00544 
00545 } //namespace dirac
00546 #endif

Generated on Tue Dec 13 14:47:12 2005 for guliverkli by  doxygen 1.4.5