2017-11-08 14:50:35 -05:00
|
|
|
#ifndef DVEREB_DUALLOOKUP_H
|
|
|
|
#define DVEREB_DUALLOOKUP_H
|
|
|
|
|
|
|
|
#include <unordered_map>
|
2017-12-19 14:04:39 -05:00
|
|
|
#include <string>
|
2017-11-08 14:50:35 -05:00
|
|
|
|
2017-11-10 15:42:32 -05:00
|
|
|
struct DualLookupBase {
|
2017-11-10 14:57:31 -05:00
|
|
|
// NOTE(dev): Used to determine which version of the string you want:
|
|
|
|
// 1 / OPPOSITE: The string it maps to, opposite of the one you pass in.
|
|
|
|
// 2 / VALUE: The string passed via the first paramater of 'add.'
|
|
|
|
// 3 / EQUIVALENT: The string passed via the second parameter of 'add.'
|
|
|
|
enum Type {
|
|
|
|
OPPOSITE = 1,
|
|
|
|
VALUE,
|
|
|
|
EQUIVALENT,
|
|
|
|
};
|
|
|
|
|
2017-11-10 15:42:32 -05:00
|
|
|
protected:
|
|
|
|
DualLookupBase() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class DualLookup : public DualLookupBase {
|
|
|
|
public:
|
|
|
|
DualLookup();
|
|
|
|
|
2017-11-08 14:50:35 -05:00
|
|
|
/* Add a mapped pair to the container
|
|
|
|
* returns false if it already exists, regardless of direction
|
2017-12-19 14:04:39 -05:00
|
|
|
* (i.e. value->equivalent is a duplicate of equivalent->value)
|
2017-11-08 14:50:35 -05:00
|
|
|
*/
|
2017-12-19 14:04:39 -05:00
|
|
|
bool add(T value, T equivalent, std::string context = "");
|
2017-11-08 14:50:35 -05:00
|
|
|
|
|
|
|
/* If found, Set result to the mapped value and true is returned.
|
|
|
|
* If not found, result remains unchanged and false is returned.
|
|
|
|
*/
|
2017-12-19 14:04:39 -05:00
|
|
|
bool get(T key, T &result, std::string context = "", const Type &type = Type::OPPOSITE);
|
2017-11-08 14:50:35 -05:00
|
|
|
|
|
|
|
/* If found, return true
|
|
|
|
* else return false */
|
2017-12-19 14:04:39 -05:00
|
|
|
bool contains(const T &key, std::string context = "");
|
2017-11-08 14:50:35 -05:00
|
|
|
|
|
|
|
private:
|
2017-12-19 14:04:39 -05:00
|
|
|
std::unordered_map<std::string, std::unordered_map<T, T> > owner;
|
|
|
|
std::unordered_map<std::string, std::unordered_map<T, T> > mirror;
|
2017-11-08 14:50:35 -05:00
|
|
|
|
|
|
|
// NOTE(dev): These functions actually do the work:
|
2017-11-10 14:57:31 -05:00
|
|
|
bool get_single(const T &key, T &result, const std::unordered_map<T, T> &umap);
|
2017-11-08 14:50:35 -05:00
|
|
|
bool contains_single(const T &key, const std::unordered_map<T, T> &umap);
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
DualLookup<T>::DualLookup()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2017-12-19 14:04:39 -05:00
|
|
|
bool DualLookup<T>::add(T value, T equivalent, std::string context)
|
2017-11-08 14:50:35 -05:00
|
|
|
{
|
|
|
|
// already exists
|
2017-12-19 14:04:39 -05:00
|
|
|
if(owner[context].find(value) != owner[context].end()
|
|
|
|
|| mirror[context].find(value) != mirror[context].end())
|
2017-11-08 14:50:35 -05:00
|
|
|
return false;
|
2017-12-19 14:04:39 -05:00
|
|
|
if(owner[context].find(equivalent) != owner[context].end()
|
|
|
|
|| mirror[context].find(equivalent) != mirror[context].end())
|
2017-11-08 14:50:35 -05:00
|
|
|
return false;
|
|
|
|
|
2017-12-19 14:04:39 -05:00
|
|
|
owner[context].insert({value, equivalent});
|
|
|
|
mirror[context].insert({equivalent, value});
|
2017-11-08 14:50:35 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2017-12-19 14:04:39 -05:00
|
|
|
bool DualLookup<T>::get(T key, T &result, std::string context, const Type &type)
|
2017-11-08 14:50:35 -05:00
|
|
|
{
|
2017-12-19 14:04:39 -05:00
|
|
|
if(get_single(key, result, owner[context]))
|
2017-11-10 14:57:31 -05:00
|
|
|
{
|
|
|
|
//if(type == Type::OPPOSITE || type == Type::EQUIVALENT)
|
|
|
|
if(type == Type::VALUE)
|
|
|
|
result = key;
|
2017-11-08 14:50:35 -05:00
|
|
|
return true;
|
2017-11-10 14:57:31 -05:00
|
|
|
}
|
2017-12-19 14:04:39 -05:00
|
|
|
if(get_single(key, result, mirror[context]))
|
2017-11-10 14:57:31 -05:00
|
|
|
{
|
|
|
|
//if(type == Type::OPPOSITE || type == Type::VALUE)
|
|
|
|
if(type == Type::EQUIVALENT)
|
|
|
|
result = key;
|
2017-11-08 14:50:35 -05:00
|
|
|
return true;
|
2017-11-10 14:57:31 -05:00
|
|
|
}
|
2017-11-08 14:50:35 -05:00
|
|
|
|
|
|
|
return false; // didn't find it
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2017-12-19 14:04:39 -05:00
|
|
|
bool DualLookup<T>::contains(const T &key, std::string context)
|
2017-11-08 14:50:35 -05:00
|
|
|
{
|
2017-12-19 14:04:39 -05:00
|
|
|
if(contains_single(key, owner[context]))
|
2017-11-08 14:50:35 -05:00
|
|
|
return true;
|
2017-12-19 14:04:39 -05:00
|
|
|
if(contains_single(key, mirror[context]))
|
2017-11-08 14:50:35 -05:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false; // didn't find it
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2017-11-10 14:57:31 -05:00
|
|
|
bool DualLookup<T>::get_single(const T &key, T &result, const std::unordered_map<T, T> &umap)
|
2017-11-08 14:50:35 -05:00
|
|
|
{
|
|
|
|
auto i = umap.find(key);
|
|
|
|
if(i == umap.end())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
result = i->second;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
bool DualLookup<T>::contains_single(const T &key, const std::unordered_map<T, T> &umap)
|
|
|
|
{
|
|
|
|
return umap.find(key) != umap.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|