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 #ifndef __bimap__
00026 #define __bimap__
00027
00028 #include <map>
00029 #include <iterator>
00030 #include <utility>
00031 using namespace std;
00032
00039 template <typename T1, typename T2>
00040 class bimap {
00041 public:
00042 bimap() {}
00043 bimap(const T1 tbl1[], const T2 tbl2[], int n);
00044 virtual ~bimap() {}
00045
00047 const T2 operator[] (const T1 key) { return fT1Map[key]; }
00049 const T1 operator[] (const T2 key) { return fT2Map[key]; }
00051 long size() { return fT1Map.size(); }
00052
00054 bimap& add (const T1& v1, const T2& v2)
00055 { fT1Map[v1]=v2; fT2Map[v2]=v1; return *this; }
00056
00057 private:
00058 map<T1, T2> fT1Map;
00059 map<T2, T1> fT2Map;
00060 };
00061
00062 template <typename T1, typename T2>
00063 bimap<T1, T2>::bimap(const T1 tbl1[], const T2 tbl2[], int n)
00064 {
00065 for (int i=0; i<n; i++) {
00066 add(tbl1[i], tbl2[i]);
00067 }
00068 }
00069
00073 template <typename T1, typename T2>
00074 class pairmap : public multimap<T1,T2> {
00075 public:
00076 pairmap() {}
00077 virtual ~pairmap() {}
00078
00079 #ifdef __MWERKS__ // this is for Code Warrior version 8.3
00080 multimap<T1,T2>::iterator insert (const pair<T1,T2>& x)
00081 { return exist (x) ? end() : multimap<T1,T2>::insert(x); }
00082
00083 multimap<T1,T2>::iterator insert (multimap<T1,T2>::iterator position, const pair<T1,T2>& x)
00084 { return exist (x) ? end() : multimap<T1,T2>::insert(position, x); }
00085 #else
00086 typedef typename pairmap<T1,T2>::iterator iterator;
00087 typedef typename pairmap<T1,T2>::const_iterator const_iterator;
00088
00089 iterator insert (const pair<T1,T2>& x)
00090 { return exist (x) ? end() : multimap<T1,T2>::insert(x); }
00091
00092 iterator insert (iterator position, const pair<T1,T2>& x)
00093 { return exist (x) ? end() : multimap<T1,T2>::insert(position, x); }
00094 #endif
00095
00096 private:
00097
00098 bool exist (const pair<T1,T2>& x) const {
00099 for (const_iterator i=find(x.first); (i!=end()) && (i->first==x.first); i++)
00100 if (i->second == x.second) return true;
00101 return false;
00102 }
00103
00104 };
00105
00106
00107 #endif
00108