OpenSWMM Engine  6.0.0-alpha.1
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.1)
Loading...
Searching...
No Matches
NameIndex.hpp
Go to the documentation of this file.
1
25#ifndef OPENSWMM_ENGINE_NAME_INDEX_HPP
26#define OPENSWMM_ENGINE_NAME_INDEX_HPP
27
28#include <string>
29#include <string_view>
30#include <unordered_map>
31#include <vector>
32#include <stdexcept>
33#include <optional>
34
35namespace openswmm {
36
45class NameIndex {
46public:
47 NameIndex() = default;
48
49 // -----------------------------------------------------------------------
50 // Insertion
51 // -----------------------------------------------------------------------
52
62 int add(const std::string& name) {
63 auto [it, inserted] = map_.emplace(name, static_cast<int>(names_.size()));
64 if (!inserted) {
65 throw std::invalid_argument("NameIndex: duplicate name '" + name + "'");
66 }
67 names_.push_back(name);
68 return it->second;
69 }
70
71 // -----------------------------------------------------------------------
72 // Lookup
73 // -----------------------------------------------------------------------
74
81 int find(std::string_view name) const noexcept {
82 auto it = map_.find(std::string(name));
83 if (it == map_.end()) return -1;
84 return it->second;
85 }
86
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;
93 return it->second;
94 }
95
100 const std::string& name_of(int idx) const {
101 return names_.at(static_cast<std::size_t>(idx));
102 }
103
104 // -----------------------------------------------------------------------
105 // Capacity
106 // -----------------------------------------------------------------------
107
109 int size() const noexcept { return static_cast<int>(names_.size()); }
110
112 bool empty() const noexcept { return names_.empty(); }
113
117 void reserve(std::size_t n) {
118 map_.reserve(n);
119 names_.reserve(n);
120 }
121
123 void clear() noexcept {
124 map_.clear();
125 names_.clear();
126 }
127
135 void pop_back() noexcept {
136 if (names_.empty()) return;
137 const std::string tail = names_.back();
138 map_.erase(tail);
139 names_.pop_back();
140 }
141
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; // would create a duplicate
153 map_.erase(names_[static_cast<std::size_t>(idx)]);
154 names_[static_cast<std::size_t>(idx)] = newName;
155 map_[newName] = idx;
156 return true;
157 }
158
167 void remove_at(int idx) noexcept {
168 if (idx < 0 || idx >= static_cast<int>(names_.size())) return;
169 names_.erase(names_.begin() + idx);
170 map_.clear();
171 map_.reserve(names_.size());
172 for (int i = 0; i < static_cast<int>(names_.size()); ++i)
173 map_[names_[i]] = i;
174 }
175
176 // -----------------------------------------------------------------------
177 // Iteration
178 // -----------------------------------------------------------------------
179
181 const std::vector<std::string>& names() const noexcept { return names_; }
182
183private:
184 std::unordered_map<std::string, int> map_;
185 std::vector<std::string> names_;
186};
187
188} /* namespace openswmm */
189
190#endif /* OPENSWMM_ENGINE_NAME_INDEX_HPP */
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