BatchedCollection.swift 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //
  2. // CryptoSwift
  3. //
  4. // Copyright (C) 2014-2021 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. @usableFromInline
  16. struct BatchedCollectionIndex<Base: Collection> {
  17. let range: Range<Base.Index>
  18. }
  19. extension BatchedCollectionIndex: Comparable {
  20. @usableFromInline
  21. static func == <Base>(lhs: BatchedCollectionIndex<Base>, rhs: BatchedCollectionIndex<Base>) -> Bool {
  22. lhs.range.lowerBound == rhs.range.lowerBound
  23. }
  24. @usableFromInline
  25. static func < <Base>(lhs: BatchedCollectionIndex<Base>, rhs: BatchedCollectionIndex<Base>) -> Bool {
  26. lhs.range.lowerBound < rhs.range.lowerBound
  27. }
  28. }
  29. protocol BatchedCollectionType: Collection {
  30. associatedtype Base: Collection
  31. }
  32. @usableFromInline
  33. struct BatchedCollection<Base: Collection>: Collection {
  34. let base: Base
  35. let size: Int
  36. @usableFromInline
  37. init(base: Base, size: Int) {
  38. self.base = base
  39. self.size = size
  40. }
  41. @usableFromInline
  42. typealias Index = BatchedCollectionIndex<Base>
  43. private func nextBreak(after idx: Base.Index) -> Base.Index {
  44. self.base.index(idx, offsetBy: self.size, limitedBy: self.base.endIndex) ?? self.base.endIndex
  45. }
  46. @usableFromInline
  47. var startIndex: Index {
  48. Index(range: self.base.startIndex..<self.nextBreak(after: self.base.startIndex))
  49. }
  50. @usableFromInline
  51. var endIndex: Index {
  52. Index(range: self.base.endIndex..<self.base.endIndex)
  53. }
  54. @usableFromInline
  55. func index(after idx: Index) -> Index {
  56. Index(range: idx.range.upperBound..<self.nextBreak(after: idx.range.upperBound))
  57. }
  58. @usableFromInline
  59. subscript(idx: Index) -> Base.SubSequence {
  60. self.base[idx.range]
  61. }
  62. }
  63. extension Collection {
  64. @inlinable
  65. func batched(by size: Int) -> BatchedCollection<Self> {
  66. BatchedCollection(base: self, size: size)
  67. }
  68. }