@@ -11,16 +11,26 @@ import Darwin
1111/// An immutable unordered sequence of distinct values. Values are checked for uniqueness using
1212/// their hashes.
1313public struct Set < A : Hashable > {
14- let bucket : Dictionary < A , Bool > = Dictionary ( )
14+ private let bucket : Dictionary < A , Bool > = Dictionary ( )
1515
16- var array : [ A ] {
16+ /// Returns all elements of the receiver in an Array in no particular order.
17+ public var toArray : [ A ] {
1718 var arr = [ A] ( )
1819 for (key, _) in bucket {
1920 arr. append ( key)
2021 }
2122 return arr
2223 }
2324
25+ /// Returns all elements of the receiver in a List in no particular order.
26+ public var toList : List < A > {
27+ var list : List < A > = [ ]
28+ for (key, _) in bucket {
29+ list = List ( key, list)
30+ }
31+ return list
32+ }
33+
2434 public var count : Int {
2535 return bucket. count
2636 }
@@ -42,7 +52,7 @@ public struct Set<A : Hashable> {
4252 ///
4353 /// If the receiver has no values this function will return nil.
4454 public func any( ) -> A ? {
45- let ar = self . array
55+ let ar = self . toArray
4656 if ar. isEmpty {
4757 return nil
4858 } else {
@@ -116,8 +126,8 @@ public struct Set<A : Hashable> {
116126
117127 /// Computes and returns the union of the reicever and a given set.
118128 public func union( set : Set < A > ) -> Set < A > {
119- var current = self . array
120- current += set. array
129+ var current = self . toArray
130+ current += set. toArray
121131 return Set ( array: current)
122132 }
123133
@@ -128,22 +138,48 @@ public struct Set<A : Hashable> {
128138 if contains ( item) {
129139 return self
130140 } else {
131- var arr = array
141+ var arr = toArray
132142 arr. append ( item)
133143 return Set ( array: arr)
134144 }
135145 }
136146
147+ /// Removes an item from the set.
148+ ///
149+ /// If the item is not a member the receiver is returned unaltered.
150+ public func remove( item : A ) -> Set < A > {
151+ if !contains( item) {
152+ return self
153+ } else {
154+ return Set ( array: toArray. filter { $0. hashValue != item. hashValue } )
155+ }
156+ }
157+
137158 /// Returns the set of elements in the receiver that pass a given predicate.
138- public func filter( f : A -> Bool ) -> Set < A > {
159+ public func filter( p : A -> Bool ) -> Set < A > {
139160 var array = [ A] ( )
140161 for x in self {
141- if f ( x) {
162+ if p ( x) {
142163 array. append ( x)
143164 }
144165 }
145166 return Set ( array: array)
146167 }
168+
169+ /// Partition the set into two sets, one with all elements that satisfy the predicate and one
170+ /// with all elements that don't satisfy the predicate.
171+ public func partition( p : A -> Bool ) -> ( Set < A > , Set < A > ) {
172+ var satis = [ A] ( )
173+ var non = [ A] ( )
174+ for x in self {
175+ if p ( x) {
176+ satis. append ( x)
177+ } else {
178+ non. append ( x)
179+ }
180+ }
181+ return ( Set ( array: satis) , Set ( array: non) )
182+ }
147183
148184 /// Maps a function over the elements of the receiver and aggregates the result in a new set.
149185 public func map< B> ( f : A -> B ) -> Set < B > {
@@ -154,6 +190,16 @@ public struct Set<A : Hashable> {
154190
155191 return Set < B > ( array: array)
156192 }
193+
194+ /// Applies a binary function to reduce the elements of the receiver to a single value.
195+ public func reduce< B> ( f : B -> A -> B , initial : B ) -> B {
196+ return toArray. reduce ( initial, combine: uncurry ( f) )
197+ }
198+
199+ /// Applies a binary operator to reduce the elements of the receiver to a single value.
200+ public func reduce< B> ( f : ( B , A ) -> B , initial : B ) -> B {
201+ return toArray. reduce ( initial, combine: f)
202+ }
157203}
158204
159205extension Set : ArrayLiteralConvertible {
@@ -166,7 +212,7 @@ extension Set : ArrayLiteralConvertible {
166212
167213extension Set : SequenceType {
168214 public func generate( ) -> SetGenerator < A > {
169- let items = self . array
215+ let items = self . toArray
170216 return SetGenerator ( items: items [ 0 ..< items. count] )
171217 }
172218}
@@ -186,11 +232,11 @@ public struct SetGenerator<A> : GeneratorType {
186232
187233extension Set : Printable , DebugPrintable {
188234 public var description : String {
189- return " \( self . array ) "
235+ return " \( self . toArray ) "
190236 }
191237
192238 public var debugDescription : String {
193- return " \( self . array ) "
239+ return " \( self . toArray ) "
194240 }
195241}
196242
0 commit comments