@@ -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,7 +138,7 @@ 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 }
@@ -141,7 +151,7 @@ public struct Set<A : Hashable> {
141151 if !contains( item) {
142152 return self
143153 } else {
144- return Set ( array: array . filter { $0. hashValue != item. hashValue } )
154+ return Set ( array: toArray . filter { $0. hashValue != item. hashValue } )
145155 }
146156 }
147157
@@ -183,22 +193,12 @@ public struct Set<A : Hashable> {
183193
184194 /// Applies a binary function to reduce the elements of the receiver to a single value.
185195 public func reduce< B> ( f : B -> A -> B , initial : B ) -> B {
186- return array . reduce ( initial, combine: uncurry ( f) )
196+ return toArray . reduce ( initial, combine: uncurry ( f) )
187197 }
188198
189199 /// Applies a binary operator to reduce the elements of the receiver to a single value.
190200 public func reduce< B> ( f : ( B , A ) -> B , initial : B ) -> B {
191- return array. reduce ( initial, combine: f)
192- }
193-
194- /// Returns all elements of the receiver in a List in no particular order.
195- public func toList( ) -> List < A > {
196- return self . reduce ( flip ( List . cons) , initial: List ( ) )
197- }
198-
199- /// Returns all elements of the receiver in an Array in no particular order.
200- public func toArray( ) -> Array < A > {
201- return self . reduce ( flip ( cons) , initial: [ ] )
201+ return toArray. reduce ( initial, combine: f)
202202 }
203203}
204204
@@ -212,7 +212,7 @@ extension Set : ArrayLiteralConvertible {
212212
213213extension Set : SequenceType {
214214 public func generate( ) -> SetGenerator < A > {
215- let items = self . array
215+ let items = self . toArray
216216 return SetGenerator ( items: items [ 0 ..< items. count] )
217217 }
218218}
@@ -232,11 +232,11 @@ public struct SetGenerator<A> : GeneratorType {
232232
233233extension Set : Printable , DebugPrintable {
234234 public var description : String {
235- return " \( self . array ) "
235+ return " \( self . toArray ) "
236236 }
237237
238238 public var debugDescription : String {
239- return " \( self . array ) "
239+ return " \( self . toArray ) "
240240 }
241241}
242242
0 commit comments