Class: Set
Overview
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Enumerable
#all?, #any?, #chunk, #chunk_while, #collect, #collect_concat, #compact, #count, #cycle, #detect, #drop, #drop_while, #each_cons, #each_entry, #each_slice, #each_with_index, #each_with_object, #entries, #enumerator_size, #filter_map, #find_all, #find_index, #first, #grep, #grep_v, #group_by, #inject, #lazy, #max, #max_by, #min, #min_by, #minmax, #minmax_by, #none?, #one?, #partition, #reject, #reverse_each, #slice_after, #slice_before, #slice_when, #sort, #sort_by, #sum, #take, #take_while, #tally, #to_h, #to_set, #uniq, #zip
Constructor Details
#initialize(enum = nil, &block) ⇒ Set
Returns a new instance of Set.
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'opal/opal/corelib/set.rb', line 13
def initialize(enum = nil, &block)
@hash = {}
return if enum.nil?
::Kernel.raise ::ArgumentError, 'value must be enumerable' unless ::Enumerable === enum
if block
enum.each { |item| add yield(item) }
else
merge(enum)
end
end
|
Class Method Details
9
10
11
|
# File 'opal/opal/corelib/set.rb', line 9
def self.[](*ary)
new(ary)
end
|
Instance Method Details
#-(enum) ⇒ Object
Also known as:
difference
31
32
33
34
35
36
37
|
# File 'opal/opal/corelib/set.rb', line 31
def -(enum)
unless enum.respond_to? :each
::Kernel.raise ::ArgumentError, 'value must be enumerable'
end
dup.subtract(enum)
end
|
#==(other) ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
|
# File 'opal/opal/corelib/set.rb', line 43
def ==(other)
if equal?(other)
true
elsif other.instance_of?(self.class)
@hash == other.instance_variable_get(:@hash)
elsif other.is_a?(::Set) && size == other.size
other.all? { |o| @hash.include?(o) }
else
false
end
end
|
#add(o) ⇒ Object
Also known as:
<<
55
56
57
58
|
# File 'opal/opal/corelib/set.rb', line 55
def add(o)
@hash[o] = true
self
end
|
137
138
139
140
141
142
143
|
# File 'opal/opal/corelib/set.rb', line 137
def add?(o)
if include?(o)
nil
else
add(o)
end
end
|
#classify(&block) ⇒ Object
60
61
62
63
64
65
66
67
68
|
# File 'opal/opal/corelib/set.rb', line 60
def classify(&block)
return enum_for(:classify) unless block_given?
result = ::Hash.new { |h, k| h[k] = self.class.new }
each { |item| result[yield(item)].add item }
result
end
|
159
160
161
162
|
# File 'opal/opal/corelib/set.rb', line 159
def clear
@hash.clear
self
end
|
#collect!(&block) ⇒ Object
Also known as:
map!
70
71
72
73
74
75
|
# File 'opal/opal/corelib/set.rb', line 70
def collect!(&block)
return enum_for(:collect!) unless block_given?
result = self.class.new
each { |item| result << yield(item) }
replace result
end
|
#compare_by_identity ⇒ Object
77
78
79
80
81
82
83
84
|
# File 'opal/opal/corelib/set.rb', line 77
def compare_by_identity
if @hash.respond_to?(:compare_by_identity)
@hash.compare_by_identity
self
else
raise NotImplementedError, "#{self.class.name}\##{__method__} is not implemented"
end
end
|
#compare_by_identity? ⇒ Boolean
86
87
88
|
# File 'opal/opal/corelib/set.rb', line 86
def compare_by_identity?
@hash.respond_to?(:compare_by_identity?) && @hash.compare_by_identity?
end
|
#delete(o) ⇒ Object
90
91
92
93
|
# File 'opal/opal/corelib/set.rb', line 90
def delete(o)
@hash.delete(o)
self
end
|
95
96
97
98
99
100
|
# File 'opal/opal/corelib/set.rb', line 95
def delete?(o)
if include?(o)
delete(o)
self
end
end
|
#delete_if ⇒ Object
102
103
104
105
106
107
108
|
# File 'opal/opal/corelib/set.rb', line 102
def delete_if
return enum_for(:delete_if) unless block_given?
select { |o| yield o }.each { |o| @hash.delete(o) }
self
end
|
#disjoint?(set) ⇒ Boolean
235
236
237
|
# File 'opal/opal/corelib/set.rb', line 235
def disjoint?(set)
!intersect?(set)
end
|
26
27
28
29
|
# File 'opal/opal/corelib/set.rb', line 26
def dup
result = self.class.new
result.merge(self)
end
|
#each(&block) ⇒ Object
145
146
147
148
149
|
# File 'opal/opal/corelib/set.rb', line 145
def each(&block)
return enum_for(:each) unless block_given?
@hash.each_key(&block)
self
end
|
151
152
153
|
# File 'opal/opal/corelib/set.rb', line 151
def empty?
@hash.empty?
end
|
#eql?(other) ⇒ Boolean
155
156
157
|
# File 'opal/opal/corelib/set.rb', line 155
def eql?(other)
@hash.eql?(other.instance_eval { @hash })
end
|
110
111
112
113
114
115
|
# File 'opal/opal/corelib/set.rb', line 110
def freeze
return self if frozen?
@hash.freeze
`$freeze(self)`
end
|
#include?(o) ⇒ Boolean
Also known as:
member?
164
165
166
|
# File 'opal/opal/corelib/set.rb', line 164
def include?(o)
@hash.include?(o)
end
|
39
40
41
|
# File 'opal/opal/corelib/set.rb', line 39
def inspect
"#<Set: {#{to_a.join(',')}}>"
end
|
#intersect?(set) ⇒ Boolean
226
227
228
229
230
231
232
233
|
# File 'opal/opal/corelib/set.rb', line 226
def intersect?(set)
`is_set(set)`
if size < set.size
any? { |o| set.include?(o) }
else
set.any? { |o| include?(o) }
end
end
|
117
118
119
120
121
|
# File 'opal/opal/corelib/set.rb', line 117
def keep_if
return enum_for(:keep_if) unless block_given?
reject { |o| yield o }.each { |o| @hash.delete(o) }
self
end
|
#merge(enum) ⇒ Object
168
169
170
171
|
# File 'opal/opal/corelib/set.rb', line 168
def merge(enum)
enum.each { |item| add item }
self
end
|
#proper_subset?(set) ⇒ Boolean
Also known as:
<
220
221
222
223
224
|
# File 'opal/opal/corelib/set.rb', line 220
def proper_subset?(set)
`is_set(set)`
return false if set.size <= size
all? { |o| set.include?(o) }
end
|
#proper_superset?(set) ⇒ Boolean
Also known as:
>
208
209
210
211
212
|
# File 'opal/opal/corelib/set.rb', line 208
def proper_superset?(set)
`is_set(set)`
return false if size <= set.size
set.all? { |o| include?(o) }
end
|
#reject!(&block) ⇒ Object
123
124
125
126
127
128
|
# File 'opal/opal/corelib/set.rb', line 123
def reject!(&block)
return enum_for(:reject!) unless block_given?
before = size
delete_if(&block)
size == before ? nil : self
end
|
#replace(enum) ⇒ Object
173
174
175
176
177
178
|
# File 'opal/opal/corelib/set.rb', line 173
def replace(enum)
clear
merge(enum)
self
end
|
#select!(&block) ⇒ Object
Also known as:
filter!
130
131
132
133
134
135
|
# File 'opal/opal/corelib/set.rb', line 130
def select!(&block)
return enum_for(:select!) unless block_given?
before = size
keep_if(&block)
size == before ? nil : self
end
|
#size ⇒ Object
Also known as:
length
180
181
182
|
# File 'opal/opal/corelib/set.rb', line 180
def size
@hash.size
end
|
#subset?(set) ⇒ Boolean
Also known as:
<=
214
215
216
217
218
|
# File 'opal/opal/corelib/set.rb', line 214
def subset?(set)
`is_set(set)`
return false if set.size < size
all? { |o| set.include?(o) }
end
|
#subtract(enum) ⇒ Object
184
185
186
187
|
# File 'opal/opal/corelib/set.rb', line 184
def subtract(enum)
enum.each { |item| delete item }
self
end
|
#superset?(set) ⇒ Boolean
Also known as:
>=
202
203
204
205
206
|
# File 'opal/opal/corelib/set.rb', line 202
def superset?(set)
`is_set(set)`
return false if size < set.size
set.all? { |o| include?(o) }
end
|
239
240
241
|
# File 'opal/opal/corelib/set.rb', line 239
def to_a
@hash.keys
end
|
#|(enum) ⇒ Object
Also known as:
+, union
189
190
191
192
193
194
|
# File 'opal/opal/corelib/set.rb', line 189
def |(enum)
unless enum.respond_to? :each
::Kernel.raise ::ArgumentError, 'value must be enumerable'
end
dup.merge(enum)
end
|