BatchedCollection.swift 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //
  2. // CryptoSwift
  3. //
  4. // Copyright (C) 2014-2017 Marcin Krzyżanowski <marcin@krzyzanowskim.com>
  5. // This software is provided 'as-is', without any express or implied warranty.
  6. //
  7. // In no event will the authors be held liable for any damages arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
  10. //
  11. // - The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation is required.
  12. // - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  13. // - This notice may not be removed or altered from any source or binary distribution.
  14. //
  15. struct BatchedCollectionIndex<Base: Collection> {
  16. let range: Range<Base.Index>
  17. }
  18. extension BatchedCollectionIndex: Comparable {
  19. static func ==<Base>(lhs: BatchedCollectionIndex<Base>, rhs: BatchedCollectionIndex<Base>) -> Bool {
  20. return lhs.range.lowerBound == rhs.range.lowerBound
  21. }
  22. static func < <Base>(lhs: BatchedCollectionIndex<Base>, rhs: BatchedCollectionIndex<Base>) -> Bool {
  23. return lhs.range.lowerBound < rhs.range.lowerBound
  24. }
  25. }
  26. protocol BatchedCollectionType: Collection {
  27. associatedtype Base: Collection
  28. }
  29. struct BatchedCollection<Base: Collection>: Collection {
  30. let base: Base
  31. let size: Base.IndexDistance
  32. typealias Index = BatchedCollectionIndex<Base>
  33. private func nextBreak(after idx: Base.Index) -> Base.Index {
  34. return base.index(idx, offsetBy: size, limitedBy: base.endIndex)
  35. ?? base.endIndex
  36. }
  37. var startIndex: Index {
  38. return Index(range: base.startIndex..<nextBreak(after: base.startIndex))
  39. }
  40. var endIndex: Index {
  41. return Index(range: base.endIndex..<base.endIndex)
  42. }
  43. func index(after idx: Index) -> Index {
  44. return Index(range: idx.range.upperBound..<nextBreak(after: idx.range.upperBound))
  45. }
  46. subscript(idx: Index) -> Base.SubSequence {
  47. return base[idx.range]
  48. }
  49. }
  50. extension Collection {
  51. func batched(by size: IndexDistance) -> BatchedCollection<Self> {
  52. return BatchedCollection(base: self, size: size)
  53. }
  54. }