00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifndef OFMAP_H
00031 #define OFMAP_H
00032
00033 #include "dcmtk/config/osconfig.h"
00034
00035 #if defined(HAVE_STL) || defined(HAVE_STL_MAP)
00036
00037
00038 #include <map>
00039 #define OFMap std::map
00040 #define OFPair std::pair
00041 #define OFMake_pair std::make_pair
00042 #else
00043
00044 #include "dcmtk/ofstd/oftypes.h"
00045 #include "dcmtk/ofstd/ofcast.h"
00046 #include "dcmtk/ofstd/oflist.h"
00047
00048 #ifndef HAVE_CLASS_TEMPLATE
00049 #error Your C++ compiler cannot handle class templates:
00050 #endif
00051
00052
00055 template<typename K, typename V> class OFPair
00056 {
00057 public:
00058
00060 K first;
00061
00063 V second;
00064
00066 OFPair() : first(), second() { }
00067
00072 OFPair(const K& f, const V& s) : first(f), second(s) { }
00073
00076 OFPair& operator=(const OFPair& other)
00077 {
00078 first = other.first;
00079 second = other.second;
00080 return *this;
00081 }
00082 };
00083
00089 template<typename K, typename V>
00090 OFPair<K, V> OFMake_pair(const K& first, const V& second)
00091 {
00092 return OFPair<K, V>(first, second);
00093 }
00094
00097 template<typename K, typename V> class OFMap
00098 {
00099 public:
00100
00102 typedef OFPair<K, V> value_type;
00103
00104 protected:
00105
00107 OFList<value_type> values_;
00108
00109 public:
00110
00114 typedef OFListIterator(value_type) iterator;
00115
00119 typedef OFListConstIterator(value_type) const_iterator;
00120
00122 OFMap() : values_() { }
00123
00125 OFMap& operator=(const OFMap& other)
00126 {
00127 clear();
00128
00129 for (const_iterator it = other.begin();
00130 it != other.end(); it++)
00131 insert(*it);
00132
00133 return *this;
00134 }
00135
00142 V& operator[](const K& key)
00143 {
00144 iterator it = find(key);
00145 if (it == end())
00146 {
00147 it = insert(value_type(key, V())).first;
00148 }
00149
00150 return it->second;
00151 }
00152
00156 iterator begin() { return values_.begin(); }
00157
00161 iterator end() { return values_.end(); }
00162
00166 const_iterator begin() const { return values_.begin(); }
00167
00171 const_iterator end() const { return values_.end(); }
00172
00177 iterator find(const K& key)
00178 {
00179 iterator it = begin();
00180 while (it != end())
00181 {
00182 if (it->first == key)
00183 break;
00184 it++;
00185 }
00186
00187 return it;
00188 }
00189
00194 const_iterator find(const K& key) const
00195 {
00196 const_iterator it = begin();
00197 while (it != end())
00198 {
00199 if (it->first == key)
00200 break;
00201 it++;
00202 }
00203
00204 return it;
00205 }
00206
00210 size_t size() const { return values_.size(); }
00211
00213 void clear() { values_.clear(); }
00214
00219 void erase(const iterator& first, const iterator& last)
00220 {
00221 values_.erase(first, last);
00222 }
00223
00227 void erase(const iterator& elem)
00228 {
00229 values_.erase(elem);
00230 }
00231
00235 int erase(const K& key)
00236 {
00237 iterator it = find(key);
00238 if (it == end())
00239 return 0;
00240
00241 values_.erase(it);
00242 return 1;
00243 }
00244
00252 OFPair<iterator, bool> insert(const value_type& val)
00253 {
00254 OFListIterator(value_type) it = find(val.first);
00255 if (it != end())
00256 return OFMake_pair(it, false);
00257
00258 it = values_.insert(values_.end(), val);
00259 return OFMake_pair(it, true);
00260 }
00261 };
00262
00263 #endif
00264 #endif
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291