00001 #ifndef DV_STRINGMAP_H 00002 #define DV_STRINGMAP_H 00003 // $Id: stringmap.h,v 1.3 2008/05/01 11:46:30 dvermeir Exp $ 00004 00005 #include <string> 00006 #include <map> 00007 #include <dvutil/iomanip.h> 00008 #include <dvutil/convert.h> 00009 #include <dvutil/debug_dump.h> 00010 00011 namespace Dv { 00012 00013 /** A utility class wrapper around @a std::map<std::string,std::string> */ 00014 template<typename T> 00015 class StringMap { 00016 public: 00017 /** Implementation type */ 00018 typedef std::map<std::string,T> Map; 00019 /** Iterator type for StringMap */ 00020 typedef typename Map::const_iterator const_iterator; 00021 /** @return iterator refering to first @a <key,value> pair in map. */ 00022 const_iterator begin() const { return map_.begin(); } 00023 /** @return iterator pointing past the last @a <key,value> pair in map. */ 00024 const_iterator end() const { return map_.end(); } 00025 /** @return number of @a <key,value> pairs in the map. */ 00026 size_t size() const { return map_.size(); } 00027 00028 /** Find the value associated with a key. 00029 * @param key to find associated value for. 00030 * @return the value associated with a key 00031 * @return 0 if no value is associated with the key 00032 */ 00033 const T* find(const std::string& key) const throw () { 00034 const_iterator i = map_.find(key); 00035 if (i == end()) 00036 return 0; 00037 else 00038 return &(i->second); 00039 } 00040 00041 /** Find the value associated with a key @a k such 00042 * that 00043 * @code 00044 * k = max { i | map_[i] defined and i <= key } 00045 * @endcode 00046 * In words: find the value associated with the largest 00047 * key @a k in the map such that @a k<=key. 00048 * @param key to find 00049 */ 00050 const T* find_glb(const std::string& key) const throw () { 00051 const_iterator i = map_.lower_bound(key); 00052 if ( (i!=end()) && (key == i->first) ) 00053 return &(i->second); 00054 if (i == begin()) 00055 return 0; 00056 --i; 00057 return &(i->second); 00058 } 00059 00060 /** Set the value for a key. 00061 * @param key to associate the value with 00062 * @param value associated with key 00063 */ 00064 StringMap& set(const std::string& key, const T& value) { 00065 map_[key] = value; 00066 return *this; 00067 } 00068 /** Access the value associated with a key. Note that 00069 * if, before, there is no value associated with the key, 00070 * after calling this function, the key will be associated 00071 * with the empty string. 00072 * @param key to find associated value for 00073 * @return associated value (possibly the empty string) 00074 */ 00075 T& operator[](const std::string& key) { 00076 return map_[key]; 00077 } 00078 00079 /** Remove a @a <key,value> pair from the map. 00080 * @param key to remove 00081 */ 00082 void erase(const std::string& key) { 00083 map_.erase(key); 00084 } 00085 00086 /** Remove all @a <key,value> pairs from the map. */ 00087 void clear() { 00088 map_.clear(); 00089 } 00090 00091 /** Print a map to a stream. Each line has the 00092 * form 00093 * @code 00094 * key: value 00095 * @endcode 00096 * @param os stream to print to 00097 * @param map to print 00098 * @return os 00099 */ 00100 friend std::ostream& operator<<( std::ostream& os, const StringMap<T>& map) { 00101 for (typename Dv::StringMap<T>::const_iterator m = map.begin(); m != map.end(); ++m) 00102 os << m->first << ": " << m->second << "\n"; 00103 return os; 00104 } 00105 00106 typedef Dv::iomanip<StringMap<T> > IoManip; 00107 00108 IoManip 00109 summary() const { 00110 return IoManip(print_summary, *this); 00111 } 00112 00113 static std::ostream& print_summary(std::ostream& os, const StringMap<T>& map) { 00114 for (typename Dv::StringMap<T>::const_iterator m = map.begin(); m != map.end(); ++m) { 00115 std::string s = Dv::tostring(m->second); 00116 os << m->first << ": " << debug_dump(s.data(), (s.size() > 20 ? 20 : s.size()) ) 00117 << " (size = " << s.size() << "bytes )\n"; 00118 } 00119 return os; 00120 } 00121 00122 protected: 00123 /** Actual map. */ 00124 std::map<std::string,T> map_; 00125 }; 00126 00127 typedef StringMap<std::string> StringStringMap; 00128 } 00129 #endif
dvutil-1.0.10 | [ 5 December, 2009] |