Last active
December 6, 2023 11:41
-
-
Save djk12587/46d85017fb3fad6946046925f36cefdc to your computer and use it in GitHub Desktop.
Swift: Array containing weak references
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
/** | |
Creates an array of weak reference objects. | |
- Important: | |
Because this is an array of weak objects, the objects in the array can be removed at any time. | |
The collection itself will handle removing nil objects (garbage collection) via the private function cleanUpNilContainers() | |
*/ | |
class WeakCollection<T>: RangeReplaceableCollection, ExpressibleByArrayLiteral { | |
typealias Index = Int | |
typealias Element = T | |
typealias Iterator = IndexingIterator<[Element]> | |
private var weakContainers: [WeakReferenceContainer] | |
required convenience init(arrayLiteral: Element...) { | |
self.init() | |
self.weakContainers = WeakCollection.createWeakContainers(from: arrayLiteral) | |
} | |
required init() { | |
weakContainers = [] | |
} | |
required init<S>(_ elements: S) where S: Sequence, WeakCollection.Element == S.Element { | |
self.weakContainers = WeakCollection.createWeakContainers(from: elements) | |
} | |
static private func createWeakContainers<S>(from weakCollection: S) -> [WeakReferenceContainer] where S: Sequence, | |
WeakCollection.Element == S.Element { | |
return weakCollection.compactMap { WeakReferenceContainer(value: $0 as AnyObject) } | |
} | |
func append<S>(contentsOf newElements: S) where S: Sequence, WeakCollection.Element == S.Element { | |
self.weakContainers.append(contentsOf: WeakCollection.createWeakContainers(from: newElements)) | |
} | |
var startIndex: Index { | |
return references.startIndex | |
} | |
var endIndex: Index { | |
return references.endIndex | |
} | |
func replaceSubrange<C, R>(_ subrange: R, with newElements: C) where | |
C: Collection, R: RangeExpression, WeakCollection.Element == C.Element, WeakCollection.Index == R.Bound { | |
weakContainers.replaceSubrange(subrange, with: WeakCollection.createWeakContainers(from: newElements)) | |
} | |
func index(after i: Int) -> Int { | |
return references.index(after: i) | |
} | |
func makeIterator() -> IndexingIterator<[Element]> { | |
return references.makeIterator() | |
} | |
subscript(index: Int) -> Element { | |
get { | |
return references[index] | |
} | |
set { | |
weakContainers[index] = WeakReferenceContainer(value: newValue as AnyObject) | |
} | |
} | |
} | |
extension WeakCollection { | |
private class WeakReferenceContainer { | |
private(set) weak var value: AnyObject? | |
init(value: AnyObject?) { | |
self.value = value | |
} | |
} | |
private func cleanUpNilContainers() { | |
weakContainers = weakContainers.compactMap { $0.value == nil ? nil : $0 } | |
} | |
private var references: [Element] { | |
cleanUpNilContainers() | |
return weakContainers.compactMap { $0.value as? T } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment