forked from shadowfacts/Tusker
WeakArray improvements
This commit is contained in:
parent
0d972d987c
commit
b80a61cc95
@ -16,12 +16,16 @@ fileprivate class WeakWrapper<T: AnyObject> {
|
||||
}
|
||||
}
|
||||
|
||||
struct WeakArray<Element: AnyObject>: Collection {
|
||||
struct WeakArray<Element: AnyObject>: MutableCollection, RangeReplaceableCollection {
|
||||
private var array: [WeakWrapper<Element>]
|
||||
|
||||
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<Element: AnyObject>: 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<C>(_ subrange: Range<Int>, with newElements: C) where C : Collection, Self.Element == C.Element {
|
||||
array.replaceSubrange(subrange, with: newElements.map { WeakWrapper($0) })
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user