SimpleArray: Provide reverse iteration via std::reverse_iterator.

NFC intended.

llvm-svn: 232330
This commit is contained in:
Benjamin Kramer 2015-03-15 18:47:26 +00:00
parent 69722ab7c1
commit 6eb776547e
1 changed files with 18 additions and 30 deletions

View 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,