Skip to content

Instantly share code, notes, and snippets.

@grosch
Created May 7, 2015 17:48
Show Gist options
  • Save grosch/0cf4952e976310b9e9c3 to your computer and use it in GitHub Desktop.
Save grosch/0cf4952e976310b9e9c3 to your computer and use it in GitHub Desktop.
Set<T> that passes by reference
// Set<T> is a struct in Swift, meaning it passes by value, not reference.
// I had to create this so I could pass a Set to another view controller
// and have it actually modify the Set on the original view controller as
// there was only a Back button, not an OK/Done button in the interface.
func ==<T : Hashable>(lhs: BoxedSet<T>, rhs: BoxedSet<T>) -> Bool {
return lhs.set == rhs.set
}
final class BoxedSet<T : Hashable> : Hashable, CollectionType, ArrayLiteralConvertible {
typealias Element = T
typealias Index = SetIndex<T>
typealias GeneratorType = SetGenerator<T>
var set = Set<T>()
init(arrayLiteral elements: Element...) {
set = Set<T>(elements)
}
init(_ foo: BoxedSet<T>) {
set = Set<T>(foo)
}
func generate() -> GeneratorType {
return set.generate()
}
var hashValue: Int {
get {
return set.hashValue
}
}
var startIndex: Index {
get {
return set.startIndex
}
}
var endIndex: Index {
get {
return set.endIndex
}
}
subscript (position: Index) -> Element {
get {
return set[position]
}
}
func contains(member: Element) -> Bool {
return set.contains(member)
}
func insert(member: Element) {
set.insert(member)
}
func remove(member: Element) -> Element? {
return set.remove(member)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment