| 1 |
#ifndef WX_ORDERED_MAP_H |
| 2 |
#define WX_ORDERED_MAP_H |
| 3 |
|
| 4 |
#include <list> |
| 5 |
#include <map> |
| 6 |
|
| 7 |
/// A container that allows fast insertions / retrieval of elements |
| 8 |
/// And also maintains the order of which elements where inserted |
| 9 |
template <typename Key, typename Value> class wxOrderedMap |
| 10 |
{ |
| 11 |
typedef std::pair<Key, Value> Pair_t; |
| 12 |
typedef std::list<Pair_t> List_t; |
| 13 |
typedef std::map<Key, typename List_t::iterator> Map_t; |
| 14 |
|
| 15 |
public: |
| 16 |
typedef typename List_t::const_iterator ConstIterator; |
| 17 |
typedef typename List_t::const_iterator const_iterator; |
| 18 |
typedef typename List_t::iterator Iterator; |
| 19 |
typedef typename List_t::iterator iterator; |
| 20 |
|
| 21 |
protected: |
| 22 |
Map_t m_map; |
| 23 |
List_t m_list; |
| 24 |
|
| 25 |
public: |
| 26 |
wxOrderedMap() {} |
| 27 |
virtual ~wxOrderedMap() {} |
| 28 |
|
| 29 |
wxOrderedMap(const wxOrderedMap& rhs) |
| 30 |
{ |
| 31 |
if(this == &rhs) return; |
| 32 |
*this = rhs; |
| 33 |
} |
| 34 |
|
| 35 |
wxOrderedMap<Key, Value>& operator=(const wxOrderedMap& rhs) |
| 36 |
{ |
| 37 |
if(this == &rhs) return *this; |
| 38 |
|
| 39 |
Clear(); |
| 40 |
m_map.insert(rhs.m_map.begin(), rhs.m_map.end()); |
| 41 |
m_list.insert(m_list.end(), rhs.m_list.begin(), rhs.m_list.end()); |
| 42 |
return *this; |
| 43 |
} |
| 44 |
|
| 45 |
void Clear() |
| 46 |
{ |
| 47 |
m_map.clear(); |
| 48 |
m_list.clear(); |
| 49 |
} |
| 50 |
|
| 51 |
void PushBack(const Key& k, const Value& v) |
| 52 |
{ |
| 53 |
if(Contains(k)) { |
| 54 |
// we already got an instance of this item |
| 55 |
Remove(k); |
| 56 |
} |
| 57 |
Iterator iter = m_list.insert(m_list.end(), Pair_t(k, v)); |
| 58 |
m_map.insert(std::make_pair(k, iter)); |
| 59 |
} |
| 60 |
|
| 61 |
void PushFront(const Key& k, const Value& v) |
| 62 |
{ |
| 63 |
if(Contains(k)) { |
| 64 |
// we already got an instance of this item |
| 65 |
Remove(k); |
| 66 |
} |
| 67 |
Iterator iter = m_list.insert(m_list.begin(), Pair_t(k, v)); |
| 68 |
m_map.insert(std::make_pair(k, iter)); |
| 69 |
} |
| 70 |
|
| 71 |
Value& Item(const Key& k) |
| 72 |
{ |
| 73 |
static Value NullValue; |
| 74 |
typename Map_t::iterator iter = m_map.find(k); |
| 75 |
if(iter == m_map.end()) |
| 76 |
return NullValue; |
| 77 |
else { |
| 78 |
return iter->second->second; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
const Value& Item(const Key& k) const |
| 83 |
{ |
| 84 |
static Value NullValue; |
| 85 |
typename Map_t::const_iterator iter = m_map.find(k); |
| 86 |
if(iter == m_map.end()) |
| 87 |
return NullValue; |
| 88 |
else { |
| 89 |
return iter->second->second; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
bool Contains(const Key& k) const { return m_map.count(k); } |
| 94 |
|
| 95 |
void Remove(const Key& k) |
| 96 |
{ |
| 97 |
typename Map_t::iterator iter = m_map.find(k); |
| 98 |
if(iter == m_map.end()) { |
| 99 |
// nothing to remove here |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
m_list.erase(iter->second); |
| 104 |
m_map.erase(iter); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* @brief is empty? |
| 109 |
*/ |
| 110 |
bool IsEmpty() const { return m_list.empty(); } |
| 111 |
|
| 112 |
// end() |
| 113 |
ConstIterator End() const { return m_list.end(); } |
| 114 |
|
| 115 |
Iterator End() { return m_list.end(); } |
| 116 |
|
| 117 |
const_iterator end() const { return m_list.end(); } |
| 118 |
|
| 119 |
iterator end() { return m_list.end(); } |
| 120 |
|
| 121 |
// begin() |
| 122 |
Iterator Begin() { return m_list.begin(); } |
| 123 |
|
| 124 |
ConstIterator Begin() const { return m_list.begin(); } |
| 125 |
|
| 126 |
const_iterator begin() const { return m_list.begin(); } |
| 127 |
|
| 128 |
iterator begin() { return m_list.begin(); } |
| 129 |
|
| 130 |
void DeleteValues() |
| 131 |
{ |
| 132 |
typename List_t::iterator iter = m_list.begin(); |
| 133 |
for(; iter != m_list.end(); ++iter) { |
| 134 |
Value v = (*iter).second; |
| 135 |
delete v; |
| 136 |
} |
| 137 |
Clear(); |
| 138 |
} |
| 139 |
}; |
| 140 |
|
| 141 |
#endif // WX_ORDERED_MAP_H |