30
30
Insert: O(1) O(n)
31
31
Delete: O(1) O(n)
32
32
*/
33
- public struct HashTable < Key: Hashable , Value> : CustomStringConvertible {
33
+ public struct HashTable < Key: Hashable , Value> {
34
34
private typealias Element = ( key: Key , value: Value )
35
35
private typealias Bucket = [ Element ]
36
36
private var buckets : [ Bucket ]
@@ -41,23 +41,6 @@ public struct HashTable<Key: Hashable, Value>: CustomStringConvertible {
41
41
/// A Boolean value that indicates whether the hash table is empty.
42
42
public var isEmpty : Bool { return count == 0 }
43
43
44
- /// A string that represents the contents of the hash table.
45
- public var description : String {
46
- let pairs = buckets. flatMap { b in b. map { e in " \( e. key) = \( e. value) " } }
47
- return pairs. joined ( separator: " , " )
48
- }
49
-
50
- /// A string that represents the contents of
51
- /// the hash table, suitable for debugging.
52
- public var debugDescription : String {
53
- var str = " "
54
- for (i, bucket) in buckets. enumerated ( ) {
55
- let pairs = bucket. map { e in " \( e. key) = \( e. value) " }
56
- str += " bucket \( i) : " + pairs. joined ( separator: " , " ) + " \n "
57
- }
58
- return str
59
- }
60
-
61
44
/**
62
45
Create a hash table with the given capacity.
63
46
*/
@@ -151,3 +134,22 @@ public struct HashTable<Key: Hashable, Value>: CustomStringConvertible {
151
134
return abs ( key. hashValue) % buckets. count
152
135
}
153
136
}
137
+
138
+ extension HashTable : CustomStringConvertible {
139
+ /// A string that represents the contents of the hash table.
140
+ public var description : String {
141
+ let pairs = buckets. flatMap { b in b. map { e in " \( e. key) = \( e. value) " } }
142
+ return pairs. joined ( separator: " , " )
143
+ }
144
+
145
+ /// A string that represents the contents of
146
+ /// the hash table, suitable for debugging.
147
+ public var debugDescription : String {
148
+ var str = " "
149
+ for (i, bucket) in buckets. enumerated ( ) {
150
+ let pairs = bucket. map { e in " \( e. key) = \( e. value) " }
151
+ str += " bucket \( i) : " + pairs. joined ( separator: " , " ) + " \n "
152
+ }
153
+ return str
154
+ }
155
+ }
0 commit comments