ofstd/include/dcmtk/ofstd/ofmap.h

00001 /*
00002  *
00003  *  Copyright (C) 2009-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:  Uli Schlachter
00017  *
00018  *  Purpose: Defines a template map class with interfaces similar to the C++
00019  *           Standard
00020  *
00021  *  Last Update:      $Author: uli $
00022  *  Update Date:      $Date: 2010-11-08 09:49:03 $
00023  *  CVS/RCS Revision: $Revision: 1.6 $
00024  *  Status:           $State: Exp $
00025  *
00026  *  CVS/RCS Log at end of file
00027  *
00028  */
00029 
00030 #ifndef OFMAP_H
00031 #define OFMAP_H
00032 
00033 #include "dcmtk/config/osconfig.h"    /* make sure OS specific configuration is included first */
00034 
00035 #if defined(HAVE_STL) || defined(HAVE_STL_MAP)
00036 // it is possible to use the standard template library map class since the
00037 // interface is nearly identical.
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 ** CVS/RCS Log:
00269 ** $Log: ofmap.h,v $
00270 ** Revision 1.6  2010-11-08 09:49:03  uli
00271 ** Fixed even more gcc warnings caused by additional compiler flags.
00272 **
00273 ** Revision 1.5  2010-10-14 13:15:50  joergr
00274 ** Updated copyright header. Added reference to COPYRIGHT file.
00275 **
00276 ** Revision 1.4  2010-10-08 12:27:07  uli
00277 ** Fixed all doxygen warnings for OFPair and OFauto_ptr.
00278 **
00279 ** Revision 1.3  2010-08-06 08:41:36  uli
00280 ** Fixed some more compiler warnings.
00281 **
00282 ** Revision 1.2  2009-09-29 13:59:34  uli
00283 ** Fix an invalid iterator use in OFMap. A iterator was used after it was passed
00284 ** to erase().
00285 ** Add a test case which verifies some of OFMap's implementation.
00286 **
00287 ** Revision 1.1  2009-08-19 10:52:08  joergr
00288 ** Added new class OFMap required for upcoming module "oflog".
00289 **
00290 **
00291 */


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