2012-01-15 08:29:57 +00:00
|
|
|
#ifndef NALL_MAP_HPP
|
|
|
|
#define NALL_MAP_HPP
|
|
|
|
|
|
|
|
#include <nall/vector.hpp>
|
|
|
|
|
|
|
|
namespace nall {
|
|
|
|
|
|
|
|
template<typename LHS, typename RHS>
|
|
|
|
struct map {
|
|
|
|
struct pair {
|
|
|
|
LHS name;
|
|
|
|
RHS data;
|
|
|
|
};
|
|
|
|
|
2012-01-26 06:50:09 +00:00
|
|
|
inline void reset() {
|
2012-01-15 08:29:57 +00:00
|
|
|
list.reset();
|
|
|
|
}
|
|
|
|
|
2012-01-26 06:50:09 +00:00
|
|
|
inline unsigned size() const {
|
2012-01-15 08:29:57 +00:00
|
|
|
return list.size();
|
|
|
|
}
|
|
|
|
|
2012-01-26 06:50:09 +00:00
|
|
|
//O(log n) find
|
|
|
|
inline optional<unsigned> find(const LHS &name) const {
|
|
|
|
signed first = 0, last = size() - 1;
|
|
|
|
while(first <= last) {
|
|
|
|
signed middle = (first + last) / 2;
|
|
|
|
if(name < list[middle].name) last = middle - 1; //search lower half
|
|
|
|
else if(list[middle].name < name) first = middle + 1; //search upper half
|
2012-03-26 10:13:02 +00:00
|
|
|
else return { true, (unsigned)middle }; //match found
|
2012-01-26 06:50:09 +00:00
|
|
|
}
|
|
|
|
return { false, 0u };
|
|
|
|
}
|
|
|
|
|
|
|
|
//O(n) insert + O(log n) find
|
|
|
|
inline RHS& insert(const LHS &name, const RHS &data) {
|
|
|
|
if(auto position = find(name)) {
|
|
|
|
list[position()].data = data;
|
|
|
|
return list[position()].data;
|
|
|
|
}
|
2012-01-15 08:29:57 +00:00
|
|
|
signed offset = size();
|
|
|
|
for(unsigned n = 0; n < size(); n++) {
|
|
|
|
if(name < list[n].name) { offset = n; break; }
|
|
|
|
}
|
|
|
|
list.insert(offset, { name, data });
|
2012-01-26 06:50:09 +00:00
|
|
|
return list[offset].data;
|
2012-01-15 08:29:57 +00:00
|
|
|
}
|
|
|
|
|
2012-01-26 06:50:09 +00:00
|
|
|
//O(log n) find
|
|
|
|
inline void modify(const LHS &name, const RHS &data) {
|
|
|
|
if(auto position = find(name)) list[position()].data = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
//O(n) remove + O(log n) find
|
|
|
|
inline void remove(const LHS &name) {
|
|
|
|
if(auto position = find(name)) list.remove(position());
|
|
|
|
}
|
|
|
|
|
|
|
|
//O(log n) find
|
|
|
|
inline RHS& operator[](const LHS &name) {
|
|
|
|
if(auto position = find(name)) return list[position()].data;
|
2012-01-15 08:29:57 +00:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2012-01-26 06:50:09 +00:00
|
|
|
inline const RHS& operator[](const LHS &name) const {
|
|
|
|
if(auto position = find(name)) return list[position()].data;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline RHS& operator()(const LHS &name) {
|
2012-04-29 23:58:41 +00:00
|
|
|
if(auto position = find(name)) return list[position()].data;
|
2012-01-26 06:50:09 +00:00
|
|
|
return insert(name, RHS());
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const RHS& operator()(const LHS &name, const RHS &data) const {
|
|
|
|
if(auto position = find(name)) return list[position()].data;
|
2012-01-15 08:29:57 +00:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2012-01-26 06:50:09 +00:00
|
|
|
inline pair* begin() { return list.begin(); }
|
|
|
|
inline pair* end() { return list.end(); }
|
|
|
|
inline const pair* begin() const { return list.begin(); }
|
|
|
|
inline const pair* end() const { return list.end(); }
|
2012-01-15 08:29:57 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
vector<pair> list;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename LHS, typename RHS>
|
|
|
|
struct bidirectional_map {
|
2012-01-26 06:50:09 +00:00
|
|
|
const map<LHS, RHS> &lhs;
|
|
|
|
const map<RHS, LHS> &rhs;
|
2012-01-15 08:29:57 +00:00
|
|
|
|
2012-01-26 06:50:09 +00:00
|
|
|
inline void reset() {
|
|
|
|
llist.reset();
|
|
|
|
rlist.reset();
|
2012-01-15 08:29:57 +00:00
|
|
|
}
|
|
|
|
|
2012-01-26 06:50:09 +00:00
|
|
|
inline unsigned size() const {
|
|
|
|
return llist.size();
|
2012-01-15 08:29:57 +00:00
|
|
|
}
|
|
|
|
|
2012-01-26 06:50:09 +00:00
|
|
|
inline void insert(const LHS &ldata, const RHS &rdata) {
|
|
|
|
llist.insert(ldata, rdata);
|
|
|
|
rlist.insert(rdata, ldata);
|
2012-01-15 08:29:57 +00:00
|
|
|
}
|
2012-01-26 06:50:09 +00:00
|
|
|
|
|
|
|
inline bidirectional_map() : lhs(llist), rhs(rlist) {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
map<LHS, RHS> llist;
|
|
|
|
map<RHS, LHS> rlist;
|
2012-01-15 08:29:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|