ArrayExtension.swift 773 B

123456789101112131415161718192021222324252627
  1. //
  2. // ArrayExtension.swift
  3. // CryptoSwift
  4. //
  5. // Created by Marcin Krzyzanowski on 10/08/14.
  6. // Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
  7. //
  8. import Foundation
  9. extension Array {
  10. /** split in chunks with given chunk size */
  11. func chunks(chunksize:Int) -> [Array<T>] {
  12. var words = [[T]]()
  13. words.reserveCapacity(self.count / chunksize)
  14. for var idx = chunksize; idx <= self.count; idx = idx + chunksize {
  15. let word = Array(self[idx - chunksize..<idx]) // this is slow for large table
  16. words.append(word)
  17. }
  18. let reminder = Array(suffix(self, self.count % chunksize))
  19. if (reminder.count > 0) {
  20. words.append(reminder)
  21. }
  22. return words
  23. }
  24. }