Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
652 views
in Technique[技术] by (71.8m points)

swift3 - Shuffle array swift 3

How can I convert the function below to to swift 3? Currently getting a Binary operator '..<' cannot be applied to operands of type 'Int' and 'Self.IndexDistance' error.

extension MutableCollection where Index == Int {
  /// Shuffle the elements of `self` in-place.
  mutating func shuffleInPlace() {
    // empty and single-element collections don't shuffle
    if count < 2 { return }

    for i in 0..<count - 1 { //error takes place here
      let j = Int(arc4random_uniform(UInt32(count - i))) + i
      guard i != j else { continue }
      swap(&self[i], &self[j])
    }
  }
}

reference: https://stackoverflow.com/a/24029847/5222077

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

count returns an IndexDistance which is the type describing the distance between two collection indices. IndexDistance is required to be a SignedInteger, but need not be an Int and can be different from Index. Therefore it is not possible to create the range 0..<count - 1.

A solution is to use startIndex and endIndex instead of 0 and count:

extension MutableCollection where Index == Int {
    /// Shuffle the elements of `self` in-place.
    mutating func shuffle() {
        // empty and single-element collections don't shuffle
        if count < 2 { return }

        for i in startIndex ..< endIndex - 1 {
            let j = Int(arc4random_uniform(UInt32(endIndex - i))) + i
            if i != j {
                swap(&self[i], &self[j])
            }
        }
    }
}

Another advantage is that this also works correctly with array slices (where the index of the first element is not necessarily zero).

Note that according to the new "Swift API Design Guidelines", shuffle() is the "proper" name for a mutating shuffle method, and shuffled() for the non-mutating counterpart which returns an array:

extension Collection {
    /// Return a copy of `self` with its elements shuffled
    func shuffled() -> [Iterator.Element] {
        var list = Array(self)
        list.shuffle()
        return list
    }
}

Update: A (even more general) Swift 3 version has been added to How do I shuffle an array in Swift? in the meantime.


For Swift 4 (Xcode 9) one has to replace the call to the swap() function by a call to the swapAt() method of the collection. Also the restriction on the Index type is no longer needed:

extension MutableCollection {
    /// Shuffle the elements of `self` in-place.
    mutating func shuffle() {
        for i in indices.dropLast() {
            let diff = distance(from: i, to: endIndex)
            let j = index(i, offsetBy: numericCast(arc4random_uniform(numericCast(diff))))
            swapAt(i, j)
        }
    }
}

See SE-0173 Add MutableCollection.swapAt(_:_:) for more information about swapAt.


As of Swift 4.2 (Xcode 10, currently in beta), with the implementation of SE-0202 Random Unification, shuffle() and shuffled() are part of the Swift standard library.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...