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
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #ifndef KEY_MAPPING_H__
00040 #define KEY_MAPPING_H__
00041
00042 #include <map>
00043
00044 using std::map;
00045 using std::pair;
00046
00047 namespace ariba {
00048 namespace utility {
00049
00054 template<class T>
00055 class KeyMapping {
00056 private:
00057 typedef map<T,unsigned long> KeyMap;
00058 typedef map<unsigned long, KeyMap> NetworkKeyMap;
00059
00060 typedef typename KeyMap::iterator KeyMapIterator;
00061 typedef typename NetworkKeyMap::iterator NetworkKeyMapIterator;
00062
00063 NetworkKeyMap networkKeyMap;
00064
00065 inline unsigned long nextid(){
00066 return rand()+time(0);
00067 }
00068
00069 public:
00070 inline KeyMapping(){
00071 srand( time(NULL) );
00072 }
00073
00074 inline ~KeyMapping(){
00075 }
00076
00077 inline bool exists(unsigned long network, T item){
00078
00079 NetworkKeyMapIterator i = networkKeyMap.find( network );
00080 if( i == networkKeyMap.end()) return false;
00081
00082 KeyMapIterator k = i->second.find( item );
00083 return ( k != i->second.end() );
00084 }
00085
00086 inline unsigned long get(unsigned long network, T item){
00087 assert( exists(network, item) );
00088
00089 NetworkKeyMapIterator i = networkKeyMap.find( network );
00090 KeyMapIterator k = i->second.find( item );
00091
00092 return k->second;
00093 }
00094
00095 inline unsigned long insert(unsigned long network, T item){
00096
00097 KeyMap* keyMap = NULL;
00098
00099
00100
00101
00102 NetworkKeyMapIterator i = networkKeyMap.find( network );
00103
00104 if( i == networkKeyMap.end() ){
00105 pair<NetworkKeyMapIterator, bool> ret =
00106 networkKeyMap.insert( make_pair(network,KeyMap()) );
00107
00108 keyMap = &(ret.first->second);
00109 } else {
00110
00111 keyMap = &(i->second);
00112 }
00113
00114 unsigned long key = nextid();
00115 keyMap->insert( make_pair( item, key) );
00116
00117 assert( get(network,item) == key );
00118 return key;
00119 }
00120
00121 inline void remove(unsigned long network, T item){
00122 NetworkKeyMapIterator i = networkKeyMap.find( network );
00123 if( i == networkKeyMap.end() ) return;
00124
00125 i->second.erase( item );
00126 }
00127
00128 };
00129
00130 }}
00131
00132 #endif // KEY_MAPPING_H__
00133