1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
// SPDX-License-Identifier: LGPL-3.0-or-later
// Author: Kristian Lytje
#pragma once
#include <utility/StringUtils.h>
#include <utility/Exceptions.h>
#include <unordered_map>
#include <string>
namespace ausaxs::saxs {
namespace detail {
/**
* @brief A simple case-insensitive hashmap.
*/
template<typename V>
struct SimpleMap {
/**
* @brief Create a new empty SimpleMap.
*/
SimpleMap() = default;
/**
* @brief Create a new SimpleMap from a std::map.
*/
SimpleMap(std::unordered_map<std::string, V> map) {
for (auto& [key, value] : map) {
insert(key, value);
}
}
/**
* @brief Get a value from the storage.
*/
virtual V get(const std::string& key) const {
std::string k2 = utility::to_lowercase(key);
if (!data.contains(k2)) {
throw except::map_error("SimpleMap::get: Key " + k2 + " not found in map");
}
return data.at(k2);
}
/**
* @brief Insert a key-value pair into the storage.
*/
void insert(const std::string& key, V val) {
std::string k2 = utility::to_lowercase(key);
data.emplace(k2, val);
}
const std::unordered_map<std::string, V>& get_map() const {
return data;
}
/**
* @brief Check if this map contains the given key.
*/
bool contains(const std::string& key) const {
return data.contains(utility::to_lowercase(key));
}
typename std::unordered_map<std::string, V>::const_iterator begin() const {
return data.begin();
}
typename std::unordered_map<std::string, V>::const_iterator end() const {
return data.end();
}
typename std::unordered_map<std::string, V>::iterator begin() {
return data.begin();
}
typename std::unordered_map<std::string, V>::iterator end() {
return data.end();
}
std::unordered_map<std::string, V> data;
};
}
}
|