-
-
Save schani/9377ad8c8bfa3ded92df 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
struct MySet<KeyType : Hashable> : Sequence, ArrayLiteralConvertible | |
{ | |
var dictionaryOfItems = Dictionary<KeyType,Bool>() | |
typealias GeneratorType = MapSequenceGenerator<Dictionary<KeyType, Bool>.GeneratorType, KeyType> | |
init() {} | |
init(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() -> GeneratorType { | |
return dictionaryOfItems.keys.generate() | |
} | |
static func convertFromArrayLiteral (elements: KeyType...) -> MySet<KeyType> { | |
return MySet (array: elements) | |
} | |
} | |
// the ArrayLiteralConvertible lets you do | |
let set : MySet<Int> = [1,2,3] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cool. A clear improvement. I don't understand the reason for "Map" in MapSequenceGenerator, or how it relates to the DictionaryGenerator.
A little worrying how reifying all these abstractions into types is quickly producing Java-esque names for things, like three car pile-ups of abstract nouns, instead of the sanity of Cocoa. We'll see how it goes..