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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2017-2023 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
/*========================== begin_copyright_notice ============================
This file is distributed under the University of Illinois Open Source License.
See LICENSE.TXT for details.
============================= end_copyright_notice ===========================*/
#pragma once
#include <map>
#include <list>
namespace IGC {
// This class implements a map that also provides access to all stored values
// in a deterministic order. The values are kept in a std::list and the
// mapping is done with DenseMap from Keys to indexes in that vector.
// Inspired by LLVM's stable data-structures.
template <typename KeyT, typename ValueT> class MapList {
using ListType = std::list<std::pair<KeyT, ValueT>>;
using iterator_type = typename ListType::iterator;
using MapType = std::map<KeyT, iterator_type>;
using SizeType = typename ListType::size_type;
MapType Map;
ListType List;
public:
using iterator = iterator_type;
using const_iterator = typename ListType::const_iterator;
SizeType size() const { return List.size(); }
iterator begin() { return List.begin(); }
const_iterator begin() const { return List.begin(); }
iterator end() { return List.end(); }
const_iterator end() const { return List.end(); }
bool empty() const { return List.empty(); }
std::pair<KeyT, ValueT> &front() { return List.front(); }
const std::pair<KeyT, ValueT> &front() const { return List.front(); }
std::pair<KeyT, ValueT> &back() { return List.back(); }
const std::pair<KeyT, ValueT> &back() const { return List.back(); }
void clear() {
Map.clear();
List.clear();
}
ValueT &operator[](const KeyT &Key) {
auto Pair = std::make_pair(Key, end());
auto Result = Map.insert(Pair);
iterator &I = Result.first->second;
if (Result.second)
I = List.insert(end(), std::make_pair(Key, ValueT()));
return (*I).second;
}
ValueT lookup(const KeyT &Key) const {
auto Pos = Map.find(Key);
return Pos == Map.end() ? ValueT() : *(Pos->second).second;
}
std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &KV) {
auto Pair = std::make_pair(KV.first, end());
auto Result = Map.insert(Pair);
iterator &I = Result.first->second;
if (Result.second) {
I = List.insert(end(), std::make_pair(KV.first, KV.second));
return std::make_pair(I, true);
}
return std::make_pair(I, false);
}
unsigned count(const KeyT &Key) const { return Map.count(Key); }
iterator find(const KeyT &Key) {
auto Pos = Map.find(Key);
return Pos == Map.end() ? end() : Pos->second;
}
const_iterator find(const KeyT &Key) const {
auto Pos = Map.find(Key);
return Pos == Map.end() ? end() : Pos->second;
}
void pop_back() {
auto Pos = Map.find(back().first);
Map.erase(Pos);
List.pop_back();
}
void erase(const iterator &I) {
auto Pos = Map.find((*I).first);
Map.erase(Pos);
List.erase(I);
}
};
} // namespace IGC
|