BatchedCollection.swift 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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: Int
  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) ?? base.endIndex
  35. }
  36. var startIndex: Index {
  37. return Index(range: base.startIndex..<nextBreak(after: base.startIndex))
  38. }
  39. var endIndex: Index {
  40. return Index(range: base.endIndex..<base.endIndex)
  41. }
  42. func index(after idx: Index) -> Index {
  43. return Index(range: idx.range.upperBound..<nextBreak(after: idx.range.upperBound))
  44. }
  45. subscript(idx: Index) -> Base.SubSequence {
  46. return base[idx.range]
  47. }
  48. }
  49. extension Collection {
  50. func batched(by size: Int) -> BatchedCollection<Self> {
  51. return BatchedCollection(base: self, size: size)
  52. }
  53. }