36 #ifndef INCLUDE_NRT_CORE_DESIGN_DETAILS_IDTABLEIMPL_H
37 #define INCLUDE_NRT_CORE_DESIGN_DETAILS_IDTABLEIMPL_H
41 template<
class ValueType,
class IdType>
45 IdNotFoundException(IdType
const &
id)
throw()
48 { whatptr =
"Invalid Reverse Lookup"; }
catch (...) { }
50 virtual ~IdNotFoundException() throw() { }
55 template<
class ValueType,
class IdType>
59 ValueNotFoundException(ValueType
const & value)
throw()
62 { whatptr =
"Invalid Forward Lookup"; }
catch (...) { }
64 virtual ~ValueNotFoundException() throw() { }
71 template<
class ValueType,
class IdType>
inline
77 template<
class ValueType,
class IdType>
inline
80 auto itr = itsValueIdMap.find(value);
82 if (itr == itsValueIdMap.end()) {
83 return create_internal(value);
86 typename std::unordered_map<IdType, ValueData>::iterator idItr = itsIdValueMap.find(itr->second);
87 ++ idItr->second.second;
93 template<
class ValueType,
class IdType>
inline
96 auto itr = itsIdValueMap.find(
id);
97 if (itr == itsIdValueMap.end())
throw IdNotFoundException(
id);
99 return itr->second.first;
103 template<
class ValueType,
class IdType>
inline
106 auto itr = itsValueIdMap.find(value);
107 if (itr == itsValueIdMap.end())
throw ValueNotFoundException(value);
113 template<
class ValueType,
class IdType>
inline
116 auto itr = itsValueIdMap.find(value);
117 if (itr == itsValueIdMap.end())
return create_internal(value);
118 else return itr->second;
122 template<
class ValueType,
class IdType>
inline
125 auto itr = itsValueIdMap.find(value);
126 if (itr == itsValueIdMap.end())
return 0;
127 else return users(itr->second);
131 template<
class ValueType,
class IdType>
inline
134 auto itr = itsIdValueMap.find(
id);
135 if (itr == itsIdValueMap.end())
return 0;
136 else return itr->second.second;
140 template<
class ValueType,
class IdType>
143 auto idItr = itsIdValueMap.find(
id);
144 if (idItr == itsIdValueMap.end())
throw IdNotFoundException(
id);
147 -- idItr->second.second;
150 if (idItr->second.second == 0) {
151 auto valueItr = itsValueIdMap.find(idItr->second.first);
153 itsRecycleBin.push(
id);
155 itsIdValueMap.erase(idItr);
156 itsValueIdMap.erase(valueItr);
159 }
else return idItr->second.second;
163 template<
class ValueType,
class IdType>
166 auto valueItr = itsValueIdMap.find(value);
167 if (valueItr == itsValueIdMap.end())
throw ValueNotFoundException(value);
169 auto idItr = itsIdValueMap.find(valueItr->second);
172 -- idItr->second.second;
175 if (idItr->second.second == 0) {
176 itsRecycleBin.push(valueItr->second);
178 itsIdValueMap.erase(idItr);
179 itsValueIdMap.erase(valueItr);
182 }
else return idItr->second.second;
186 template<
class ValueType,
class IdType>
189 itsIdValueMap.clear();
190 itsValueIdMap.clear();
191 itsRecycleBin = std::queue<IdType>();
196 template<
class ValueType,
class IdType>
inline
201 if (itsRecycleBin.size()) {
id = itsRecycleBin.front(); itsRecycleBin.pop(); }
202 else {
id = ++itsMaxId; }
204 itsValueIdMap[value] = id;
205 itsIdValueMap[id] = std::pair<ValueType, size_t>(value, size_t(1));
210 #endif // INCLUDE_NRT_CORE_DESIGN_DETAILS_IDTABLEIMPL_H