Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
struct RotatedCollection<Base: Collection> {
let base: Base
let index: Base.Index
init(base: Base, shiftingToStart index: Base.Index) {
self.base = base
self.index = index
}
}
extension RotatedCollection: Collection {
struct Index {
let base: Base.Index
let isWrapped: Bool
}
var startIndex: Index {
Index(base: index, isWrapped: false)
}
var endIndex: Index {
Index(base: index, isWrapped: true)
}
subscript(index: Index) -> Base.SubSequence.Element {
base[index.base]
}
func index(after index: Index) -> Index {
let next = base.index(after: index.base)
if !index.isWrapped && next == base.endIndex {
return Index(base: base.startIndex, isWrapped: true)
} else {
return Index(base: next, isWrapped: index.isWrapped)
}
}
}
extension RotatedCollection.Index: Comparable {
static func == (lhs: RotatedCollection.Index, rhs: RotatedCollection.Index) -> Bool {
lhs.isWrapped == rhs.isWrapped && lhs.base == rhs.base
}
static func < (lhs: RotatedCollection.Index, rhs: RotatedCollection.Index) -> Bool {
if lhs.isWrapped == rhs.isWrapped {
return lhs.base < rhs.base
} else {
return rhs.isWrapped
}
}
}
extension Collection {
func rotated(shiftingToStart index: Index) -> RotatedCollection<Self> {
RotatedCollection(base: self, shiftingToStart: index)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment