Created
June 6, 2024 18:58
-
-
Save JadenGeller/d23f90ca547262a80da03a2d4b0916cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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