<commit_before>//
//  shuffle.swift
//
//  Created by <NAME> on 2014-08-28.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//
//  https://gist.github.com/glessard/7140fe885af3eb874e11
//

import Darwin

/**
  Get a sequence/generator that will return a collection's elements in a random order.
  The input collection is not modified.

  - parameter c: The collection to be shuffled
  - returns: A `PermutationGenerator` of `c`'s elements, shuffled.
*/

func shuffle<C: CollectionType>(c: C) -> PermutationGenerator<C, IndexShuffler<C.Index>>
{
  return PermutationGenerator(elements: c, indices: IndexShuffler(c.indices))
}

/**
  Get a sequence/generator of this collection's elements in a random order.
  The collection is not modified.

  - returns: A `PermutationGenerator` of self's elements, shuffled.
*/

extension CollectionType
{
  func shuffle() -> PermutationGenerator<Self, IndexShuffler<Self.Index>>
  {
    return PermutationGenerator(elements: self, indices: IndexShuffler(self.indices))
  }
}


/**
  A stepwise (lazy-ish) implementation of the Knuth Shuffle (a.k.a. Fisher-Yates Shuffle),
  using a sequence of indices for the input. Elements (indices) from
  the input sequence are returned in a random order until exhaustion.
*/

struct IndexShuffler<I: ForwardIndexType>: SequenceType, GeneratorType
{
  let count: Int
  var step = -1
  var i: [I]

  init<S: SequenceType where S.Generator.Element == I>(_ input: S)
  {
    self.init(Array(input))
  }

  init(_ input: Range<I>)
  {
    self.init(Array(input))
  }

  init(_ input: Array<I>)
  {
    i = input
    count = input.count
  }

  mutating func next() -> I?
  {
    // current position in the array
    step += 1

    if step < count
    {
      // select a random Index from the rest of the array
      let j = step + Int(arc4random_uniform(UInt32(count-step)))

      // swap that Index with the Index present at the current step in the array
      swap(&i[j], &i[step])

      // return the new random Index.
      return i[step]
    }

    return nil
  }

  func generate() -> IndexShuffler
  {
    return self
  }
}
<commit_msg>Updated version of shuffle from the gist.<commit_after>//
//  shuffle.swift
//
//  Created by <NAME> on 2014-08-28.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//
//  https://gist.github.com/glessard/7140fe885af3eb874e11
//

import Darwin

extension CollectionType
{
  /// Get a sequence/generator that will return a collection's elements in a random order.
  /// The input collection is not modified.
  ///
  /// - returns: A `PermutationGenerator` of `c`'s elements, shuffled.

  func shuffle() -> PermutationGenerator<Self, IndexShuffler<Self.Index>>
  {
    return PermutationGenerator(elements: self, indices: IndexShuffler(self.indices))
  }
}


/// A stepwise (lazy-ish) implementation of the Knuth Shuffle (a.k.a. Fisher-Yates Shuffle),
/// using a sequence of indices for the input. Elements (indices) from
/// the input sequence are returned in a random order until exhaustion.

struct IndexShuffler<I: ForwardIndexType>: SequenceType, GeneratorType
{
  let count: Int
  var step = -1
  var i: [I]

  init<S: SequenceType where S.Generator.Element == I>(_ input: S)
  {
    self.init(Array(input))
  }

  init(_ input: Array<I>)
  {
    i = input
    count = input.count
  }

  mutating func next() -> I?
  {
    // current position in the array
    step += 1

    if step < count
    {
      // select a random Index from the rest of the array
      let j = step + Int(arc4random_uniform(UInt32(count-step)))

      // swap that Index with the Index present at the current step in the array
      swap(&i[j], &i[step])

      // return the new random Index.
      return i[step]
    }
    
    return nil
  }
}

{'fraction_non_alphanumeric': 0.11419423692636073, 'fraction_numerical': 0.016008537886873, 'mean_word_length': 2.9338929695697797, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 27, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1425635', 'n_tokens_mistral': 1257, 'n_tokens_neox': 1169, 'n_words': 394}