llmap.h

Go to the documentation of this file.
00001 
00032 #ifndef LL_LLMAP_H
00033 #define LL_LLMAP_H
00034 
00035 // llmap uses the fast stl library code in a manner consistant with LLSkipMap, et. al. 
00036 
00037 template<class INDEX_TYPE, class MAPPED_TYPE> class LLMap
00038 {
00039 private:
00040         typedef typename std::map<INDEX_TYPE, MAPPED_TYPE> stl_map_t;
00041         typedef typename stl_map_t::iterator stl_iter_t;
00042         typedef typename stl_map_t::value_type stl_value_t;
00043         
00044         stl_map_t mStlMap;
00045         stl_iter_t mCurIter;    // *iterator = pair<const INDEX_TYPE, MAPPED_TYPE>
00046         MAPPED_TYPE dummy_data;
00047         INDEX_TYPE dummy_index;
00048         
00049 public:
00050         LLMap() : mStlMap()
00051         {
00052                 memset((void*)(&dummy_data),  0x0, sizeof(MAPPED_TYPE));
00053                 memset((void*)(&dummy_index), 0x0, sizeof(INDEX_TYPE));
00054                 mCurIter = mStlMap.begin();
00055         }
00056         ~LLMap()
00057         {
00058                 mStlMap.clear();
00059         }
00060 
00061         // use these functions to itterate through a list
00062         void resetMap()
00063         {
00064                 mCurIter = mStlMap.begin();
00065         }
00066 
00067         // get the current data and bump mCurrentp
00068         // This is kind of screwy since it returns a reference;
00069         //   We have to have a dummy value for when we reach the end
00070         //   or in case we have an empty list. Presumably, this value
00071         //   will initialize to some NULL value that will end the iterator.
00072         // We really shouldn't be using getNextData() or getNextKey() anyway...
00073         MAPPED_TYPE &getNextData()
00074         {
00075                 if (mCurIter == mStlMap.end())
00076                 {
00077                         return dummy_data;
00078                 }
00079                 else
00080                 {
00081                         return (*mCurIter++).second;
00082                 }
00083         }
00084 
00085         const INDEX_TYPE &getNextKey()
00086         {
00087                 if (mCurIter == mStlMap.end())
00088                 {
00089                         return dummy_index;
00090                 }
00091                 else
00092                 {
00093                         return (*mCurIter++).first;
00094                 }
00095         }
00096 
00097         MAPPED_TYPE &getFirstData()
00098         {
00099                 resetMap();
00100                 return getNextData();
00101         }
00102 
00103         const INDEX_TYPE &getFirstKey()
00104         {
00105                 resetMap();
00106                 return getNextKey();
00107         }
00108 
00109         S32 getLength()
00110         {
00111                 return mStlMap.size();
00112         }
00113 
00114         void addData(const INDEX_TYPE &index, MAPPED_TYPE pointed_to)
00115         {
00116                 mStlMap.insert(stl_value_t(index, pointed_to));
00117         }
00118 
00119         void addData(const INDEX_TYPE &index)
00120         {
00121                 mStlMap.insert(stl_value_t(index, dummy_data));
00122         }
00123 
00124         // if index doesn't exist, then insert a new node and return it
00125         MAPPED_TYPE &getData(const INDEX_TYPE &index)
00126         {
00127                 std::pair<stl_iter_t, bool> res;
00128                 res = mStlMap.insert(stl_value_t(index, dummy_data));
00129                 return res.first->second;
00130         }
00131 
00132         // if index doesn't exist, then insert a new node, return it, and set b_new_entry to true
00133         MAPPED_TYPE &getData(const INDEX_TYPE &index, BOOL &b_new_entry)
00134         {
00135                 std::pair<stl_iter_t, bool> res;
00136                 res = mStlMap.insert(stl_value_t(index, dummy_data));
00137                 b_new_entry = res.second;
00138                 return res.first->second;
00139         }
00140 
00141         // If there, returns the data.
00142         // If not, returns NULL.
00143         // Never adds entries to the map.
00144         MAPPED_TYPE getIfThere(const INDEX_TYPE &index)
00145         {
00146                 stl_iter_t iter;
00147                 iter = mStlMap.find(index);
00148                 if (iter == mStlMap.end())
00149                 {
00150                         return (MAPPED_TYPE)0;
00151                 }
00152                 else
00153                 {
00154                         return (*iter).second;
00155                 }
00156         }
00157 
00158 
00159         // if index doesn't exist, then make a new node and return it
00160         MAPPED_TYPE &operator[](const INDEX_TYPE &index)
00161         {
00162                 return getData(index);
00163         }
00164 
00165         // do a reverse look-up, return NULL if failed
00166         INDEX_TYPE reverseLookup(const MAPPED_TYPE data)
00167         {
00168                 stl_iter_t iter;
00169                 stl_iter_t end_iter;
00170                 iter = mStlMap.begin();
00171                 end_iter = mStlMap.end();
00172                 while (iter != end_iter)
00173                 {
00174                         if ((*iter).second == data)
00175                                 return (*iter).first;
00176                         iter++;
00177                 }
00178                 return (INDEX_TYPE)0;
00179         }
00180 
00181         BOOL removeData(const INDEX_TYPE &index)
00182         {
00183                 mCurIter = mStlMap.find(index);
00184                 if (mCurIter == mStlMap.end())
00185                 {
00186                         return FALSE;
00187                 }
00188                 else
00189                 {
00190                         stl_iter_t iter = mCurIter++; // incrament mCurIter to the next element
00191                         mStlMap.erase(iter);
00192                         return TRUE;
00193                 }
00194         }
00195 
00196         // does this index exist?
00197         BOOL checkData(const INDEX_TYPE &index)
00198         {
00199                 stl_iter_t iter;
00200                 iter = mStlMap.find(index);
00201                 if (iter == mStlMap.end())
00202                 {
00203                         return FALSE;
00204                 }
00205                 else
00206                 {
00207                         mCurIter = iter;
00208                         return TRUE;
00209                 }
00210         }
00211 
00212         BOOL deleteData(const INDEX_TYPE &index)
00213         {
00214                 mCurIter = mStlMap.find(index);
00215                 if (mCurIter == mStlMap.end())
00216                 {
00217                         return FALSE;
00218                 }
00219                 else
00220                 {
00221                         stl_iter_t iter = mCurIter++; // incrament mCurIter to the next element
00222                         delete (*iter).second;
00223                         mStlMap.erase(iter);
00224                         return TRUE;
00225                 }
00226         }
00227 
00228         void deleteAllData()
00229         {
00230                 stl_iter_t iter;
00231                 stl_iter_t end_iter;
00232                 iter = mStlMap.begin();
00233                 end_iter = mStlMap.end();
00234                 while (iter != end_iter)
00235                 {
00236                         delete (*iter).second;
00237                         iter++;
00238                 }
00239                 mStlMap.clear();
00240                 mCurIter = mStlMap.end();
00241         }
00242         
00243         void removeAllData()
00244         {
00245                 mStlMap.clear();
00246         }
00247 };
00248 
00249 
00250 #endif

Generated on Thu Jul 1 06:08:51 2010 for Second Life Viewer by  doxygen 1.4.7