এই উত্তরে কীভাবে দ্রুত এবং অভিন্ন অ্যালগরিদম (ফিশার-ইয়েটস) সুইফট ৪.২+ এ পরিবর্তন করা যায় এবং কীভাবে একই বৈশিষ্ট্যটি সুইফ্টের পূর্ববর্তী বিভিন্ন সংস্করণে যুক্ত করা যায় তা বিশদ করে। প্রতিটি সুইফ্ট সংস্করণটির নামকরণ এবং আচরণ সেই সংস্করণটির জন্য পরিবর্তিত এবং ননমুটটিং বাছাই করার পদ্ধতির সাথে মেলে।
সুইফট 4.2+ +
shuffle
এবং shuffled
স্থানীয় সুইফট 4.2 শুরু হয়। ব্যবহারের উদাহরণ:
let x = [1, 2, 3].shuffled()
// x == [2, 3, 1]
let fiveStrings = stride(from: 0, through: 100, by: 5).map(String.init).shuffled()
// fiveStrings == ["20", "45", "70", "30", ...]
var numbers = [1, 2, 3, 4]
numbers.shuffle()
// numbers == [3, 2, 1, 4]
সুইফট 4.0 এবং 4.1
এই এক্সটেনশানগুলি shuffle()
কোনও পরিবর্তনযোগ্য সংগ্রহ (অ্যারে এবং অনিরাপদ মিউটেবল বাফারস) এবং shuffled()
কোনও অনুক্রমের জন্য একটি পদ্ধতি যুক্ত করে:
extension MutableCollection {
/// Shuffles the contents of this collection.
mutating func shuffle() {
let c = count
guard c > 1 else { return }
for (firstUnshuffled, unshuffledCount) in zip(indices, stride(from: c, to: 1, by: -1)) {
// Change `Int` in the next line to `IndexDistance` in < Swift 4.1
let d: Int = numericCast(arc4random_uniform(numericCast(unshuffledCount)))
let i = index(firstUnshuffled, offsetBy: d)
swapAt(firstUnshuffled, i)
}
}
}
extension Sequence {
/// Returns an array with the contents of this sequence, shuffled.
func shuffled() -> [Element] {
var result = Array(self)
result.shuffle()
return result
}
}
উপরের সুইফট ৪.২ উদাহরণগুলির মতো একই ব্যবহার।
সুইফট 3
এই এক্সটেনশনগুলি shuffle()
কোনও পরিবর্তনযোগ্য সংগ্রহের shuffled()
জন্য কোনও পদ্ধতি এবং যে কোনও অনুক্রমের জন্য একটি পদ্ধতি যুক্ত করে:
extension MutableCollection where Indices.Iterator.Element == Index {
/// Shuffles the contents of this collection.
mutating func shuffle() {
let c = count
guard c > 1 else { return }
for (firstUnshuffled , unshuffledCount) in zip(indices, stride(from: c, to: 1, by: -1)) {
// Change `Int` in the next line to `IndexDistance` in < Swift 3.2
let d: Int = numericCast(arc4random_uniform(numericCast(unshuffledCount)))
guard d != 0 else { continue }
let i = index(firstUnshuffled, offsetBy: d)
self.swapAt(firstUnshuffled, i)
}
}
}
extension Sequence {
/// Returns an array with the contents of this sequence, shuffled.
func shuffled() -> [Iterator.Element] {
var result = Array(self)
result.shuffle()
return result
}
}
উপরের সুইফট ৪.২ উদাহরণগুলির মতো একই ব্যবহার।
সুইফট 2
(অপ্রচলিত ভাষা: আপনি জুলাই 2018 থেকে আইটিউনস কানেক্টে প্রকাশের জন্য সুইফট 2.x ব্যবহার করতে পারবেন না)
extension MutableCollectionType 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 startIndex ..< endIndex - 1 {
let j = Int(arc4random_uniform(UInt32(count - i))) + i
guard i != j else { continue }
swap(&self[i], &self[j])
}
}
}
extension CollectionType {
/// Return a copy of `self` with its elements shuffled.
func shuffle() -> [Generator.Element] {
var list = Array(self)
list.shuffleInPlace()
return list
}
}
ব্যবহার:
[1, 2, 3].shuffle()
// [2, 3, 1]
let fiveStrings = 0.stride(through: 100, by: 5).map(String.init).shuffle()
// ["20", "45", "70", "30", ...]
var numbers = [1, 2, 3, 4]
numbers.shuffleInPlace()
// [3, 2, 1, 4]
সুইফট ১.২
(অপ্রচলিত ভাষা: আপনি জুলাই 2018 থেকে আইটিউনস কানেক্টে প্রকাশের জন্য সুইফট 1.x ব্যবহার করতে পারবেন না)
shuffle
পরিবর্তিত অ্যারে পদ্ধতি হিসাবে
এই এক্সটেনশনটি আপনাকে Array
স্থান পরিবর্তনযোগ্য দৃষ্টান্তটি বদল করতে দেবে :
extension Array {
mutating func shuffle() {
if count < 2 { return }
for i in 0..<(count - 1) {
let j = Int(arc4random_uniform(UInt32(count - i))) + i
swap(&self[i], &self[j])
}
}
}
var numbers = [1, 2, 3, 4, 5, 6, 7, 8]
numbers.shuffle() // e.g., numbers == [6, 1, 8, 3, 2, 4, 7, 5]
shuffled
একটি অ-পরিবর্তনকারী অ্যারে পদ্ধতি হিসাবে
এই এক্সটেনশানটি আপনাকে একটি Array
উদাহরণের বদলানো কপিটি পুনরুদ্ধার করতে দেবে :
extension Array {
func shuffled() -> [T] {
if count < 2 { return self }
var list = self
for i in 0..<(list.count - 1) {
let j = Int(arc4random_uniform(UInt32(list.count - i))) + i
swap(&list[i], &list[j])
}
return list
}
}
let numbers = [1, 2, 3, 4, 5, 6, 7, 8]
let mixedup = numbers.shuffled() // e.g., mixedup == [6, 1, 8, 3, 2, 4, 7, 5]