121 using storage_type = std::vector<std::pair<std::string, T>>;
122 using index_type = std::unordered_map<std::string,
typename storage_type::size_type,
125 using iter_type =
typename storage_type::iterator;
126 using const_iter_type =
typename storage_type::const_iterator;
130 storage_type m_vector;
134 OrderedMap() =
default;
136 OrderedMap(
const index_type& index,
const storage_type& storage)
142 const index_type& getIndex()
const {
return m_map; }
144 const storage_type& getStorage()
const {
return m_vector; }
146 std::size_t count(
const std::string& key)
const {
147 return this->m_map.count(key);
152 T& operator[](
const std::string& key) {
153 if (this->count(key) == 0)
154 this->insert( std::make_pair(key, T()));
156 return this->at(key);
160 std::size_t erase(
const std::string& key) {
161 if (this->count(key) == 0)
164 const std::size_t idx = this->m_map.at(key);
165 this->m_map.erase(key);
166 this->m_vector.erase(this->m_vector.begin() + idx);
168 for (
const auto& index_pair : this->m_map) {
169 auto target_index = index_pair.second;
170 if (target_index > idx) {
174 this->m_map[index_pair.first] = target_index;
180 void insert(std::pair<std::string,T> key_value_pair) {
181 if (this->count(key_value_pair.first) > 0) {
182 auto iter = m_map.find( key_value_pair.first );
183 const std::size_t idx = iter->second;
184 m_vector[idx] = key_value_pair;
186 const std::size_t idx = m_vector.size();
187 this->m_map.emplace(key_value_pair.first, idx);
188 this->m_vector.push_back(std::move(key_value_pair));
193 T& get(
const std::string& key) {
194 auto iter = m_map.find( key );
195 if (iter == m_map.end())
197 using namespace std::string_literals;
198 auto startsWithSame = OrderedMapDetail::findSimilarStrings(key, m_vector);
199 if (!startsWithSame.empty())
201 startsWithSame =
" Similar entries are "s +
202 startsWithSame +
"."s;
204 throw std::invalid_argument(
"Key "s + key +
" not found."s
208 const std::size_t idx = iter->second;
214 T& iget(std::size_t index) {
215 if (index >= m_vector.size())
216 throw std::invalid_argument(
"Invalid index");
217 return m_vector[index].second;
220 const T& get(
const std::string& key)
const {
221 const auto& iter = this->m_map.find( key );
222 if (iter == m_map.end())
224 auto startsWithSame = OrderedMapDetail::findSimilarStrings(key, m_vector);
225 if (!startsWithSame.empty())
227 startsWithSame = std::string(
" Similar entries are ") +
228 startsWithSame + std::string(
".");
230 using namespace std::string_literals;
231 throw std::invalid_argument(
"Key "s + key +
" not found."s
235 const std::size_t idx = iter->second;
241 const T& iget(std::size_t index)
const {
242 if (index >= m_vector.size())
244 using namespace std::string_literals;
245 throw std::invalid_argument(
"Invalid index "s +
246 std::to_string(index) +
247 " is larger than container size"s);
249 return m_vector[index].second;
252 const T& at(std::size_t index)
const {
253 return this->iget(index);
256 const T& at(
const std::string& key)
const {
257 return this->get(key);
260 T& at(std::size_t index) {
261 return this->iget(index);
264 T& at(
const std::string& key) {
265 return this->get(key);
268 std::size_t size()
const {
269 return m_vector.size();
273 const_iter_type begin()
const {
274 return m_vector.begin();
278 const_iter_type end()
const {
279 return m_vector.end();
283 return m_vector.begin();
287 return m_vector.end();
290 iter_type find(
const std::string& key) {
291 const auto map_iter = this->m_map.find(key);
292 if (map_iter == this->m_map.end())
293 return this->m_vector.end();
295 return std::next(this->m_vector.begin(), map_iter->second);
298 const_iter_type find(
const std::string& key)
const {
299 const auto map_iter = this->m_map.find(key);
300 if (map_iter == this->m_map.end())
301 return this->m_vector.end();
303 return std::next(this->m_vector.begin(), map_iter->second);
306 template<std::
size_t n>
307 bool operator==(
const OrderedMap<T,n>& data)
const {
308 return this->getIndex() == data.getIndex() &&
309 this->getStorage() == data.getStorage();
312 template<
class Serializer>
316 serializer(m_vector);