Created
November 21, 2014 15:13
-
-
Save aspitz/d14f6936ae9b2bf9d0f1 to your computer and use it in GitHub Desktop.
Swift implementation of an ordered Dictionary
This file contains 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
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