Created
October 8, 2014 01:21
-
-
Save ericcgu/0352fff45582552686d5 to your computer and use it in GitHub Desktop.
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 UIKit | |
struct OrderedDictionary<Tk: Hashable, Tv> { | |
var keys: Array<Tk> | |
var values: Dictionary<Tk,Tv> | |
// 1 | |
mutating func insert(value: Tv, forKey key: Tk, atIndex index: Int) -> Tv? | |
{ | |
var adjustedIndex = index | |
// 2 | |
let existingValue = self.values[key] | |
if existingValue != nil { | |
// 3 | |
let existingIndex = find(self.keys, key)! | |
// 4 | |
if existingIndex < index { | |
adjustedIndex-- | |
} | |
self.keys.removeAtIndex(existingIndex) | |
} | |
// 5 | |
self.keys.insert(key, atIndex:adjustedIndex) | |
self.values[key] = value | |
// 6 | |
return existingValue | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment