struct Placeholder<Key, Value where Key: Hashable> { | |
var values: [(Key, Value)] = [] | |
func firstValue(matchingKey key: Key) -> Value? { | |
return values.lazy.filter({ $0.0 == key }).first?.1 | |
} | |
} | |
struct Dictionary<Key, Value where Key: Hashable> { | |
private var storage = Array(count: 8, repeatedValue: Placeholder<Key, Value>()) | |
subscript(key: Key) -> Value? { | |
get { | |
let position = abs(key.hashValue) % storage.count | |
return storage[position].firstValue(matchingKey: key) | |
} | |
set { | |
guard let value = newValue else { return } | |
let position = abs(key.hashValue) % storage.count | |
storage[position].values.append((key, value)) | |
} | |
} | |
} | |
var d = Dictionary<String, String>() | |
d["123"] = "asdf" | |
d["123"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment