Skip to content

Instantly share code, notes, and snippets.

@JadenGeller
Created June 6, 2024 18:58
Show Gist options
  • Save JadenGeller/d23f90ca547262a80da03a2d4b0916cc to your computer and use it in GitHub Desktop.
Save JadenGeller/d23f90ca547262a80da03a2d4b0916cc to your computer and use it in GitHub Desktop.
struct RangeSliceCollection<Base: Collection, Ranges: Collection>: Collection where Ranges.Element == Range<Base.Index> {
var base: Base
var ranges: Ranges
var startIndex: Ranges.Index { ranges.startIndex }
var endIndex: Ranges.Index { ranges.endIndex }
func index(after i: Ranges.Index) -> Ranges.Index {
ranges.index(after: i)
}
subscript(position: Ranges.Index) -> Base.SubSequence {
let range = ranges[position]
return base[range]
}
}
extension RangeSliceCollection: BidirectionalCollection where Base: BidirectionalCollection, Ranges: BidirectionalCollection {
func index(before i: Ranges.Index) -> Ranges.Index {
ranges.index(before: i)
}
}
extension RangeSliceCollection: RandomAccessCollection where Base: RandomAccessCollection, Ranges: RandomAccessCollection {
func index(_ i: Ranges.Index, offsetBy distance: Int) -> Ranges.Index {
ranges.index(i, offsetBy: distance)
}
func distance(from start: Ranges.Index, to end: Ranges.Index) -> Int {
ranges.distance(from: start, to: end)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment