cgma
|
00001 //------------------------------------------------------------------------- 00002 // Filename : OrderedMap.hpp 00003 // 00004 // Purpose : An std::map that keeps order that things were inserted 00005 // into it. 00006 // 00007 // Creator : Matt Staten 00008 // 00009 // Creation Date : 01/27/2006 00010 // 00011 // Owner : Matt Staten 00012 //------------------------------------------------------------------------- 00013 00014 #ifndef OrderedMap_HPP 00015 #define OrderedMap_HPP 00016 00017 #include <map> 00018 #include "DLIList.hpp" 00019 00020 template<class X, class Y> class OrderedMap 00021 { 00022 private: 00023 DLIList<X> mList1; 00024 std::map<X,Y> mMap; 00025 00026 public: 00027 00028 OrderedMap( void ) {}; 00029 00030 bool find( X key, Y *data = NULL ) const 00031 { 00032 typename std::map<X,Y>::const_iterator iter = mMap.find( key ); 00033 if ( iter == mMap.end() ) 00034 { 00035 return CUBIT_FALSE; 00036 } 00037 if ( data ) 00038 { 00039 *data = iter->second; 00040 } 00041 return CUBIT_TRUE; 00042 }; 00043 00044 // Return true if the new pair was inserted. 00045 // Return false if this key is already being used. 00046 bool insert( X new_key, Y new_data ) 00047 { 00048 if ( !find( new_key ) ) 00049 { 00050 mList1.append( new_key ); 00051 mMap.insert( std::pair<X,Y>( new_key, new_data ) ); 00052 return CUBIT_TRUE; 00053 } 00054 return CUBIT_FALSE; 00055 }; 00056 00057 // This can be inefficient 00058 void erase( X key_to_erase ) 00059 { 00060 typename std::map<X,Y>::iterator iter = mMap.find( key_to_erase ); 00061 if ( iter != mMap.end() ) 00062 { 00063 mMap.erase( iter ); 00064 mList1.remove_all_with_value( key_to_erase ); 00065 } 00066 }; 00067 00068 void erase_iter( int iter_to_erase ) 00069 { 00070 X key_to_erase = mList1[iter_to_erase]; 00071 typename std::map<X,Y>::iterator iter = mMap.find( key_to_erase ); 00072 if ( iter != mMap.end() ) 00073 { 00074 mMap.erase( iter ); 00075 mList1.remove_all_with_value( key_to_erase ); 00076 } 00077 }; 00078 00079 X first( int iter ) const 00080 { 00081 return mList1[iter]; 00082 } 00083 Y second( int iter ) const 00084 { 00085 Y data; 00086 bool stat = find( mList1[iter], &data ); 00087 assert(stat); 00088 return data; 00089 } 00090 00091 bool empty( void ) const { return ( mList1.size() == 0 ? CUBIT_TRUE : CUBIT_FALSE ); }; 00092 int size( void ) const { return mList1.size(); }; 00093 00094 void clean_out( void ) 00095 { 00096 mList1.clean_out(); 00097 mMap.clear(); 00098 }; 00099 00100 OrderedMap<X,Y>& operator=(const OrderedMap<X,Y>&from) 00101 { 00102 clean_out(); 00103 for ( int i = 0; i < from.size(); i++ ) 00104 { 00105 X key = from.first( i ); 00106 Y data = from.second( i ); 00107 this->insert( key, data ); 00108 } 00109 return *this; 00110 }; 00111 00112 Y operator[]( X key ) const 00113 { 00114 Y data; 00115 bool stat = find( key, &data ); 00116 //assert(stat); 00117 if(!stat) 00118 throw std::invalid_argument("The key specified was not found"); 00119 return data; 00120 }; 00121 }; 00122 00123 #endif // OrderedMap_HPP