Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

oarray.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2001 The AUTHORS 
00003  *   Romeu Andre' Pieritz, Ph.D.        - romeu_pieritz@hotmail.com
00004  *   Rafael Mendes, Eng.                – mendes_rafael@yahoo.com
00005  *   Rodrigo Ferraz de Andrade, Eng.    – rferraz@iname.com 
00006  * All rights reserved.
00007  *
00008  * Permission to use, copy and distribute this software and its
00009  * documentation for educational and personal use, without fee is hereby granted, 
00010  * provided that the above copyright notice and the following
00011  * two paragraphs appear in all copies of this software.
00012  *
00013  * IN NO EVENT SHALL THE AUTHORS BE LIABLE TO ANY PARTY FOR
00014  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
00015  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE AUTHORS
00016  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00017  *
00018  * THE AUTHORS SPECIFICALLY DISCLAIMS ANY WARRANTIES,
00019  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
00020  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HERE UNDER IS
00021  * ON AN "AS IS" BASIS, AND THE AUTHORS HAVE NO OBLIGATION TO
00022  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
00023  *
00024  * SINMEC Lab. - CFD Sinflow Project - http://www.sinmec.ufsc.br/cfd
00025  */
00026 
00027 
00028 // File Define
00029 #ifndef __CSFL_SYS_CORE_OARRAY_H__
00030 #define __CSFL_SYS_CORE_OARRAY_H__
00031 
00032 // Include 
00033 #include <csfl/sys/core/types.h>
00034 
00035 
00036 // Namespace
00037 namespace csfl {
00038 
00039 
00040 //==============================================================================
00041 // enum TDataGravity
00042 //==============================================================================
00043 
00044 typedef
00045         enum _TDataGravity {
00046                 dgNoGravity,
00047                 dgNormal,
00048                 dgStack
00049         }
00050         TDataGravity;
00051 
00052 //==============================================================================
00053 // template class ISimpleVectorOf<T>
00054 // Description: template class for basic vector functionality
00055 //==============================================================================
00056 
00057 template <class T>
00058 class ISimpleVectorOf
00063 {
00064 // ----------------------------------------------------------------------- C & D
00065 public:
00066         inline ISimpleVectorOf();
00067         
00068         inline ISimpleVectorOf( int _size, const T *_data = NULL );
00069         inline ISimpleVectorOf( const ISimpleVectorOf<T> & );
00070 
00071         inline ~ISimpleVectorOf();
00072 
00073 // ------------------------------------------------------------------- Operators
00074 public:
00075         inline T & operator [] ( int );
00076         inline const T & operator [] ( int ) const;
00077 
00078         inline ISimpleVectorOf<T> & operator = ( const ISimpleVectorOf<T> & );
00079 
00080 // --------------------------------------------------------------------- Methods
00081 public:
00082         int Size() const { return size; }
00083         inline int SetSize( int _size, TDataGravity = dgNoGravity );
00084         bool IsNull() const { return data==NULL; }
00085 
00086         T * Data() { return data; }
00087         const T * Data() const { return data; }
00088 
00089         inline T & Data( int );
00090         inline const T & Data( int ) const;
00091 
00092         int IndexOf( const T *value ) const
00093                 { return (value!=NULL && value>=data && value<=data+size)
00094                         ? value-data : -1; }
00095 
00096 // ------------------------------------------------------------------ Attributes
00097 protected:
00098         T *data;
00099         int size;
00100 };
00101 
00102 // Template implementation =====================================================
00103 
00104 // ISimpleVectorOf =============================================================
00105 
00106 // ----------------------------------------------------------------------- C & D
00107 
00108 template <class T>
00109 inline ISimpleVectorOf<T>::ISimpleVectorOf()
00110 {
00111         size = 0;
00112         data = NULL;
00113 }
00114 
00115 template <class T>
00116 inline ISimpleVectorOf<T>::ISimpleVectorOf( int _size, const T *_data )
00117 {
00118         if (_size > 0) {
00119                 size = _size;
00120                 data = new T [size];
00121                 if (_data) ::memcpy( data, _data, sizeof(T) * _size );
00122         }
00123         else {
00124                 size = 0;
00125                 data = NULL;
00126         }
00127 }
00128 
00129 template <class T>
00130 inline ISimpleVectorOf<T>::ISimpleVectorOf( const ISimpleVectorOf<T> &_vector )
00131 {
00132         if (_vector.size > 0) {
00133                 size = _vector.size;
00134                 data = new T [size];
00135                 ::memcpy( data, _vector.data, sizeof(T) * size );
00136         }
00137         else {
00138                 size = 0;
00139                 data = NULL;
00140         }
00141 }
00142 
00143 template <class T>
00144 inline ISimpleVectorOf<T>::~ISimpleVectorOf()
00145 {
00146         if (data) delete [] data;
00147 }
00148 
00149 // ------------------------------------------------------------------- Operators
00150 
00151 template <class T>
00152 inline T & ISimpleVectorOf<T>::operator [] ( int idx )
00153 {
00154         assert( idx>=0 && idx<size );
00155         return data[idx];
00156 }
00157 
00158 template <class T>
00159 inline const T & ISimpleVectorOf<T>::operator [] ( int idx ) const
00160 {
00161         assert( idx>=0 && idx<size );
00162         return data[idx];
00163 }
00164 
00165 template <class T>
00166 inline ISimpleVectorOf<T> & ISimpleVectorOf<T>::operator = (
00167         const ISimpleVectorOf<T> &_vector )
00168 {
00169         if (size!=_vector.size && data)
00170                 delete [] data;
00171         if (_vector.size > 0) {
00172                 size = _vector.size;
00173                 data = new T [size];
00174                 ::memcpy( data, _vector.data, sizeof(T) * size );
00175         }
00176         else {
00177                 size = 0;
00178                 data = NULL;
00179         }
00180         return *this;
00181 }
00182 
00183 // --------------------------------------------------------------------- Methods
00184 
00185 template <class T>
00186 inline int ISimpleVectorOf<T>::SetSize( int _size, TDataGravity gravity )
00187 {
00188         if (size != _size)
00189                 if (gravity != dgNoGravity) {
00190                         T *newdata = (_size>0) ? new T [_size] : NULL;
00191                         if (newdata!=NULL && data!=NULL) {
00192                                 T *src = data, *dst = newdata;
00193                                 if (gravity == dgStack) {
00194                                         if (size > _size)
00195                                                 src += size - _size;
00196                                         else
00197                                                 dst += _size - size;
00198                                 }
00199                                 ::memcpy( dst, src, sizeof (T)*  ((_size<size) ? _size : size ));
00200                         }
00201                         if (data) delete [] data;
00202                         size = _size;
00203                         data = newdata;
00204                 }
00205                 else {
00206                         if (data) delete [] data;
00207                         size = _size;
00208                         data = (size > 0) ? new T [size] : NULL;
00209                 }
00210         return size;
00211 }
00212 
00213 template <class T>
00214 inline T & ISimpleVectorOf<T>::Data( int idx )
00215 {
00216         assert( idx>=0 && idx<size );
00217         return data[idx];
00218 }
00219 
00220 template <class T>
00221 inline const T & ISimpleVectorOf<T>::Data( int idx ) const
00222 {
00223         assert( idx>=0 && idx<size );
00224         return data[idx];
00225 }
00226 
00227 //==============================================================================
00228 // template class ISimpleArrayOf<T>
00229 // Description: template class for basic array functionality
00230 //==============================================================================
00231 
00232 template <class T>
00233 class ISimpleArrayOf : private ISimpleVectorOf<T>
00238 {
00239 // ----------------------------------------------------------------------- C & D
00240 public:
00241         inline ISimpleArrayOf( int = 0, int = 16, int = 0, const T * = NULL );
00242         inline ISimpleArrayOf( const ISimpleArrayOf<T> & );
00243 
00244 // ------------------------------------------------------------------- Operators
00245 public:
00246         inline ISimpleArrayOf<T> & operator =( const ISimpleArrayOf<T> & );
00247 
00248         inline T & operator [] ( int );
00249         inline const T & operator [] ( int ) const;
00250 
00251 // ------------------------------------------------------------------- Accessing
00252 public:
00253         T * Data() { return data; }
00254         const T * Data() const { return data; }
00255 
00256 // ------------------------------------------------------------------ Publishing
00257 public:
00258         ISimpleVectorOf<T>::Size;
00259 
00260 // --------------------------------------------------------------------- Methods
00261 public:
00262         inline int SetSize( int );
00263         int Count() const { return count; }
00264         void Clear() { count = 0; }
00265 
00266         int Delta() const { return delta; }
00267         inline int SetDelta( int );
00268 
00269         inline T * New();
00270         inline T * New( int );
00271 
00272         inline int Add( const T & );
00273         inline int Add( const T *, int );
00274         inline int Add( const ISimpleArrayOf<T> & );
00275         inline int Insert( int, const T & );
00276         inline int Insert( int, const T *, int );
00277         inline int Insert( int, const ISimpleArrayOf<T> & );
00278         inline int Remove( int, int = 1 );
00279 
00280         int Fit() { return SetSize( count, dgNormal ); }
00281 
00282         int IndexOf( const T *value ) const
00283         { return (value!=NULL && value>=data && value<=data+count)
00284                         ? value-data : -1; }
00285 
00286 // ------------------------------------------------------------------ Attributes
00287 protected:
00288         int count, delta;
00289 
00290         int SetSize( int _size, TDataGravity dg )
00291                 { return ISimpleVectorOf<T>::SetSize( _size, dg ); }
00292 };
00293 // Template implementation =====================================================
00294 
00295 // ISimpleArrayOf ==============================================================
00296 
00297 // ----------------------------------------------------------------------- C & D
00298 template <class T>
00299 inline ISimpleArrayOf<T>::ISimpleArrayOf(
00300                 int _size, int _delta, int _count, const T *_data ) :
00301         ISimpleVectorOf<T>( _size )
00302 {
00303         delta = _delta > 0 ? _delta : 0;
00304         if (_count>0 && _count<=_size) {
00305                 count = _count;
00306                 if (_data != NULL)
00307                         ::memcpy( data, _data, sizeof (T) * count );
00308         }
00309         else
00310                 count = 0;
00311 }
00312 
00313 template <class T>
00314 inline ISimpleArrayOf<T>::ISimpleArrayOf( const ISimpleArrayOf<T> &array ) :
00315         ISimpleVectorOf<T>( array )
00316 {
00317         count = array.count;
00318         delta = array.delta;
00319 }
00320 
00321 // ------------------------------------------------------------------- Operators
00322 template <class T>
00323 inline ISimpleArrayOf<T> & ISimpleArrayOf<T>::operator =(
00324         const ISimpleArrayOf<T> &array )
00325 {
00326         if (size < array.count)
00327                 SetSize( array.count, dgNoGravity );
00328         count = array.count;
00329         delta = array.delta;
00330         if (count > 0)
00331                 ::memcpy( data, array.data, sizeof (T) * count );
00332         return *this;
00333 }
00334 
00335 template <class T>
00336 inline T & ISimpleArrayOf<T>::operator [] ( int idx )
00337 {
00338         assert(idx>=0 && idx<count );
00339         return data[idx];
00340 }
00341 
00342 template <class T>
00343 inline const T & ISimpleArrayOf<T>::operator [] ( int idx ) const
00344 {
00345         assert(idx>=0 && idx<count );
00346         return data[idx];
00347 }
00348 
00349 // --------------------------------------------------------------------- Methods
00350 template <class T>
00351 inline int ISimpleArrayOf<T>::SetSize( int _size )
00352 {
00353         int result = SetSize( _size, dgNormal );
00354         count = _size;
00355         return result;
00356 }
00357 
00358 template <class T>
00359 inline int ISimpleArrayOf<T>::SetDelta( int _delta )
00360 {
00361         delta = _delta>0 ? delta : 0;
00362         return delta;
00363 }
00364 
00365 template <class T>
00366 inline T * ISimpleArrayOf<T>::New()
00367 {
00368         return (count == size && (delta == 0 ||
00369                         !SetSize( size + delta, dgNormal )))
00370                 ? NULL : (data + count++);
00371 }
00372 
00373 template <class T>
00374 inline T * ISimpleArrayOf<T>::New( int pos )
00375 {
00376         if (pos<0 || pos>count || (count == size && (delta == 0 ||
00377                         !SetSize( size + delta, dgNormal ))))
00378                 return NULL;
00379         else {
00380                 int dif = count++ - pos;
00381                 if (dif > 0)
00382                         ::memmove( data+pos+1, data+pos, sizeof (T) * dif );
00383                 return data + pos;
00384         }
00385 }
00386 
00387 template <class T>
00388 inline int ISimpleArrayOf<T>::Add( const T &value )
00389 {
00390         if (count == size && (delta == 0 ||
00391                         !SetSize( size + delta, dgNormal )))
00392                 return -1;
00393         else {
00394                 int pos = count++;
00395                 data[ pos ] = value;
00396                 return pos;
00397         }
00398 }
00399 
00400 template <class T>
00401 inline int ISimpleArrayOf<T>::Add( const T *values, int n )
00402 {
00403         if (n<=0 || (count+n-1 >= size && (delta == 0 ||
00404                         !SetSize( count + n, dgNormal ))))
00405                 return -1;
00406         else {
00407                 int pos = count;
00408                 count += n;
00409                 ::memcpy( data + pos, values, sizeof(T) * n );
00410                 return pos;
00411         }
00412 }
00413 
00414 template <class T>
00415 inline int ISimpleArrayOf<T>::Add( const ISimpleArrayOf<T> &array )
00416 {
00417         return array.count>0 ? Add( array.data, array.count ) : -1;
00418 }
00419 
00420 template <class T>
00421 inline int ISimpleArrayOf<T>::Insert( int pos, const T &value )
00422 {
00423         if (pos<0 || pos>count || (count == size && (delta == 0 ||
00424                         !SetSize( size + delta, dgNormal ))))
00425                 return -1;
00426         else {
00427                 int dif = count++ - pos;
00428                 if (dif > 0)
00429                         ::memmove( data+pos+1, data+pos, sizeof (T) * dif );
00430                 data[ pos ] = value;
00431                 return pos;
00432         }
00433 }
00434 
00435 template <class T>
00436 inline int ISimpleArrayOf<T>::Insert( int pos, const T *values, int n )
00437 {
00438         if (n<=0 || (count+n+1 >= size && (delta == 0 ||
00439                         !SetSize( count + n, dgNormal ))))
00440                 return -1;
00441         else {
00442                 int dif = count - pos;
00443                 count += n;
00444                 if (dif > 0)
00445                         ::memmove( data+pos+n, data+pos, sizeof (T) * dif );
00446                 ::memcpy( data+pos, values, sizeof (T) * n );
00447                 return pos;
00448         }
00449 }
00450 
00451 template <class T>
00452 inline int ISimpleArrayOf<T>::Insert( int pos, const ISimpleArrayOf<T> &array )
00453 {
00454         return array.count>0 ? Insert( pos, array.data, array.count ) : -1;
00455 }
00456 
00457 template <class T>
00458 inline int ISimpleArrayOf<T>::Remove( int pos, int n )
00459 {
00460         if (pos<0 || pos>=count || n<=0 || (pos+n)>count)
00461                 return -1;
00462         else {
00463                 int dif = (count -= n) - pos;
00464                 if (dif > 0)
00465                         ::memmove( data+pos, data+pos+n, sizeof (T) * dif );
00466                 return pos;
00467         }
00468 }
00469 
00470 
00471 //==============================================================================
00472 // template class IObjectArrayOf<T>
00473 // Description: template class for object array functionality
00474 //==============================================================================
00475 
00476 template <class T>
00477 class IObjectArrayOf : private ISimpleArrayOf<T *>
00482 {
00483 
00484 // ----------------------------------------------------------------------- C & D
00485 public:
00486         typedef T * const * TObjs;
00487 
00488         inline IObjectArrayOf( bool = true, int = 0, int = 16 );
00489         inline ~IObjectArrayOf();
00490 
00491 // ------------------------------------------------------------------- Operators
00492 public:
00493         inline T * operator [] ( int );
00494         inline const T * operator [] ( int ) const;
00495 
00496 // -------------------------------------------------------------- Object Methods
00497 public:
00498         T * const * Objects() { return ISimpleArrayOf<T *>::Data(); }
00499         const T * const * Objects() const { return ISimpleArrayOf<T *>::Data(); }
00500 
00501 // ------------------------------------------------------------------ Publishing
00502 public:
00503         ISimpleArrayOf<T *>::Size;
00504         ISimpleArrayOf<T *>::Count;
00505 
00506         ISimpleArrayOf<T *>::Delta;
00507         ISimpleArrayOf<T *>::SetDelta;
00508 
00509 // --------------------------------------------------------------------- Methods
00510 public:
00511         virtual void Clear();
00512         
00513         inline int Add( T * );
00514         inline int Add( IObjectArrayOf<T> & );
00515         inline int Insert( int, T * );
00516         inline int Insert( int, IObjectArrayOf<T> & );
00517         inline int Remove( int, int = 1 );
00518 
00519         bool IsOwner() const { return owner; }
00520         inline bool SetOwner( bool _owner );
00521 
00522 public:
00523         T * const * Data() const { return ISimpleArrayOf<T *>::Data(); }
00524 
00525 protected:
00526         T ** Data() { return ISimpleArrayOf<T *>::Data(); }
00527         void FreeObjects( int, int );
00528 
00529 // ------------------------------------------------------------------ Attributes
00530 protected:
00531         bool owner;
00532 };
00533 
00534 
00535 // algorithms ==================================================================
00536 
00537 // ----------------------------------------------------------- object allocation
00538 
00539 template <class T>
00540 IObjectArrayOf<T> & Init( IObjectArrayOf<T> &array, int n )
00541 {
00542         while (n-- > 0) array.Add( new T );
00543         return array;
00544 }
00545 
00546 template <class T>
00547 IObjectArrayOf<T> & Init( IObjectArrayOf<T> &array, int pos, int n )
00548 {
00549         if (pos>=0 && pos<=array.Count())
00550                 while (n-- > 0) array.Insert( pos++, new T );
00551         return array;
00552 }
00553 
00554 // Template implementation =====================================================
00555 
00556 // IObjectArrayOf ==============================================================
00557 
00558 // ----------------------------------------------------------------------- C & D
00559 template <class T>
00560 inline IObjectArrayOf<T>::IObjectArrayOf( bool _owner, int _size, int _delta ) :
00561         ISimpleArrayOf<T*>( _size, _delta )
00562 {
00563         owner = _owner;
00564 }
00565 
00566 template <class T>
00567 inline IObjectArrayOf<T>::~IObjectArrayOf()
00568 {
00569         Clear();
00570 }
00571 
00572 // ------------------------------------------------------------------- Operators
00573 
00574 template <class T>
00575 inline T * IObjectArrayOf<T>::operator [] ( int idx )
00576 {
00577         assert( idx>=0 && idx<count && Data()[idx] );
00578         return Data()[idx];
00579 }
00580 
00581 template <class T>
00582 inline const T * IObjectArrayOf<T>::operator [] ( int idx ) const
00583 {
00584         assert( idx>=0 && idx<count && Data()[idx] );
00585         return Data()[idx];
00586 }
00587 
00588 // --------------------------------------------------------------------- Methods
00589 template <class T>
00590 inline void IObjectArrayOf<T>::Clear()
00591 {
00592         if (owner)
00593                 FreeObjects( 0, count );
00594         count = 0;
00595 }
00596 
00597 template <class T>
00598 inline int IObjectArrayOf<T>::Add( T *object )
00599 {
00600         return object!=NULL ? ISimpleArrayOf<T*>::Add( object ) : -1;
00601 }
00602 
00603 template <class T>
00604 inline int IObjectArrayOf<T>::Add( IObjectArrayOf<T> &array )
00605 {
00606         assert( !owner || !array.owner );
00607         return ISimpleArrayOf<T*>::Add( array );
00608 }
00609 
00610 template <class T>
00611 inline int IObjectArrayOf<T>::Insert( int pos, T *object )
00612 {
00613         return object!=NULL ? ISimpleArrayOf<T*>::Insert( pos, object ) : -1;
00614 }
00615 
00616 template <class T>
00617 inline int IObjectArrayOf<T>::Insert( int pos, IObjectArrayOf<T> &array )
00618 {
00619         return ISimpleArrayOf<T*>::Insert( pos, array );
00620 }
00621 
00622 template <class T>
00623 inline int IObjectArrayOf<T>::Remove( int pos, int n )
00624 {
00625         if (pos<0 || pos>=count || n<=0 || (pos+n)>count)
00626                 return -1;
00627         else {
00628                 if (owner) FreeObjects( pos, n );
00629                 return ISimpleArrayOf<T*>::Remove( pos, n );
00630         }
00631 }
00632 
00633 template <class T>
00634 inline bool IObjectArrayOf<T>::SetOwner( bool _owner )
00635 {
00636         bool old = owner;
00637         owner = _owner;
00638         return old;
00639 }
00640 
00641 template <class T>
00642 void IObjectArrayOf<T>::FreeObjects( int pos, int n )
00643 {
00644         T **cell = Data() + pos;
00645         while (n--) {
00646                 delete *cell;
00647                 *cell++ = NULL;
00648         }
00649 }
00650 
00651 
00652 } // csfl namespace
00653 
00654 #endif // __CSFL_SYS_CORE_OARRAY_H__
00655 

 

CFD Project | Documents | Downloads | Contact us | Use Terms

 

© SINMEC/EMC/UFSC, 2001.

All rights reserved.

Last Update: Jan. 18, 2002

 Webmaster