Added ability to store lookups per context.
This commit is contained in:
parent
6e603b412f
commit
1a6607cbe3
37
DualLookup.h
37
DualLookup.h
@ -2,6 +2,7 @@
|
|||||||
#define DVEREB_DUALLOOKUP_H
|
#define DVEREB_DUALLOOKUP_H
|
||||||
|
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
struct DualLookupBase {
|
struct DualLookupBase {
|
||||||
// NOTE(dev): Used to determine which version of the string you want:
|
// NOTE(dev): Used to determine which version of the string you want:
|
||||||
@ -25,22 +26,22 @@ public:
|
|||||||
|
|
||||||
/* Add a mapped pair to the container
|
/* Add a mapped pair to the container
|
||||||
* returns false if it already exists, regardless of direction
|
* returns false if it already exists, regardless of direction
|
||||||
* (i.e. value->equivilent is a duplicate of equivilent->value)
|
* (i.e. value->equivalent is a duplicate of equivalent->value)
|
||||||
*/
|
*/
|
||||||
bool add(T value, T equivilent);
|
bool add(T value, T equivalent, std::string context = "");
|
||||||
|
|
||||||
/* If found, Set result to the mapped value and true is returned.
|
/* If found, Set result to the mapped value and true is returned.
|
||||||
* If not found, result remains unchanged and false is returned.
|
* If not found, result remains unchanged and false is returned.
|
||||||
*/
|
*/
|
||||||
bool get(T key, T &result, const Type &type = Type::OPPOSITE);
|
bool get(T key, T &result, std::string context = "", const Type &type = Type::OPPOSITE);
|
||||||
|
|
||||||
/* If found, return true
|
/* If found, return true
|
||||||
* else return false */
|
* else return false */
|
||||||
bool contains(const T &key);
|
bool contains(const T &key, std::string context = "");
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unordered_map<T, T> owner;
|
std::unordered_map<std::string, std::unordered_map<T, T> > owner;
|
||||||
std::unordered_map<T, T> mirror;
|
std::unordered_map<std::string, std::unordered_map<T, T> > mirror;
|
||||||
|
|
||||||
// NOTE(dev): These functions actually do the work:
|
// NOTE(dev): These functions actually do the work:
|
||||||
bool get_single(const T &key, T &result, const std::unordered_map<T, T> &umap);
|
bool get_single(const T &key, T &result, const std::unordered_map<T, T> &umap);
|
||||||
@ -53,31 +54,33 @@ DualLookup<T>::DualLookup()
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
bool DualLookup<T>::add(T value, T equivilent)
|
bool DualLookup<T>::add(T value, T equivalent, std::string context)
|
||||||
{
|
{
|
||||||
// already exists
|
// already exists
|
||||||
if(owner.find(value) != owner.end() || mirror.find(value) != mirror.end())
|
if(owner[context].find(value) != owner[context].end()
|
||||||
|
|| mirror[context].find(value) != mirror[context].end())
|
||||||
return false;
|
return false;
|
||||||
if(owner.find(equivilent) != owner.end() || mirror.find(equivilent) != mirror.end())
|
if(owner[context].find(equivalent) != owner[context].end()
|
||||||
|
|| mirror[context].find(equivalent) != mirror[context].end())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
owner.insert({value, equivilent});
|
owner[context].insert({value, equivalent});
|
||||||
mirror.insert({equivilent, value});
|
mirror[context].insert({equivalent, value});
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
bool DualLookup<T>::get(T key, T &result, const Type &type)
|
bool DualLookup<T>::get(T key, T &result, std::string context, const Type &type)
|
||||||
{
|
{
|
||||||
if(get_single(key, result, owner))
|
if(get_single(key, result, owner[context]))
|
||||||
{
|
{
|
||||||
//if(type == Type::OPPOSITE || type == Type::EQUIVALENT)
|
//if(type == Type::OPPOSITE || type == Type::EQUIVALENT)
|
||||||
if(type == Type::VALUE)
|
if(type == Type::VALUE)
|
||||||
result = key;
|
result = key;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if(get_single(key, result, mirror))
|
if(get_single(key, result, mirror[context]))
|
||||||
{
|
{
|
||||||
//if(type == Type::OPPOSITE || type == Type::VALUE)
|
//if(type == Type::OPPOSITE || type == Type::VALUE)
|
||||||
if(type == Type::EQUIVALENT)
|
if(type == Type::EQUIVALENT)
|
||||||
@ -89,11 +92,11 @@ bool DualLookup<T>::get(T key, T &result, const Type &type)
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
bool DualLookup<T>::contains(const T &key)
|
bool DualLookup<T>::contains(const T &key, std::string context)
|
||||||
{
|
{
|
||||||
if(contains_single(key, owner))
|
if(contains_single(key, owner[context]))
|
||||||
return true;
|
return true;
|
||||||
if(contains_single(key, mirror))
|
if(contains_single(key, mirror[context]))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
return false; // didn't find it
|
return false; // didn't find it
|
||||||
|
113
main.cpp
113
main.cpp
@ -12,6 +12,14 @@ TEST_CASE("all tests", "all") {
|
|||||||
REQUIRE(container.add(1, 2));
|
REQUIRE(container.add(1, 2));
|
||||||
REQUIRE(string_container.add("1", "2"));
|
REQUIRE(string_container.add("1", "2"));
|
||||||
|
|
||||||
|
// context with same VALUE
|
||||||
|
REQUIRE(container.add(1, 3, "a"));
|
||||||
|
REQUIRE(string_container.add("1", "3", "a"));
|
||||||
|
|
||||||
|
// context with save EQUIVALENT
|
||||||
|
REQUIRE(container.add(3, 2, "b"));
|
||||||
|
REQUIRE(string_container.add("3", "2", "b"));
|
||||||
|
|
||||||
SECTION ("no duplicates") {
|
SECTION ("no duplicates") {
|
||||||
REQUIRE_FALSE(container.add(1, 2));
|
REQUIRE_FALSE(container.add(1, 2));
|
||||||
REQUIRE_FALSE(container.add(2, 1));
|
REQUIRE_FALSE(container.add(2, 1));
|
||||||
@ -25,6 +33,34 @@ TEST_CASE("all tests", "all") {
|
|||||||
REQUIRE_FALSE(string_container.add("2", "3"));
|
REQUIRE_FALSE(string_container.add("2", "3"));
|
||||||
REQUIRE_FALSE(string_container.add("3", "1"));
|
REQUIRE_FALSE(string_container.add("3", "1"));
|
||||||
REQUIRE_FALSE(string_container.add("3", "2"));
|
REQUIRE_FALSE(string_container.add("3", "2"));
|
||||||
|
|
||||||
|
// context with same VALUE
|
||||||
|
REQUIRE_FALSE(container.add(1, 3, "a"));
|
||||||
|
REQUIRE_FALSE(container.add(3, 1, "a"));
|
||||||
|
REQUIRE_FALSE(container.add(1, 4, "a"));
|
||||||
|
REQUIRE_FALSE(container.add(3, 4, "a"));
|
||||||
|
REQUIRE_FALSE(container.add(4, 1, "a"));
|
||||||
|
REQUIRE_FALSE(container.add(4, 3, "a"));
|
||||||
|
REQUIRE_FALSE(string_container.add("1", "3", "a"));
|
||||||
|
REQUIRE_FALSE(string_container.add("3", "1", "a"));
|
||||||
|
REQUIRE_FALSE(string_container.add("1", "4", "a"));
|
||||||
|
REQUIRE_FALSE(string_container.add("3", "4", "a"));
|
||||||
|
REQUIRE_FALSE(string_container.add("4", "1", "a"));
|
||||||
|
REQUIRE_FALSE(string_container.add("4", "3", "a"));
|
||||||
|
|
||||||
|
// context with save EQUIVALENT
|
||||||
|
REQUIRE_FALSE(container.add(3, 2, "b"));
|
||||||
|
REQUIRE_FALSE(container.add(2, 3, "b"));
|
||||||
|
REQUIRE_FALSE(container.add(3, 4, "b"));
|
||||||
|
REQUIRE_FALSE(container.add(2, 4, "b"));
|
||||||
|
REQUIRE_FALSE(container.add(4, 3, "b"));
|
||||||
|
REQUIRE_FALSE(container.add(4, 2, "b"));
|
||||||
|
REQUIRE_FALSE(string_container.add("3", "2", "b"));
|
||||||
|
REQUIRE_FALSE(string_container.add("2", "3", "b"));
|
||||||
|
REQUIRE_FALSE(string_container.add("3", "4", "b"));
|
||||||
|
REQUIRE_FALSE(string_container.add("2", "4", "b"));
|
||||||
|
REQUIRE_FALSE(string_container.add("4", "3", "b"));
|
||||||
|
REQUIRE_FALSE(string_container.add("4", "2", "b"));
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION ("searches") {
|
SECTION ("searches") {
|
||||||
@ -37,6 +73,18 @@ TEST_CASE("all tests", "all") {
|
|||||||
REQUIRE(string_container.contains("1"));
|
REQUIRE(string_container.contains("1"));
|
||||||
REQUIRE_FALSE(container.contains(10));
|
REQUIRE_FALSE(container.contains(10));
|
||||||
REQUIRE_FALSE(string_container.contains("10"));
|
REQUIRE_FALSE(string_container.contains("10"));
|
||||||
|
|
||||||
|
// context with same VALUE
|
||||||
|
REQUIRE(container.contains(1, "a"));
|
||||||
|
REQUIRE(string_container.contains("1", "a"));
|
||||||
|
REQUIRE_FALSE(container.contains(10, "a"));
|
||||||
|
REQUIRE_FALSE(string_container.contains("10", "a"));
|
||||||
|
|
||||||
|
// context with save EQUIVALENT
|
||||||
|
REQUIRE(container.contains(2, "b"));
|
||||||
|
REQUIRE(string_container.contains("2", "b"));
|
||||||
|
REQUIRE_FALSE(container.contains(10, "b"));
|
||||||
|
REQUIRE_FALSE(string_container.contains("10", "b"));
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION ("not found in container and unedited") {
|
SECTION ("not found in container and unedited") {
|
||||||
@ -46,14 +94,65 @@ TEST_CASE("all tests", "all") {
|
|||||||
REQUIRE(container_result == 10);
|
REQUIRE(container_result == 10);
|
||||||
REQUIRE_FALSE(string_container.get("20", string_container_result));
|
REQUIRE_FALSE(string_container.get("20", string_container_result));
|
||||||
REQUIRE(string_container_result == "10");
|
REQUIRE(string_container_result == "10");
|
||||||
|
|
||||||
|
// context with same VALUE
|
||||||
|
container_result = 10;
|
||||||
|
string_container_result = "10";
|
||||||
|
REQUIRE_FALSE(container.get(2, container_result, "a"));
|
||||||
|
REQUIRE(container_result == 10);
|
||||||
|
REQUIRE_FALSE(string_container.get("2", string_container_result, "a"));
|
||||||
|
REQUIRE(string_container_result == "10");
|
||||||
|
|
||||||
|
// context with save EQUIVALENT
|
||||||
|
container_result = 10;
|
||||||
|
string_container_result = "10";
|
||||||
|
REQUIRE_FALSE(container.get(1, container_result, "b"));
|
||||||
|
REQUIRE(container_result == 10);
|
||||||
|
REQUIRE_FALSE(string_container.get("1", string_container_result, "b"));
|
||||||
|
REQUIRE(string_container_result == "10");
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION ("in container and set") {
|
SECTION ("in container and set") {
|
||||||
container_result = 0;
|
container_result = 0;
|
||||||
REQUIRE(container.get(1, container_result));
|
REQUIRE(container.get(1, container_result));
|
||||||
REQUIRE(container_result == 2);
|
REQUIRE(container_result == 2);
|
||||||
|
container_result = 0;
|
||||||
|
REQUIRE(container.get(2, container_result));
|
||||||
|
REQUIRE(container_result == 1);
|
||||||
|
string_container_result = "0";
|
||||||
REQUIRE(string_container.get("1", string_container_result));
|
REQUIRE(string_container.get("1", string_container_result));
|
||||||
REQUIRE(string_container_result == "2");
|
REQUIRE(string_container_result == "2");
|
||||||
|
string_container_result = "0";
|
||||||
|
REQUIRE(string_container.get("2", string_container_result));
|
||||||
|
REQUIRE(string_container_result == "1");
|
||||||
|
|
||||||
|
// context with same VALUE
|
||||||
|
container_result = 0;
|
||||||
|
REQUIRE(container.get(1, container_result, "a"));
|
||||||
|
REQUIRE(container_result == 3);
|
||||||
|
container_result = 0;
|
||||||
|
REQUIRE(container.get(3, container_result, "a"));
|
||||||
|
REQUIRE(container_result == 1);
|
||||||
|
string_container_result = "0";
|
||||||
|
REQUIRE(string_container.get("1", string_container_result, "a"));
|
||||||
|
REQUIRE(string_container_result == "3");
|
||||||
|
string_container_result = "0";
|
||||||
|
REQUIRE(string_container.get("3", string_container_result, "a"));
|
||||||
|
REQUIRE(string_container_result == "1");
|
||||||
|
|
||||||
|
// context with save EQUIVALENT
|
||||||
|
container_result = 0;
|
||||||
|
REQUIRE(container.get(3, container_result, "b"));
|
||||||
|
REQUIRE(container_result == 2);
|
||||||
|
container_result = 0;
|
||||||
|
REQUIRE(container.get(2, container_result, "b"));
|
||||||
|
REQUIRE(container_result == 3);
|
||||||
|
string_container_result = "0";
|
||||||
|
REQUIRE(string_container.get("3", string_container_result, "b"));
|
||||||
|
REQUIRE(string_container_result == "2");
|
||||||
|
string_container_result = "0";
|
||||||
|
REQUIRE(string_container.get("2", string_container_result, "b"));
|
||||||
|
REQUIRE(string_container_result == "3");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,27 +164,29 @@ TEST_CASE("all tests", "all") {
|
|||||||
REQUIRE(container.get(1, container_result));
|
REQUIRE(container.get(1, container_result));
|
||||||
CHECK(container_result == 2);
|
CHECK(container_result == 2);
|
||||||
container_result = 0;
|
container_result = 0;
|
||||||
REQUIRE(container.get(1, container_result, DualLookupBase::Type::OPPOSITE));
|
REQUIRE(container.get(1, container_result, "", DualLookupBase::Type::OPPOSITE));
|
||||||
CHECK(container_result == 2);
|
CHECK(container_result == 2);
|
||||||
container_result = 0;
|
container_result = 0;
|
||||||
REQUIRE(container.get(1, container_result, DualLookupBase::Type::EQUIVALENT));
|
REQUIRE(container.get(1, container_result, "", DualLookupBase::Type::EQUIVALENT));
|
||||||
CHECK(container_result == 2);
|
CHECK(container_result == 2);
|
||||||
container_result = 0;
|
container_result = 0;
|
||||||
REQUIRE(container.get(1, container_result, DualLookupBase::Type::VALUE));
|
REQUIRE(container.get(1, container_result, "", DualLookupBase::Type::VALUE));
|
||||||
CHECK(container_result == 1);
|
CHECK(container_result == 1);
|
||||||
|
|
||||||
container_result = 0;
|
container_result = 0;
|
||||||
REQUIRE(container.get(2, container_result));
|
REQUIRE(container.get(2, container_result));
|
||||||
CHECK(container_result == 1);
|
CHECK(container_result == 1);
|
||||||
container_result = 0;
|
container_result = 0;
|
||||||
REQUIRE(container.get(2, container_result, DualLookup<int>::Type::OPPOSITE));
|
REQUIRE(container.get(2, container_result, "", DualLookup<int>::Type::OPPOSITE));
|
||||||
CHECK(container_result == 1);
|
CHECK(container_result == 1);
|
||||||
container_result = 0;
|
container_result = 0;
|
||||||
REQUIRE(container.get(2, container_result, DualLookup<int>::Type::EQUIVALENT));
|
REQUIRE(container.get(2, container_result, "", DualLookup<int>::Type::EQUIVALENT));
|
||||||
CHECK(container_result == 2);
|
CHECK(container_result == 2);
|
||||||
container_result = 0;
|
container_result = 0;
|
||||||
REQUIRE(container.get(2, container_result, DualLookup<int>::Type::VALUE));
|
REQUIRE(container.get(2, container_result, "", DualLookup<int>::Type::VALUE));
|
||||||
CHECK(container_result == 1);
|
CHECK(container_result == 1);
|
||||||
|
// context with same VALUE
|
||||||
|
// context with save EQUIVALENT
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user