目录
目录README.md

Shuffle Build Status

Adds a shuffled() method to any Collection, and a shuffle() method to any MutableCollection– for Swift prior to 4.2

It shuffles the Collection as lazily as possible, without mutating the collection. The collection’s indices are copied to an array, which in turn gets shuffled.

This allows the input collection to be indexed only. In general this should minimize memory allocation, for example in cases where the size of Element is greater than the size of Index.

For Swift 4.2 and up, the standard library’s Collection.shuffle() and Collection.randomElement() will perform much better. While it may be possible to craft examples where using the approach in this package manages to perform better, they are undoubtedly few and far between.

Example:

import Shuffle

let a = Array(0...4)

var stats = Array<Array<Int>>(repeating: Array(repeating: 0, count: a.count), count: a.count)

(0..<8000).forEach {
  _ in
  for (i,j) in a.shuffled().enumerated()  // shuffled() used here
  {
    stats[i][j] += 1
  }
}

var sums = Array<Int>(repeating: 0, count: a.count)
for total in stats
{
  let t = total.reduce(0, +)
  for i in 0..<sums.count
  {
    sums[i] += total[i]
  }
  print(total, t)
}
print(sums)
关于
73.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

©Copyright 2023 CCF 开源发展委员会
Powered by Trustie& IntelliDE 京ICP备13000930号