Skip to content

Instantly share code, notes, and snippets.

@a2
Created August 4, 2015 15:49
Show Gist options
  • Save a2/80d52b2b72c6cf3effed to your computer and use it in GitHub Desktop.
Save a2/80d52b2b72c6cf3effed to your computer and use it in GitHub Desktop.
extension RangeReplaceableCollectionType {
init<S: SequenceType where S.Generator.Element == Self.Generator.Element>(sequence: S) {
self.init()
extend(sequence)
}
}
extension RangeReplaceableCollectionType where Index: IntegerType {
init<S: SequenceType where S.Generator.Element == Self.Generator.Element>(sequence: S) {
self.init()
self.reserveCapacity(numericCast(sequence.underestimateCount()))
self.extend(sequence)
}
}
extension Dictionary: RangeReplaceableCollectionType {
public mutating func replaceRange<C : CollectionType where C.Generator.Element == Generator.Element>(subRange: Range<Index>, with newElements: C) {
for index in subRange {
removeAtIndex(index)
}
extend(newElements)
}
public mutating func reserveCapacity(n: Index.Distance) {}
public mutating func append(newElement: Generator.Element) {
let (key, value) = newElement
self[key] = value
}
public mutating func extend<S : SequenceType where S.Generator.Element == Generator.Element>(newElements: S) {
for newElement in newElements {
append(newElement)
}
}
public mutating func insert(newElement: Generator.Element, atIndex i: Index) {
append(newElement)
}
public mutating func splice<C : CollectionType where C.Generator.Element == Generator.Element>(newElements: C, atIndex i: Index) {
extend(newElements)
}
public mutating func removeRange(subRange: Range<Index>) {
for index in subRange {
removeAtIndex(index)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment