Array+Extension.swift 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. extension Array {
  9. /** split in chunks with given chunk size */
  10. func chunks(chunksize:Int) -> [Array<Element>] {
  11. var words = [[Element]]()
  12. words.reserveCapacity(self.count / chunksize)
  13. for idx in chunksize.stride(through: self.count, by: chunksize) {
  14. let word = Array(self[idx - chunksize..<idx]) // this is slow for large table
  15. words.append(word)
  16. }
  17. let reminder = Array(self.suffix(self.count % chunksize))
  18. if (reminder.count > 0) {
  19. words.append(reminder)
  20. }
  21. return words
  22. }
  23. /*
  24. This helper call is slow, therefore don't use it. It is due to extension, or due to optimization that can be done
  25. subscript(index: UInt32) -> Element {
  26. get {
  27. return self[Int(index)]
  28. }
  29. set {
  30. self[Int(index)] = newValue
  31. }
  32. }
  33. */
  34. }