88 lines
1.9 KiB
C++
88 lines
1.9 KiB
C++
|
|
#include <ntxmapindex.h>
|
||
|
|
#include <ranges> // Zwingend erforderlich für std::views und std::ranges::to
|
||
|
|
|
||
|
|
namespace ntx
|
||
|
|
{
|
||
|
|
|
||
|
|
// --- Lookup ---
|
||
|
|
|
||
|
|
std::optional<size_t> NtxMapIndex::indexOf(const std::string& key) const noexcept
|
||
|
|
{
|
||
|
|
auto it = m_keyToIndex.find(key);
|
||
|
|
if (it != m_keyToIndex.end())
|
||
|
|
return it->second;
|
||
|
|
return std::nullopt;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool NtxMapIndex::containsKey(const std::string& key) const noexcept
|
||
|
|
{
|
||
|
|
return m_keyToIndex.contains(key);
|
||
|
|
}
|
||
|
|
|
||
|
|
// --- Modification ---
|
||
|
|
|
||
|
|
void NtxMapIndex::addKey(const std::string& key, size_t index)
|
||
|
|
{
|
||
|
|
m_keyToIndex[key] = index;
|
||
|
|
}
|
||
|
|
|
||
|
|
void NtxMapIndex::removeAt(size_t index)
|
||
|
|
{
|
||
|
|
auto it = m_keyToIndex.begin();
|
||
|
|
while (it != m_keyToIndex.end())
|
||
|
|
{
|
||
|
|
if (it->second == index)
|
||
|
|
it = m_keyToIndex.erase(it);
|
||
|
|
else
|
||
|
|
{
|
||
|
|
if (it->second > index)
|
||
|
|
it->second--;
|
||
|
|
++it;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
bool NtxMapIndex::removeKey(const std::string& key)
|
||
|
|
{
|
||
|
|
return m_keyToIndex.erase(key) > 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool NtxMapIndex::replaceKey(const std::string& oldKey, const std::string& newKey)
|
||
|
|
{
|
||
|
|
auto it = m_keyToIndex.find(oldKey);
|
||
|
|
if (it == m_keyToIndex.end() || oldKey == newKey)
|
||
|
|
return false;
|
||
|
|
if (m_keyToIndex.contains(newKey))
|
||
|
|
return false;
|
||
|
|
|
||
|
|
size_t idx = it->second;
|
||
|
|
m_keyToIndex.erase(it);
|
||
|
|
m_keyToIndex[newKey] = idx;
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool NtxMapIndex::addAlias(const std::string& existingKey, const std::string& alias)
|
||
|
|
{
|
||
|
|
auto idx = indexOf(existingKey);
|
||
|
|
if (!idx || containsKey(alias))
|
||
|
|
return false;
|
||
|
|
m_keyToIndex[alias] = *idx;
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
size_t NtxMapIndex::size() const noexcept
|
||
|
|
{
|
||
|
|
return m_keyToIndex.size();
|
||
|
|
}
|
||
|
|
|
||
|
|
bool NtxMapIndex::empty() const noexcept
|
||
|
|
{
|
||
|
|
return m_keyToIndex.empty();
|
||
|
|
}
|
||
|
|
|
||
|
|
void NtxMapIndex::clear() noexcept
|
||
|
|
{
|
||
|
|
m_keyToIndex.clear();
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace ntx
|