|
6 | 6 | // It should be possible to conform to any combination of
|
7 | 7 | // (1 + RangeReplaceable) * (1 + Bidirectional + RandomAccess) * (1 + Mutable)
|
8 | 8 | // Collection and get reasonable default implementations for slicing
|
9 |
| -// operations from |
10 |
| -// the standard library. |
| 9 | +// operations from the standard library. |
| 10 | + |
| 11 | +struct CustomIndex : Comparable { |
| 12 | + static func <(lhs: CustomIndex, rhs: CustomIndex) -> Bool { |
| 13 | + return false |
| 14 | + } |
| 15 | + static func ==(lhs: CustomIndex, rhs: CustomIndex) -> Bool { |
| 16 | + return false |
| 17 | + } |
| 18 | + static var min: CustomIndex { return CustomIndex() } |
| 19 | + static var max: CustomIndex { return CustomIndex() } |
| 20 | +} |
11 | 21 |
|
12 | 22 | % for mutable in ['', 'Mutable']:
|
13 | 23 | % for rangeReplaceable in ['', 'RangeReplaceable']:
|
14 | 24 | % for capability in ['', 'Bidirectional', 'RandomAccess']:
|
| 25 | +% for index in ['Int', 'CustomIndex']: |
| 26 | +% indexLabel = 'Custom' if index == 'CustomIndex' else '' |
15 | 27 |
|
16 |
| -struct ${mutable}${rangeReplaceable}${capability}Butt |
| 28 | +struct ${indexLabel}${mutable}${rangeReplaceable}${capability}Butt |
17 | 29 | : ${mutable}Collection
|
18 |
| -% if rangeReplaceable: |
| 30 | +% if rangeReplaceable: |
19 | 31 | , ${rangeReplaceable}Collection
|
20 |
| -% end |
21 |
| -% if capability: |
| 32 | +% end |
| 33 | +% if capability: |
22 | 34 | , ${capability}Collection
|
23 |
| -% end |
| 35 | +% end |
24 | 36 | {
|
25 |
| - subscript(i: Int) -> Int { |
| 37 | + subscript(i: ${index}) -> Int { |
26 | 38 | get { return 0 }
|
27 |
| -% if mutable: |
| 39 | +% if mutable: |
28 | 40 | set { }
|
29 |
| -% end |
| 41 | +% end |
30 | 42 | }
|
31 | 43 |
|
32 |
| -% if capability: |
33 |
| - func index(before i: Int) -> Int { |
34 |
| - return i - 1 |
| 44 | +% if capability: |
| 45 | + func index(before i: ${index}) -> ${index} { |
| 46 | + return i |
35 | 47 | }
|
36 |
| -% end |
37 |
| - func index(after i: Int) -> Int { |
38 |
| - return i + 1 |
| 48 | +% end |
| 49 | + func index(after i: ${index}) -> ${index} { |
| 50 | + return i |
39 | 51 | }
|
40 | 52 |
|
41 |
| - var startIndex: Int { return .min } |
42 |
| - var endIndex: Int { return .max } |
| 53 | + var startIndex: ${index} { return .min } |
| 54 | + var endIndex: ${index} { return .max } |
43 | 55 |
|
44 |
| -% if rangeReplaceable: |
| 56 | +% if rangeReplaceable: |
45 | 57 | init() {}
|
46 | 58 |
|
47 |
| - mutating func replaceSubrange<C: Collection>(_: Range<Int>, with: C) |
| 59 | + mutating func replaceSubrange<C: Collection>(_: Range<${index}>, with: C) |
48 | 60 | where C.Iterator.Element == Int
|
49 | 61 | {}
|
50 |
| -% end |
| 62 | +% end |
| 63 | + |
| 64 | +% if capability == 'RandomAccess' and index == 'CustomIndex': |
| 65 | + // This type alias is required for some random-access collections with |
| 66 | + // a custom index type -- without it the default implementation for |
| 67 | + // `indices` doesn't attach. |
| 68 | + typealias Indices = DefaultRandomAccessIndices<${indexLabel}${mutable}${rangeReplaceable}${capability}Butt> |
| 69 | +% end |
51 | 70 | }
|
0 commit comments