forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayCore.swift
171 lines (134 loc) · 4.26 KB
/
ArrayCore.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//===--- ArrayCore.swift --------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// RUN: %target-run-stdlib-swift | FileCheck %s
import Swift
//===--- class Tracked ----------------------------------------------------===//
// Instead of testing with Int elements, we use this wrapper class
// that can help us track allocations and find issues with object
// lifetime inside Array implementations.
var trackedCount = 0
var nextTrackedSerialNumber = 0
final class Tracked : ForwardIndexType, Printable {
required init(_ value: Int) {
++trackedCount
serialNumber = ++nextTrackedSerialNumber
self.value = value
}
deinit {
assert(serialNumber > 0, "double destruction!")
--trackedCount
serialNumber = -serialNumber
}
var description: String {
assert(serialNumber > 0, "dead Tracked!")
return value.description
}
func successor() -> Self {
return self.dynamicType(self.value.successor())
}
var value: Int
var serialNumber: Int
}
func == (x: Tracked, y: Tracked) -> Bool {
return x.value == y.value
}
//===--- struct MrMcRange -------------------------------------------------===//
// A wrapper around Range<Tracked> that allows us to detect when it is
// being treated as a CollectionType rather than merely a SequenceType, which
// helps us to prove that an optimization is being used. In
// particular, when constructing a _ContiguousArrayBuffer from a
// CollectionType, the necessary storage should be pre-allocated.
struct MrMcRange : CollectionType {
typealias Base = Range<Int>
init(_ base: Base) {
self.base = base
}
func generate() -> IndexingGenerator<MrMcRange> {
return IndexingGenerator(self)
}
var startIndex: Int {
println("using collection API")
return base.startIndex
}
var endIndex: Int {
return base.endIndex
}
subscript(i: Int) -> Tracked {
return Tracked(i)
}
var base: Base
}
//===--- struct MrMcArray<T> ----------------------------------------------===//
// A faux ArrayType that allows us to detect that, rather than being
// treated as an arbitrary CollectionType when converting to a
// _ContiguousArrayBuffer, it is first asked for its underlying
// _ContiguousArrayBuffer.
struct MrMcArray<T> : CollectionType, __ArrayType {
typealias _Buffer = _ContiguousArrayBuffer<T>
init(_ buffer: _Buffer) {
self._buffer = buffer
}
var count: Int {
return _buffer.count
}
typealias Generator = IndexingGenerator<MrMcArray>
func generate() -> Generator {
return IndexingGenerator(self)
}
var startIndex: Int {
return 0
}
var endIndex: Int {
return _buffer.count
}
subscript(i: Int) -> T {
return _buffer[i]
}
func _doCopyToNativeArrayBuffer() -> _ContiguousArrayBuffer<T> {
return _buffer
}
var _buffer: _Buffer
}
func printSequence<T: SequenceType>(x: T) {
print("<")
var prefix = ""
for a in x {
print(prefix)
print(a)
prefix = " "
}
println(">")
}
// CHECK: testing...
println("testing...")
func test() {
//===--- Sequences can be converted -------------------------------------===//
let n0 = ((Tracked(10)..<Tracked(27)).generate())~>_copyToNativeArrayBuffer()
// CHECK-NEXT: <10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26>
printSequence(n0)
//===--- Collections get measured ---------------------------------------===//
// CHECK-NEXT: using collection API
let n1 = MrMcRange(3..<23)~>_copyToNativeArrayBuffer()
// CHECK: <3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22>
printSequence(n1)
//===--- _ArrayTypes get asked for a NativeBuffer -----------------------===//
let a0 = MrMcArray<Tracked>(n1)
let cp = _copyToNativeArrayBuffer()
let n2 = a0~>cp
// CHECK-NEXT: true
println(n1.identity == n2.identity)
}
test()
// CHECK-NEXT: trackedCount = 0
println("trackedCount = \(trackedCount)")
// CHECK-NEXT: done.
println("done.")