Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef NaryQuickSet_INCLUDE_ONCE
00033 #define NaryQuickSet_INCLUDE_ONCE
00034
00035 #include <vlCore/Object.hpp>
00036
00037 namespace vl
00038 {
00042 template<typename KeyType, typename ValueType, int MaxMapType>
00043 class NaryQuickMap: public Object
00044 {
00045 public:
00046 NaryQuickMap()
00047 {
00048 reset();
00049 }
00050
00051 void reset()
00052 {
00053 mMapSize = 0;
00054 memset(mKeyToValue, 0, sizeof(mKeyToValue));
00055 memset(mValueToKey, 0, sizeof(mValueToKey));
00056 }
00057
00058 void clear()
00059 {
00060
00061 mMapSize = 0;
00062 }
00063
00064 int size() const { return mMapSize; }
00065
00066 ValueType* begin() { return mValues; }
00067 ValueType* end() { return mValues + mMapSize; }
00068 const ValueType* begin() const { return mValues; }
00069 const ValueType* end() const { return mValues + mMapSize; }
00070
00071 void append(KeyType key)
00072 {
00073 append(key, key);
00074 }
00075
00076
00077 void append(KeyType key, ValueType value)
00078 {
00079
00080 int pos = mMapSize++;
00081 mKeyToValue[key] = pos;
00082 mValueToKey[pos] = key;
00083 mValues[pos] = value;
00084 }
00085
00086 void insert(KeyType key)
00087 {
00088 insert(key, key);
00089 }
00090
00091 void insert(KeyType key, const ValueType& value)
00092 {
00093 VL_CHECK(key < MaxMapType)
00094 int pos = find(key);
00095 if (pos == -1)
00096 {
00097 pos = mMapSize++;
00098 VL_CHECK(pos < MaxMapType)
00099 mKeyToValue[key] = pos;
00100 mValueToKey[pos] = key;
00101 }
00102 mValues[pos] = value;
00103 }
00104
00105 void erase(KeyType key)
00106 {
00107 int pos = find(key);
00108 if (pos != -1)
00109 {
00110
00111 if (mMapSize>1)
00112 {
00113
00114 mValues[pos] = mValues[mMapSize-1];
00115
00116 mValueToKey[pos] = mValueToKey[mMapSize-1];
00117
00118 mKeyToValue[mValueToKey[pos]] = pos;
00119 }
00120 mMapSize--;
00121 VL_CHECK(mMapSize >= 0)
00122 }
00123 }
00124
00125 int find(KeyType key) const
00126 {
00127 int pos = mKeyToValue[key];
00128 VL_CHECK(pos >= 0)
00129 VL_CHECK(pos < MaxMapType)
00130 if (pos < mMapSize)
00131 {
00132 KeyType e = mValueToKey[pos];
00133 VL_CHECK(e >= 0)
00134 VL_CHECK(e < MaxMapType)
00135 if (e == key)
00136 return pos;
00137 else
00138 return -1;
00139 }
00140 else
00141 return -1;
00142 }
00143
00144 bool hasKey(KeyType key) const
00145 {
00146 return find(key) != -1;
00147 }
00148
00149 const ValueType& valueFromKey(KeyType key) const
00150 {
00151 VL_CHECK(key >= 0)
00152 VL_CHECK(key < MaxMapType)
00153 VL_CHECK(mKeyToValue[key] >= 0)
00154 VL_CHECK(mKeyToValue[key] < MaxMapType)
00155 return mValues[mKeyToValue[key]];
00156 }
00157
00158 const ValueType& valueFromIndex(int i) const
00159 {
00160 return mValues[i];
00161 }
00162
00163 KeyType key(int i) const
00164 {
00165 return mValueToKey[i];
00166 }
00167
00168 protected:
00169
00170
00171
00172
00173 ValueType mValues[MaxMapType];
00174
00175 KeyType mValueToKey[MaxMapType];
00176
00177 int mMapSize;
00178
00179 int mKeyToValue[MaxMapType];
00180 };
00181
00182 }
00183
00184 #endif