llvm.org GIT mirror llvm / 123d0b8
Revert "Improve StringMap iterator support." This is causing crashes in clang, so reverting until the problem is figured out. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@298440 91177308-0d34-0410-b5e6-96231b3b80d8 Zachary Turner 3 years ago
3 changed file(s) with 38 addition(s) and 116 deletion(s). Raw diff Collapse all Expand all
892892 /// SmallVector with elements of the vector. This is useful, for example,
893893 /// when you want to iterate a range and then sort the results.
894894 template
895 SmallVector>::type, Size>
896 to_vector(R &&Range) {
895 SmallVector, Size> to_vector(R &&Range) {
897896 return {std::begin(Range), std::end(Range)};
898897 }
899898
1414 #define LLVM_ADT_STRINGMAP_H
1515
1616 #include "llvm/ADT/StringRef.h"
17 #include "llvm/ADT/iterator.h"
1817 #include "llvm/Support/Allocator.h"
1918 #include "llvm/Support/PointerLikeTypeTraits.h"
2019 #include
2120 #include
2221 #include
2322 #include
23 #include
2424 #include
2525 #include
2626 #include
3131 class StringMapConstIterator;
3232 template
3333 class StringMapIterator;
34 template class StringMapKeyIterator;
3534 template
3635 class StringMapEntry;
3736
312311 return const_iterator(TheTable+NumBuckets, true);
313312 }
314313
315 llvm::iterator_range> keys() const {
316 return make_range(StringMapKeyIterator(begin()),
317 StringMapKeyIterator(end()));
318 }
319
320314 iterator find(StringRef Key) {
321315 int Bucket = FindKey(Key);
322316 if (Bucket == -1) return end();
449443 }
450444 };
451445
452 template
453 class StringMapIterBase
454 : public iterator_facade_base
455 ValueTy> {
446 template class StringMapConstIterator {
456447 protected:
457448 StringMapEntryBase **Ptr = nullptr;
458449
459450 public:
460 StringMapIterBase() = default;
461
462 explicit StringMapIterBase(StringMapEntryBase **Bucket,
463 bool NoAdvance = false)
464 : Ptr(Bucket) {
451 typedef StringMapEntry value_type;
452
453 StringMapConstIterator() = default;
454
455 explicit StringMapConstIterator(StringMapEntryBase **Bucket,
456 bool NoAdvance = false)
457 : Ptr(Bucket) {
465458 if (!NoAdvance) AdvancePastEmptyBuckets();
466459 }
467460
468 DerivedTy &operator=(const DerivedTy &Other) {
469 Ptr = Other.Ptr;
470 return static_cast(*this);
471 }
472
473 bool operator==(const DerivedTy &RHS) const { return Ptr == RHS.Ptr; }
474
475 DerivedTy &operator++() { // Preincrement
461 const value_type &operator*() const {
462 return *static_cast*>(*Ptr);
463 }
464 const value_type *operator->() const {
465 return static_cast*>(*Ptr);
466 }
467
468 bool operator==(const StringMapConstIterator &RHS) const {
469 return Ptr == RHS.Ptr;
470 }
471 bool operator!=(const StringMapConstIterator &RHS) const {
472 return Ptr != RHS.Ptr;
473 }
474
475 inline StringMapConstIterator& operator++() { // Preincrement
476476 ++Ptr;
477477 AdvancePastEmptyBuckets();
478 return static_cast(*this);
479 }
480
481 DerivedTy operator++(int) { // Post-increment
482 DerivedTy Tmp(Ptr);
483 ++*this;
484 return Tmp;
478 return *this;
479 }
480 StringMapConstIterator operator++(int) { // Postincrement
481 StringMapConstIterator tmp = *this; ++*this; return tmp;
485482 }
486483
487484 private:
491488 }
492489 };
493490
494 template
495 class StringMapConstIterator
496 : public StringMapIterBase,
497 const StringMapEntry> {
498 using base = StringMapIterBase,
499 const StringMapEntry>;
500
501 public:
502 StringMapConstIterator() = default;
503 explicit StringMapConstIterator(StringMapEntryBase **Bucket,
504 bool NoAdvance = false)
505 : base(Bucket, NoAdvance) {}
506
507 const StringMapEntry &operator*() const {
508 return *static_cast *>(*this->Ptr);
509 }
510 };
511
512 template
513 class StringMapIterator : public StringMapIterBase,
514 StringMapEntry> {
515 using base =
516 StringMapIterBase, StringMapEntry>;
517
491 template
492 class StringMapIterator : public StringMapConstIterator {
518493 public:
519494 StringMapIterator() = default;
495
520496 explicit StringMapIterator(StringMapEntryBase **Bucket,
521497 bool NoAdvance = false)
522 : base(Bucket, NoAdvance) {}
498 : StringMapConstIterator(Bucket, NoAdvance) {
499 }
523500
524501 StringMapEntry &operator*() const {
525 return *static_cast *>(*this->Ptr);
526 }
527
528 operator StringMapConstIterator() const {
529 return StringMapConstIterator(this->Ptr, false);
502 return *static_cast*>(*this->Ptr);
503 }
504 StringMapEntry *operator->() const {
505 return static_cast*>(*this->Ptr);
530506 }
531507 };
532508
533 template
534 class StringMapKeyIterator
535 : public iterator_adaptor_base,
536 StringMapConstIterator,
537 std::forward_iterator_tag, StringRef> {
538 using base = iterator_adaptor_base,
539 StringMapConstIterator,
540 std::forward_iterator_tag, StringRef>;
541
542 public:
543 StringMapKeyIterator() = default;
544
545 explicit StringMapKeyIterator(StringMapConstIterator Iter)
546 : base(std::move(Iter)) {}
547
548 StringRef &operator*() {
549 Key = this->wrapped()->getKey();
550 return Key;
551 }
552
553 private:
554 StringRef Key;
555 };
556
557509 } // end namespace llvm
558510
559511 #endif // LLVM_ADT_STRINGMAP_H
77 //===----------------------------------------------------------------------===//
88
99 #include "llvm/ADT/StringMap.h"
10 #include "llvm/ADT/StringSet.h"
1110 #include "llvm/ADT/Twine.h"
1211 #include "llvm/Support/DataTypes.h"
1312 #include "gtest/gtest.h"
267266 EXPECT_EQ(16u, t.getNumBuckets());
268267 EXPECT_EQ("abcdef", It->first());
269268 EXPECT_EQ(42u, It->second);
270 }
271
272 TEST_F(StringMapTest, IterMapKeys) {
273 StringMap Map;
274 Map["A"] = 1;
275 Map["B"] = 2;
276 Map["C"] = 3;
277 Map["D"] = 3;
278
279 auto Keys = to_vector<4>(Map.keys());
280 std::sort(Keys.begin(), Keys.end());
281
282 SmallVector Expected = {"A", "B", "C", "D"};
283 EXPECT_EQ(Expected, Keys);
284 }
285
286 TEST_F(StringMapTest, IterSetKeys) {
287 StringSet<> Set;
288 Set.insert("A");
289 Set.insert("B");
290 Set.insert("C");
291 Set.insert("D");
292
293 auto Keys = to_vector<4>(Set.keys());
294 std::sort(Keys.begin(), Keys.end());
295
296 SmallVector Expected = {"A", "B", "C", "D"};
297 EXPECT_EQ(Expected, Keys);
298269 }
299270
300271 // Create a non-default constructable value