Last active
June 18, 2019 11:58
-
-
Save dduan/cbfccd57578118b46b53 to your computer and use it in GitHub Desktop.
A Dictionary implementation in which the key can be case-insensitive.
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
// CaseInsensitiveDictionary.swift | |
// Created by Daniel Duan on 12/19/14. | |
// Usaege Example: | |
// var test = CaseInsensitiveDictionary<String, Int>() | |
// test["Winter is coming"] = 1 | |
// | |
// test["WINTER is Coming"] = test["Winter Is Coming"] // true | |
// test["Hear Our Roar?"] = 1 | |
// | |
// test.count == 2 // true | |
// | |
// for (word, value) in test { | |
// print(word) | |
// } | |
import Foundation | |
struct CaseInsensitiveDictionary<Key: Hashable, Value>: CollectionType, DictionaryLiteralConvertible { | |
private var _data:[Key: Value] = [:] | |
private var _keyMap: [String: Key] = [:] | |
typealias Element = (Key, Value) | |
typealias Index = DictionaryIndex<Key, Value> | |
var startIndex: Index | |
var endIndex: Index | |
var count: Int { | |
assert(_data.count == _keyMap.count, "internal keys out of sync") | |
return _data.count | |
} | |
var isEmpty: Bool { | |
return _data.isEmpty | |
} | |
init() { | |
startIndex = _data.startIndex | |
endIndex = _data.endIndex | |
} | |
init(dictionaryLiteral elements: (Key, Value)...) { | |
for (key, value) in elements { | |
_keyMap["\(key)".lowercaseString] = key | |
_data[key] = value | |
} | |
startIndex = _data.startIndex | |
endIndex = _data.endIndex | |
} | |
subscript (position: Index) -> Element { | |
return _data[position] | |
} | |
subscript (key: Key) -> Value? { | |
get { | |
if let realKey = _keyMap["\(key)".lowercaseString] { | |
return _data[realKey] | |
} | |
return nil | |
} | |
set(newValue) { | |
let lowerKey = "\(key)".lowercaseString | |
if _keyMap[lowerKey] == nil { | |
_keyMap[lowerKey] = key | |
} | |
_data[_keyMap[lowerKey]!] = newValue | |
} | |
} | |
func generate() -> DictionaryGenerator<Key, Value> { | |
return _data.generate() | |
} | |
var keys: LazyForwardCollection<MapCollectionView<[Key : Value], Key>> { | |
return _data.keys | |
} | |
var values: LazyForwardCollection<MapCollectionView<[Key : Value], Value>> { | |
return _data.values | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment