@@ -19,20 +19,37 @@ import XCTest
19
19
@testable import HTMLEntitiesTests
20
20
21
21
// Implementation taken from http://stackoverflow.com/a/24029847
22
- extension MutableCollection where Indices. Iterator. Element == Index {
23
- mutating func shuffle( ) {
24
- let c = count
25
- guard c > 1 else { return }
22
+ #if swift(>=3.2)
23
+ extension MutableCollection {
24
+ mutating func shuffle( ) {
25
+ let c = count
26
+ guard c > 1 else { return }
26
27
27
- srand ( UInt32 ( time ( nil ) ) )
28
- for (firstUnshuffled , unshuffledCount) in zip ( indices, stride ( from: c, to: 1 , by: - 1 ) ) {
29
- let d : IndexDistance = numericCast ( random ( ) % numericCast( unshuffledCount) )
30
- guard d != 0 else { continue }
31
- let i = index ( firstUnshuffled, offsetBy: d)
32
- swap ( & self [ firstUnshuffled] , & self [ i] )
28
+ srand ( UInt32 ( time ( nil ) ) )
29
+ for (firstUnshuffled , unshuffledCount) in zip ( indices, stride ( from: c, to: 1 , by: - 1 ) ) {
30
+ let d : IndexDistance = numericCast ( random ( ) % numericCast( unshuffledCount) )
31
+ guard d != 0 else { continue }
32
+ let i = index ( firstUnshuffled, offsetBy: d)
33
+ swapAt ( firstUnshuffled, i)
34
+ }
33
35
}
34
36
}
35
- }
37
+ #else
38
+ extension MutableCollection where Indices. Iterator. Element == Index {
39
+ mutating func shuffle( ) {
40
+ let c = count
41
+ guard c > 1 else { return }
42
+
43
+ srand ( UInt32 ( time ( nil ) ) )
44
+ for (firstUnshuffled , unshuffledCount) in zip ( indices, stride ( from: c, to: 1 , by: - 1 ) ) {
45
+ let d : IndexDistance = numericCast ( random ( ) % numericCast( unshuffledCount) )
46
+ guard d != 0 else { continue }
47
+ let i = index ( firstUnshuffled, offsetBy: d)
48
+ swap ( & self [ firstUnshuffled] , & self [ i] )
49
+ }
50
+ }
51
+ }
52
+ #endif
36
53
37
54
extension Sequence {
38
55
func shuffled( ) -> [ Iterator . Element ] {
@@ -43,5 +60,5 @@ extension Sequence {
43
60
}
44
61
45
62
XCTMain ( [
46
- testCase ( HTMLEntitiesTests . allTests. shuffled ( ) ) ,
47
- ] . shuffled ( ) )
63
+ testCase ( HTMLEntitiesTests . allTests. shuffled ( ) ) ,
64
+ ] . shuffled ( ) )
0 commit comments