OFFIS DCMTK  Version 3.6.0
ofset.h
1 /*
2  *
3  * Copyright (C) 2002-2010, OFFIS e.V.
4  * All rights reserved. See COPYRIGHT file for details.
5  *
6  * This software and supporting documentation were developed by
7  *
8  * OFFIS e.V.
9  * R&D Division Health
10  * Escherweg 2
11  * D-26121 Oldenburg, Germany
12  *
13  *
14  * Module: ofstd
15  *
16  * Author: Thomas Wilkens
17  *
18  * Purpose: Template class for administrating a set of elements of an
19  * arbitrary type.
20  *
21  * Last Update: $Author: joergr $
22  * Update Date: $Date: 2010-10-14 13:15:50 $
23  * CVS/RCS Revision: $Revision: 1.13 $
24  * Status: $State: Exp $
25  *
26  * CVS/RCS Log at end of file
27  *
28  */
29 
30 #ifndef OFSET_H
31 #define OFSET_H
32 
33 #include "dcmtk/config/osconfig.h"
34 #include "dcmtk/ofstd/oftypes.h"
35 
36 #define INCLUDE_CSTDDEF /* For NULL */
37 #include "dcmtk/ofstd/ofstdinc.h"
38 
39 #define STARTING_SIZE 8
40 
44 template <class T> class OFSet
45 {
46  protected:
48  T **items;
50  unsigned int num;
52  unsigned int size;
53 
54  public:
58  : items( new T*[ STARTING_SIZE ] ), num( 0 ), size( STARTING_SIZE )
59  {
60  init();
61  }
62 
63 
67  void init()
68  {
69  for( unsigned i=0 ; i<size ; i++ )
70  items[i] = NULL;
71  }
72 
73 
77  OFSet( const OFSet<T> &src )
78  : items( NULL ), num ( src.num ), size ( src.size )
79  {
80  init( src );
81  }
82 
83 
87  void init( const OFSet<T> &src )
88  {
89  items = new T*[size];
90  for( unsigned int i=0 ; i<size ; i++ )
91  {
92  if( i<num )
93  items[i] = new T( *src.items[i] );
94  else
95  items[i] = NULL;
96  }
97  }
98 
99 
102  virtual ~OFSet()
103  {
104  for( unsigned int i=0 ; i<num ; i++ )
105  delete items[i];
106  delete[] items;
107  }
108 
109 
114  const OFSet<T> &operator=( const OFSet<T> &src )
115  {
116  if( this == &src )
117  return( *this );
118 
119  unsigned int i;
120 
121  for( i=0 ; i<num ; i++ )
122  delete items[i];
123  delete[] items;
124 
125  num = src.num;
126  size = src.size;
127  items = new T*[size];
128  for( i=0 ; i<size ; i++ )
129  {
130  if( i<num )
131  items[i] = new T( *src.items[i] );
132  else
133  items[i] = NULL;
134  }
135 
136  return( *this );
137  }
138 
139 
140 
148  virtual T &operator[]( unsigned int i ) const
149  {
150  if( i<num )
151  return( *items[i] );
152  else
153  {
154  T *obj = new T();
155  T &ret = *obj;
156  return( ret );
157  }
158  }
159 
160 
165  virtual void Resize( unsigned int newSize )
166  {
167  unsigned int i;
168 
169  if( newSize >= num )
170  {
171  T **tmp = new T*[newSize];
172 
173  for( i=0 ; i<newSize ; i++ )
174  {
175  if( i<num )
176  tmp[i] = items[i];
177  else
178  tmp[i] = NULL;
179  }
180 
181  delete[] items;
182  items = tmp;
183 
184  size = newSize;
185  }
186  }
187 
188 
191  virtual void Clear()
192  {
193  for( unsigned int i=0 ; i<num ; i++ )
194  {
195  delete items[i];
196  items[i] = NULL;
197  }
198 
199  num = 0;
200  }
201 
202 
206  virtual OFBool IsEmpty() const
207  {
208  if( num == 0 )
209  return( OFTrue );
210  else
211  return( OFFalse );
212  }
213 
214 
218  virtual unsigned int NumberOfElements() const
219  {
220  return( num );
221  }
222 
223 
227  virtual void Insert( const T &item ) = 0;
228 
229 
233  virtual void Remove( const T &item ) = 0;
234 
235 
239  virtual void RemoveByIndex( unsigned int idx ) = 0;
240 
241 
248  virtual T *Find( const T &item ) const = 0;
249 
250 
255  virtual OFBool Contains( const T &item ) const = 0;
256 };
257 
258 
259 #endif
260 
261 /*
262 ** CVS/RCS Log:
263 ** $Log: ofset.h,v $
264 ** Revision 1.13 2010-10-14 13:15:50 joergr
265 ** Updated copyright header. Added reference to COPYRIGHT file.
266 **
267 ** Revision 1.12 2010-10-05 08:36:51 joergr
268 ** Fixed various Doxygen API documentation issues.
269 **
270 ** Revision 1.11 2010-03-01 09:08:51 uli
271 ** Removed some unnecessary include directives in the headers.
272 **
273 ** Revision 1.10 2005-12-08 16:06:01 meichel
274 ** Changed include path schema for all DCMTK header files
275 **
276 ** Revision 1.9 2005/07/01 10:01:50 wilkens
277 ** Modified a couple of "delete" statements to "delete[]" in order to get rid of
278 ** valgrind's "Mismatched free() / delete / delete []" error messages.
279 **
280 ** Revision 1.8 2002/12/18 09:06:41 wilkens
281 ** Had forgotten to delete some superfluous code. Did it now.
282 **
283 ** Revision 1.7 2002/12/17 17:01:34 wilkens
284 ** Modified code again to keep Sun CC 2.0.1 happy on Solaris 2.5.1 (template
285 ** errors).
286 **
287 ** Revision 1.6 2002/12/16 10:40:25 wilkens
288 ** Removed superfluous implementation files and modified header and make files.
289 **
290 ** Revision 1.5 2002/12/13 12:26:51 wilkens
291 ** Modified code to keep Sun CC 2.0.1 happy on Solaris 2.5.1 (template errors).
292 **
293 ** Revision 1.4 2002/12/09 13:07:03 joergr
294 ** Renamed parameter to avoid name clash with global function index().
295 ** Initialize member variables in the member initialization list.
296 **
297 ** Revision 1.3 2002/07/09 18:29:46 wilkens
298 ** Added some more functionality.
299 **
300 **
301 */


Generated on Thu Dec 20 2012 for OFFIS DCMTK Version 3.6.0 by Doxygen 1.8.2