Home || Visual Search || Applications || Architecture || Important Messages || OGL || Src

SimpleMap.h

Go to the documentation of this file.
00001 #ifndef Impala_Util_SimpleMap_h
00002 #define Impala_Util_SimpleMap_h
00003 
00004 #include <iostream>
00005 #include <map>
00006 #include <vector>
00007 #include "Basis/ILog.h"
00008 #include "Util/Traits.h"
00009 
00010 namespace Impala
00011 {
00012 namespace Util
00013 {
00014 
00015 template<class IdxT, class ElemT, class DelT>
00016 class SimpleMapBase
00017 {
00018 public:
00019 
00020     SimpleMapBase()
00021     {
00022     }
00023 
00024     int
00025     Size() const
00026     {
00027         return mMap.size();
00028     }
00029 
00030     void
00031     Add(IdxT idx, ElemT elem)
00032     {
00033         mMap[idx] = elem;
00034     }
00035 
00036     bool
00037     Get(IdxT idx, ElemT& elem) const
00038     {
00039         ConstIter it = mMap.find(idx);
00040         if (it == mMap.end())
00041             return false;
00042         elem = it->second;
00043         return true;
00044     }
00045 
00046     std::vector<ElemT>
00047     GetAll() const
00048     {
00049         std::vector<ElemT> res;
00050         for (ConstIter it=mMap.begin() ; it!=mMap.end() ; it++)
00051             res.push_back(it->second);
00052         return res;
00053     }
00054 
00055     bool
00056     GetIdx(ElemT elem, IdxT& idx) const
00057     {
00058         for (ConstIter it=mMap.begin() ; it!=mMap.end() ; it++)
00059         {
00060             if (it->second == elem)
00061             {
00062                 idx = it->first;
00063                 return true;
00064             }
00065         }
00066         return false;
00067     }
00068 
00069     std::vector<IdxT>
00070     GetAllIdx() const
00071     {
00072         std::vector<IdxT> res;
00073         for (ConstIter it=mMap.begin() ; it!=mMap.end() ; it++)
00074             res.push_back(it->first);
00075         return res;
00076     }
00077 
00078     bool
00079     Remove(IdxT idx)
00080     {
00081         Iter it = mMap.find(idx);
00082         if (it == mMap.end())
00083             return false;
00084         mDelete(it->second);
00085         mMap.erase(it);
00086         return true;
00087     }
00088 
00089     void
00090     Dump(CString name) const
00091     {
00092         ILOG_VAR(Util.SimpleMapBase);
00093         ILOG_INFO(name << "Dump start");
00094         for (ConstIter i=mMap.begin() ; i!=mMap.end() ; i++)
00095             ILOG_INFO(i->first << " = " << i->second);
00096         ILOG_INFO("Dump end");
00097     }
00098 
00099     void
00100     Clear()
00101     {
00102         for(Iter it=mMap.begin() ; it!=mMap.end() ; ++it)
00103         {
00104             mDelete(it->second);
00105         }
00106         mMap.clear();
00107     }
00108 
00109 private:
00110 
00111     typedef typename std::map<IdxT,ElemT>::const_iterator ConstIter;
00112     typedef typename std::map<IdxT,ElemT>::iterator Iter;
00113 
00114     std::map<IdxT,ElemT> mMap;
00115     DelT                 mDelete;
00116 
00117 };
00118 
00119 //too bad template typedefs don't exist yet
00120 template<class IdxT, class ElemT>
00121 class SimpleMapDelete : public SimpleMapBase<IdxT, ElemT, Deletor<ElemT> >
00122 {
00123 };
00124 
00125 template<class IdxT, class ElemT>    
00126 class SimpleMap : public SimpleMapBase<IdxT, ElemT, Nop<ElemT> >
00127 {
00128 public:
00129     SimpleMap()
00130     {
00131     }
00132     
00134     /*
00135     SimpleMap(bool dummyParam)
00136     {
00137         ILOG_VAR(Impala.Util.SimpleMap);
00138         if(dummyParam == true)
00139             ILOG_ERROR("wrong value: you should use SimpleMapDelete");
00140     }
00141     */
00142 };
00143     
00144 } // namespace Util
00145 } // namespace Impala
00146 
00147 #endif

Generated on Thu Jan 13 09:05:16 2011 for ImpalaSrc by  doxygen 1.5.1