]> granicus.if.org Git - llvm/commitdiff
Revert "Improve StringMap iterator support."
authorZachary Turner <zturner@google.com>
Tue, 21 Mar 2017 21:23:57 +0000 (21:23 +0000)
committerZachary Turner <zturner@google.com>
Tue, 21 Mar 2017 21:23:57 +0000 (21:23 +0000)
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

include/llvm/ADT/STLExtras.h
include/llvm/ADT/StringMap.h
unittests/ADT/StringMapTest.cpp

index 2d475e10738759847c672dabf8424b9d0bda00fd..038f0717674b9d6a4dcdf4d3ced3a7175437a687 100644 (file)
@@ -893,8 +893,7 @@ auto partition(R &&Range, UnaryPredicate P) -> decltype(std::begin(Range)) {
 /// SmallVector with elements of the vector.  This is useful, for example,
 /// when you want to iterate a range and then sort the results.
 template <unsigned Size, typename R>
-SmallVector<typename std::remove_const<detail::ValueOfRange<R>>::type, Size>
-to_vector(R &&Range) {
+SmallVector<detail::ValueOfRange<R>, Size> to_vector(R &&Range) {
   return {std::begin(Range), std::end(Range)};
 }
 
index cfdf9e872559cd32c7207b7c3532dbb3626a67bf..24e3ecf71b13534353db9ecd05cc6760e0a65eb1 100644 (file)
 #define LLVM_ADT_STRINGMAP_H
 
 #include "llvm/ADT/StringRef.h"
-#include "llvm/ADT/iterator.h"
 #include "llvm/Support/Allocator.h"
 #include "llvm/Support/PointerLikeTypeTraits.h"
 #include <cassert>
 #include <cstdint>
 #include <cstdlib>
 #include <cstring>
+#include <utility>
 #include <initializer_list>
 #include <new>
 #include <utility>
@@ -32,7 +32,6 @@ namespace llvm {
   class StringMapConstIterator;
   template<typename ValueT>
   class StringMapIterator;
-  template <typename ValueT> class StringMapKeyIterator;
   template<typename ValueTy>
   class StringMapEntry;
 
@@ -313,11 +312,6 @@ public:
     return const_iterator(TheTable+NumBuckets, true);
   }
 
-  llvm::iterator_range<StringMapKeyIterator<ValueTy>> keys() const {
-    return make_range(StringMapKeyIterator<ValueTy>(begin()),
-                      StringMapKeyIterator<ValueTy>(end()));
-  }
-
   iterator find(StringRef Key) {
     int Bucket = FindKey(Key);
     if (Bucket == -1) return end();
@@ -450,39 +444,42 @@ public:
   }
 };
 
-template <typename DerivedTy, typename ValueTy>
-class StringMapIterBase
-    : public iterator_facade_base<DerivedTy, std::forward_iterator_tag,
-                                  ValueTy> {
+template <typename ValueTy> class StringMapConstIterator {
 protected:
   StringMapEntryBase **Ptr = nullptr;
 
 public:
-  StringMapIterBase() = default;
+  typedef StringMapEntry<ValueTy> value_type;
 
-  explicit StringMapIterBase(StringMapEntryBase **Bucket,
-                             bool NoAdvance = false)
-      : Ptr(Bucket) {
+  StringMapConstIterator() = default;
+
+  explicit StringMapConstIterator(StringMapEntryBase **Bucket,
+                                  bool NoAdvance = false)
+  : Ptr(Bucket) {
     if (!NoAdvance) AdvancePastEmptyBuckets();
   }
 
-  DerivedTy &operator=(const DerivedTy &Other) {
-    Ptr = Other.Ptr;
-    return static_cast<DerivedTy &>(*this);
+  const value_type &operator*() const {
+    return *static_cast<StringMapEntry<ValueTy>*>(*Ptr);
+  }
+  const value_type *operator->() const {
+    return static_cast<StringMapEntry<ValueTy>*>(*Ptr);
   }
 
-  bool operator==(const DerivedTy &RHS) const { return Ptr == RHS.Ptr; }
+  bool operator==(const StringMapConstIterator &RHS) const {
+    return Ptr == RHS.Ptr;
+  }
+  bool operator!=(const StringMapConstIterator &RHS) const {
+    return Ptr != RHS.Ptr;
+  }
 
-  DerivedTy &operator++() { // Preincrement
+  inline StringMapConstIterator& operator++() {   // Preincrement
     ++Ptr;
     AdvancePastEmptyBuckets();
-    return static_cast<DerivedTy &>(*this);
+    return *this;
   }
-
-  DerivedTy operator++(int) { // Post-increment
-    DerivedTy Tmp(Ptr);
-    ++*this;
-    return Tmp;
+  StringMapConstIterator operator++(int) {        // Postincrement
+    StringMapConstIterator tmp = *this; ++*this; return tmp;
   }
 
 private:
@@ -492,67 +489,22 @@ private:
   }
 };
 
-template <typename ValueTy>
-class StringMapConstIterator
-    : public StringMapIterBase<StringMapConstIterator<ValueTy>,
-                               const StringMapEntry<ValueTy>> {
-  using base = StringMapIterBase<StringMapConstIterator<ValueTy>,
-                                 const StringMapEntry<ValueTy>>;
-
-public:
-  StringMapConstIterator() = default;
-  explicit StringMapConstIterator(StringMapEntryBase **Bucket,
-                                  bool NoAdvance = false)
-      : base(Bucket, NoAdvance) {}
-
-  const StringMapEntry<ValueTy> &operator*() const {
-    return *static_cast<const StringMapEntry<ValueTy> *>(*this->Ptr);
-  }
-};
-
-template <typename ValueTy>
-class StringMapIterator : public StringMapIterBase<StringMapIterator<ValueTy>,
-                                                   StringMapEntry<ValueTy>> {
-  using base =
-      StringMapIterBase<StringMapIterator<ValueTy>, StringMapEntry<ValueTy>>;
-
+template<typename ValueTy>
+class StringMapIterator : public StringMapConstIterator<ValueTy> {
 public:
   StringMapIterator() = default;
+
   explicit StringMapIterator(StringMapEntryBase **Bucket,
                              bool NoAdvance = false)
-      : base(Bucket, NoAdvance) {}
-
-  StringMapEntry<ValueTy> &operator*() const {
-    return *static_cast<StringMapEntry<ValueTy> *>(*this->Ptr);
+    : StringMapConstIterator<ValueTy>(Bucket, NoAdvance) {
   }
 
-  operator StringMapConstIterator<ValueTy>() const {
-    return StringMapConstIterator<ValueTy>(this->Ptr, false);
+  StringMapEntry<ValueTy> &operator*() const {
+    return *static_cast<StringMapEntry<ValueTy>*>(*this->Ptr);
   }
-};
-
-template <typename ValueTy>
-class StringMapKeyIterator
-    : public iterator_adaptor_base<StringMapKeyIterator<ValueTy>,
-                                   StringMapConstIterator<ValueTy>,
-                                   std::forward_iterator_tag, StringRef> {
-  using base = iterator_adaptor_base<StringMapKeyIterator<ValueTy>,
-                                     StringMapConstIterator<ValueTy>,
-                                     std::forward_iterator_tag, StringRef>;
-
-public:
-  StringMapKeyIterator() = default;
-
-  explicit StringMapKeyIterator(StringMapConstIterator<ValueTy> Iter)
-      : base(std::move(Iter)) {}
-
-  StringRef &operator*() {
-    Key = this->wrapped()->getKey();
-    return Key;
+  StringMapEntry<ValueTy> *operator->() const {
+    return static_cast<StringMapEntry<ValueTy>*>(*this->Ptr);
   }
-
-private:
-  StringRef Key;
 };
 
 } // end namespace llvm
index d2b1c31805e6c0e694dc6a9a12dc758e3d919d81..911c72d7496192833894c7b942d221fe16a36880 100644 (file)
@@ -8,7 +8,6 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/ADT/StringMap.h"
-#include "llvm/ADT/StringSet.h"
 #include "llvm/ADT/Twine.h"
 #include "llvm/Support/DataTypes.h"
 #include "gtest/gtest.h"
@@ -270,34 +269,6 @@ TEST_F(StringMapTest, InsertRehashingPairTest) {
   EXPECT_EQ(42u, It->second);
 }
 
-TEST_F(StringMapTest, IterMapKeys) {
-  StringMap<int> Map;
-  Map["A"] = 1;
-  Map["B"] = 2;
-  Map["C"] = 3;
-  Map["D"] = 3;
-
-  auto Keys = to_vector<4>(Map.keys());
-  std::sort(Keys.begin(), Keys.end());
-
-  SmallVector<StringRef, 4> Expected = {"A", "B", "C", "D"};
-  EXPECT_EQ(Expected, Keys);
-}
-
-TEST_F(StringMapTest, IterSetKeys) {
-  StringSet<> Set;
-  Set.insert("A");
-  Set.insert("B");
-  Set.insert("C");
-  Set.insert("D");
-
-  auto Keys = to_vector<4>(Set.keys());
-  std::sort(Keys.begin(), Keys.end());
-
-  SmallVector<StringRef, 4> Expected = {"A", "B", "C", "D"};
-  EXPECT_EQ(Expected, Keys);
-}
-
 // Create a non-default constructable value
 struct StringMapTestStruct {
   StringMapTestStruct(int i) : i(i) {}