ofstd/include/dcmtk/ofstd/ofset.h

00001 /*
00002  *
00003  *  Copyright (C) 2002-2010, OFFIS e.V.
00004  *  All rights reserved.  See COPYRIGHT file for details.
00005  *
00006  *  This software and supporting documentation were developed by
00007  *
00008  *    OFFIS e.V.
00009  *    R&D Division Health
00010  *    Escherweg 2
00011  *    D-26121 Oldenburg, Germany
00012  *
00013  *
00014  *  Module:  ofstd
00015  *
00016  *  Author:  Thomas Wilkens
00017  *
00018  *  Purpose: Template class for administrating a set of elements of an
00019  *           arbitrary type.
00020  *
00021  *  Last Update:      $Author: joergr $
00022  *  Update Date:      $Date: 2010-10-14 13:15:50 $
00023  *  CVS/RCS Revision: $Revision: 1.13 $
00024  *  Status:           $State: Exp $
00025  *
00026  *  CVS/RCS Log at end of file
00027  *
00028  */
00029 
00030 #ifndef OFSET_H
00031 #define OFSET_H
00032 
00033 #include "dcmtk/config/osconfig.h"
00034 #include "dcmtk/ofstd/oftypes.h"
00035 
00036 #define INCLUDE_CSTDDEF               /* For NULL */
00037 #include "dcmtk/ofstd/ofstdinc.h"
00038 
00039 #define STARTING_SIZE 8
00040 
00044 template <class T> class OFSet
00045 {
00046   protected:
00048     T **items;
00050     unsigned int num;
00052     unsigned int size;
00053 
00054   public:
00057     OFSet()
00058         : items( new T*[ STARTING_SIZE ] ), num( 0 ), size( STARTING_SIZE )
00059       {
00060         init();
00061       }
00062 
00063 
00067     void init()
00068       {
00069         for( unsigned i=0 ; i<size ; i++ )
00070           items[i] = NULL;
00071       }
00072 
00073 
00077     OFSet( const OFSet<T> &src )
00078         : items( NULL ), num ( src.num ), size ( src.size )
00079       {
00080         init( src );
00081       }
00082 
00083 
00087     void init( const OFSet<T> &src )
00088       {
00089         items = new T*[size];
00090         for( unsigned int i=0 ; i<size ; i++ )
00091         {
00092           if( i<num )
00093             items[i] = new T( *src.items[i] );
00094           else
00095             items[i] = NULL;
00096         }
00097       }
00098 
00099 
00102     virtual ~OFSet()
00103       {
00104         for( unsigned int i=0 ; i<num ; i++ )
00105           delete items[i];
00106         delete[] items;
00107       }
00108 
00109 
00114     const OFSet<T> &operator=( const OFSet<T> &src )
00115       {
00116         if( this == &src )
00117           return( *this );
00118 
00119         unsigned int i;
00120 
00121         for( i=0 ; i<num ; i++ )
00122           delete items[i];
00123         delete[] items;
00124 
00125         num = src.num;
00126         size = src.size;
00127         items = new T*[size];
00128         for( i=0 ; i<size ; i++ )
00129         {
00130           if( i<num )
00131             items[i] = new T( *src.items[i] );
00132           else
00133             items[i] = NULL;
00134         }
00135 
00136         return( *this );
00137       }
00138 
00139 
00140 
00148     virtual T &operator[]( unsigned int i ) const
00149       {
00150         if( i<num )
00151           return( *items[i] );
00152         else
00153         {
00154           T *obj = new T();
00155           T &ret = *obj;
00156           return( ret );
00157         }
00158       }
00159 
00160 
00165     virtual void Resize( unsigned int newSize )
00166       {
00167         unsigned int i;
00168 
00169         if( newSize >= num )
00170         {
00171           T **tmp = new T*[newSize];
00172 
00173           for( i=0 ; i<newSize ; i++ )
00174           {
00175             if( i<num )
00176               tmp[i] = items[i];
00177             else
00178               tmp[i] = NULL;
00179           }
00180 
00181           delete[] items;
00182           items = tmp;
00183 
00184           size = newSize;
00185         }
00186       }
00187 
00188 
00191     virtual void Clear()
00192       {
00193         for( unsigned int i=0 ; i<num ; i++ )
00194         {
00195           delete items[i];
00196           items[i] = NULL;
00197         }
00198 
00199         num = 0;
00200       }
00201 
00202 
00206     virtual OFBool IsEmpty() const
00207       {
00208         if( num == 0 )
00209           return( OFTrue );
00210         else
00211           return( OFFalse );
00212       }
00213 
00214 
00218     virtual unsigned int NumberOfElements() const
00219       {
00220         return( num );
00221       }
00222 
00223 
00227     virtual void Insert( const T &item ) = 0;
00228 
00229 
00233     virtual void Remove( const T &item ) = 0;
00234 
00235 
00239     virtual void RemoveByIndex( unsigned int idx ) = 0;
00240 
00241 
00248     virtual T *Find( const T &item ) const = 0;
00249 
00250 
00255     virtual OFBool Contains( const T &item ) const = 0;
00256 };
00257 
00258 
00259 #endif
00260 
00261 /*
00262 ** CVS/RCS Log:
00263 ** $Log: ofset.h,v $
00264 ** Revision 1.13  2010-10-14 13:15:50  joergr
00265 ** Updated copyright header. Added reference to COPYRIGHT file.
00266 **
00267 ** Revision 1.12  2010-10-05 08:36:51  joergr
00268 ** Fixed various Doxygen API documentation issues.
00269 **
00270 ** Revision 1.11  2010-03-01 09:08:51  uli
00271 ** Removed some unnecessary include directives in the headers.
00272 **
00273 ** Revision 1.10  2005-12-08 16:06:01  meichel
00274 ** Changed include path schema for all DCMTK header files
00275 **
00276 ** Revision 1.9  2005/07/01 10:01:50  wilkens
00277 ** Modified a couple of "delete" statements to "delete[]" in order to get rid of
00278 ** valgrind's "Mismatched free() / delete / delete []" error messages.
00279 **
00280 ** Revision 1.8  2002/12/18 09:06:41  wilkens
00281 ** Had forgotten to delete some superfluous code. Did it now.
00282 **
00283 ** Revision 1.7  2002/12/17 17:01:34  wilkens
00284 ** Modified code again to keep Sun CC 2.0.1 happy on Solaris 2.5.1 (template
00285 ** errors).
00286 **
00287 ** Revision 1.6  2002/12/16 10:40:25  wilkens
00288 ** Removed superfluous implementation files and modified header and make files.
00289 **
00290 ** Revision 1.5  2002/12/13 12:26:51  wilkens
00291 ** Modified code to keep Sun CC 2.0.1 happy on Solaris 2.5.1 (template errors).
00292 **
00293 ** Revision 1.4  2002/12/09 13:07:03  joergr
00294 ** Renamed parameter to avoid name clash with global function index().
00295 ** Initialize member variables in the member initialization list.
00296 **
00297 ** Revision 1.3  2002/07/09 18:29:46  wilkens
00298 ** Added some more functionality.
00299 **
00300 **
00301 */


Generated on 6 Jan 2011 for OFFIS DCMTK Version 3.6.0 by Doxygen 1.5.1