Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
//
// 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