Skip to content

Commit 70e83b5

Browse files
committed
Make it easier to define iterator ranges for APIs like getFoo(index).
We'll use this in AbstractionPattern.
1 parent bb05a0e commit 70e83b5

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
//===- IndexedViewRange.h - Iterators for indexed projections ---*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This file defines IndexedViewRange, a template class which makes it
14+
// easy to define a range for a "collection" that is normally just vended
15+
// with an indexed accessor.
16+
//
17+
//===----------------------------------------------------------------------===//
18+
19+
#ifndef SWIFT_BASIC_INDEXEDVIEWRANGE_H
20+
#define SWIFT_BASIC_INDEXEDVIEWRANGE_H
21+
22+
#include <iterator>
23+
#include <memory>
24+
#include "llvm/ADT/iterator_range.h"
25+
26+
namespace swift {
27+
28+
/// An iterator over a range of values provided by an indexed accessor
29+
/// on a base type.
30+
template <class BaseType, class ProjectedType,
31+
ProjectedType (&Project)(BaseType, size_t)>
32+
class IndexedViewIterator {
33+
public:
34+
using value_type = ProjectedType;
35+
using reference = ProjectedType;
36+
using pointer = void;
37+
using difference_type = ptrdiff_t;
38+
using iterator_category = std::random_access_iterator_tag;
39+
private:
40+
BaseType Base;
41+
size_t Index;
42+
public:
43+
IndexedViewIterator(BaseType base, size_t index)
44+
: Base(base), Index(index) {}
45+
public:
46+
ProjectedType operator*() const { return Project(Base, Index); }
47+
ProjectedType operator->() const { return Project(Base, Index); }
48+
IndexedViewIterator &operator++() { Index++; return *this; }
49+
IndexedViewIterator operator++(int) { return iterator(Base, Index++); }
50+
IndexedViewIterator &operator--() { Index--; return *this; }
51+
IndexedViewIterator operator--(int) { return iterator(Base, Index--); }
52+
bool operator==(IndexedViewIterator rhs) const { return Index == rhs.Index; }
53+
bool operator!=(IndexedViewIterator rhs) const { return Index != rhs.Index; }
54+
55+
IndexedViewIterator &operator+=(difference_type i) {
56+
Index += i;
57+
return *this;
58+
}
59+
IndexedViewIterator operator+(difference_type i) const {
60+
return IndexedViewIterator(Base, Index + i);
61+
}
62+
friend IndexedViewIterator operator+(difference_type i,
63+
IndexedViewIterator rhs) {
64+
return IndexedViewIterator(rhs.Base, rhs.Index + i);
65+
}
66+
IndexedViewIterator &operator-=(difference_type i) {
67+
Index -= i;
68+
return *this;
69+
}
70+
IndexedViewIterator operator-(difference_type i) const {
71+
return IndexedViewIterator(Base, Index - i);
72+
}
73+
difference_type operator-(IndexedViewIterator rhs) const {
74+
return Index - rhs.Index;
75+
}
76+
ProjectedType operator[](difference_type i) const {
77+
return Project(Base, Index + i);
78+
}
79+
bool operator<(IndexedViewIterator rhs) const {
80+
return Index < rhs.Index;
81+
}
82+
bool operator<=(IndexedViewIterator rhs) const {
83+
return Index <= rhs.Index;
84+
}
85+
bool operator>(IndexedViewIterator rhs) const {
86+
return Index > rhs.Index;
87+
}
88+
bool operator>=(IndexedViewIterator rhs) const {
89+
return Index >= rhs.Index;
90+
}
91+
};
92+
93+
template <class BaseType, class ProjectedType,
94+
ProjectedType (&Project)(BaseType, size_t)>
95+
using IndexedViewRange =
96+
llvm::iterator_range<IndexedViewIterator<BaseType, ProjectedType, Project>>;
97+
98+
} // end namespace swift
99+
100+
#endif // SWIFT_BASIC_INDEXEDVIEWRANGE_H

0 commit comments

Comments
 (0)