25#ifndef OPENSWMM_ENGINE_NAME_INDEX_HPP
26#define OPENSWMM_ENGINE_NAME_INDEX_HPP
30#include <unordered_map>
62 int add(
const std::string& name) {
63 auto [it, inserted] = map_.emplace(name,
static_cast<int>(names_.size()));
65 throw std::invalid_argument(
"NameIndex: duplicate name '" + name +
"'");
67 names_.push_back(name);
81 int find(std::string_view name)
const noexcept {
82 auto it = map_.find(std::string(name));
83 if (it == map_.end())
return -1;
90 std::optional<int>
try_find(std::string_view name)
const noexcept {
91 auto it = map_.find(std::string(name));
92 if (it == map_.end())
return std::nullopt;
101 return names_.at(
static_cast<std::size_t
>(idx));
109 int size() const noexcept {
return static_cast<int>(names_.size()); }
112 bool empty() const noexcept {
return names_.empty(); }
136 if (names_.empty())
return;
137 const std::string tail = names_.back();
150 bool rename(
int idx,
const std::string& newName)
noexcept {
151 if (idx < 0 || idx >=
static_cast<int>(names_.size()))
return false;
152 if (map_.count(newName))
return false;
153 map_.erase(names_[
static_cast<std::size_t
>(idx)]);
154 names_[
static_cast<std::size_t
>(idx)] = newName;
168 if (idx < 0 || idx >=
static_cast<int>(names_.size()))
return;
169 names_.erase(names_.begin() + idx);
171 map_.reserve(names_.size());
172 for (
int i = 0; i < static_cast<int>(names_.size()); ++i)
181 const std::vector<std::string>&
names() const noexcept {
return names_; }
184 std::unordered_map<std::string, int> map_;
185 std::vector<std::string> names_;
Bidirectional name↔index registry for SWMM objects.
Definition NameIndex.hpp:45
void remove_at(int idx) noexcept
Remove the entry at idx and rebuild the name→index map.
Definition NameIndex.hpp:167
bool rename(int idx, const std::string &newName) noexcept
Rename the entry at idx to newName.
Definition NameIndex.hpp:150
bool empty() const noexcept
True if no names are registered.
Definition NameIndex.hpp:112
int size() const noexcept
Number of registered names.
Definition NameIndex.hpp:109
int find(std::string_view name) const noexcept
Look up the index for a name.
Definition NameIndex.hpp:81
int add(const std::string &name)
Add a new name and assign the next sequential index.
Definition NameIndex.hpp:62
const std::string & name_of(int idx) const
Return the name for a given index.
Definition NameIndex.hpp:100
void pop_back() noexcept
Pop the tail entry — the name added most recently.
Definition NameIndex.hpp:135
const std::vector< std::string > & names() const noexcept
Read-only access to the ordered name list.
Definition NameIndex.hpp:181
void clear() noexcept
Remove all entries.
Definition NameIndex.hpp:123
std::optional< int > try_find(std::string_view name) const noexcept
Look up the index, returning std::optional.
Definition NameIndex.hpp:90
void reserve(std::size_t n)
Pre-allocate for a known count (avoids rehash during input).
Definition NameIndex.hpp:117
Definition NodeCoupling.cpp:15