/
set.cr
429 lines (391 loc) · 9.73 KB
/
set.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# `Set` implements a collection of unordered values with no duplicates.
#
# An `Enumerable` object can be converted to `Set` using the `#to_set` method.
#
# `Set` uses `Hash` as storage, so you must note the following points:
#
# * Equality of elements is determined according to `Object#==` and `Object#hash`.
# * `Set` assumes that the identity of each element does not change while it is stored. Modifying an element of a set will render the set to an unreliable state.
#
# ### Example
#
# ```
# s1 = Set{1, 2}
# s2 = [1, 2].to_set
# s3 = Set.new [1, 2]
# s1 == s2 # => true
# s1 == s3 # => true
# s1.add(2)
# s1.concat([6, 8])
# s1.subset? s2 # => false
# s2.subset? s1 # => true
# ```
struct Set(T)
include Enumerable(T)
include Iterable(T)
# Creates a new, empty `Set`.
#
# ```
# s = Set(Int32).new
# s.empty? # => true
# ```
#
# An initial capacity can be specified, and it will be set as the initial capacity
# of the internal `Hash`.
def initialize(initial_capacity = nil)
@hash = Hash(T, Nil).new(initial_capacity: initial_capacity)
end
# Optimized version of `new` used when *other* is also an `Indexable`
def self.new(other : Indexable(T))
Set(T).new(other.size).concat(other)
end
# Creates a new set from the elements in *enumerable*.
#
# ```
# a = [1, 3, 5]
# s = Set.new a
# s.empty? # => false
# ```
def self.new(enumerable : Enumerable(T))
Set(T).new.concat(enumerable)
end
# Alias for `add`
def <<(object : T)
add object
end
# Adds *object* to the set and returns `self`.
#
# ```
# s = Set{1, 5}
# s.includes? 8 # => false
# s << 8
# s.includes? 8 # => true
# ```
def add(object : T)
@hash[object] = nil
self
end
# Adds `#each` element of *elems* to the set and returns `self`.
#
# ```
# s = Set{1, 5}
# s.concat [5, 5, 8, 9]
# s.size # => 4
# ```
#
# See also: `#|` to merge two sets and return a new one.
def concat(elems)
elems.each { |elem| self << elem }
self
end
# Returns `true` if *object* exists in the set.
#
# ```
# s = Set{1, 5}
# s.includes? 5 # => true
# s.includes? 9 # => false
# ```
def includes?(object)
@hash.has_key?(object)
end
# Removes the *object* from the set and returns `self`.
#
# ```
# s = Set{1, 5}
# s.includes? 5 # => true
# s.delete 5
# s.includes? 5 # => false
# ```
def delete(object)
@hash.delete(object)
self
end
# Returns the number of elements in the set.
#
# ```
# s = Set{1, 5}
# s.size # => 2
# ```
def size
@hash.size
end
# Removes all elements in the set, and returns `self`.
#
# ```
# s = Set{1, 5}
# s.size # => 2
# s.clear
# s.size # => 0
# ```
def clear
@hash.clear
self
end
# Returns `true` if the set is empty.
#
# ```
# s = Set(Int32).new
# s.empty? # => true
# s << 3
# s.empty? # => false
# ```
def empty?
@hash.empty?
end
# Yields each element of the set, and returns `self`.
def each
@hash.each_key do |key|
yield key
end
end
# Returns an iterator for each element of the set.
def each
@hash.each_key
end
# Intersection: returns a new set containing elements common to both sets.
#
# ```
# Set{1, 1, 3, 5} & Set{1, 2, 3} # => Set{1, 3}
# Set{'a', 'b', 'b', 'z'} & Set{'a', 'b', 'c'} # => Set{'a', 'b'}
# ```
def &(other : Set)
smallest, largest = self, other
if largest.size < smallest.size
smallest, largest = largest, smallest
end
set = Set(T).new
smallest.each do |value|
set.add value if largest.includes?(value)
end
set
end
# Union: returns a new set containing all unique elements from both sets.
#
# ```
# Set{1, 1, 3, 5} | Set{1, 2, 3} # => Set{1, 3, 5, 2}
# Set{'a', 'b', 'b', 'z'} | Set{'a', 'b', 'c'} # => Set{'a', 'b', 'z', 'c'}
# ```
#
# See also: `#concat` to add elements from a set to `self`.
def |(other : Set(U)) forall U
set = Set(T | U).new(Math.max(size, other.size))
each { |value| set.add value }
other.each { |value| set.add value }
set
end
# Difference: returns a new set containing elements in this set that are not
# present in the other.
#
# ```
# Set{1, 2, 3, 4, 5} - Set{2, 4} # => Set{1, 3, 5}
# Set{'a', 'b', 'b', 'z'} - Set{'a', 'b', 'c'} # => Set{'z'}
# ```
def -(other : Set)
set = Set(T).new
each do |value|
set.add value unless other.includes?(value)
end
set
end
# Difference: returns a new set containing elements in this set that are not
# present in the other enumerable.
#
# ```
# Set{1, 2, 3, 4, 5} - [2, 4] # => Set{1, 3, 5}
# Set{'a', 'b', 'b', 'z'} - ['a', 'b', 'c'] # => Set{'z'}
# ```
def -(other : Enumerable)
dup.subtract other
end
# Symmetric Difference: returns a new set `(self - other) | (other - self)`.
# Equivalently, returns `(self | other) - (self & other)`.
#
# ```
# Set{1, 2, 3, 4, 5} ^ Set{2, 4, 6} # => Set{1, 3, 5, 6}
# Set{'a', 'b', 'b', 'z'} ^ Set{'a', 'b', 'c'} # => Set{'z', 'c'}
# ```
def ^(other : Set(U)) forall U
set = Set(T | U).new
each do |value|
set.add value unless other.includes?(value)
end
other.each do |value|
set.add value unless includes?(value)
end
set
end
# Symmetric Difference: returns a new set `(self - other) | (other - self)`.
# Equivalently, returns `(self | other) - (self & other)`.
#
# ```
# Set{1, 2, 3, 4, 5} ^ [2, 4, 6] # => Set{1, 3, 5, 6}
# Set{'a', 'b', 'b', 'z'} ^ ['a', 'b', 'c'] # => Set{'z', 'c'}
# ```
def ^(other : Enumerable(U)) forall U
set = Set(T | U).new(self)
other.each do |value|
if includes?(value)
set.delete value
else
set.add value
end
end
set
end
# Returns `self` after removing from it those elements that are present in
# the given enumerable.
#
# ```
# Set{'a', 'b', 'b', 'z'}.subtract Set{'a', 'b', 'c'} # => Set{'z'}
# Set{1, 2, 3, 4, 5}.subtract [2, 4, 6] # => Set{1, 3, 5}
# ```
def subtract(other : Enumerable)
other.each do |value|
delete value
end
self
end
# Returns `true` if both sets have the same elements.
#
# ```
# Set{1, 5} == Set{1, 5} # => true
# ```
def ==(other : Set)
same?(other) || @hash == other.@hash
end
# Same as `#includes?`.
#
# It is for convenience with using on `case` statement.
#
# ```
# red_like = Set{"red", "pink", "violet"}
# blue_like = Set{"blue", "azure", "violet"}
#
# case "violet"
# when red_like & blue_like
# puts "red & blue like color!"
# when red_like
# puts "red like color!"
# when blue_like
# puts "blue like color!"
# end
# ```
#
# See also: `Object#===`.
def ===(object : T)
includes? object
end
# Returns a new `Set` with all of the same elements.
def dup
Set.new(self)
end
# Returns a new `Set` with all of the elements cloned.
def clone
clone = Set(T).new(self.size)
each do |element|
clone << element.clone
end
clone
end
# Returns the elements as an `Array`.
#
# ```
# Set{1, 5}.to_a # => [1,5]
# ```
def to_a
@hash.keys
end
# Alias of `#to_s`.
def inspect(io)
to_s(io)
end
def pretty_print(pp) : Nil
pp.list("Set{", self, "}")
end
# See `Object#hash(hasher)`
def_hash @hash
# Returns `true` if the set and the given set have at least one element in
# common.
#
# ```
# Set{1, 2, 3}.intersects? Set{4, 5} # => false
# Set{1, 2, 3}.intersects? Set{3, 4} # => true
# ```
def intersects?(other : Set)
if size < other.size
any? { |o| other.includes?(o) }
else
other.any? { |o| includes?(o) }
end
end
# Writes a string representation of the set to *io*.
def to_s(io)
io << "Set{"
join ", ", io, &.inspect(io)
io << '}'
end
# Returns `true` if the set is a subset of the *other* set.
#
# This set must have the same or fewer elements than the *other* set, and all
# of elements in this set must be present in the *other* set.
#
# ```
# Set{1, 5}.subset? Set{1, 3, 5} # => true
# Set{1, 3, 5}.subset? Set{1, 3, 5} # => true
# ```
def subset?(other : Set)
return false if other.size < size
all? { |value| other.includes?(value) }
end
# Returns `true` if the set is a proper subset of the *other* set.
#
# This set must have fewer elements than the *other* set, and all
# of elements in this set must be present in the *other* set.
#
# ```
# Set{1, 5}.proper_subset? Set{1, 3, 5} # => true
# Set{1, 3, 5}.proper_subset? Set{1, 3, 5} # => false
# ```
def proper_subset?(other : Set)
return false if other.size <= size
all? { |value| other.includes?(value) }
end
# Returns `true` if the set is a superset of the *other* set.
#
# The *other* must have the same or fewer elements than this set, and all of
# elements in the *other* set must be present in this set.
#
# ```
# Set{1, 3, 5}.superset? Set{1, 5} # => true
# Set{1, 3, 5}.superset? Set{1, 3, 5} # => true
# ```
def superset?(other : Set)
other.subset?(self)
end
# Returns `true` if the set is a superset of the *other* set.
#
# The *other* must have the same or fewer elements than this set, and all of
# elements in the *other* set must be present in this set.
#
# ```
# Set{1, 3, 5}.proper_superset? Set{1, 5} # => true
# Set{1, 3, 5}.proper_superset? Set{1, 3, 5} # => false
# ```
def proper_superset?(other : Set)
other.proper_subset?(self)
end
# :nodoc:
def object_id
@hash.object_id
end
# :nodoc:
def same?(other : Set)
@hash.same?(other.@hash)
end
end
module Enumerable
# Returns a new `Set` with each unique element in the enumerable.
def to_set
Set.new(self)
end
end