Last active
August 29, 2015 14:02
-
-
Save algal/e1813f94c484a0c19d7f to your computer and use it in GitHub Desktop.
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
// | |
// Set | |
// | |
struct MySet<KeyType : Hashable> : Sequence | |
{ | |
var dictionaryOfItems = Dictionary<KeyType,Bool>() | |
init() {} | |
init(array:Array<KeyType>) { | |
for item in array { | |
self.dictionaryOfItems.updateValue(true, forKey: item) | |
} | |
} | |
mutating | |
func addObject(item:KeyType) -> Void { | |
self.dictionaryOfItems.updateValue(true, forKey: item) | |
} | |
func containsObject(item:KeyType) -> Bool { | |
return self.dictionaryOfItems[item].getLogicValue() | |
} | |
func generate() -> SetGeneratorType<KeyType> { | |
let dictGenerator = self.dictionaryOfItems.generate() | |
return SetGeneratorType<KeyType>(dictGenerator) | |
} | |
} | |
struct SetGeneratorType<KeyType:Hashable> : Generator | |
{ | |
var backingDictionaryGenerator:DictionaryGenerator<KeyType,Bool> | |
init(_ dictGenerator:DictionaryGenerator<KeyType,Bool>) | |
{ | |
self.backingDictionaryGenerator = dictGenerator | |
} | |
mutating | |
func next() -> (KeyType)? | |
{ | |
let nextDictElement = backingDictionaryGenerator.next() | |
switch (nextDictElement) { | |
case let Optional<(KeyType,Bool)>.None: | |
return Optional<KeyType>.None | |
case let Optional<(KeyType,Bool)>.Some(item,_): | |
return Optional<KeyType>.Some(item) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment