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 Transposition<Base: Collection>: RandomAccessCollection | |
where Base.Element: RandomAccessCollection | |
{ | |
typealias Index = Int | |
typealias Indices = Range<Int> | |
var base: Base | |
struct Transposed: Collection { | |
typealias Index = Base.Index | |
var base: Base | |
var offset: Int | |
var startIndex: Base.Index { base.startIndex } | |
var endIndex: Base.Index { base.endIndex } | |
subscript(position: Base.Index) -> Base.Element.Element { | |
let subCollection = base[position] | |
let subIndex = subCollection.index(subCollection.startIndex, offsetBy: offset) | |
return subCollection[subIndex] | |
} | |
func index(after i: Base.Index) -> Base.Index { | |
base.index(after: i) | |
} | |
} | |
var startIndex: Int { 0 } | |
var endIndex: Int { base.first?.count ?? 0 } | |
subscript(i: Int) -> Transposed { | |
Transposed(base: base, offset: i) | |
} | |
} | |
extension Collection where Element: RandomAccessCollection { | |
func transposed() -> Transposition<Self> { | |
Transposition(base: self) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment