[llvm] r243581 - Reapply "Add reverse(ContainerTy) range adapter."
Pete Cooper
peter_cooper at apple.com
Wed Jul 29 15:19:09 PDT 2015
Author: pete
Date: Wed Jul 29 17:19:09 2015
New Revision: 243581
URL: http://llvm.org/viewvc/llvm-project?rev=243581&view=rev
Log:
Reapply "Add reverse(ContainerTy) range adapter."
This reverts commit r243567, which ultimately reapplies r243563.
The fix here was to use std::enable_if for overload resolution. Thanks to David
Blaikie for lots of help on this, and for the extra tests!
Original commit message follows:
For cases where we needed a foreach loop in reverse over a container,
we had to do something like
for (const GlobalValue *GV : make_range(TypeInfos.rbegin(),
TypeInfos.rend())) {
This provides a convenience method which shortens this to
for (const GlobalValue *GV : reverse(TypeInfos)) {
There are 2 versions of this, with a preference to the rbegin() version.
The first uses rbegin() and rend() to construct an iterator_range.
The second constructs an iterator_range from the begin() and end() methods
wrapped in std::reverse_iterator's.
Reviewed by David Blaikie.
Added:
llvm/trunk/unittests/ADT/RangeAdapterTest.cpp
Modified:
llvm/trunk/include/llvm/ADT/STLExtras.h
llvm/trunk/lib/CodeGen/AsmPrinter/ARMException.cpp
llvm/trunk/unittests/ADT/CMakeLists.txt
Modified: llvm/trunk/include/llvm/ADT/STLExtras.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/STLExtras.h?rev=243581&r1=243580&r2=243581&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/STLExtras.h (original)
+++ llvm/trunk/include/llvm/ADT/STLExtras.h Wed Jul 29 17:19:09 2015
@@ -196,6 +196,41 @@ inline mapped_iterator<ItTy, FuncTy> map
return mapped_iterator<ItTy, FuncTy>(I, F);
}
+/// \brief Metafunction to determine if type T has a member called rbegin().
+template <typename T> struct has_rbegin {
+ template <typename U> static char(&f(const U &, decltype(&U::rbegin)))[1];
+ static char(&f(...))[2];
+ const static bool value = sizeof(f(std::declval<T>(), nullptr)) == 1;
+};
+
+// Returns an iterator_range over the given container which iterates in reverse.
+// Note that the container must have rbegin()/rend() methods for this to work.
+template <typename ContainerTy>
+auto reverse(ContainerTy &&C,
+ typename std::enable_if<has_rbegin<ContainerTy>::value>::type * =
+ nullptr) -> decltype(make_range(C.rbegin(), C.rend())) {
+ return make_range(C.rbegin(), C.rend());
+}
+
+// Returns a std::reverse_iterator wrapped around the given iterator.
+template <typename IteratorTy>
+std::reverse_iterator<IteratorTy> make_reverse_iterator(IteratorTy It) {
+ return std::reverse_iterator<IteratorTy>(It);
+}
+
+// Returns an iterator_range over the given container which iterates in reverse.
+// Note that the container must have begin()/end() methods which return
+// bidirectional iterators for this to work.
+template <typename ContainerTy>
+auto reverse(
+ ContainerTy &&C,
+ typename std::enable_if<!has_rbegin<ContainerTy>::value>::type * = nullptr)
+ -> decltype(make_range(make_reverse_iterator(std::end(C)),
+ make_reverse_iterator(std::begin(C)))) {
+ return make_range(make_reverse_iterator(std::end(C)),
+ make_reverse_iterator(std::begin(C)));
+}
+
//===----------------------------------------------------------------------===//
// Extra additions to <utility>
//===----------------------------------------------------------------------===//
Modified: llvm/trunk/lib/CodeGen/AsmPrinter/ARMException.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/AsmPrinter/ARMException.cpp?rev=243581&r1=243580&r2=243581&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/AsmPrinter/ARMException.cpp (original)
+++ llvm/trunk/lib/CodeGen/AsmPrinter/ARMException.cpp Wed Jul 29 17:19:09 2015
@@ -115,8 +115,7 @@ void ARMException::emitTypeInfos(unsigne
Entry = TypeInfos.size();
}
- for (const GlobalValue *GV : make_range(TypeInfos.rbegin(),
- TypeInfos.rend())) {
+ for (const GlobalValue *GV : reverse(TypeInfos)) {
if (VerboseAsm)
Asm->OutStreamer->AddComment("TypeInfo " + Twine(Entry--));
Asm->EmitTTypeReference(GV, TTypeEncoding);
Modified: llvm/trunk/unittests/ADT/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/CMakeLists.txt?rev=243581&r1=243580&r2=243581&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/CMakeLists.txt (original)
+++ llvm/trunk/unittests/ADT/CMakeLists.txt Wed Jul 29 17:19:09 2015
@@ -28,6 +28,7 @@ set(ADTSources
PointerIntPairTest.cpp
PointerUnionTest.cpp
PostOrderIteratorTest.cpp
+ RangeAdapterTest.cpp
SCCIteratorTest.cpp
SmallPtrSetTest.cpp
SmallStringTest.cpp
Added: llvm/trunk/unittests/ADT/RangeAdapterTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/RangeAdapterTest.cpp?rev=243581&view=auto
==============================================================================
--- llvm/trunk/unittests/ADT/RangeAdapterTest.cpp (added)
+++ llvm/trunk/unittests/ADT/RangeAdapterTest.cpp Wed Jul 29 17:19:09 2015
@@ -0,0 +1,83 @@
+//===- RangeAdapterTest.cpp - Unit tests for range adapters --------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ADT/iterator_range.h"
+#include "llvm/ADT/STLExtras.h"
+#include "gtest/gtest.h"
+
+#include <iterator>
+#include <list>
+#include <vector>
+
+using namespace llvm;
+
+namespace {
+
+// A wrapper around vector which exposes rbegin(), rend().
+class ReverseOnlyVector {
+ std::vector<int> Vec;
+
+public:
+ ReverseOnlyVector(std::initializer_list<int> list) : Vec(list) {}
+
+ typedef std::vector<int>::reverse_iterator reverse_iterator;
+ reverse_iterator rbegin() { return Vec.rbegin(); }
+ reverse_iterator rend() { return Vec.rend(); }
+};
+
+// A wrapper around vector which exposes begin(), end(), rbegin() and rend().
+// begin() and end() don't have implementations as this ensures that we will
+// get a linker error if reverse() chooses begin()/end() over rbegin(), rend().
+class BidirectionalVector {
+ std::vector<int> Vec;
+
+public:
+ BidirectionalVector(std::initializer_list<int> list) : Vec(list) {}
+
+ typedef std::vector<int>::iterator iterator;
+ iterator begin();
+ iterator end();
+
+ typedef std::vector<int>::reverse_iterator reverse_iterator;
+ reverse_iterator rbegin() { return Vec.rbegin(); }
+ reverse_iterator rend() { return Vec.rend(); }
+};
+
+template <typename R> void TestRev(const R &r) {
+ int counter = 3;
+ for (int i : r)
+ EXPECT_EQ(i, counter--);
+}
+
+// Test fixture
+template <typename T> class RangeAdapterLValueTest : public ::testing::Test {};
+
+typedef ::testing::Types<std::vector<int>, std::list<int>, int[4]>
+ RangeAdapterLValueTestTypes;
+TYPED_TEST_CASE(RangeAdapterLValueTest, RangeAdapterLValueTestTypes);
+
+TYPED_TEST(RangeAdapterLValueTest, TrivialOperation) {
+ TypeParam v = {0, 1, 2, 3};
+ TestRev(reverse(v));
+
+ const TypeParam c = {0, 1, 2, 3};
+ TestRev(reverse(c));
+}
+
+template <typename T> struct RangeAdapterRValueTest : testing::Test {};
+
+typedef ::testing::Types<std::vector<int>, std::list<int>, ReverseOnlyVector,
+ BidirectionalVector> RangeAdapterRValueTestTypes;
+TYPED_TEST_CASE(RangeAdapterRValueTest, RangeAdapterRValueTestTypes);
+
+TYPED_TEST(RangeAdapterRValueTest, TrivialOperation) {
+ TestRev(reverse(TypeParam({0, 1, 2, 3})));
+}
+
+} // anonymous namespace
More information about the llvm-commits
mailing list