Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members

OneToOneMap.h

Go to the documentation of this file.
00001 #ifndef ONETOONEMAP_H 00002 #define ONETOONEMAP_H 00003 00004 #include <map> 00005 00006 using namespace std; 00007 00008 // Your basic 1:1 map container... 00009 00010 template<typename T1, typename T2> 00011 class OneToOneMap<T1, T2> 00012 { 00013 public: 00014 OneToOneMap(); 00015 virtual ~OneToOneMap(); 00016 00017 // Overwrite on conflict with either key, don't barf. 00018 void insert(const T1 & aT1, const T2 & aT2) 00019 throw (exception); 00020 00021 // Ensures the bimap contains no record whose T1 value is aT1. 00022 void clearByT1(const T1 & aT1) 00023 throw (exception); 00024 00025 // Ensures the bimap contains no record whose T1 value is aT2. 00026 void clearByT2(const T2 & aT2) 00027 throw (exception); 00028 00029 const map<T1, T2> & getT1T2Map() const; 00030 const map<T2, T1> & getT2T1Map() const; 00031 00032 private: 00033 map<T1, T2> _map12; 00034 map<T2, T1> _map21; 00035 }; 00036 00037 //=============================================================================== 00038 00039 template<typename T1, typename T2> 00040 OneToOneMap<T1, T2>::OneToOneMap() {} 00041 00042 //=============================================================================== 00043 00044 template<typename T1, typename T2> 00045 OneToOneMap<T1, T2>::~OneToOneMap() {} 00046 00047 //=============================================================================== 00048 00049 template<typename T1, typename T2> 00050 void OneToOneMap<T1, T2>::insert(const T1 & aT1, const T2 & aT2) 00051 throw (exception) 00052 { 00053 map<T1, T2>::iterator pos1 = _map12.find(aT1); 00054 if (pos1 != _map12.end()) 00055 { 00056 _map12.erase(pos1); 00057 } 00058 00059 map<T1, T2>::iterator pos2 = _map21.find(aT2); 00060 if (pos2 != _map21.end()) 00061 { 00062 _map21.erase(pos2); 00063 } 00064 00065 _map12.insert(make_pair(aT1, aT2)); 00066 _map21.insert(make_pair(aT2, aT1)); 00067 } 00068 00069 //=============================================================================== 00070 00071 template<typename T1, typename T2> 00072 void OneToOneMap<T1, T2>::clearByT1(const T1 & aT1) 00073 throw (exception) 00074 { 00075 map<T1, T2>::iterator pos1 = _map12.find(aT1); 00076 if (pos1 == _map12.end()) 00077 { 00078 return; 00079 } 00080 00081 map<T1, T2>::iterator pos2 = _map21.find(pos1->second); 00082 if (pos2 != _map21.end()) 00083 { 00084 _map21.erase(pos2); 00085 } 00086 00087 _map12.erase(pos1); 00088 } 00089 00090 //=============================================================================== 00091 00092 template<typename T1, typename T2> 00093 void OneToOneMap<T1, T2>::clearByT2(const T2 & aT2) 00094 throw (exception) 00095 { 00096 map<T1, T2>::iterator pos2 = _map21.find(aT2); 00097 if (pos2 == _map21.end()) 00098 { 00099 return; 00100 } 00101 00102 map<T1, T2>::iterator pos1 = _map12.find(pos2->second); 00103 if (pos1 != _map12.end()) 00104 { 00105 _map12.erase(pos1); 00106 } 00107 00108 _map21.erase(pos2); 00109 } 00110 00111 //=============================================================================== 00112 00113 template<typename T1, typename T2> 00114 const map<T1, T2> & OneToOneMap<T1, T2>::getT1T2Map() const 00115 { 00116 return _map12; 00117 } 00118 00119 //=============================================================================== 00120 00121 template<typename T1, typename T2> 00122 const map<T2, T1> & OneToOneMap<T1, T2>::getT2T1Map() const 00123 { 00124 return _map21; 00125 } 00126 00127 //=============================================================================== 00128 00129 #endif

Generated on Fri Nov 12 15:15:21 2004 for Borealis by doxygen 1.3.8