]> granicus.if.org Git - clang/commitdiff
SimpleArray: Provide reverse iteration via std::reverse_iterator.
authorBenjamin Kramer <benny.kra@googlemail.com>
Sun, 15 Mar 2015 18:47:26 +0000 (18:47 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Sun, 15 Mar 2015 18:47:26 +0000 (18:47 +0000)
NFC intended.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@232330 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Analysis/Analyses/ThreadSafetyUtil.h

index 42808a80ecb9e44a640add0c63443dddf5d422d6..4d3402f8c00b1a5c2bc9c4d6ef0b02eb8946918f 100644 (file)
@@ -130,6 +130,8 @@ public:
 
   typedef T *iterator;
   typedef const T *const_iterator;
+  typedef std::reverse_iterator<iterator> reverse_iterator;
+  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
 
   size_t size() const { return Size; }
   size_t capacity() const { return Capacity; }
@@ -160,6 +162,16 @@ public:
   const_iterator cbegin() const { return Data; }
   const_iterator cend()   const { return Data + Size; }
 
+  reverse_iterator rbegin() { return reverse_iterator(end()); }
+  reverse_iterator rend() { return reverse_iterator(begin()); }
+
+  const_reverse_iterator rbegin() const {
+    return const_reverse_iterator(end());
+  }
+  const_reverse_iterator rend() const {
+    return const_reverse_iterator(begin());
+  }
+
   void push_back(const T &Elem) {
     assert(Size < Capacity);
     Data[Size++] = Elem;
@@ -188,36 +200,12 @@ public:
     return J - Osz;
   }
 
-  // An adaptor to reverse a simple array
-  class ReverseAdaptor {
-   public:
-    ReverseAdaptor(SimpleArray &Array) : Array(Array) {}
-    // A reverse iterator used by the reverse adaptor
-    class Iterator {
-     public:
-      Iterator(T *Data) : Data(Data) {}
-      T &operator*() { return *Data; }
-      const T &operator*() const { return *Data; }
-      Iterator &operator++() {
-        --Data;
-        return *this;
-      }
-      bool operator!=(Iterator Other) { return Data != Other.Data; }
-
-     private:
-      T *Data;
-    };
-    Iterator begin() { return Array.end() - 1; }
-    Iterator end() { return Array.begin() - 1; }
-    const Iterator begin() const { return Array.end() - 1; }
-    const Iterator end() const { return Array.begin() - 1; }
-
-   private:
-    SimpleArray &Array;
-  };
-
-  const ReverseAdaptor reverse() const { return ReverseAdaptor(*this); }
-  ReverseAdaptor reverse() { return ReverseAdaptor(*this); }
+  llvm::iterator_range<reverse_iterator> reverse() {
+    return llvm::make_range(rbegin(), rend());
+  }
+  llvm::iterator_range<const_reverse_iterator> reverse() const {
+    return llvm::make_range(rbegin(), rend());
+  }
 
 private:
   // std::max is annoying here, because it requires a reference,