mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-12-29 10:32:47 +00:00
[ADT] Add a generic iterator utility for adapting iterators much like
Boost's iterator_adaptor, and a specific adaptor which iterates over pointees when wrapped around an iterator over pointers. This is the result of a long discussion on IRC with Duncan Smith, Dave Blaikie, Richard Smith, and myself. Essentially, I could use some subset of the iterator facade facilities often used from Boost, and everyone seemed interested in having the functionality in a reasonably generic form. I've tried to strike a balance between the pragmatism and the established Boost design. The primary differences are: 1) Delegating to the standard iterator interface names rather than special names that then make up a second iterator-like API. 2) Using the name 'pointee_iterator' which seems more clear than 'indirect_iterator'. The whole business of calling the '*p' operation 'pointer indirection' in the standard is ... quite confusing. And 'dereference' is no better of a term for moving from a pointer to a reference. Hoping Duncan, and others continue to provide comments on this until we've got a nice, minimal abstraction. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@207069 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
c118614379
commit
beee61d3e6
145
include/llvm/ADT/iterator.h
Normal file
145
include/llvm/ADT/iterator.h
Normal file
@ -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 <iterator>
|
||||
|
||||
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 <typename DerivedT, typename WrappedIteratorT, typename T,
|
||||
typename PointerT = T *, typename ReferenceT = T &,
|
||||
// Don't provide these, they are mostly to act as aliases below.
|
||||
typename WrappedTraitsT = std::iterator_traits<WrappedIteratorT>>
|
||||
class iterator_adaptor_base
|
||||
: public std::iterator<typename WrappedTraitsT::iterator_category, T,
|
||||
typename WrappedTraitsT::difference_type, PointerT,
|
||||
ReferenceT> {
|
||||
protected:
|
||||
WrappedIteratorT I;
|
||||
|
||||
iterator_adaptor_base() {}
|
||||
|
||||
template <
|
||||
typename U,
|
||||
typename = typename std::enable_if<
|
||||
!std::is_same<typename std::remove_cv<
|
||||
typename std::remove_reference<U>::type>::type,
|
||||
DerivedT>::value>::type>
|
||||
explicit iterator_adaptor_base(U &&u)
|
||||
: I(std::forward<U &&>(u)) {}
|
||||
|
||||
public:
|
||||
typedef typename iterator_adaptor_base::iterator::difference_type
|
||||
difference_type;
|
||||
|
||||
DerivedT &operator+=(difference_type n) {
|
||||
I += n;
|
||||
return *static_cast<DerivedT *>(this);
|
||||
}
|
||||
DerivedT &operator-=(difference_type n) {
|
||||
I -= n;
|
||||
return *static_cast<DerivedT *>(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<DerivedT *>(this);
|
||||
}
|
||||
DerivedT &operator--() {
|
||||
--I;
|
||||
return *static_cast<DerivedT *>(this);
|
||||
}
|
||||
DerivedT operator++(int) {
|
||||
DerivedT tmp = *static_cast<DerivedT *>(this);
|
||||
++*this;
|
||||
return tmp;
|
||||
}
|
||||
DerivedT operator--(int) {
|
||||
DerivedT tmp = *static_cast<DerivedT *>(this);
|
||||
--*this;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
bool operator==(const DerivedT &RHS) const { return I == RHS.I; }
|
||||
bool operator!=(const DerivedT &RHS) const {
|
||||
return !static_cast<const DerivedT *>(this)->operator==(RHS);
|
||||
}
|
||||
|
||||
bool operator<(const DerivedT &RHS) const { return I < RHS.I; }
|
||||
bool operator>(const DerivedT &RHS) const {
|
||||
return !static_cast<const DerivedT *>(this)->operator<(RHS) &&
|
||||
!static_cast<const DerivedT *>(this)->operator==(RHS);
|
||||
}
|
||||
bool operator<=(const DerivedT &RHS) const {
|
||||
return !static_cast<const DerivedT *>(this)->operator>(RHS);
|
||||
}
|
||||
bool operator>=(const DerivedT &RHS) const {
|
||||
return !static_cast<const DerivedT *>(this)->operator<(RHS);
|
||||
}
|
||||
|
||||
ReferenceT operator*() const { return *I; }
|
||||
PointerT operator->() const {
|
||||
return static_cast<const DerivedT *>(this)->operator*();
|
||||
}
|
||||
ReferenceT operator[](difference_type n) const {
|
||||
return *static_cast<const DerivedT *>(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<SmallVectorImpl<T *>::iterator> iterator;
|
||||
/// \endcode
|
||||
template <
|
||||
typename WrappedIteratorT,
|
||||
typename T = typename std::remove_pointer<
|
||||
typename std::iterator_traits<WrappedIteratorT>::value_type>::type>
|
||||
struct pointee_iterator
|
||||
: iterator_adaptor_base<pointee_iterator<WrappedIteratorT>,
|
||||
WrappedIteratorT, T> {
|
||||
pointee_iterator() {}
|
||||
template <typename U>
|
||||
pointee_iterator(U &&u)
|
||||
: pointee_iterator::iterator_adaptor_base(std::forward<U &&>(u)) {}
|
||||
|
||||
T &operator*() const { return **this->I; }
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
@ -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
|
||||
|
59
unittests/Support/IteratorTest.cpp
Normal file
59
unittests/Support/IteratorTest.cpp
Normal file
@ -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<int *, 4> V;
|
||||
V.push_back(&arr[0]);
|
||||
V.push_back(&arr[1]);
|
||||
V.push_back(&arr[2]);
|
||||
V.push_back(&arr[3]);
|
||||
|
||||
typedef pointee_iterator<SmallVectorImpl<int *>::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
|
Loading…
Reference in New Issue
Block a user