2012-01-15 08:29:57 +00:00
|
|
|
#ifndef NALL_MAP_HPP
|
|
|
|
#define NALL_MAP_HPP
|
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
#include <nall/set.hpp>
|
2012-01-15 08:29:57 +00:00
|
|
|
|
|
|
|
namespace nall {
|
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
template<typename T, typename U> struct map {
|
|
|
|
struct node_t {
|
|
|
|
T key;
|
|
|
|
U value;
|
|
|
|
node_t() = default;
|
|
|
|
node_t(const T& key) : key(key) {}
|
|
|
|
node_t(const T& key, const U& value) : key(key), value(value) {}
|
2015-07-14 09:32:43 +00:00
|
|
|
auto operator< (const node_t& source) const -> bool { return key < source.key; }
|
|
|
|
auto operator==(const node_t& source) const -> bool { return key == source.key; }
|
2012-01-15 08:29:57 +00:00
|
|
|
};
|
|
|
|
|
2015-07-14 09:32:43 +00:00
|
|
|
auto find(const T& key) const -> maybe<U&> {
|
2014-02-09 05:59:46 +00:00
|
|
|
if(auto node = root.find({key})) return node().value;
|
|
|
|
return nothing;
|
2012-01-15 08:29:57 +00:00
|
|
|
}
|
|
|
|
|
2015-07-14 09:32:43 +00:00
|
|
|
auto insert(const T& key, const U& value) -> void { root.insert({key, value}); }
|
|
|
|
auto remove(const T& key) -> void { root.remove({key}); }
|
|
|
|
auto size() const -> unsigned { return root.size(); }
|
|
|
|
auto reset() -> void { root.reset(); }
|
2012-01-26 06:50:09 +00:00
|
|
|
|
2015-07-14 09:32:43 +00:00
|
|
|
auto begin() -> typename set<node_t>::iterator { return root.begin(); }
|
|
|
|
auto end() -> typename set<node_t>::iterator { return root.end(); }
|
|
|
|
|
|
|
|
auto begin() const -> const typename set<node_t>::iterator { return root.begin(); }
|
|
|
|
auto end() const -> const typename set<node_t>::iterator { return root.end(); }
|
2012-01-15 08:29:57 +00:00
|
|
|
|
|
|
|
protected:
|
2013-05-02 11:25:45 +00:00
|
|
|
set<node_t> root;
|
2012-01-15 08:29:57 +00:00
|
|
|
};
|
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
template<typename T, typename U> struct bimap {
|
2015-07-14 09:32:43 +00:00
|
|
|
auto find(const T& key) const -> maybe<U&> { return tmap.find(key); }
|
|
|
|
auto find(const U& key) const -> maybe<T&> { return umap.find(key); }
|
|
|
|
auto insert(const T& key, const U& value) -> void { tmap.insert(key, value); umap.insert(value, key); }
|
|
|
|
auto remove(const T& key) -> void { if(auto p = tmap.find(key)) { umap.remove(p().value); tmap.remove(key); } }
|
|
|
|
auto remove(const U& key) -> void { if(auto p = umap.find(key)) { tmap.remove(p().value); umap.remove(key); } }
|
|
|
|
auto size() const -> unsigned { return tmap.size(); }
|
|
|
|
auto reset() -> void { tmap.reset(); umap.reset(); }
|
|
|
|
|
|
|
|
auto begin() -> typename set<typename map<T, U>::node_t>::iterator { return tmap.begin(); }
|
|
|
|
auto end() -> typename set<typename map<T, U>::node_t>::iterator { return tmap.end(); }
|
|
|
|
|
|
|
|
auto begin() const -> const typename set<typename map<T, U>::node_t>::iterator { return tmap.begin(); }
|
|
|
|
auto end() const -> const typename set<typename map<T, U>::node_t>::iterator { return tmap.end(); }
|
2012-01-26 06:50:09 +00:00
|
|
|
|
|
|
|
protected:
|
2013-05-02 11:25:45 +00:00
|
|
|
map<T, U> tmap;
|
|
|
|
map<U, T> umap;
|
2012-01-15 08:29:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|