OFFIS DCMTK
Version 3.6.0
Main Page
Related Pages
Classes
Files
File List
File Members
ofstd
include
dcmtk
ofstd
ofvector.h
1
/*
2
*
3
* Copyright (C) 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: Uli Schlachter
17
*
18
* Purpose: Defines a template vector class based on the STL vector class
19
*
20
* Last Update: $Author: joergr $
21
* Update Date: $Date: 2010-10-14 13:15:51 $
22
* CVS/RCS Revision: $Revision: 1.3 $
23
* Status: $State: Exp $
24
*
25
* CVS/RCS Log at end of file
26
*
27
*/
28
29
#ifndef OFVECTOR_H
30
#define OFVECTOR_H
31
32
#include "dcmtk/config/osconfig.h"
/* make sure OS specific configuration is included first */
33
34
#ifndef HAVE_CLASS_TEMPLATE
35
#error Your C++ compiler cannot handle class templates:
36
#endif
37
38
#if defined(HAVE_STL) || defined(HAVE_STL_VECTOR)
39
40
// Use the standard template library (STL) vector class.
41
#include <vector>
42
43
#ifdef HAVE_STD_NAMESPACE
44
#define OFVector std::vector
45
#else
46
#define OFVector vector
47
#endif
48
49
#else
50
51
#define INCLUDE_CASSERT
/* for assert() */
52
#define INCLUDE_CSTDLIB
/* for NULL */
53
#include "dcmtk/ofstd/ofstdinc.h"
54
#include "dcmtk/ofstd/oftypes.h"
/* for OFBool */
55
60
template
<
typename
T>
61
class
OFVector
62
{
63
public
:
65
typedef
T
value_type
;
67
typedef
size_t
size_type
;
69
typedef
T*
iterator
;
71
typedef
const
T*
const_iterator
;
72
73
protected
:
74
76
T*
values_
;
77
79
size_type
allocated_
;
80
84
size_type
size_
;
85
86
public
:
87
89
OFVector
() :
values_
(NULL),
allocated_
(0),
size_
(0)
90
{
91
reserve
(0);
92
}
93
97
OFVector
(
const
OFVector
& other) :
values_
(NULL),
allocated_
(0),
size_
(0)
98
{
99
reserve
(other.
size
());
100
for
(
const_iterator
it = other.
begin
(); it != other.
end
(); ++it)
101
push_back
(*it);
102
}
103
108
explicit
OFVector
(
size_type
n,
const
T& v = T()) :
values_
(NULL),
allocated_
(0),
size_
(0)
109
{
110
if
(n > 0)
111
resize
(n, v);
112
else
113
// Make sure that values_ never is a NULL pointer
114
reserve
(0);
115
}
116
121
OFVector
(
const_iterator
from,
const_iterator
to) :
values_
(NULL),
allocated_
(0),
size_
(0)
122
{
123
reserve
(to - from);
124
while
(from != to)
125
push_back
(*(from++));
126
}
127
130
~OFVector
()
131
{
132
delete
[]
values_
;
133
}
134
140
OFVector
&
operator=
(
const
OFVector
& other)
141
{
142
clear
();
143
reserve
(other.
size
());
144
for
(
const_iterator
it = other.
begin
(); it != other.
end
(); ++it)
145
push_back
(*it);
146
return
*
this
;
147
}
148
153
void
swap
(
OFVector
& other)
154
{
155
T* tmp_val =
values_
;
156
size_type
tmp_all =
allocated_
;
157
size_type
tmp_size =
size_
;
158
159
values_
= other.
values_
;
160
allocated_
= other.
allocated_
;
161
size_
= other.
size_
;
162
163
other.
values_
= tmp_val;
164
other.
allocated_
= tmp_all;
165
other.
size_
= tmp_size;
166
}
167
171
iterator
begin
() {
return
&
values_
[0]; }
172
176
const_iterator
begin
()
const
{
return
&
values_
[0]; }
177
181
iterator
end
() {
return
&
values_
[
size_
]; }
182
186
const_iterator
end
()
const
{
return
&
values_
[
size_
]; }
187
191
size_type
size
()
const
{
return
size_
; }
192
196
OFBool
empty
()
const
{
return
size_
== 0; }
197
201
void
clear
()
202
{
203
delete
[]
values_
;
204
values_
= NULL;
205
size_
= 0;
206
allocated_
= 0;
207
// We must never have values_ == NULL
208
reserve
(0);
209
}
210
216
void
erase
(
iterator
it)
217
{
218
size_type
idx = it -
begin
();
219
for
(
size_type
i = idx + 1; i <
size_
; i++) {
220
values_
[i - 1] =
values_
[i];
221
}
222
size_--;
223
}
224
232
iterator
insert
(
iterator
it,
const
T& v)
233
{
234
size_type
idx = it -
begin
();
235
reserve
(
size_
+ 1);
236
if
(idx <
size_
)
237
for
(
size_type
i =
size_
; i > idx; i--) {
238
values_
[i] =
values_
[i - 1];
239
}
240
values_
[idx] = v;
241
size_
++;
242
return
&
values_
[idx];
243
}
244
252
template
<
class
InputIterator>
253
void
insert
(
iterator
it, InputIterator from, InputIterator to)
254
{
255
while
(from != to)
256
{
257
it =
insert
(it, *from);
258
it++;
259
from++;
260
}
261
}
262
266
void
push_back
(
const
T& v)
267
{
268
insert
(
end
(), v);
269
}
270
273
void
pop_back
()
274
{
275
erase
(
end
() - 1);
276
}
277
283
T&
operator[]
(
size_type
i)
284
{
285
return
values_
[i];
286
}
287
293
const
T&
operator[]
(
size_type
i)
const
294
{
295
return
values_
[i];
296
}
297
302
T&
at
(
size_type
i)
303
{
304
assert(i <
size_
);
305
return
(*
this
)[i];
306
}
307
312
const
T&
at
(
size_type
i)
const
313
{
314
assert(i <
size_
);
315
return
(*
this
)[i];
316
}
317
322
void
resize
(
size_type
n, T v = T())
323
{
324
if
(n >
size_
)
325
{
326
reserve
(n);
327
// Set up the new elements
328
for
(
size_t
i =
size_
; i < n; i++)
329
values_
[i] = v;
330
}
331
size_
= n;
332
}
333
339
void
reserve
(
size_type
n)
340
{
341
T* old_values =
values_
;
342
T* new_values;
343
344
if
(n == 0)
345
n = 1;
346
if
(n <=
allocated_
)
347
return
;
348
349
// While we are at it, let's reserve some extra space
350
n += 10;
351
352
new_values =
new
T[n];
353
if
(old_values)
354
{
355
for
(
size_type
i = 0; i <
size_
; i++)
356
new_values[i] = old_values[i];
357
delete
[] old_values;
358
}
359
360
values_
= new_values;
361
allocated_
= n;
362
}
363
};
364
365
#endif
366
367
#endif
368
369
370
/*
371
* CVS/RCS Log:
372
* $Log: ofvector.h,v $
373
* Revision 1.3 2010-10-14 13:15:51 joergr
374
* Updated copyright header. Added reference to COPYRIGHT file.
375
*
376
* Revision 1.2 2010-10-08 13:25:33 uli
377
* Implement OFVector.
378
*
379
* Revision 1.1 2010-04-26 11:57:35 joergr
380
* Added initial definitions for using the STL vector class. Please note that
381
* there is currently no alternative implementation to this standard class.
382
*
383
*
384
*/
Generated on Thu Dec 20 2012 for
OFFIS DCMTK
Version 3.6.0 by
Doxygen
1.8.2