initial version.
This commit is contained in:
106
openVulkanoCpp/Data/Containers/BinSearchArrayMap.hpp
Normal file
106
openVulkanoCpp/Data/Containers/BinSearchArrayMap.hpp
Normal file
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <utility>
|
||||
#include <type_traits>
|
||||
|
||||
namespace OpenVulkano
|
||||
{
|
||||
template<typename K, typename V, typename Pair = std::pair<K, V>, typename Vec = std::vector<Pair>,
|
||||
typename = std::enable_if_t<std::is_integral<K>::value>>
|
||||
class BinSearchArrayMap
|
||||
{
|
||||
public:
|
||||
BinSearchArrayMap() = default;
|
||||
~BinSearchArrayMap() = default;
|
||||
|
||||
void Insert(K key, V value) noexcept
|
||||
{
|
||||
// Check if the key is bigger than the last element
|
||||
if (m_data.empty() || key > m_data.back().first)
|
||||
{
|
||||
m_data.emplace_back(key, value);
|
||||
return;
|
||||
}
|
||||
|
||||
throw std::runtime_error("Key cannot be lesser than the last used key.");
|
||||
}
|
||||
|
||||
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...));
|
||||
return;
|
||||
}
|
||||
|
||||
throw std::runtime_error("Key cannot be lesser than the last used key.");
|
||||
}
|
||||
|
||||
void Remove(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())
|
||||
{
|
||||
m_data.erase(it);
|
||||
}
|
||||
else
|
||||
{
|
||||
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();
|
||||
}
|
||||
|
||||
private:
|
||||
Vec m_data;
|
||||
};
|
||||
}
|
||||
42
tests/BinSearchArrayMapTest.cpp
Normal file
42
tests/BinSearchArrayMapTest.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#include <catch2/catch_all.hpp>
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <utility>
|
||||
#include <type_traits>
|
||||
|
||||
#include "Data/Containers/BinSearchArrayMap.hpp"
|
||||
|
||||
using namespace OpenVulkano;
|
||||
|
||||
TEST_CASE("BinSearchArrayMap")
|
||||
{
|
||||
std::vector<std::pair<int, std::string>> data;
|
||||
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(2, "Two");
|
||||
map.Insert(6, "Three");
|
||||
map.Insert(8, "Four");
|
||||
map.Insert(10, "Five");
|
||||
|
||||
REQUIRE(map[6] == "Three");
|
||||
}
|
||||
Reference in New Issue
Block a user