Skip to content

Instantly share code, notes, and snippets.

@schani
Forked from algal/SwiftSet.swift
Last active August 13, 2018 05:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schani/9377ad8c8bfa3ded92df to your computer and use it in GitHub Desktop.
Save schani/9377ad8c8bfa3ded92df to your computer and use it in GitHub Desktop.
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]
@algal
Copy link

algal commented Jun 11, 2014

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..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment