Files
BionxControl/nodes/util/ntxmapindex.h
Christoph Holzheuer 0b54793b08 added nodes.
2026-04-02 15:15:28 +02:00

61 lines
1.4 KiB
C++

#ifndef NTX_MAPINDEX_H
#define NTX_MAPINDEX_H
#include <string>
#include <unordered_map>
#include <optional>
#include <functional>
#include <memory>
namespace ntx
{
/**
* @brief Bidirektionaler Index: String-Key → size_t Position.
*
* Verwendet Komposition statt Vererbung.
* Key → Index: O(1) via unordered_map.
*/
class NtxMapIndex
{
public:
NtxMapIndex() = default;
// --- Lookup ---
std::optional<size_t> indexOf(const std::string& key) const noexcept;
bool containsKey(const std::string& key) const noexcept;
// --- Iteration ---
/// Iteriert über alle Key→Index Paare.
/// Template muss im Header bleiben.
template <typename Fn>
void forEachKey(Fn&& fn) const
{
for (const auto& [key, idx] : m_keyToIndex)
fn(key, idx);
}
// --- Modification ---
void addKey(const std::string& key, size_t index);
void removeAt(size_t index);
bool removeKey(const std::string& key);
bool replaceKey(const std::string& oldKey, const std::string& newKey);
bool addAlias(const std::string& existingKey, const std::string& alias);
size_t size() const noexcept;
bool empty() const noexcept;
void clear() noexcept;
private:
std::unordered_map<std::string, size_t> m_keyToIndex;
};
using NtxMapIndexPtr = std::shared_ptr<NtxMapIndex>;
} // namespace ntx
#endif // NTX_MAPINDEX_H