Skip to content

Instantly share code, notes, and snippets.

@aryaxt
Created August 15, 2014 16:20
Show Gist options
  • Save aryaxt/113516d556536629b451 to your computer and use it in GitHub Desktop.
Save aryaxt/113516d556536629b451 to your computer and use it in GitHub Desktop.
Swift Ordered Dictionary
public class OrderedDictionary <K: Hashable, V> {
typealias Key = K
typealias Value = V
private final var dictionary = [K: V]()
private final var orderedKeys = [K]()
init() {
}
subscript(key: K) -> V? {
get {
return dictionary[key]
}
set (newValue) {
if (orderedKeys.filter({$0 == key}).count == 0) {
orderedKeys.append(key)
}
dictionary[key] = newValue
}
}
subscript(index: Int) -> V? {
get {
var key = orderedKeys[index]
return dictionary[key]
}
set (newValue) {
var key = orderedKeys[index]
dictionary[key] = newValue
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment