41#ifndef OPENSWMM_ENGINE_NAME_INDEX_HPP
42#define OPENSWMM_ENGINE_NAME_INDEX_HPP
46#include <unordered_map>
96 int add(
const std::string& name) {
99 throw std::invalid_argument(
"NameIndex: duplicate name '" + name +
"'");
116 auto [it, inserted] = map_.emplace(name,
static_cast<int>(names_.size()));
119 names_.push_back(name);
136 int find(std::string_view name)
const noexcept {
137 auto it = map_.find(name);
138 if (it == map_.end())
152 std::optional<int>
try_find(std::string_view name)
const noexcept {
153 auto it = map_.find(name);
154 if (it == map_.end())
169 const std::string*
canonical(std::string_view name)
const noexcept {
170 auto it = map_.find(name);
171 if (it == map_.end())
173 return &names_[
static_cast<std::size_t
>(it->second)];
186 const std::string&
name_of(
int idx)
const {
return names_.at(
static_cast<std::size_t
>(idx)); }
196 int size() const noexcept {
return static_cast<int>(names_.size()); }
202 bool empty() const noexcept {
return names_.empty(); }
238 const std::string tail = names_.back();
255 bool rename(
int idx,
const std::string& newName)
noexcept {
256 if (idx < 0 || idx >=
static_cast<int>(names_.size()))
258 auto it = map_.find(newName);
259 if (it != map_.end() && it->second != idx)
261 map_.erase(names_[
static_cast<std::size_t
>(idx)]);
262 names_[
static_cast<std::size_t
>(idx)] = newName;
277 if (idx < 0 || idx >=
static_cast<int>(names_.size()))
279 names_.erase(names_.begin() + idx);
301 if (bulk_depth_ == 0 || --bulk_depth_ > 0)
317 const std::vector<std::string>&
names() const noexcept {
return names_; }
327 void rebuild_map_() {
329 map_.reserve(names_.size());
330 for (
int i = 0; i < static_cast<int>(names_.size()); ++i)
331 map_[names_[
static_cast<std::size_t
>(i)]] = i;
335 std::unordered_map<std::string, int, CiHash, CiEqual> map_;
336 std::vector<std::string> names_;
Case-insensitive string helpers matching legacy SWMM name semantics.
void remove_at(int idx) noexcept
Erase an ordered name and renumber following indices.
Definition NameIndex.hpp:276
bool rename(int idx, const std::string &newName) noexcept
Replace one name while retaining its numeric index.
Definition NameIndex.hpp:255
void begin_bulk_remove() noexcept
Enter a nestable scope that defers hash-map rebuilding.
Definition NameIndex.hpp:292
int try_add(const std::string &name)
Copy a new name, returning a duplicate sentinel instead of a duplicate exception.
Definition NameIndex.hpp:115
bool empty() const noexcept
Test whether the ordered name vector is empty.
Definition NameIndex.hpp:202
int size() const noexcept
Count names in the ordered vector.
Definition NameIndex.hpp:196
int find(std::string_view name) const noexcept
Look up a borrowed name without allocating a temporary key.
Definition NameIndex.hpp:136
int add(const std::string &name)
Copy a new name and assign the next sequential index.
Definition NameIndex.hpp:96
const std::string * canonical(std::string_view name) const noexcept
Borrow the original spelling of an equivalent registry key.
Definition NameIndex.hpp:169
const std::string & name_of(int idx) const
Borrow a registered name by its ordered index.
Definition NameIndex.hpp:186
void pop_back() noexcept
Remove the most recently ordered name without renumbering earlier rows.
Definition NameIndex.hpp:235
const std::vector< std::string > & names() const noexcept
Borrow ordered stored spellings.
Definition NameIndex.hpp:317
void end_bulk_remove()
Close one deferred-removal scope and rebuild on the outermost close.
Definition NameIndex.hpp:300
void clear() noexcept
Erase all names and map entries.
Definition NameIndex.hpp:223
std::optional< int > try_find(std::string_view name) const noexcept
Look up a borrowed name with an optional result.
Definition NameIndex.hpp:152
NameIndex()=default
Create an empty registry with no deferred-removal scope.
void reserve(std::size_t n)
Request capacity for the map and ordered name vector.
Definition NameIndex.hpp:212
Definition ExchangeLaw.hpp:60