fix on BinSearchArrayMap (W.I.P)
This commit is contained in:
@@ -18,10 +18,7 @@ namespace OpenVulkano
|
|||||||
class BinSearchArrayMap
|
class BinSearchArrayMap
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
BinSearchArrayMap() = default;
|
void Insert(K key, const V& value)
|
||||||
~BinSearchArrayMap() = default;
|
|
||||||
|
|
||||||
void Insert(K key, V value) noexcept
|
|
||||||
{
|
{
|
||||||
// Check if the key is bigger than the last element
|
// Check if the key is bigger than the last element
|
||||||
if (m_data.empty() || key > m_data.back().first)
|
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.");
|
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
|
// Check if the key is bigger than the last element
|
||||||
if (m_data.empty() || key > m_data.back().first)
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw std::runtime_error("Key cannot be lesser than the last used key.");
|
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,
|
auto& it = FindPair(key);
|
||||||
[](const auto& pair, const K& key) { return pair.first < 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);
|
int mid = low + (high - low) / 2;
|
||||||
}
|
|
||||||
else
|
if (m_data[mid].first == key)
|
||||||
{
|
{
|
||||||
throw std::runtime_error("Key not found.");
|
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(); }
|
bool Contains(const K key) const { return true; }
|
||||||
|
bool Contains(const V& value) const { return true; }
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Vec m_data;
|
Vec m_data;
|
||||||
|
|||||||
@@ -18,26 +18,22 @@ using namespace OpenVulkano;
|
|||||||
|
|
||||||
TEST_CASE("BinSearchArrayMap")
|
TEST_CASE("BinSearchArrayMap")
|
||||||
{
|
{
|
||||||
std::vector<std::pair<int, std::string>> data;
|
BinSearchArrayMap<int, std::string, std::pair<int, std::string>, StableVector<std::pair<int, std::string>>> map;
|
||||||
data.push_back(std::make_pair(1, "One"));
|
|
||||||
data.push_back(std::make_pair(2, "Two"));
|
|
||||||
data.push_back(std::make_pair(6, "Three"));
|
|
||||||
data.push_back(std::make_pair(8, "Four"));
|
|
||||||
data.push_back(std::make_pair(10, "Five"));
|
|
||||||
|
|
||||||
auto wtf =
|
|
||||||
std::lower_bound(data.begin(), data.end(), 10, [](const auto& pair, const int& key) { return pair.first < key; });
|
|
||||||
auto test = *wtf;
|
|
||||||
REQUIRE(test.first == 10);
|
|
||||||
REQUIRE(test.second == "Five");
|
|
||||||
|
|
||||||
BinSearchArrayMap<int, std::string> map;
|
|
||||||
map.Insert(1, "One");
|
map.Insert(1, "One");
|
||||||
map.Insert(2, "Two");
|
map.Insert(2, "Two");
|
||||||
map.Insert(6, "Three");
|
map.Insert(6, "Three");
|
||||||
map.Insert(8, "Four");
|
map.Insert(8, "Four");
|
||||||
map.Insert(10, "Five");
|
map.Insert(10, "Five");
|
||||||
|
|
||||||
REQUIRE(map[6] == "Three");
|
REQUIRE(map.Get(1) == "One");
|
||||||
REQUIRE(map[10] == "Five");
|
REQUIRE(map.Get(2) == "Two");
|
||||||
|
REQUIRE(map.Get(6) == "Three");
|
||||||
|
REQUIRE(map.Get(8) == "Four");
|
||||||
|
REQUIRE(map.Get(10) == "Five");
|
||||||
|
|
||||||
|
REQUIRE(map.Size() == 5);
|
||||||
|
|
||||||
|
map.Remove(6);
|
||||||
|
REQUIRE(map.Size() == 4);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user