fix on BinSearchArrayMap (W.I.P)

This commit is contained in:
Metehan Tuncbilek
2024-09-26 15:48:37 +03:00
parent 41b5a046be
commit fc675f9f50
2 changed files with 48 additions and 70 deletions

View File

@@ -18,10 +18,7 @@ namespace OpenVulkano
class BinSearchArrayMap
{
public:
BinSearchArrayMap() = default;
~BinSearchArrayMap() = default;
void Insert(K key, V value) noexcept
void Insert(K key, const V& value)
{
// Check if the key is bigger than the last element
if (m_data.empty() || key > m_data.back().first)
@@ -33,73 +30,58 @@ namespace OpenVulkano
throw std::runtime_error("Key cannot be lesser than the last used key.");
}
template<typename... Args> void Emplace(K key, Args... args)
template<typename... Args> void Emplace(K key, Args&&... args)
{
// Check if the key is bigger than the last element
if (m_data.empty() || key > m_data.back().first)
{
m_data.emplace_back(key, V(args...));
m_data.emplace_back(key, std::forward<Args>(args)...);
return;
}
throw std::runtime_error("Key cannot be lesser than the last used key.");
}
void Remove(K key) noexcept
void Remove(const K key)
{
auto it = std::lower_bound(m_data.begin(), m_data.end(), key,
[](const auto& pair, const K& key) { return pair.first < key; });
auto& it = FindPair(key);
m_data.erase(it);
}
if (it != m_data.end())
size_t Size() const { return m_data.size(); }
V& operator[](const K key) noexcept { return Get(key); }
V& Get(const K key) { return FindPair(key).second; }
Pair& FindPair(const K key)
{
int low = 0;
int high = m_data.size() - 1;
while (low <= high)
{
m_data.erase(it);
}
else
{
throw std::runtime_error("Key not found.");
int mid = low + (high - low) / 2;
if (m_data[mid].first == key)
{
return m_data[mid]; // The difference
}
else if (m_data[mid].first < key)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
throw std::runtime_error("Key not found.");
}
void Size() const noexcept { m_data.size(); }
V& operator[](K key) noexcept
{
auto it = std::lower_bound(m_data.begin(), m_data.end(), key,
[](const auto& pair, const K& key) { return pair.first < key; });
return it->second;
}
V& Get(K key) noexcept
{
auto it = std::lower_bound(m_data.begin(), m_data.end(), key,
[](const auto& pair, const K& key) { return pair.first < key; });
if (it != m_data.end())
{
return it->second;
}
else
{
throw std::runtime_error("Key not found.");
}
}
bool Contains(K key) const noexcept
{
auto it = std::lower_bound(m_data.begin(), m_data.end(), key,
[](const auto& pair, const K& key) { return pair.first < key; });
return it != m_data.end();
}
bool Contains(V& value) const noexcept
{
auto it =
std::find_if(m_data.begin(), m_data.end(), [&value](const auto& pair) { return pair.second == value; });
return it != m_data.end();
}
bool Contains(const K key) const { return true; }
bool Contains(const V& value) const { return true; }
private:
Vec m_data;