00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00022 #ifndef __LRT_MAP__
00023 #define __LRT_MAP__
00024
00025 #include "rtiterator.h"
00026 #include "rtlist.h"
00027 #include "rtstring.h"
00028
00029 namespace lrt {
00030
00031
00032 template<class K, class V> class Map;
00033
00035 template<class K, class V> class Pair {
00036 public:
00039 Pair() : key(), value() {}
00041 Pair(const K& k, const V& v) : key(k), value(v) {}
00043 inline const K& getKey() const { return key; }
00045 inline const V& getValue() const { return value; }
00047 inline K& getKey() { return key; }
00049 inline V& getValue() { return value; }
00050 private:
00051 friend class Map<K,V>;
00052 K key;
00053 V value;
00054 };
00055
00056
00063 template<class K, class V> class Map {
00064 public:
00065
00069 typedef Iterator<Pair<K,V> > Iterator;
00070
00073 Map();
00076 Map(int (*compareFun)(const K&, const K&));
00078 Map(const Map &);
00079 virtual ~Map() {}
00081 const Map& operator=(const Map &);
00082
00084 bool isSet(const K&) const;
00087 const V& get(const K&) const;
00091 V& get(const K&);
00095 Pair<K,V>& put(const K&, const V&);
00098 void putAll(const Map<K,V>&);
00102 bool remove(const K&);
00105 void remove(Iterator& iter);
00107 void clear();
00108
00109
00112 inline const V& operator[](const K&) const;
00116 inline V& operator[](const K&);
00117
00119 int length() const;
00123 Iterator begin() const;
00127 Iterator end() const;
00131 Iterator begin();
00135 Iterator end();
00136
00138 static Pair<K,V> noValue;
00139
00140 protected:
00142 int (*compareFun)(const K&, const K&);
00143
00144 private:
00145 List< Pair<K,V> > data;
00146
00147 };
00148
00153 template<class V> class StringMap : public Map<String,V>
00154 {
00155 public:
00157 StringMap() : Map<String,V>(&String::compare) {}
00159 StringMap(bool caseSensitive) :
00160 Map<String,V>(&String::compare)
00161 {
00162 if(!caseSensitive) compareFun = &String::compareIgnoreCase;
00163 }
00165 StringMap(const StringMap& sm) : Map<String,V>(sm) {}
00166 virtual ~StringMap() {}
00168 const StringMap& operator=(const StringMap& sm)
00169 { Map<String,V>::operator=(sm); return *this; }
00170
00171 };
00172
00173 }
00174
00175 #include "rtmap.templ.cpp"
00176
00177 #endif