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}; return {_iterable};
} }
// A map from `std::string` keys to `ItemT` items, iterable in the order the items were inserted. // A map from `KeyT` keys to `ItemT` items, iterable in the order the items were inserted.
template<typename ItemT> template<typename KeyT, typename ItemT>
class InsertionOrderedMap { class InsertionOrderedMap {
std::deque<ItemT> list; // `deque` does not invalidate item references std::deque<ItemT> list; // `deque` does not invalidate item references
std::unordered_map<std::string, size_t> map; // Indexes into `list` std::unordered_map<KeyT, size_t> map; // Indexes into `list`
public: public:
size_t size() const { return list.size(); } size_t size() const { return list.size(); }
bool empty() const { return list.empty(); } 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]; } 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 begin() const { return list.begin(); }
typename decltype(list)::const_iterator end() const { return list.end(); } typename decltype(list)::const_iterator end() const { return list.end(); }
ItemT &add(std::string const &name) { ItemT &add(KeyT const &key) {
map[name] = list.size(); map[key] = list.size();
return list.emplace_back(); return list.emplace_back();
} }
ItemT &add(std::string const &name, ItemT &&value) { ItemT &add(KeyT const &key, ItemT &&value) {
map[name] = list.size(); map[key] = list.size();
list.emplace_back(std::move(value)); list.emplace_back(std::move(value));
return list.back(); return list.back();
} }
@@ -71,8 +71,8 @@ public:
return list.emplace_back(); return list.emplace_back();
} }
std::optional<size_t> findIndex(std::string const &name) const { std::optional<size_t> findIndex(KeyT const &key) const {
if (auto search = map.find(name); search != map.end()) { if (auto search = map.find(key); search != map.end()) {
return search->second; return search->second;
} }
return std::nullopt; return std::nullopt;
+1 -1
View File
@@ -88,7 +88,7 @@ bool forEachChar(
return true; return true;
} }
static InsertionOrderedMap<Charmap> charmaps; static InsertionOrderedMap<std::string, Charmap> charmaps;
static Charmap *currentCharmap; static Charmap *currentCharmap;
static std::stack<Charmap *> charmapStack; static std::stack<Charmap *> charmapStack;
+1 -1
View File
@@ -47,7 +47,7 @@ struct SectionStackEntry {
}; };
static Section *currentSection = nullptr; 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`) 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/main.hpp"
#include "link/warning.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 &)) { void sect_ForEach(void (*callback)(Section &)) {
for (std::unique_ptr<Section> &ptr : sections) { for (std::unique_ptr<Section> &ptr : sections) {