Skip to content

Instantly share code, notes, and snippets.

@aspitz
Created November 21, 2014 15:13
Show Gist options
  • Save aspitz/d14f6936ae9b2bf9d0f1 to your computer and use it in GitHub Desktop.
Save aspitz/d14f6936ae9b2bf9d0f1 to your computer and use it in GitHub Desktop.
Swift implementation of an ordered Dictionary
import Foundation
struct OrderedDictionary<KeyType:Hashable, ValueType:Hashable>{
var keyArray = [KeyType]()
var keyToValue = [KeyType:ValueType]()
subscript(key:KeyType) -> ValueType? {
get{
return keyToValue[key]
}
set{
let existingValue = keyToValue[key]
keyToValue[key] = newValue
if existingValue == nil {
keyArray.append(key)
}
}
}
subscript(value:Int) -> (KeyType,ValueType?) {
get{
let key = keyArray[value]
return (key,keyToValue[key])
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment