Home || Architecture || Video Search || Visual Search || Scripts || Applications || 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()
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(std::string 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     SimpleMapBase(const SimpleMapBase&)
00112     {
00113     }
00114 
00115     SimpleMapBase&
00116     operator=(const SimpleMapBase&)
00117     {
00118     }
00119 
00120     typedef typename std::map<IdxT,ElemT>::const_iterator ConstIter;
00121     typedef typename std::map<IdxT,ElemT>::iterator Iter;
00122 
00123     std::map<IdxT,ElemT> mMap;
00124     DelT                 mDelete;
00125 
00126 };
00127 
00128 //too bad template typedefs don't exist yet
00129 template<class IdxT, class ElemT>
00130 class SimpleMapDelete : public SimpleMapBase<IdxT, ElemT, Deletor<ElemT> >
00131 {
00132 };
00133 
00134 template<class IdxT, class ElemT>    
00135 class SimpleMap : public SimpleMapBase<IdxT, ElemT, Nop<ElemT> >
00136 {
00137 public:
00138     SimpleMap()
00139     {
00140     }
00141     
00143     /*
00144     SimpleMap(bool dummyParam)
00145     {
00146         ILOG_VAR(Impala.Util.SimpleMap);
00147         if(dummyParam == true)
00148             ILOG_ERROR("wrong value: you should use SimpleMapDelete");
00149     }
00150     */
00151 };
00152     
00153 } // namespace Util
00154 } // namespace Impala
00155 
00156 #endif

Generated on Fri Mar 19 09:31:47 2010 for ImpalaSrc by  doxygen 1.5.1