llvm.org GIT mirror llvm / 147d9e0
Add range erase, element insert, and range insert methods to TinyPtrVector. With these, it is sufficiently functional for my more normal / pedestrian uses. I've not included some r-value reference stuff here because the value type for a TinyPtrVector is, necessarily, just a pointer. I've added tests that cover the basic behavior of these routines, but they aren't as comprehensive as I'd like. In particular, they don't really test the iterator semantics as thoroughly as they should. Maybe some brave soul will feel enterprising and flesh them out. ;] git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@161104 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 7 years ago
2 changed file(s) with 149 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
1010 #define LLVM_ADT_TINYPTRVECTOR_H
1111
1212 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/PointerUnion.h"
14 #include "llvm/ADT/STLExtras.h"
1315 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/ADT/PointerUnion.h"
1516 #include "llvm/Support/Compiler.h"
1617
1718 namespace llvm {
228229 }
229230 return end();
230231 }
232
233 iterator erase(iterator S, iterator E) {
234 assert(S >= begin() && "Range to erase is out of bounds.");
235 assert(S <= E && "Trying to erase invalid range.");
236 assert(E <= end() && "Trying to erase past the end.");
237
238 if (Val.template is()) {
239 if (S == begin() && S != E)
240 Val = (EltTy)0;
241 } else if (VecTy *Vec = Val.template dyn_cast()) {
242 return Vec->erase(S, E);
243 }
244 return end();
245 }
246
247 iterator insert(iterator I, const EltTy &Elt) {
248 assert(I >= this->begin() && "Insertion iterator is out of bounds.");
249 assert(I <= this->end() && "Inserting past the end of the vector.");
250 if (I == end()) {
251 push_back(Elt);
252 return llvm::prior(end());
253 }
254 assert(!Val.isNull() && "Null value with non-end insert iterator.");
255 if (EltTy V = Val.template dyn_cast()) {
256 assert(I == begin());
257 Val = Elt;
258 push_back(V);
259 return begin();
260 }
261
262 return Val.template get()->insert(I, Elt);
263 }
264
265 template
266 iterator insert(iterator I, ItTy From, ItTy To) {
267 assert(I >= this->begin() && "Insertion iterator is out of bounds.");
268 assert(I <= this->end() && "Inserting past the end of the vector.");
269 if (From == To)
270 return I;
271
272 // If we have a single value, convert to a vector.
273 ptrdiff_t Offset = I - begin();
274 if (Val.isNull()) {
275 if (llvm::next(From) == To) {
276 Val = *From;
277 return begin();
278 }
279
280 Val = new VecTy();
281 } else if (EltTy V = Val.template dyn_cast()) {
282 Val = new VecTy();
283 Val.template get()->push_back(V);
284 }
285 return Val.template get()->insert(begin() + Offset, From, To);
286 }
231287 };
232288 } // end namespace llvm
233289
353353 this->expectValues(this->V, this->testArray(0));
354354 }
355355
356 }
356 TYPED_TEST(TinyPtrVectorTest, EraseRangeTest) {
357 this->appendValues(this->V, this->testArray(1));
358 this->expectValues(this->V, this->testArray(1));
359 this->V.erase(this->V.begin(), this->V.begin());
360 this->expectValues(this->V, this->testArray(1));
361 this->V.erase(this->V.end(), this->V.end());
362 this->expectValues(this->V, this->testArray(1));
363 this->V.erase(this->V.begin(), this->V.end());
364 this->expectValues(this->V, this->testArray(0));
365
366 this->appendValues(this->V, this->testArray(42));
367 this->expectValues(this->V, this->testArray(42));
368 this->V.erase(this->V.begin(), llvm::next(this->V.begin(), 1));
369 this->TestPtrs.erase(this->TestPtrs.begin(),
370 llvm::next(this->TestPtrs.begin(), 1));
371 this->expectValues(this->V, this->testArray(41));
372 this->V.erase(llvm::next(this->V.begin(), 1), llvm::next(this->V.begin(), 2));
373 this->TestPtrs.erase(llvm::next(this->TestPtrs.begin(), 1),
374 llvm::next(this->TestPtrs.begin(), 2));
375 this->expectValues(this->V, this->testArray(40));
376 this->V.erase(llvm::next(this->V.begin(), 2), llvm::next(this->V.begin(), 4));
377 this->TestPtrs.erase(llvm::next(this->TestPtrs.begin(), 2),
378 llvm::next(this->TestPtrs.begin(), 4));
379 this->expectValues(this->V, this->testArray(38));
380 this->V.erase(llvm::next(this->V.begin(), 5), llvm::next(this->V.begin(), 10));
381 this->TestPtrs.erase(llvm::next(this->TestPtrs.begin(), 5),
382 llvm::next(this->TestPtrs.begin(), 10));
383 this->expectValues(this->V, this->testArray(33));
384 this->V.erase(llvm::next(this->V.begin(), 13), llvm::next(this->V.begin(), 26));
385 this->TestPtrs.erase(llvm::next(this->TestPtrs.begin(), 13),
386 llvm::next(this->TestPtrs.begin(), 26));
387 this->expectValues(this->V, this->testArray(20));
388 this->V.erase(llvm::next(this->V.begin(), 7), this->V.end());
389 this->expectValues(this->V, this->testArray(7));
390 this->V.erase(this->V.begin(), this->V.end());
391 this->expectValues(this->V, this->testArray(0));
392 }
393
394 TYPED_TEST(TinyPtrVectorTest, Insert) {
395 this->V.insert(this->V.end(), this->TestPtrs[0]);
396 this->expectValues(this->V, this->testArray(1));
397 this->V.clear();
398 this->appendValues(this->V, this->testArray(4));
399 this->expectValues(this->V, this->testArray(4));
400 this->V.insert(this->V.end(), this->TestPtrs[4]);
401 this->expectValues(this->V, this->testArray(5));
402 this->V.insert(this->V.begin(), this->TestPtrs[42]);
403 this->TestPtrs.insert(this->TestPtrs.begin(), this->TestPtrs[42]);
404 this->expectValues(this->V, this->testArray(6));
405 this->V.insert(llvm::next(this->V.begin(), 3), this->TestPtrs[43]);
406 this->TestPtrs.insert(llvm::next(this->TestPtrs.begin(), 3),
407 this->TestPtrs[43]);
408 this->expectValues(this->V, this->testArray(7));
409 }
410
411 TYPED_TEST(TinyPtrVectorTest, InsertRange) {
412 this->V.insert(this->V.end(), this->TestPtrs.begin(), this->TestPtrs.begin());
413 this->expectValues(this->V, this->testArray(0));
414 this->V.insert(this->V.begin(), this->TestPtrs.begin(),
415 this->TestPtrs.begin());
416 this->expectValues(this->V, this->testArray(0));
417 this->V.insert(this->V.end(), this->TestPtrs.end(), this->TestPtrs.end());
418 this->expectValues(this->V, this->testArray(0));
419 this->V.insert(this->V.end(), this->TestPtrs.begin(),
420 llvm::next(this->TestPtrs.begin()));
421 this->expectValues(this->V, this->testArray(1));
422 this->V.clear();
423 this->V.insert(this->V.end(), this->TestPtrs.begin(),
424 llvm::next(this->TestPtrs.begin(), 2));
425 this->expectValues(this->V, this->testArray(2));
426 this->V.clear();
427 this->V.insert(this->V.end(), this->TestPtrs.begin(),
428 llvm::next(this->TestPtrs.begin(), 42));
429 this->expectValues(this->V, this->testArray(42));
430 this->V.clear();
431 this->V.insert(this->V.end(),
432 llvm::next(this->TestPtrs.begin(), 5),
433 llvm::next(this->TestPtrs.begin(), 13));
434 this->V.insert(this->V.begin(),
435 llvm::next(this->TestPtrs.begin(), 0),
436 llvm::next(this->TestPtrs.begin(), 3));
437 this->V.insert(llvm::next(this->V.begin(), 2),
438 llvm::next(this->TestPtrs.begin(), 2),
439 llvm::next(this->TestPtrs.begin(), 4));
440 this->V.erase(llvm::next(this->V.begin(), 4));
441 this->V.insert(llvm::next(this->V.begin(), 4),
442 llvm::next(this->TestPtrs.begin(), 4),
443 llvm::next(this->TestPtrs.begin(), 5));
444 this->expectValues(this->V, this->testArray(13));
445 }
446
447 }