mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
...replacing it with the new, after passing API review! * The lazy free function has become a property. * Before we could extend protocols, we lacked a means for value types to share implementations, and each new lazy algorithm had to be added to each of up to four types: LazySequence, LazyForwardCollection, LazyBidirectionalCollection, and LazyRandomAccessCollection. These generic adapters hid the usual algorithms by defining their own versions that returned new lazy generic adapters. Now users can extend just one of two protocols to do the same thing: LazySequenceType or LazyCollectionType. * To avoid making the code duplication worse than it already was, the generic adapters mentioned above were used to add the lazy generic algorithms around simpler adapters such as MapSequence that just provided the basic requirements of SequenceType by applying a transformation to some base sequence, resulting in deeply nested generic types as shown here. Now, MapSequence is an instance of LazySequenceType (and is renamed LazyMapSequence), and thus transmits laziness to its algorithms automatically. * Documentation comments have been rewritten. * The .array property was retired * various renamings * A bunch of Gyb files were retired. Swift SVN r30902
205 lines
4.8 KiB
Swift
205 lines
4.8 KiB
Swift
// RUN: %target-run-simple-swift | FileCheck %s
|
|
// REQUIRES: executable_test
|
|
|
|
struct X : CollectionType {
|
|
typealias Element = String.CharacterView.Generator.Element
|
|
typealias Index = String.Index
|
|
var msg: String
|
|
|
|
init(_ msg: String) { self.msg = msg }
|
|
var startIndex: Index {
|
|
return msg.startIndex
|
|
}
|
|
var endIndex: Index {
|
|
return msg.endIndex
|
|
}
|
|
subscript(i: Index) -> Element { return msg[i] }
|
|
|
|
func generate() -> IndexingGenerator<X> {
|
|
return IndexingGenerator(self)
|
|
}
|
|
}
|
|
|
|
var foobar = X("foobar")
|
|
|
|
// CHECK: foobar
|
|
for a in foobar {
|
|
print(a, appendNewline: false)
|
|
}
|
|
print("")
|
|
|
|
// FIXME: separate r from the expression below pending
|
|
// <rdar://problem/15772601> Type checking failure
|
|
// CHECK: raboof
|
|
let i = foobar.indices
|
|
let r = i.lazy.reverse()
|
|
for a in PermutationGenerator(elements: foobar, indices: r) {
|
|
|
|
print(a, appendNewline: false)
|
|
}
|
|
print("")
|
|
|
|
func isPalindrome0<
|
|
S: CollectionType
|
|
where S.Index: BidirectionalIndexType, S.Generator.Element: Equatable
|
|
>(seq: S) -> Bool {
|
|
typealias Index = S.Index
|
|
|
|
var a = seq.indices
|
|
var i = seq.indices
|
|
var ir = i.lazy.reverse()
|
|
var b = ir.generate()
|
|
for i in a {
|
|
if seq[i] != seq[b.next()!] {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// CHECK: false
|
|
print(isPalindrome0(X("GoHangaSalamiImaLasagneHoG")))
|
|
// CHECK: true
|
|
print(isPalindrome0(X("GoHangaSalamiimalaSagnaHoG")))
|
|
|
|
func isPalindrome1<
|
|
S: CollectionType
|
|
where S.Index: BidirectionalIndexType, S.Generator.Element: Equatable
|
|
>(seq: S) -> Bool {
|
|
|
|
var a = PermutationGenerator(elements: seq, indices: seq.indices)
|
|
var b = seq.lazy.reverse().generate()
|
|
for nextChar in a {
|
|
if nextChar != b.next()! {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func isPalindrome1_5<
|
|
S: CollectionType
|
|
where S.Index: BidirectionalIndexType, S.Generator.Element == S.Generator.Element, S.Generator.Element: Equatable
|
|
>(seq: S) -> Bool {
|
|
|
|
var b = seq.lazy.reverse().generate()
|
|
for nextChar in seq {
|
|
if nextChar != b.next()! {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// CHECK: false
|
|
print(isPalindrome1(X("MADAMINEDENIMWILLIAM")))
|
|
// CHECK: true
|
|
print(isPalindrome1(X("MadamInEdEnImadaM")))
|
|
|
|
// CHECK: false
|
|
print(isPalindrome1_5(X("FleetoMeRemoteelF")))
|
|
// CHECK: true
|
|
print(isPalindrome1_5(X("FleetoMeReMoteelF")))
|
|
|
|
// Finally, one that actually uses indexing to do half as much work.
|
|
// BidirectionalIndexType traversal finally pays off!
|
|
func isPalindrome2<
|
|
S: CollectionType
|
|
where S.Index: BidirectionalIndexType, S.Generator.Element: Equatable
|
|
>(seq: S) -> Bool {
|
|
|
|
var b = seq.startIndex, e = seq.endIndex
|
|
|
|
while (b != e) {
|
|
if (b == --e) {
|
|
break
|
|
}
|
|
if seq[b++] != seq[e] {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// Test even length
|
|
// CHECK: false
|
|
print(isPalindrome2(X("ZerimarRamireZ")))
|
|
// CHECK: true
|
|
print(isPalindrome2(X("ZerimaRRamireZ")))
|
|
|
|
// Test odd length
|
|
// CHECK: false
|
|
print(isPalindrome2(X("ZerimarORamireZ")))
|
|
// CHECK: true
|
|
print(isPalindrome2(X("Zerimar-O-ramireZ")))
|
|
|
|
func isPalindrome4<
|
|
S: CollectionType
|
|
where S.Index: BidirectionalIndexType, S.Generator.Element: Equatable
|
|
>(seq: S) -> Bool {
|
|
typealias Index = S.Index
|
|
|
|
var a = PermutationGenerator(elements: seq, indices: seq.indices)
|
|
// FIXME: separate ri from the expression below pending
|
|
// <rdar://problem/15772601> Type checking failure
|
|
var i = seq.indices
|
|
let ri = i.lazy.reverse()
|
|
var b = PermutationGenerator(elements: seq, indices: ri)
|
|
for nextChar in a {
|
|
if nextChar != b.next()! {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// Can't put these literals into string interpolations pending
|
|
// <rdar://problem/16401145> hella-slow compilation
|
|
let array = [1, 2, 3, 4]
|
|
let dict = [0:0, 1:1, 2:2, 3:3, 4:4]
|
|
|
|
func testCount() {
|
|
// CHECK: testing count
|
|
print("testing count")
|
|
// CHECK-NEXT: random access: 4
|
|
print("random access: \(array.count)")
|
|
// CHECK-NEXT: bidirectional: 5
|
|
print("bidirectional: \(dict.count)")
|
|
}
|
|
testCount()
|
|
|
|
struct SequenceOnly<T: SequenceType> : SequenceType {
|
|
var base: T
|
|
func generate() -> T.Generator { return base.generate() }
|
|
}
|
|
|
|
func testUnderestimateCount() {
|
|
// CHECK: testing underestimateCount
|
|
print("testing underestimateCount")
|
|
// CHECK-NEXT: random access: 4
|
|
print("random access: \(array.underestimateCount())")
|
|
// CHECK-NEXT: bidirectional: 5
|
|
print("bidirectional: \(dict.underestimateCount())")
|
|
// CHECK-NEXT: SequenceType only: 0
|
|
let s = SequenceOnly(base: array)
|
|
print("SequenceType only: \(s.underestimateCount())")
|
|
}
|
|
testUnderestimateCount()
|
|
|
|
func testIsEmptyFirstLast() {
|
|
// CHECK: testing isEmpty
|
|
print("testing isEmpty")
|
|
// CHECK-NEXT: true
|
|
print((10..<10).isEmpty)
|
|
// CHECK-NEXT: false
|
|
print((10...10).isEmpty)
|
|
// CHECK-NEXT: 10
|
|
print((10..<100).first)
|
|
// CHECK-NEXT: 99
|
|
print((10..<100).last)
|
|
}
|
|
testIsEmptyFirstLast()
|
|
|
|
// CHECK: all done.
|
|
print("all done.")
|