diff --git a/Tusker/WeakArray.swift b/Tusker/WeakArray.swift index c5affc98..6228bc80 100644 --- a/Tusker/WeakArray.swift +++ b/Tusker/WeakArray.swift @@ -16,12 +16,16 @@ fileprivate class WeakWrapper { } } -struct WeakArray: Collection { +struct WeakArray: MutableCollection, RangeReplaceableCollection { private var array: [WeakWrapper] var startIndex: Int { array.startIndex } var endIndex: Int { array.endIndex } + init() { + array = [] + } + init(_ elements: [Element]) { array = elements.map { WeakWrapper($0) } } @@ -30,11 +34,20 @@ struct WeakArray: Collection { array = elements.map { WeakWrapper($0) } } - subscript(_ index: Int) -> Element? { - return array[index].value + subscript(position: Int) -> Element? { + get { + array[position].value + } + set(newValue) { + array[position] = WeakWrapper(newValue) + } } func index(after i: Int) -> Int { return array.index(after: i) } + + mutating func replaceSubrange(_ subrange: Range, with newElements: C) where C : Collection, Self.Element == C.Element { + array.replaceSubrange(subrange, with: newElements.map { WeakWrapper($0) }) + } }