llvm.org GIT mirror llvm / 5c54a09
[ADT] Add a new data structure for managing a priority worklist where re-insertion of entries into the worklist moves them to the end. This is fairly similar to a SetVector, but helps in the case where in addition to not inserting duplicates you want to adjust the sequence of a pop-off-the-back worklist. I'm not at all attached to the name of this data structure if others have better suggestions, but this is one that David Majnemer brought up in IRC discussions that seems plausible. I've trimmed the interface down somewhat from SetVector's interface because several things make less sense here IMO: iteration primarily. I'd prefer to add these back as we have users that need them. My use case doesn't even need all of what is provided here. =] I've also included a basic unittest to make sure this functions reasonably. Differential Revision: http://reviews.llvm.org/D21866 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@274198 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 4 years ago
3 changed file(s) with 331 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 //===- PriorityWorklist.h - Worklist with insertion priority ----*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 ///
11 /// This file provides a priority worklist. See the class comments for details.
12 ///
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_ADT_PRIORITYWORKLIST_H
16 #define LLVM_ADT_PRIORITYWORKLIST_H
17
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include
21 #include
22 #include
23 #include
24
25 namespace llvm {
26
27 /// A FILO worklist that prioritizes on re-insertion without duplication.
28 ///
29 /// This is very similar to a \c SetVector with the primary difference that
30 /// while re-insertion does not create a duplicate, it does adjust the
31 /// visitation order to respect the last insertion point. This can be useful
32 /// when the visit order needs to be prioritized based on insertion point
33 /// without actually having duplicate visits.
34 ///
35 /// Note that this doesn't prevent re-insertion of elements which have been
36 /// visited -- if you need to break cycles, a set will still be necessary.
37 ///
38 /// The type \c T must be default constructable to a null value that will be
39 /// ignored. It is an error to insert such a value, and popping elements will
40 /// never produce such a value. It is expected to be used with common nullable
41 /// types like pointers or optionals.
42 ///
43 /// Internally this uses a vector to store the worklist and a map to identify
44 /// existing elements in the worklist. Both of these may be customized, but the
45 /// map must support the basic DenseMap API for mapping from a T to an integer
46 /// index into the vector.
47 ///
48 /// A partial specialization is provided to automatically select a SmallVector
49 /// and a SmallDenseMap if custom data structures are not provided.
50 template ,
51 typename MapT = DenseMap>
52 class PriorityWorklist {
53 public:
54 typedef T value_type;
55 typedef T key_type;
56 typedef T& reference;
57 typedef const T& const_reference;
58 typedef typename MapT::size_type size_type;
59
60 /// Construct an empty PriorityWorklist
61 PriorityWorklist() {}
62
63 /// Determine if the PriorityWorklist is empty or not.
64 bool empty() const {
65 return V.empty();
66 }
67
68 /// Returns the number of elements in the worklist.
69 size_type size() const {
70 return M.size();
71 }
72
73 /// Count the number of elements of a given key in the PriorityWorklist.
74 /// \returns 0 if the element is not in the PriorityWorklist, 1 if it is.
75 size_type count(const key_type &key) const {
76 return M.count(key);
77 }
78
79 /// Return the last element of the PriorityWorklist.
80 const T &back() const {
81 assert(!empty() && "Cannot call back() on empty PriorityWorklist!");
82 return V.back();
83 }
84
85 /// Insert a new element into the PriorityWorklist.
86 /// \returns true if the element was inserted into the PriorityWorklist.
87 bool insert(const T &X) {
88 assert(X != T() && "Cannot insert a null (default constructed) value!");
89 auto InsertResult = M.insert({X, V.size()});
90 if (InsertResult.second) {
91 // Fresh value, just append it to the vector.
92 V.push_back(X);
93 return true;
94 }
95
96 auto &Index = InsertResult.first->second;
97 assert(V[Index] == X && "Value not actually at index in map!");
98 if (Index != (ptrdiff_t)(V.size() - 1)) {
99 // If the element isn't at the back, null it out and append a fresh one.
100 V[Index] = T();
101 Index = (ptrdiff_t)V.size();
102 V.push_back(X);
103 }
104 return false;
105 }
106
107 /// Remove the last element of the PriorityWorklist.
108 void pop_back() {
109 assert(!empty() && "Cannot remove an element when empty!");
110 assert(back() != T() && "Cannot have a null element at the back!");
111 M.erase(back());
112 do {
113 V.pop_back();
114 } while (!V.empty() && V.back() == T());
115 }
116
117 T LLVM_ATTRIBUTE_UNUSED_RESULT pop_back_val() {
118 T Ret = back();
119 pop_back();
120 return Ret;
121 }
122
123 /// Erase an item from the worklist.
124 ///
125 /// Note that this is constant time due to the nature of the worklist implementation.
126 bool erase(const T& X) {
127 auto I = M.find(X);
128 if (I == M.end())
129 return false;
130
131 assert(V[I->second] == X && "Value not actually at index in map!");
132 if (I->second == (ptrdiff_t)(V.size() - 1)) {
133 do {
134 V.pop_back();
135 } while (!V.empty() && V.back() == T());
136 } else {
137 V[I->second] = T();
138 }
139 M.erase(I);
140 return true;
141 }
142
143 /// Erase items from the set vector based on a predicate function.
144 ///
145 /// This is intended to be equivalent to the following code, if we could
146 /// write it:
147 ///
148 /// \code
149 /// V.erase(std::remove_if(V.begin(), V.end(), P), V.end());
150 /// \endcode
151 ///
152 /// However, PriorityWorklist doesn't expose non-const iterators, making any
153 /// algorithm like remove_if impossible to use.
154 ///
155 /// \returns true if any element is removed.
156 template
157 bool erase_if(UnaryPredicate P) {
158 typename VectorT::iterator E = std::remove_if(
159 V.begin(), V.end(), TestAndEraseFromMap(P, M));
160 if (E == V.end())
161 return false;
162 for (auto I = V.begin(); I != E; ++I)
163 if (*I != T())
164 M[*I] = I - V.begin();
165 V.erase(E, V.end());
166 return true;
167 }
168
169 /// Completely clear the PriorityWorklist
170 void clear() {
171 M.clear();
172 V.clear();
173 }
174
175 private:
176 /// A wrapper predicate designed for use with std::remove_if.
177 ///
178 /// This predicate wraps a predicate suitable for use with std::remove_if to
179 /// call M.erase(x) on each element which is slated for removal. This just
180 /// allows the predicate to be move only which we can't do with lambdas
181 /// today.
182 template
183 class TestAndEraseFromMap {
184 UnaryPredicateT P;
185 MapT &M;
186
187 public:
188 TestAndEraseFromMap(UnaryPredicateT P, MapT &M)
189 : P(std::move(P)), M(M) {}
190
191 bool operator()(const T &Arg) {
192 if (Arg == T())
193 // Skip null values in the PriorityWorklist.
194 return false;
195
196 if (P(Arg)) {
197 M.erase(Arg);
198 return true;
199 }
200 return false;
201 }
202 };
203
204 /// The map from value to index in the vector.
205 MapT M;
206
207 /// The vector of elements in insertion order.
208 VectorT V;
209 };
210
211 /// A version of \c PriorityWorklist that selects small size optimized data
212 /// structures for the vector and map.
213 template
214 class SmallPriorityWorklist
215 : public PriorityWorklist,
216 SmallDenseMap> {
217 public:
218 SmallPriorityWorklist() {}
219 };
220
221 }
222
223 #endif
2929 PointerSumTypeTest.cpp
3030 PointerUnionTest.cpp
3131 PostOrderIteratorTest.cpp
32 PriorityWorklistTest.cpp
3233 RangeAdapterTest.cpp
3334 SCCIteratorTest.cpp
3435 SequenceTest.cpp
0 //===- llvm/unittest/ADT/PriorityWorklist.cpp -----------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // PriorityWorklist unit tests.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/ADT/PriorityWorklist.h"
14 #include "gtest/gtest.h"
15
16 namespace {
17
18 using namespace llvm;
19
20 template class PriorityWorklistTest : public ::testing::Test {};
21 typedef ::testing::Types, SmallPriorityWorklist>
22 TestTypes;
23 TYPED_TEST_CASE(PriorityWorklistTest, TestTypes);
24
25 TYPED_TEST(PriorityWorklistTest, Basic) {
26 TypeParam W;
27 EXPECT_TRUE(W.empty());
28 EXPECT_EQ(0u, W.size());
29 EXPECT_FALSE(W.count(42));
30
31 EXPECT_TRUE(W.insert(21));
32 EXPECT_TRUE(W.insert(42));
33 EXPECT_TRUE(W.insert(17));
34
35 EXPECT_FALSE(W.empty());
36 EXPECT_EQ(3u, W.size());
37 EXPECT_TRUE(W.count(42));
38
39 EXPECT_FALSE(W.erase(75));
40 EXPECT_EQ(3u, W.size());
41 EXPECT_EQ(17, W.back());
42
43 EXPECT_TRUE(W.erase(17));
44 EXPECT_FALSE(W.count(17));
45 EXPECT_EQ(2u, W.size());
46 EXPECT_EQ(42, W.back());
47
48 W.clear();
49 EXPECT_TRUE(W.empty());
50 EXPECT_EQ(0u, W.size());
51
52 EXPECT_TRUE(W.insert(21));
53 EXPECT_TRUE(W.insert(42));
54 EXPECT_TRUE(W.insert(12));
55 EXPECT_TRUE(W.insert(17));
56 EXPECT_TRUE(W.count(12));
57 EXPECT_TRUE(W.count(17));
58 EXPECT_EQ(4u, W.size());
59 EXPECT_EQ(17, W.back());
60 EXPECT_TRUE(W.erase(12));
61 EXPECT_FALSE(W.count(12));
62 EXPECT_TRUE(W.count(17));
63 EXPECT_EQ(3u, W.size());
64 EXPECT_EQ(17, W.back());
65
66 EXPECT_FALSE(W.insert(42));
67 EXPECT_EQ(3u, W.size());
68 EXPECT_EQ(42, W.pop_back_val());
69 EXPECT_EQ(17, W.pop_back_val());
70 EXPECT_EQ(21, W.pop_back_val());
71 EXPECT_TRUE(W.empty());
72 }
73
74 TYPED_TEST(PriorityWorklistTest, EraseIf) {
75 TypeParam W;
76 W.insert(23);
77 W.insert(10);
78 W.insert(47);
79 W.insert(42);
80 W.insert(23);
81 W.insert(13);
82 W.insert(26);
83 W.insert(42);
84 EXPECT_EQ(6u, W.size());
85
86 EXPECT_FALSE(W.erase_if([](int i) { return i > 100; }));
87 EXPECT_EQ(6u, W.size());
88 EXPECT_EQ(42, W.back());
89
90 EXPECT_TRUE(W.erase_if([](int i) {
91 assert(i != 0 && "Saw a null value!");
92 return (i & 1) == 0;
93 }));
94 EXPECT_EQ(3u, W.size());
95 EXPECT_FALSE(W.count(42));
96 EXPECT_FALSE(W.count(26));
97 EXPECT_FALSE(W.count(10));
98 EXPECT_FALSE(W.insert(47));
99 EXPECT_FALSE(W.insert(23));
100 EXPECT_EQ(23, W.pop_back_val());
101 EXPECT_EQ(47, W.pop_back_val());
102 EXPECT_EQ(13, W.pop_back_val());
103 }
104
105 }