ofset.h

00001 /*
00002  *
00003  *  Copyright (C) 2002-2005, OFFIS
00004  *
00005  *  This software and supporting documentation were developed by
00006  *
00007  *    Kuratorium OFFIS e.V.
00008  *    Healthcare Information and Communication Systems
00009  *    Escherweg 2
00010  *    D-26121 Oldenburg, Germany
00011  *
00012  *  THIS SOFTWARE IS MADE AVAILABLE,  AS IS,  AND OFFIS MAKES NO  WARRANTY
00013  *  REGARDING  THE  SOFTWARE,  ITS  PERFORMANCE,  ITS  MERCHANTABILITY  OR
00014  *  FITNESS FOR ANY PARTICULAR USE, FREEDOM FROM ANY COMPUTER DISEASES  OR
00015  *  ITS CONFORMITY TO ANY SPECIFICATION. THE ENTIRE RISK AS TO QUALITY AND
00016  *  PERFORMANCE OF THE SOFTWARE IS WITH THE USER.
00017  *
00018  *  Module:  ofstd
00019  *
00020  *  Author:  Thomas Wilkens
00021  *
00022  *  Purpose: Template class for administrating a set of elements of an
00023  *           arbitrary type.
00024  *
00025  *  Last Update:      $Author: meichel $
00026  *  Update Date:      $Date: 2005/12/08 16:06:01 $
00027  *  Source File:      $Source: /share/dicom/cvs-depot/dcmtk/ofstd/include/dcmtk/ofstd/ofset.h,v $
00028  *  CVS/RCS Revision: $Revision: 1.10 $
00029  *  Status:           $State: Exp $
00030  *
00031  *  CVS/RCS Log at end of file
00032  *
00033  */
00034 
00035 #ifndef OFSet_h
00036 #define OFSet_h
00037 
00038 #include "dcmtk/config/osconfig.h"
00039 #include "dcmtk/ofstd/oftypes.h"
00040 
00041 #define STARTING_SIZE 8
00042 
00046 template <class T> class OFSet
00047 {
00048   protected:
00049     T **items;
00050     unsigned int num;
00051     unsigned int size;
00052 
00053   public:
00056     OFSet()
00057         : items( new T*[ STARTING_SIZE ] ), num( 0 ), size( STARTING_SIZE )
00058       {
00059         init();
00060       }
00061 
00062 
00066     void init()
00067       {
00068         for( unsigned i=0 ; i<size ; i++ )
00069           items[i] = NULL;
00070       }
00071 
00072 
00076     OFSet( const OFSet<T> &src )
00077         : items( NULL ), num ( src.num ), size ( src.size )
00078       {
00079         init( src );
00080       }
00081 
00082 
00086     void init( const OFSet<T> &src )    
00087       {
00088         items = new T*[size];
00089         for( unsigned int i=0 ; i<size ; i++ )
00090         {
00091           if( i<num )
00092             items[i] = new T( *src.items[i] );
00093           else
00094             items[i] = NULL;
00095         }
00096       }
00097 
00098 
00101     virtual ~OFSet()
00102       {
00103         for( unsigned int i=0 ; i<num ; i++ )
00104           delete items[i];
00105         delete[] items;
00106       }
00107 
00108 
00113     const OFSet<T> &operator=( const OFSet<T> &src )
00114       {
00115         if( this == &src )
00116           return( *this );
00117 
00118         unsigned int i;
00119 
00120         for( i=0 ; i<num ; i++ )
00121           delete items[i];
00122         delete[] items;
00123 
00124         num = src.num;
00125         size = src.size;
00126         items = new T*[size];
00127         for( i=0 ; i<size ; i++ )
00128         {
00129           if( i<num )
00130             items[i] = new T( *src.items[i] );
00131           else
00132             items[i] = NULL;
00133         }
00134 
00135         return( *this );
00136       }
00137 
00138 
00139 
00147     virtual T &operator[]( unsigned int i ) const
00148       {
00149         if( i<num )
00150           return( *items[i] );
00151         else
00152         {
00153           T *obj = new T();
00154           T &ret = *obj;
00155           return( ret );
00156         }
00157       }
00158 
00159 
00164     virtual void Resize( unsigned int newSize )
00165       {
00166         unsigned int i;
00167 
00168         if( newSize >= num )
00169         {
00170           T **tmp = new T*[newSize];
00171 
00172           for( i=0 ; i<newSize ; i++ )
00173           {
00174             if( i<num )
00175               tmp[i] = items[i];
00176             else
00177               tmp[i] = NULL;
00178           }
00179 
00180           delete[] items;
00181           items = tmp;
00182 
00183           size = newSize;
00184         }
00185       }
00186 
00187 
00190     virtual void Clear()
00191       {
00192         for( unsigned int i=0 ; i<num ; i++ )
00193         {
00194           delete items[i];
00195           items[i] = NULL;
00196         }
00197 
00198         num = 0;
00199       }
00200 
00201 
00205     virtual OFBool IsEmpty() const
00206       {
00207         if( num == 0 )
00208           return( OFTrue );
00209         else
00210           return( OFFalse );
00211       }
00212 
00213 
00217     virtual unsigned int NumberOfElements() const
00218       {
00219         return( num );
00220       }
00221 
00222 
00226     virtual void Insert( const T &item ) = 0;
00227 
00228 
00232     virtual void Remove( const T &item ) = 0;
00233 
00234 
00238     virtual void RemoveByIndex( unsigned int idx ) = 0;
00239 
00240 
00247     virtual T *Find( const T &item ) const = 0;
00248 
00249 
00254     virtual OFBool Contains( const T &item ) const = 0;
00255 };
00256 
00257 
00258 #endif
00259 
00260 /*
00261 ** CVS/RCS Log:
00262 ** $Log: ofset.h,v $
00263 ** Revision 1.10  2005/12/08 16:06:01  meichel
00264 ** Changed include path schema for all DCMTK header files
00265 **
00266 ** Revision 1.9  2005/07/01 10:01:50  wilkens
00267 ** Modified a couple of "delete" statements to "delete[]" in order to get rid of
00268 ** valgrind's "Mismatched free() / delete / delete []" error messages.
00269 **
00270 ** Revision 1.8  2002/12/18 09:06:41  wilkens
00271 ** Had forgotten to delete some superfluous code. Did it now.
00272 **
00273 ** Revision 1.7  2002/12/17 17:01:34  wilkens
00274 ** Modified code again to keep Sun CC 2.0.1 happy on Solaris 2.5.1 (template
00275 ** errors).
00276 **
00277 ** Revision 1.6  2002/12/16 10:40:25  wilkens
00278 ** Removed superfluous implementation files and modified header and make files.
00279 **
00280 ** Revision 1.5  2002/12/13 12:26:51  wilkens
00281 ** Modified code to keep Sun CC 2.0.1 happy on Solaris 2.5.1 (template errors).
00282 **
00283 ** Revision 1.4  2002/12/09 13:07:03  joergr
00284 ** Renamed parameter to avoid name clash with global function index().
00285 ** Initialize member variables in the member initialization list.
00286 **
00287 ** Revision 1.3  2002/07/09 18:29:46  wilkens
00288 ** Added some more functionality.
00289 **
00290 **
00291 */


Generated on 20 Dec 2005 for OFFIS DCMTK Version 3.5.4 by Doxygen 1.4.5