OFFIS DCMTK  Version 3.6.0
ofmap.h
1 /*
2  *
3  * Copyright (C) 2009-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 map class with interfaces similar to the C++
19  * Standard
20  *
21  * Last Update: $Author: uli $
22  * Update Date: $Date: 2010-11-08 09:49:03 $
23  * CVS/RCS Revision: $Revision: 1.6 $
24  * Status: $State: Exp $
25  *
26  * CVS/RCS Log at end of file
27  *
28  */
29 
30 #ifndef OFMAP_H
31 #define OFMAP_H
32 
33 #include "dcmtk/config/osconfig.h" /* make sure OS specific configuration is included first */
34 
35 #if defined(HAVE_STL) || defined(HAVE_STL_MAP)
36 // it is possible to use the standard template library map class since the
37 // interface is nearly identical.
38 #include <map>
39 #define OFMap std::map
40 #define OFPair std::pair
41 #define OFMake_pair std::make_pair
42 #else
43 
44 #include "dcmtk/ofstd/oftypes.h"
45 #include "dcmtk/ofstd/ofcast.h"
46 #include "dcmtk/ofstd/oflist.h"
47 
48 #ifndef HAVE_CLASS_TEMPLATE
49 #error Your C++ compiler cannot handle class templates:
50 #endif
51 
52 
55 template<typename K, typename V> class OFPair
56 {
57 public:
58 
60  K first;
61 
63  V second;
64 
66  OFPair() : first(), second() { }
67 
72  OFPair(const K& f, const V& s) : first(f), second(s) { }
73 
76  OFPair& operator=(const OFPair& other)
77  {
78  first = other.first;
79  second = other.second;
80  return *this;
81  }
82 };
83 
89 template<typename K, typename V>
90 OFPair<K, V> OFMake_pair(const K& first, const V& second)
91 {
92  return OFPair<K, V>(first, second);
93 }
94 
97 template<typename K, typename V> class OFMap
98 {
99 public:
100 
103 
104 protected:
105 
108 
109 public:
110 
114  typedef OFListIterator(value_type) iterator;
115 
119  typedef OFListConstIterator(value_type) const_iterator;
120 
122  OFMap() : values_() { }
123 
125  OFMap& operator=(const OFMap& other)
126  {
127  clear();
128 
129  for (const_iterator it = other.begin();
130  it != other.end(); it++)
131  insert(*it);
132 
133  return *this;
134  }
135 
142  V& operator[](const K& key)
143  {
144  iterator it = find(key);
145  if (it == end())
146  {
147  it = insert(value_type(key, V())).first;
148  }
149 
150  return it->second;
151  }
152 
156  iterator begin() { return values_.begin(); }
157 
161  iterator end() { return values_.end(); }
162 
166  const_iterator begin() const { return values_.begin(); }
167 
171  const_iterator end() const { return values_.end(); }
172 
177  iterator find(const K& key)
178  {
179  iterator it = begin();
180  while (it != end())
181  {
182  if (it->first == key)
183  break;
184  it++;
185  }
186 
187  return it;
188  }
189 
194  const_iterator find(const K& key) const
195  {
196  const_iterator it = begin();
197  while (it != end())
198  {
199  if (it->first == key)
200  break;
201  it++;
202  }
203 
204  return it;
205  }
206 
210  size_t size() const { return values_.size(); }
211 
213  void clear() { values_.clear(); }
214 
219  void erase(const iterator& first, const iterator& last)
220  {
221  values_.erase(first, last);
222  }
223 
227  void erase(const iterator& elem)
228  {
229  values_.erase(elem);
230  }
231 
235  int erase(const K& key)
236  {
237  iterator it = find(key);
238  if (it == end())
239  return 0;
240 
241  values_.erase(it);
242  return 1;
243  }
244 
253  {
254  OFListIterator(value_type) it = find(val.first);
255  if (it != end())
256  return OFMake_pair(it, false);
257 
258  it = values_.insert(values_.end(), val);
259  return OFMake_pair(it, true);
260  }
261 };
262 
263 #endif
264 #endif
265 
266 
267 /*
268 ** CVS/RCS Log:
269 ** $Log: ofmap.h,v $
270 ** Revision 1.6 2010-11-08 09:49:03 uli
271 ** Fixed even more gcc warnings caused by additional compiler flags.
272 **
273 ** Revision 1.5 2010-10-14 13:15:50 joergr
274 ** Updated copyright header. Added reference to COPYRIGHT file.
275 **
276 ** Revision 1.4 2010-10-08 12:27:07 uli
277 ** Fixed all doxygen warnings for OFPair and OFauto_ptr.
278 **
279 ** Revision 1.3 2010-08-06 08:41:36 uli
280 ** Fixed some more compiler warnings.
281 **
282 ** Revision 1.2 2009-09-29 13:59:34 uli
283 ** Fix an invalid iterator use in OFMap. A iterator was used after it was passed
284 ** to erase().
285 ** Add a test case which verifies some of OFMap's implementation.
286 **
287 ** Revision 1.1 2009-08-19 10:52:08 joergr
288 ** Added new class OFMap required for upcoming module "oflog".
289 **
290 **
291 */


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