| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | #ifndef TRISTRIP_HEAP_ARRAY_H |
|---|
| 45 | #define TRISTRIP_HEAP_ARRAY_H |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | namespace common_structures { |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | template <class T, class CmpT = std::less<T> > |
|---|
| 55 | class heap_array |
|---|
| 56 | { |
|---|
| 57 | public: |
|---|
| 58 | |
|---|
| 59 | struct heap_is_locked { }; |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | heap_array() : m_Locked(false) { } |
|---|
| 65 | |
|---|
| 66 | void clear(); |
|---|
| 67 | |
|---|
| 68 | void reserve(size_t Size); |
|---|
| 69 | size_t size() const; |
|---|
| 70 | |
|---|
| 71 | bool empty() const; |
|---|
| 72 | bool locked() const; |
|---|
| 73 | bool removed(size_t i) const; |
|---|
| 74 | bool valid(size_t i) const; |
|---|
| 75 | |
|---|
| 76 | const T & top() const; |
|---|
| 77 | const T & peek(size_t i) const; |
|---|
| 78 | const T & operator [] (size_t i) const; |
|---|
| 79 | |
|---|
| 80 | size_t push(const T & Elem); |
|---|
| 81 | |
|---|
| 82 | void pop(); |
|---|
| 83 | void erase(size_t i); |
|---|
| 84 | void update(size_t i, const T & Elem); |
|---|
| 85 | |
|---|
| 86 | protected: |
|---|
| 87 | |
|---|
| 88 | struct linker { |
|---|
| 89 | linker(const T & Elem, size_t i) : m_Elem(Elem), m_Indice(i) { } |
|---|
| 90 | |
|---|
| 91 | T m_Elem; |
|---|
| 92 | size_t m_Indice; |
|---|
| 93 | }; |
|---|
| 94 | |
|---|
| 95 | typedef std::vector<linker> linked_heap; |
|---|
| 96 | typedef std::vector<size_t> finder; |
|---|
| 97 | |
|---|
| 98 | void Adjust(size_t i); |
|---|
| 99 | void Swap(size_t a, size_t b); |
|---|
| 100 | bool Less(const linker & a, const linker & b) const; |
|---|
| 101 | |
|---|
| 102 | linked_heap m_Heap; |
|---|
| 103 | finder m_Finder; |
|---|
| 104 | CmpT m_Compare; |
|---|
| 105 | bool m_Locked; |
|---|
| 106 | }; |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | template <class T, class CmpT> |
|---|
| 116 | inline void heap_array<T, CmpT>::clear() { |
|---|
| 117 | m_Heap.clear(); |
|---|
| 118 | m_Finder.clear(); |
|---|
| 119 | m_Locked = false; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | template <class T, class CmpT> |
|---|
| 124 | inline bool heap_array<T, CmpT>::empty() const { |
|---|
| 125 | return m_Heap.empty(); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | template <class T, class CmpT> |
|---|
| 130 | inline bool heap_array<T, CmpT>::locked() const { |
|---|
| 131 | return m_Locked; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | template <class T, class CmpT> |
|---|
| 136 | inline void heap_array<T, CmpT>::reserve(size_t Size) { |
|---|
| 137 | m_Heap.reserve(Size); |
|---|
| 138 | m_Finder.reserve(Size); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | template <class T, class CmpT> |
|---|
| 143 | inline size_t heap_array<T, CmpT>::size() const { |
|---|
| 144 | return m_Heap.size(); |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | template <class T, class CmpT> |
|---|
| 149 | inline const T & heap_array<T, CmpT>::top() const { |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | if (empty()) throw "heap_array<T, CmpT>::top() error, heap empty"; |
|---|
| 153 | |
|---|
| 154 | return m_Heap.front().m_Elem; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | template <class T, class CmpT> |
|---|
| 159 | inline const T & heap_array<T, CmpT>::peek(size_t i) const { |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | if (removed(i)) throw "heap_array<T, CmpT>::peek(size_t i) error"; |
|---|
| 163 | |
|---|
| 164 | return (m_Heap[m_Finder[i]].m_Elem); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | template <class T, class CmpT> |
|---|
| 169 | inline const T & heap_array<T, CmpT>::operator [] (size_t i) const { |
|---|
| 170 | return peek(i); |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | template <class T, class CmpT> |
|---|
| 175 | inline void heap_array<T, CmpT>::pop() { |
|---|
| 176 | m_Locked = true; |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | if (empty()) throw "heap_array<T, CmpT>::pop() error, heap empty"; |
|---|
| 181 | |
|---|
| 182 | Swap(0, size() - 1); |
|---|
| 183 | m_Heap.pop_back(); |
|---|
| 184 | Adjust(0); |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | template <class T, class CmpT> |
|---|
| 189 | inline size_t heap_array<T, CmpT>::push(const T & Elem) { |
|---|
| 190 | if (m_Locked) |
|---|
| 191 | throw "heap_is_locked"; |
|---|
| 192 | |
|---|
| 193 | size_t Id = size(); |
|---|
| 194 | m_Finder.push_back(Id); |
|---|
| 195 | m_Heap.push_back(linker(Elem, Id)); |
|---|
| 196 | Adjust(Id); |
|---|
| 197 | |
|---|
| 198 | return Id; |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | template <class T, class CmpT> |
|---|
| 203 | inline void heap_array<T, CmpT>::erase(size_t i) { |
|---|
| 204 | m_Locked = true; |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | if (removed(i)) throw "heap_array<T, CmpT>::erase(size_t i) error"; |
|---|
| 208 | |
|---|
| 209 | size_t j = m_Finder[i]; |
|---|
| 210 | |
|---|
| 211 | if (j==m_Heap.size()-1) |
|---|
| 212 | { |
|---|
| 213 | m_Heap.pop_back(); |
|---|
| 214 | } |
|---|
| 215 | else |
|---|
| 216 | { |
|---|
| 217 | Swap(j, size() - 1); |
|---|
| 218 | m_Heap.pop_back(); |
|---|
| 219 | Adjust(j); |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | template <class T, class CmpT> |
|---|
| 227 | inline bool heap_array<T, CmpT>::removed(size_t i) const { |
|---|
| 228 | return (m_Finder[i] >= m_Heap.size()); |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | template <class T, class CmpT> |
|---|
| 233 | inline bool heap_array<T, CmpT>::valid(size_t i) const { |
|---|
| 234 | return (i < m_Finder.size()); |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | template <class T, class CmpT> |
|---|
| 239 | inline void heap_array<T, CmpT>::update(size_t i, const T & Elem) { |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | if (removed(i)) throw "heap_array<T, CmpT>::update(size_t i, const T & Elem) error"; |
|---|
| 243 | |
|---|
| 244 | size_t j = m_Finder[i]; |
|---|
| 245 | m_Heap[j].m_Elem = Elem; |
|---|
| 246 | Adjust(j); |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | |
|---|
| 250 | template <class T, class CmpT> |
|---|
| 251 | inline void heap_array<T, CmpT>::Adjust(size_t i) |
|---|
| 252 | { |
|---|
| 253 | if (m_Heap.size()<=1) return; |
|---|
| 254 | |
|---|
| 255 | size_t j; |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | for (j = i; (j > 0) && (Less(m_Heap[(j - 1) / 2], m_Heap[j])); j = ((j - 1) / 2)) |
|---|
| 259 | Swap(j, (j - 1) / 2); |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | for (i = j; (j = 2 * i + 1) < size(); i = j) { |
|---|
| 263 | if ((j + 1 < size()) && (Less(m_Heap[j], m_Heap[j + 1]))) |
|---|
| 264 | ++j; |
|---|
| 265 | |
|---|
| 266 | if (Less(m_Heap[j], m_Heap[i])) |
|---|
| 267 | return; |
|---|
| 268 | |
|---|
| 269 | Swap(i, j); |
|---|
| 270 | } |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | template <class T, class CmpT> |
|---|
| 275 | inline void heap_array<T, CmpT>::Swap(size_t a, size_t b) { |
|---|
| 276 | std::swap(m_Heap[a], m_Heap[b]); |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | (size_t &) (m_Finder[(m_Heap[a].m_Indice)]) = a; |
|---|
| 280 | (size_t &) (m_Finder[(m_Heap[b].m_Indice)]) = b; |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | |
|---|
| 284 | template <class T, class CmpT> |
|---|
| 285 | inline bool heap_array<T, CmpT>::Less(const linker & a, const linker & b) const { |
|---|
| 286 | return m_Compare(a.m_Elem, b.m_Elem); |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | #endif |
|---|