Created
August 15, 2014 16:20
-
-
Save aryaxt/113516d556536629b451 to your computer and use it in GitHub Desktop.
Swift Ordered Dictionary
This file contains hidden or 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
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