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
128 // -----------------------------------------------------------------------
129 // Iteration
130 // -----------------------------------------------------------------------
131
133 const std::vector<std::string>& names() const noexcept { return names_; }
134
135private:
136 std::unordered_map<std::string, int> map_;
137 std::vector<std::string> names_;
138};
139
140} /* namespace openswmm */
141
142#endif /* OPENSWMM_ENGINE_NAME_INDEX_HPP */
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