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(); }
133 const std::vector<std::string>&
names() const noexcept {
return names_; }
136 std::unordered_map<std::string, int> map_;
137 std::vector<std::string> names_;
Bidirectional name↔index registry for SWMM objects.
Definition NameIndex.hpp:45
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
const std::vector< std::string > & names() const noexcept
Read-only access to the ordered name list.
Definition NameIndex.hpp:133
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 Controls.cpp:24