| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// copyright : (C) 2014 Eran Ifrah |
| 5 |
// file name : wx_ordered_map.h |
| 6 |
// |
| 7 |
// ------------------------------------------------------------------------- |
| 8 |
// A |
| 9 |
// _____ _ _ _ _ |
| 10 |
// / __ \ | | | | (_) | |
| 11 |
// | / \/ ___ __| | ___| | _| |_ ___ |
| 12 |
// | | / _ \ / _ |/ _ \ | | | __/ _ ) |
| 13 |
// | \__/\ (_) | (_| | __/ |___| | || __/ |
| 14 |
// \____/\___/ \__,_|\___\_____/_|\__\___| |
| 15 |
// |
| 16 |
// F i l e |
| 17 |
// |
| 18 |
// This program is free software; you can redistribute it and/or modify |
| 19 |
// it under the terms of the GNU General Public License as published by |
| 20 |
// the Free Software Foundation; either version 2 of the License, or |
| 21 |
// (at your option) any later version. |
| 22 |
// |
| 23 |
////////////////////////////////////////////////////////////////////////////// |
| 24 |
////////////////////////////////////////////////////////////////////////////// |
| 25 |
|
| 26 |
#ifndef WX_ORDERED_MAP_H |
| 27 |
#define WX_ORDERED_MAP_H |
| 28 |
|
| 29 |
#include "codelite_exports.h" |
| 30 |
|
| 31 |
#include <list> |
| 32 |
#include <map> |
| 33 |
|
| 34 |
/// A container that allows fast insertions / retrieval of elements |
| 35 |
/// And also maintains the order of which elements where inserted |
| 36 |
template <typename Key, typename Value> class wxOrderedMap |
| 37 |
{ |
| 38 |
typedef std::pair<Key, Value> Pair_t; |
| 39 |
typedef std::list<Pair_t> List_t; |
| 40 |
typedef std::map<Key, typename List_t::iterator> Map_t; |
| 41 |
|
| 42 |
public: |
| 43 |
typedef typename List_t::const_iterator ConstIterator; |
| 44 |
typedef typename List_t::const_iterator const_iterator; |
| 45 |
typedef typename List_t::iterator Iterator; |
| 46 |
typedef typename List_t::iterator iterator; |
| 47 |
|
| 48 |
protected: |
| 49 |
Map_t m_map; |
| 50 |
List_t m_list; |
| 51 |
|
| 52 |
public: |
| 53 |
wxOrderedMap() {} |
| 54 |
virtual ~wxOrderedMap() { Clear(); } |
| 55 |
|
| 56 |
wxOrderedMap(const wxOrderedMap& rhs) |
| 57 |
{ |
| 58 |
if(this == &rhs) |
| 59 |
return; |
| 60 |
*this = rhs; |
| 61 |
} |
| 62 |
|
| 63 |
wxOrderedMap<Key, Value>& operator=(const wxOrderedMap<Key, Value>& rhs) |
| 64 |
{ |
| 65 |
if(this == &rhs) { |
| 66 |
return *this; |
| 67 |
} |
| 68 |
|
| 69 |
Clear(); |
| 70 |
m_map.insert(rhs.m_map.begin(), rhs.m_map.end()); |
| 71 |
m_list.insert(m_list.end(), rhs.m_list.begin(), rhs.m_list.end()); |
| 72 |
return *this; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* @brief fast swap implementation |
| 77 |
*/ |
| 78 |
void Swap(wxOrderedMap<Key, Value>& other) |
| 79 |
{ |
| 80 |
if(this == &other) { |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
m_map.swap(other.m_map); |
| 85 |
m_list.swap(other.m_list); |
| 86 |
} |
| 87 |
|
| 88 |
void Clear() |
| 89 |
{ |
| 90 |
m_map.clear(); |
| 91 |
m_list.clear(); |
| 92 |
} |
| 93 |
|
| 94 |
void PushBack(const Key& k, const Value& v) |
| 95 |
{ |
| 96 |
if(Contains(k)) { |
| 97 |
// we already got an instance of this item |
| 98 |
Remove(k); |
| 99 |
} |
| 100 |
Iterator iter = m_list.insert(m_list.end(), Pair_t(k, v)); |
| 101 |
m_map.insert(std::make_pair(k, iter)); |
| 102 |
} |
| 103 |
|
| 104 |
void PushFront(const Key& k, const Value& v) |
| 105 |
{ |
| 106 |
if(Contains(k)) { |
| 107 |
// we already got an instance of this item |
| 108 |
Remove(k); |
| 109 |
} |
| 110 |
Iterator iter = m_list.insert(m_list.begin(), Pair_t(k, v)); |
| 111 |
m_map.insert(std::make_pair<Key, Iterator>(k, iter)); |
| 112 |
} |
| 113 |
|
| 114 |
Value& Item(const Key& k) |
| 115 |
{ |
| 116 |
static Value NullValue; |
| 117 |
typename Map_t::iterator iter = m_map.find(k); |
| 118 |
if(iter == m_map.end()) |
| 119 |
return NullValue; |
| 120 |
else { |
| 121 |
return iter->second->second; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
const Value& Item(const Key& k) const |
| 126 |
{ |
| 127 |
static Value NullValue; |
| 128 |
typename Map_t::const_iterator iter = m_map.find(k); |
| 129 |
if(iter == m_map.end()) |
| 130 |
return NullValue; |
| 131 |
else { |
| 132 |
return iter->second->second; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
bool Contains(const Key& k) const { return m_map.count(k); } |
| 137 |
|
| 138 |
void Remove(const Key& k) |
| 139 |
{ |
| 140 |
typename Map_t::iterator iter = m_map.find(k); |
| 141 |
if(iter == m_map.end()) { |
| 142 |
// nothing to remove here |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
m_list.erase(iter->second); |
| 147 |
m_map.erase(iter); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* @brief is empty? |
| 152 |
*/ |
| 153 |
bool IsEmpty() const { return m_list.empty(); } |
| 154 |
|
| 155 |
// end() |
| 156 |
ConstIterator End() const { return m_list.end(); } |
| 157 |
|
| 158 |
Iterator End() { return m_list.end(); } |
| 159 |
|
| 160 |
const_iterator end() const { return m_list.end(); } |
| 161 |
|
| 162 |
iterator end() { return m_list.end(); } |
| 163 |
|
| 164 |
// begin() |
| 165 |
Iterator Begin() { return m_list.begin(); } |
| 166 |
|
| 167 |
ConstIterator Begin() const { return m_list.begin(); } |
| 168 |
|
| 169 |
const_iterator begin() const { return m_list.begin(); } |
| 170 |
|
| 171 |
iterator begin() { return m_list.begin(); } |
| 172 |
|
| 173 |
void DeleteValues() |
| 174 |
{ |
| 175 |
typename List_t::iterator iter = m_list.begin(); |
| 176 |
for(; iter != m_list.end(); ++iter) { |
| 177 |
Value v = (*iter).second; |
| 178 |
delete v; |
| 179 |
} |
| 180 |
Clear(); |
| 181 |
} |
| 182 |
}; |
| 183 |
|
| 184 |
#endif // WX_ORDERED_MAP_H |