Skip to content

Instantly share code, notes, and snippets.

@oisdk
Created July 8, 2015 13:36
Show Gist options
  • Save oisdk/68c2c5fe751c8372418a to your computer and use it in GitHub Desktop.
Save oisdk/68c2c5fe751c8372418a to your computer and use it in GitHub Desktop.
public struct StridingSliceView<C : CollectionType where C.Index : Strideable> : SequenceType {
private let col: C
private let str: StrideTo<C.Index>
public func generate() -> PermutationGenerator<C, StrideTo<C.Index>> {
return PermutationGenerator(elements: col, indices: str)
}
}
public extension CollectionType where Index: Strideable {
public subscript(range: Range<Index>, by: Index.Stride) -> StridingSliceView<Self> {
return StridingSliceView(col: self, str: stride(from: range.startIndex, to: range.endIndex, by: by))
}
}
public extension MutableCollectionType where Index : Strideable {
public subscript(range: Range<Index>, by: Index.Stride) -> StridingSliceView<Self> {
get {
return StridingSliceView(col: self, str: stride(from: range.startIndex, to: range.endIndex, by: by))
} set (values) {
for (index, value) in zip(stride(from: range.startIndex, to: range.endIndex, by: by), values) {
self[index] = value
}
}
}
}
public extension CollectionType {
public subscript(i1: Index, i2: Index, rest: Index...) -> PermutationGenerator<Self, [Index]> {
return PermutationGenerator(elements: self, indices: [i1, i2] + rest)
}
}
public extension MutableCollectionType {
public subscript(i1: Index, i2: Index, rest: Index...) -> PermutationGenerator<Self, [Index]> {
get {
return PermutationGenerator(elements: self, indices: [i1, i2] + rest)
} set(values) {
for (index, value) in zip([i1, i2] + rest, values) {
self[index] = value
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment