BatchedCollection.swift 2.4 KB

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