File indexing completed on 2026-05-10 08:43:07
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #ifndef LLVM_ADT_PRIORITYWORKLIST_H
0016 #define LLVM_ADT_PRIORITYWORKLIST_H
0017
0018 #include "llvm/ADT/DenseMap.h"
0019 #include "llvm/ADT/STLExtras.h"
0020 #include "llvm/ADT/SmallVector.h"
0021 #include "llvm/Support/Compiler.h"
0022 #include <cassert>
0023 #include <cstddef>
0024 #include <iterator>
0025 #include <type_traits>
0026 #include <vector>
0027
0028 namespace llvm {
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053 template <typename T, typename VectorT = std::vector<T>,
0054 typename MapT = DenseMap<T, ptrdiff_t>>
0055 class PriorityWorklist {
0056 public:
0057 using value_type = T;
0058 using key_type = T;
0059 using reference = T&;
0060 using const_reference = const T&;
0061 using size_type = typename MapT::size_type;
0062
0063
0064 PriorityWorklist() = default;
0065
0066
0067 bool empty() const {
0068 return V.empty();
0069 }
0070
0071
0072 size_type size() const {
0073 return M.size();
0074 }
0075
0076
0077
0078 size_type count(const key_type &key) const {
0079 return M.count(key);
0080 }
0081
0082
0083 const T &back() const {
0084 assert(!empty() && "Cannot call back() on empty PriorityWorklist!");
0085 return V.back();
0086 }
0087
0088
0089
0090 bool insert(const T &X) {
0091 assert(X != T() && "Cannot insert a null (default constructed) value!");
0092 auto InsertResult = M.insert({X, V.size()});
0093 if (InsertResult.second) {
0094
0095 V.push_back(X);
0096 return true;
0097 }
0098
0099 auto &Index = InsertResult.first->second;
0100 assert(V[Index] == X && "Value not actually at index in map!");
0101 if (Index != (ptrdiff_t)(V.size() - 1)) {
0102
0103 V[Index] = T();
0104 Index = (ptrdiff_t)V.size();
0105 V.push_back(X);
0106 }
0107 return false;
0108 }
0109
0110
0111 template <typename SequenceT>
0112 std::enable_if_t<!std::is_convertible<SequenceT, T>::value>
0113 insert(SequenceT &&Input) {
0114 if (std::begin(Input) == std::end(Input))
0115
0116 return;
0117
0118
0119
0120 ptrdiff_t StartIndex = V.size();
0121 V.insert(V.end(), std::begin(Input), std::end(Input));
0122
0123 for (ptrdiff_t i = V.size() - 1; i >= StartIndex; --i) {
0124 auto InsertResult = M.insert({V[i], i});
0125 if (InsertResult.second)
0126 continue;
0127
0128
0129
0130 ptrdiff_t &Index = InsertResult.first->second;
0131 if (Index < StartIndex) {
0132 V[Index] = T();
0133 Index = i;
0134 continue;
0135 }
0136
0137
0138
0139 V[i] = T();
0140 }
0141 }
0142
0143
0144 void pop_back() {
0145 assert(!empty() && "Cannot remove an element when empty!");
0146 assert(back() != T() && "Cannot have a null element at the back!");
0147 M.erase(back());
0148 do {
0149 V.pop_back();
0150 } while (!V.empty() && V.back() == T());
0151 }
0152
0153 [[nodiscard]] T pop_back_val() {
0154 T Ret = back();
0155 pop_back();
0156 return Ret;
0157 }
0158
0159
0160
0161
0162 bool erase(const T& X) {
0163 auto I = M.find(X);
0164 if (I == M.end())
0165 return false;
0166
0167 assert(V[I->second] == X && "Value not actually at index in map!");
0168 if (I->second == (ptrdiff_t)(V.size() - 1)) {
0169 do {
0170 V.pop_back();
0171 } while (!V.empty() && V.back() == T());
0172 } else {
0173 V[I->second] = T();
0174 }
0175 M.erase(I);
0176 return true;
0177 }
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192 template <typename UnaryPredicate>
0193 bool erase_if(UnaryPredicate P) {
0194 typename VectorT::iterator E =
0195 remove_if(V, TestAndEraseFromMap<UnaryPredicate>(P, M));
0196 if (E == V.end())
0197 return false;
0198 for (auto I = V.begin(); I != E; ++I)
0199 if (*I != T())
0200 M[*I] = I - V.begin();
0201 V.erase(E, V.end());
0202 return true;
0203 }
0204
0205
0206
0207
0208
0209
0210
0211 void clear() {
0212 M.clear();
0213 V.clear();
0214 }
0215
0216 private:
0217
0218
0219
0220
0221
0222
0223 template <typename UnaryPredicateT>
0224 class TestAndEraseFromMap {
0225 UnaryPredicateT P;
0226 MapT &M;
0227
0228 public:
0229 TestAndEraseFromMap(UnaryPredicateT P, MapT &M)
0230 : P(std::move(P)), M(M) {}
0231
0232 bool operator()(const T &Arg) {
0233 if (Arg == T())
0234
0235 return false;
0236
0237 if (P(Arg)) {
0238 M.erase(Arg);
0239 return true;
0240 }
0241 return false;
0242 }
0243 };
0244
0245
0246 MapT M;
0247
0248
0249 VectorT V;
0250 };
0251
0252
0253
0254 template <typename T, unsigned N>
0255 class SmallPriorityWorklist
0256 : public PriorityWorklist<T, SmallVector<T, N>,
0257 SmallDenseMap<T, ptrdiff_t>> {
0258 public:
0259 SmallPriorityWorklist() = default;
0260 };
0261
0262 }
0263
0264 #endif