|
8 | 8 | // REQUIRES: asserts
|
9 | 9 | // REQUIRES: swift_in_compiler
|
10 | 10 |
|
11 |
| -struct MBV : ~Escapable, ~Copyable { |
12 |
| - let p: UnsafeMutableRawPointer |
13 |
| - let c: Int |
| 11 | +struct MutableSpan : ~Escapable, ~Copyable { |
| 12 | + let base: UnsafeMutableRawPointer |
| 13 | + let count: Int |
14 | 14 |
|
15 | 15 | init(_ p: UnsafeMutableRawPointer, _ c: Int) -> dependsOn(p) Self {
|
16 |
| - self.p = p |
17 |
| - self.c = c |
| 16 | + self.base = p |
| 17 | + self.count = c |
18 | 18 | }
|
19 | 19 |
|
20 | 20 | subscript(position: Int) -> Int {
|
21 | 21 | get {
|
22 | 22 | let offset = position * MemoryLayout<Int>.stride
|
23 |
| - return p.loadUnaligned(fromByteOffset: offset, as: Int.self) |
| 23 | + return base.loadUnaligned(fromByteOffset: offset, as: Int.self) |
24 | 24 | }
|
25 | 25 | nonmutating set(newValue) {
|
26 | 26 | let offset = position * MemoryLayout<Int>.stride
|
27 |
| - p.storeBytes(of: newValue, toByteOffset: offset, |
| 27 | + base.storeBytes(of: newValue, toByteOffset: offset, |
28 | 28 | as: Int.self)
|
29 | 29 | }
|
30 | 30 | }
|
| 31 | + |
| 32 | + struct Iter: ~Escapable { |
| 33 | + var base: UnsafeMutableRawPointer |
| 34 | + var count: Int |
| 35 | + |
| 36 | + init(base: UnsafeMutableRawPointer, count: Int) -> dependsOn(base) Self { |
| 37 | + self.base = base |
| 38 | + self.count = count |
| 39 | + } |
| 40 | + |
| 41 | + mutating func next() -> Int? { |
| 42 | + guard count > 0 else { return nil } |
| 43 | + count -= 1 |
| 44 | + let n = base.load(as: Int.self) |
| 45 | + base = base + MemoryLayout<Int>.stride |
| 46 | + return n |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + var iterator: Iter { Iter(base: base, count: count) } |
| 51 | +} |
| 52 | + |
| 53 | +extension Array where Element == Int { |
| 54 | + // TODO: comment out dependsOn(scoped) |
| 55 | + mutating func mutspan() -> /* dependsOn(scoped self) */ MutableSpan { |
| 56 | + /* not the real implementation */ |
| 57 | + let p = self.withUnsafeMutableBufferPointer { $0.baseAddress! } |
| 58 | + return MutableSpan(p, count) |
| 59 | + } |
31 | 60 | }
|
32 | 61 |
|
33 | 62 | struct NC : ~Copyable {
|
34 | 63 | let p: UnsafeMutableRawPointer
|
35 | 64 | let c: Int
|
36 | 65 |
|
37 | 66 | // Requires a mutable borrow.
|
38 |
| - mutating func getMBV() -> dependsOn(self) MBV { |
39 |
| - MBV(p, c) |
| 67 | + mutating func getMutableSpan() -> dependsOn(self) MutableSpan { |
| 68 | + MutableSpan(p, c) |
40 | 69 | }
|
41 | 70 | }
|
42 | 71 |
|
43 | 72 | func mbv_set_element(nc: inout NC, e: Int) {
|
44 |
| - nc.getMBV()[3] = e |
| 73 | + nc.getMutableSpan()[3] = e |
| 74 | +} |
| 75 | + |
| 76 | +func test_mutate_iterate() { |
| 77 | + var array = [1,2,3] |
| 78 | + // mutable 'array' access |
| 79 | + let mutatingSpan = array.mutspan() |
| 80 | + var iterator = mutatingSpan.iterator |
| 81 | + var i = 0 |
| 82 | + while let n = iterator.next() { |
| 83 | + _ = n |
| 84 | + if i < mutatingSpan.count { |
| 85 | + mutatingSpan[i+1] = mutatingSpan[i] |
| 86 | + i += 1 |
| 87 | + } |
| 88 | + } |
| 89 | + // end mutable 'array' access |
45 | 90 | }
|
0 commit comments