Move WeakDictionary to the Vendor folder and clarify where it comes from and under what license

This commit is contained in:
Stefan Ceriu
2022-09-28 11:33:58 +03:00
committed by Stefan Ceriu
parent 29f61461aa
commit 9e16e2f729
10 changed files with 185 additions and 93 deletions
@@ -1,102 +0,0 @@
//
// WeakDictionary.swift
// WeakDictionary
//
// Created by Nicholas Cross on 19/10/2016.
// Copyright © 2016 Nicholas Cross. All rights reserved.
//
import Foundation
public struct WeakDictionary<Key: Hashable, Value: AnyObject> {
private var storage: [Key: WeakDictionaryReference<Value>]
public init() {
self.init(storage: [Key: WeakDictionaryReference<Value>]())
}
public init(dictionary: [Key: Value]) {
var newStorage = [Key: WeakDictionaryReference<Value>]()
dictionary.forEach { key, value in newStorage[key] = WeakDictionaryReference<Value>(value: value) }
self.init(storage: newStorage)
}
private init(storage: [Key: WeakDictionaryReference<Value>]) {
self.storage = storage
}
public mutating func reap() {
storage = weakDictionary().storage
}
public func weakDictionary() -> WeakDictionary<Key, Value> {
self[startIndex..<endIndex]
}
public func dictionary() -> [Key: Value] {
var newStorage = [Key: Value]()
storage.forEach { key, value in
if let retainedValue = value.value {
newStorage[key] = retainedValue
}
}
return newStorage
}
}
extension WeakDictionary: Collection {
public typealias Index = DictionaryIndex<Key, WeakDictionaryReference<Value>>
public var startIndex: Index {
storage.startIndex
}
public var endIndex: Index {
storage.endIndex
}
public func index(after index: Index) -> Index {
storage.index(after: index)
}
public subscript(position: Index) -> (Key, WeakDictionaryReference<Value>) {
return storage[position]
}
public subscript(key: Key) -> Value? {
get {
guard let valueRef = storage[key] else {
return nil
}
return valueRef.value
}
set {
guard let value = newValue else {
storage[key] = nil
return
}
storage[key] = WeakDictionaryReference<Value>(value: value)
}
}
public subscript(bounds: Range<Index>) -> WeakDictionary<Key, Value> {
let subStorage = storage[bounds.lowerBound..<bounds.upperBound]
var newStorage = [Key: WeakDictionaryReference<Value>]()
subStorage.filter { _, value in value.value != nil }
.forEach { key, value in newStorage[key] = value }
return WeakDictionary<Key, Value>(storage: newStorage)
}
}
public extension Dictionary where Value: AnyObject {
func weakDictionary() -> WeakDictionary<Key, Value> {
WeakDictionary<Key, Value>(dictionary: self)
}
}
@@ -1,39 +0,0 @@
//
// WeakDictionaryKeyReference.swift
// WeakDictionary-iOS
//
// Created by Nicholas Cross on 2/1/19.
// Copyright © 2019 Nicholas Cross. All rights reserved.
//
import Foundation
public struct WeakDictionaryKey<Key: AnyObject & Hashable, Value: AnyObject>: Hashable {
private weak var baseKey: Key?
private let hash: Int
private var retainedValue: Value?
private let nilKeyHash = UUID().hashValue
public init(key: Key, value: Value? = nil) {
baseKey = key
retainedValue = value
hash = key.hashValue
}
public static func == (lhs: WeakDictionaryKey, rhs: WeakDictionaryKey) -> Bool {
(lhs.baseKey != nil && rhs.baseKey != nil && lhs.baseKey == rhs.baseKey)
|| lhs.hashValue == rhs.hashValue
}
public func hash(into hasher: inout Hasher) {
if baseKey == nil {
hasher.combine(nilKeyHash)
} else {
hasher.combine(baseKey)
}
}
public var key: Key? {
baseKey
}
}
@@ -1,21 +0,0 @@
//
// WeakDictionaryReference.swift
// WeakDictionary-iOS
//
// Created by Nicholas Cross on 2/1/19.
// Copyright © 2019 Nicholas Cross. All rights reserved.
//
import Foundation
public struct WeakDictionaryReference<Value: AnyObject> {
private weak var referencedValue: Value?
init(value: Value) {
referencedValue = value
}
public var value: Value? {
referencedValue
}
}
@@ -1,122 +0,0 @@
//
// WeakKeyDictionary.swift
// WeakDictionary-iOS
//
// Created by Nicholas Cross on 2/1/19.
// Copyright © 2019 Nicholas Cross. All rights reserved.
//
import Foundation
public struct WeakKeyDictionary<Key: AnyObject & Hashable, Value: AnyObject> {
private var storage: WeakDictionary<WeakDictionaryKey<Key, Value>, Value>
private let valuesRetainedByKey: Bool
public init(valuesRetainedByKey: Bool = false) {
self.init(
storage: WeakDictionary<WeakDictionaryKey<Key, Value>, Value>(),
valuesRetainedByKey: valuesRetainedByKey
)
}
public init(dictionary: [Key: Value], valuesRetainedByKey: Bool = false) {
var newStorage = WeakDictionary<WeakDictionaryKey<Key, Value>, Value>()
dictionary.forEach { key, value in
var keyRef: WeakDictionaryKey<Key, Value>!
if valuesRetainedByKey {
keyRef = WeakDictionaryKey<Key, Value>(key: key, value: value)
} else {
keyRef = WeakDictionaryKey<Key, Value>(key: key)
}
newStorage[keyRef] = value
}
self.init(storage: newStorage, valuesRetainedByKey: valuesRetainedByKey)
}
private init(storage: WeakDictionary<WeakDictionaryKey<Key, Value>, Value>, valuesRetainedByKey: Bool = false) {
self.storage = storage
self.valuesRetainedByKey = valuesRetainedByKey
}
public mutating func reap() {
storage = weakKeyDictionary().storage
}
public func weakDictionary() -> WeakDictionary<Key, Value> {
dictionary().weakDictionary()
}
public func weakKeyDictionary() -> WeakKeyDictionary<Key, Value> {
self[startIndex..<endIndex]
}
public func dictionary() -> [Key: Value] {
var newStorage = [Key: Value]()
storage.forEach { key, value in
if let retainedKey = key.key, let retainedValue = value.value {
newStorage[retainedKey] = retainedValue
}
}
return newStorage
}
}
extension WeakKeyDictionary: Collection {
public typealias Index = DictionaryIndex<WeakDictionaryKey<Key, Value>, WeakDictionaryReference<Value>>
public var startIndex: Index {
storage.startIndex
}
public var endIndex: Index {
storage.endIndex
}
public func index(after index: Index) -> Index {
storage.index(after: index)
}
public subscript(position: Index) -> (WeakDictionaryKey<Key, Value>, WeakDictionaryReference<Value>) {
return storage[position]
}
public subscript(key: Key) -> Value? {
get {
storage[WeakDictionaryKey<Key, Value>(key: key)]
}
set {
let retainedValue = valuesRetainedByKey ? newValue : nil
let weakKey = WeakDictionaryKey<Key, Value>(key: key, value: retainedValue)
storage[weakKey] = newValue
}
}
public subscript(bounds: Range<Index>) -> WeakKeyDictionary<Key, Value> {
let subStorage = storage[bounds.lowerBound..<bounds.upperBound]
var newStorage = WeakDictionary<WeakDictionaryKey<Key, Value>, Value>()
subStorage.filter { key, value in key.key != nil && value.value != nil }
.forEach { key, value in newStorage[key] = value.value }
return WeakKeyDictionary<Key, Value>(storage: newStorage)
}
}
public extension WeakDictionary where Key: AnyObject {
func weakKeyDictionary(valuesRetainedByKey: Bool = false) -> WeakKeyDictionary<Key, Value> {
WeakKeyDictionary<Key, Value>(dictionary: dictionary(), valuesRetainedByKey: valuesRetainedByKey)
}
}
public extension Dictionary where Key: AnyObject, Value: AnyObject {
func weakKeyDictionary(valuesRetainedByKey: Bool = false) -> WeakKeyDictionary<Key, Value> {
WeakKeyDictionary<Key, Value>(dictionary: self, valuesRetainedByKey: valuesRetainedByKey)
}
}