diff --git a/include/llvm/ADT/iterator.h b/include/llvm/ADT/iterator.h new file mode 100644 index 00000000000..1edf61ca0c1 --- /dev/null +++ b/include/llvm/ADT/iterator.h @@ -0,0 +1,145 @@ +//===- iterator.h - Utilities for using and defining iterators --*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_ADT_ITERATOR_H +#define LLVM_ADT_ITERATOR_H + +#include + +namespace llvm { + +/// \brief CRTP base class for adapting an iterator to a different type. +/// +/// This class can be used through CRTP to adapt one iterator into another. +/// Typically this is done through providing in the derived class a custom \c +/// operator* implementation. Other methods can be overridden as well. +/// +/// FIXME: Factor out the iterator-facade-like aspects into a base class that +/// can be used for defining completely custom iterators. +template > +class iterator_adaptor_base + : public std::iterator { +protected: + WrappedIteratorT I; + + iterator_adaptor_base() {} + + template < + typename U, + typename = typename std::enable_if< + !std::is_same::type>::type, + DerivedT>::value>::type> + explicit iterator_adaptor_base(U &&u) + : I(std::forward(u)) {} + +public: + typedef typename iterator_adaptor_base::iterator::difference_type + difference_type; + + DerivedT &operator+=(difference_type n) { + I += n; + return *static_cast(this); + } + DerivedT &operator-=(difference_type n) { + I -= n; + return *static_cast(this); + } + DerivedT operator+(difference_type n) const { + DerivedT tmp = *this; + tmp += n; + return tmp; + } + friend DerivedT operator+(difference_type n, const DerivedT &i) { + return i + n; + } + DerivedT operator-(difference_type n) const { + DerivedT tmp = *this; + tmp -= n; + return tmp; + } + difference_type operator-(const DerivedT &RHS) const { return I - RHS.I; } + + DerivedT &operator++() { + ++I; + return *static_cast(this); + } + DerivedT &operator--() { + --I; + return *static_cast(this); + } + DerivedT operator++(int) { + DerivedT tmp = *static_cast(this); + ++*this; + return tmp; + } + DerivedT operator--(int) { + DerivedT tmp = *static_cast(this); + --*this; + return tmp; + } + + bool operator==(const DerivedT &RHS) const { return I == RHS.I; } + bool operator!=(const DerivedT &RHS) const { + return !static_cast(this)->operator==(RHS); + } + + bool operator<(const DerivedT &RHS) const { return I < RHS.I; } + bool operator>(const DerivedT &RHS) const { + return !static_cast(this)->operator<(RHS) && + !static_cast(this)->operator==(RHS); + } + bool operator<=(const DerivedT &RHS) const { + return !static_cast(this)->operator>(RHS); + } + bool operator>=(const DerivedT &RHS) const { + return !static_cast(this)->operator<(RHS); + } + + ReferenceT operator*() const { return *I; } + PointerT operator->() const { + return static_cast(this)->operator*(); + } + ReferenceT operator[](difference_type n) const { + return *static_cast(this)->operator+(n); + } +}; + +/// \brief An iterator type that allows iterating over the pointees via some +/// other iterator. +/// +/// The typical usage of this is to expose a type that iterates over Ts, but +/// which is implemented with some iterator over T*s: +/// +/// \code +/// typedef pointee_iterator::iterator> iterator; +/// \endcode +template < + typename WrappedIteratorT, + typename T = typename std::remove_pointer< + typename std::iterator_traits::value_type>::type> +struct pointee_iterator + : iterator_adaptor_base, + WrappedIteratorT, T> { + pointee_iterator() {} + template + pointee_iterator(U &&u) + : pointee_iterator::iterator_adaptor_base(std::forward(u)) {} + + T &operator*() const { return **this->I; } +}; + +} + +#endif diff --git a/unittests/Support/CMakeLists.txt b/unittests/Support/CMakeLists.txt index 4afa4fd7012..44f80ac222c 100644 --- a/unittests/Support/CMakeLists.txt +++ b/unittests/Support/CMakeLists.txt @@ -15,12 +15,13 @@ add_llvm_unittest(SupportTests EndianTest.cpp ErrorOrTest.cpp FileOutputBufferTest.cpp + IteratorTest.cpp LEB128Test.cpp LineIteratorTest.cpp LockFileManagerTest.cpp + MD5Test.cpp ManagedStatic.cpp MathExtrasTest.cpp - MD5Test.cpp MemoryBufferTest.cpp MemoryTest.cpp Path.cpp diff --git a/unittests/Support/IteratorTest.cpp b/unittests/Support/IteratorTest.cpp new file mode 100644 index 00000000000..3a16406d6fa --- /dev/null +++ b/unittests/Support/IteratorTest.cpp @@ -0,0 +1,59 @@ +//===- IteratorTest.cpp - Unit tests for iterator utilities ---------------===// +// +// 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.h" +#include "llvm/ADT/SmallVector.h" +#include "gtest/gtest.h" + +using namespace llvm; + +namespace { + +TEST(PointeeIteratorTest, Basic) { + int arr[4] = { 1, 2, 3, 4 }; + SmallVector V; + V.push_back(&arr[0]); + V.push_back(&arr[1]); + V.push_back(&arr[2]); + V.push_back(&arr[3]); + + typedef pointee_iterator::const_iterator> test_iterator; + + test_iterator Begin, End; + Begin = V.begin(); + End = test_iterator(V.end()); + + test_iterator I = Begin; + for (int i = 0; i < 4; ++i) { + EXPECT_EQ(*V[i], *I); + + EXPECT_EQ(I, Begin + i); + EXPECT_EQ(I, std::next(Begin, i)); + test_iterator J = Begin; + J += i; + EXPECT_EQ(I, J); + EXPECT_EQ(*V[i], Begin[i]); + + EXPECT_NE(I, End); + EXPECT_GT(End, I); + EXPECT_LT(I, End); + EXPECT_GE(I, Begin); + EXPECT_LE(Begin, I); + + EXPECT_EQ(i, I - Begin); + EXPECT_EQ(i, std::distance(Begin, I)); + EXPECT_EQ(Begin, I - i); + + test_iterator K = I++; + EXPECT_EQ(K, std::prev(I)); + } + EXPECT_EQ(End, I); +} + +} // anonymous namespace