Don't hard-code std::string as the key type for InsertionOrderedMap

This commit is contained in:
Rangi
2026-05-25 13:57:33 -04:00
parent a0d96a0856
commit ed19806434
4 changed files with 14 additions and 14 deletions
+11 -11
View File
@@ -35,18 +35,18 @@ ReversedIterable<IterableT> reversed(IterableT &&_iterable) {
return {_iterable};
}
// A map from `std::string` keys to `ItemT` items, iterable in the order the items were inserted.
template<typename ItemT>
// A map from `KeyT` keys to `ItemT` items, iterable in the order the items were inserted.
template<typename KeyT, typename ItemT>
class InsertionOrderedMap {
std::deque<ItemT> list; // `deque` does not invalidate item references
std::unordered_map<std::string, size_t> map; // Indexes into `list`
std::deque<ItemT> list; // `deque` does not invalidate item references
std::unordered_map<KeyT, size_t> map; // Indexes into `list`
public:
size_t size() const { return list.size(); }
bool empty() const { return list.empty(); }
bool contains(std::string const &name) const { return map.find(name) != map.end(); }
bool contains(KeyT const &key) const { return map.find(key) != map.end(); }
ItemT &operator[](size_t i) { return list[i]; }
@@ -55,13 +55,13 @@ public:
typename decltype(list)::const_iterator begin() const { return list.begin(); }
typename decltype(list)::const_iterator end() const { return list.end(); }
ItemT &add(std::string const &name) {
map[name] = list.size();
ItemT &add(KeyT const &key) {
map[key] = list.size();
return list.emplace_back();
}
ItemT &add(std::string const &name, ItemT &&value) {
map[name] = list.size();
ItemT &add(KeyT const &key, ItemT &&value) {
map[key] = list.size();
list.emplace_back(std::move(value));
return list.back();
}
@@ -71,8 +71,8 @@ public:
return list.emplace_back();
}
std::optional<size_t> findIndex(std::string const &name) const {
if (auto search = map.find(name); search != map.end()) {
std::optional<size_t> findIndex(KeyT const &key) const {
if (auto search = map.find(key); search != map.end()) {
return search->second;
}
return std::nullopt;
+1 -1
View File
@@ -88,7 +88,7 @@ bool forEachChar(
return true;
}
static InsertionOrderedMap<Charmap> charmaps;
static InsertionOrderedMap<std::string, Charmap> charmaps;
static Charmap *currentCharmap;
static std::stack<Charmap *> charmapStack;
+1 -1
View File
@@ -47,7 +47,7 @@ struct SectionStackEntry {
};
static Section *currentSection = nullptr;
static InsertionOrderedMap<Section> sections;
static InsertionOrderedMap<std::string, Section> sections;
static uint32_t curOffset; // Offset into the current section (see `sect_GetSymbolOffset`)
+1 -1
View File
@@ -17,7 +17,7 @@
#include "link/main.hpp"
#include "link/warning.hpp"
static InsertionOrderedMap<std::unique_ptr<Section>> sections;
static InsertionOrderedMap<std::string, std::unique_ptr<Section>> sections;
void sect_ForEach(void (*callback)(Section &)) {
for (std::unique_ptr<Section> &ptr : sections) {