00001
00024 #ifndef INC_MI32_MIMAP3_H
00025 #define INC_MI32_MIMAP3_H
00026
00027 #include <map>
00028 #include <vector>
00029
00030 #ifndef INC_MI32_MISTRING_H
00031 #include <mi32/mistring.h>
00032 #endif
00033
00034
00036 template <typename STORED_TYPE> class MIMAP3 {
00037 public:
00038
00039
00045 explicit MIMAP3 (
00046 unsigned NumValues = 0,
00047 unsigned MaxValues = 0
00048 ):
00049 m_VectorStored(NumValues),
00050 m_VectorValid(NumValues,false),
00051 m_MaxValues((NumValues > MaxValues) ? NumValues : MaxValues),
00052 m_UseMap(m_MaxValues==0)
00053 { }
00054
00056 bool Find (
00057 unsigned value,
00058 STORED_TYPE &ret
00059 ) const {
00060 if (value >= m_VectorValid.size() || !m_VectorValid[value]) {
00061 if (m_UseMap) return (Find(static_cast<double>(value),ret));
00062 else return (false);
00063 }
00064 ret = m_VectorStored[value];
00065 return (true);
00066 }
00067
00069 bool Find (
00070 double value,
00071 STORED_TYPE &ret
00072 ) const {
00073 typename MAP_DOUBLE::const_iterator it = m_MapDouble.find(value);
00074 if (it == m_MapDouble.end()) return (false);
00075 ret = it->second;
00076 return (true);
00077 }
00078
00080 bool Find (
00081 const MISTRING& value,
00082 STORED_TYPE &ret
00083 ) const {
00084 typename MAP_STRING::const_iterator it = m_MapString.find(value);
00085 if (it == m_MapString.end()) return (false);
00086 ret = it->second;
00087 return (true);
00088 }
00089
00091 void Store (
00092 unsigned value,
00093 const STORED_TYPE& stored
00094 ) {
00095 if (value >= m_MaxValues) {
00096 Store(static_cast<double>(value),stored);
00097 m_UseMap = true;
00098 }
00099 else {
00100 if (value >= m_VectorValid.size()) {
00101 m_VectorValid.resize(value+1,false);
00102 m_VectorStored.resize(value+1);
00103 }
00104 m_VectorValid[value] = true;
00105 m_VectorStored[value] = stored;
00106 }
00107 }
00108
00110 void Store (
00111 double value,
00112 const STORED_TYPE& stored
00113 ) { m_MapDouble.insert(std::pair<double,STORED_TYPE>(value,stored)); }
00114
00116 void Store (
00117 const MISTRING& value,
00118 const STORED_TYPE& stored
00119 ) { m_MapString.insert(std::pair<MISTRING,STORED_TYPE>(value,stored)); }
00120
00121 private:
00122 #ifndef GENERATING_DOXYGEN_OUTPUT
00123 typedef std::map<double,STORED_TYPE> MAP_DOUBLE;
00124 typedef std::map<MISTRING,STORED_TYPE> MAP_STRING;
00125
00126 std::vector<STORED_TYPE> m_VectorStored;
00127 std::vector<UINT8> m_VectorValid;
00128 MAP_DOUBLE m_MapDouble;
00129 MAP_STRING m_MapString;
00130 unsigned m_MaxValues;
00131 bool m_UseMap;
00132
00133 #endif
00134 };
00135
00136 #endif INC_MI32_MIMAP3_H