Skip to content

Instantly share code, notes, and snippets.

@algal
Last active August 29, 2015 14:02
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save algal/e1813f94c484a0c19d7f to your computer and use it in GitHub Desktop.
//
// 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