Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UnorderedBimap #696

Merged
merged 2 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 164 additions & 0 deletions core/include/cubos/core/memory/unordered_bimap.hpp
RiscadoA marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/// @file
/// @brief Class @ref cubos::core::memory::UnorderedBimap.
/// @ingroup core-memory

#pragma once

#include <unordered_map>

namespace cubos::core::memory
{
/// @brief A bidirectional hash table.
/// @tparam L Left type.
/// @tparam R Right type.
/// @tparam LHash Hash functor type for @p L.
/// @tparam RHash Hash functor type for @p R.
/// @ingroup core-memory
template <typename L, typename R, typename LHash = std::hash<L>, typename RHash = std::hash<R>>
class UnorderedBimap final
{
public:
RiscadoA marked this conversation as resolved.
Show resolved Hide resolved
using Iterator = typename std::unordered_map<L, R, LHash>::const_iterator;
RiscadoA marked this conversation as resolved.
Show resolved Hide resolved

/// @brief Adds a new entry to the map.
/// @note If any of the values already exists, the old entries with them are removed.
/// @param left Left value.
/// @param right Right value.
void insert(L left, R right)
{
auto leftIt = mLeftToRight.find(left);
if (leftIt != mLeftToRight.end())
{
mRightToLeft.erase(leftIt->second);
}

auto rightIt = mRightToLeft.find(right);
if (rightIt != mRightToLeft.end())
{
mLeftToRight.erase(rightIt->second);
}
RiscadoA marked this conversation as resolved.
Show resolved Hide resolved

mLeftToRight.insert_or_assign(left, right);
mRightToLeft.insert_or_assign(right, left);
}

/// @brief Removes the entry associated to the given left value.
/// @param left Left value.
/// @return Whether the entry was removed.
bool eraseLeft(const L& left)
{
auto it = mLeftToRight.find(left);
if (it == mLeftToRight.end())
{
return false;
}

mRightToLeft.erase(it->second);
mLeftToRight.erase(it);
return true;
}

/// @brief Removes the entry associated to the given right value.
/// @param right Right value.
/// @return Whether the entry was removed.
bool eraseRight(const R& right)
{
auto it = mRightToLeft.find(right);
if (it == mRightToLeft.end())
{
return false;
}

mLeftToRight.erase(it->second);
mRightToLeft.erase(it);
return true;
}

/// @brief Checks if the map has the given entry.
/// @param left Left value.
/// @param right Right value.
/// @return Whether the map has the entry.
bool contains(const L& left, const R& right) const
{
auto it = mLeftToRight.find(left);
if (it == mLeftToRight.end())
{
return false;
}
return it->second == right;
}

/// @brief Checks if the map contains the given left value.
/// @param left Left value.
/// @return Whether the map contains the value.
bool containsLeft(const L& left) const
{
return mLeftToRight.contains(left);
}

/// @brief Checks if the map contains the given right value.
/// @param right Right value.
/// @return Whether the map contains the value.
bool containsRight(const R& right) const
{
return mRightToLeft.contains(right);
}

/// @brief Gets the right value associated to the given left value.
/// @note Aborts if the left value isn't stored.
/// @param left Left value.
/// @return Right value.
const R& atRight(const L& left) const
{
return mLeftToRight.at(left);
}

/// @brief Gets the left value associated to the given right value.
/// @note Aborts if the right value isn't stored.
/// @param right Right value.
/// @return Left value.
const L& atLeft(const R& right) const
{
return mRightToLeft.at(right);
}

/// @brief Clears the map.
void clear()
{
mLeftToRight.clear();
mRightToLeft.clear();
}

/// @brief Gets the number of entries in the map.
/// @return Entry count.
std::size_t size() const
{
return mLeftToRight.size();
}

/// @brief Checks if the map is empty.
/// @return Whether the map is empty.
bool empty() const
{
return mLeftToRight.empty();
}

/// @brief Gets an iterator to the beginning of the map.
/// @return Iterator.
Iterator begin() const
{
return mLeftToRight.begin();
}

/// @brief Gets an iterator to the end of the map.
/// @return Iterator.
Iterator end() const
{
return mLeftToRight.end();
}

private:
std::unordered_map<L, R, LHash> mLeftToRight;
std::unordered_map<R, L, RHash> mRightToLeft;
};
} // namespace cubos::core::memory
2 changes: 2 additions & 0 deletions core/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ add_executable(
data/fs/file_system.cpp
data/context.cpp

memory/unordered_bimap.cpp

ecs/utils.cpp
ecs/registry.cpp
ecs/world.cpp
Expand Down
80 changes: 80 additions & 0 deletions core/tests/memory/unordered_bimap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <doctest/doctest.h>

#include <cubos/core/memory/unordered_bimap.hpp>

TEST_CASE("memory::UnorderedBimap")
{
using cubos::core::memory::UnorderedBimap;

UnorderedBimap<int, char> bimap{};

SUBCASE("just initialized")
{
}

SUBCASE("had entries")
{
bimap.insert(1, 'a');

CHECK_FALSE(bimap.empty());
CHECK(bimap.size() == 1);

CHECK(bimap.contains(1, 'a'));
CHECK(bimap.containsLeft(1));
CHECK(bimap.containsRight('a'));
CHECK(bimap.atLeft('a') == 1);
CHECK(bimap.atRight(1) == 'a');

CHECK(bimap.begin() != bimap.end());
CHECK(bimap.begin()->first == 1);
CHECK(bimap.begin()->second == 'a');
CHECK(++bimap.begin() == bimap.end());

bimap.insert(2, 'b');

CHECK(bimap.size() == 2);
CHECK(bimap.contains(1, 'a'));
CHECK(bimap.contains(2, 'b'));
CHECK(bimap.atRight(1) == 'a');
CHECK(bimap.atRight(2) == 'b');
CHECK(bimap.atLeft('a') == 1);
CHECK(bimap.atLeft('b') == 2);

bimap.insert(1, 'b');

CHECK(bimap.size() == 1);
CHECK(bimap.contains(1, 'b'));
CHECK_FALSE(bimap.contains(1, 'a'));
CHECK_FALSE(bimap.contains(2, 'b'));
CHECK(bimap.atRight(1) == 'b');
CHECK(bimap.atLeft('b') == 1);

SUBCASE("remove left")
{
CHECK(bimap.eraseLeft(1));
}

SUBCASE("remove right")
{
CHECK(bimap.eraseRight('b'));
}

SUBCASE("clear")
{
bimap.clear();
}
}

// Shouldn't contain anything
CHECK(bimap.empty());
CHECK(bimap.size() == 0);

CHECK_FALSE(bimap.contains(1, 'a'));
CHECK_FALSE(bimap.containsLeft(1));
CHECK_FALSE(bimap.containsRight('a'));

CHECK(bimap.begin() == bimap.end());

CHECK_FALSE(bimap.eraseLeft(1));
CHECK_FALSE(bimap.eraseRight('a'));
}
Loading