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.
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'opal/opal/corelib/set.rb', line 12
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
8
9
10
|
# File 'opal/opal/corelib/set.rb', line 8
def self.[](*ary)
new(ary)
end
|
Instance Method Details
#-(enum) ⇒ Object
Also known as:
difference
30
31
32
33
34
35
36
|
# File 'opal/opal/corelib/set.rb', line 30
def -(enum)
unless enum.respond_to? :each
::Kernel.raise ::ArgumentError, 'value must be enumerable'
end
dup.subtract(enum)
end
|
#==(other) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
|
# File 'opal/opal/corelib/set.rb', line 42
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:
<<
54
55
56
57
|
# File 'opal/opal/corelib/set.rb', line 54
def add(o)
@hash[o] = true
self
end
|
136
137
138
139
140
141
142
|
# File 'opal/opal/corelib/set.rb', line 136
def add?(o)
if include?(o)
nil
else
add(o)
end
end
|
#classify(&block) ⇒ Object
59
60
61
62
63
64
65
66
67
|
# File 'opal/opal/corelib/set.rb', line 59
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
|
158
159
160
161
|
# File 'opal/opal/corelib/set.rb', line 158
def clear
@hash.clear
self
end
|
#collect!(&block) ⇒ Object
Also known as:
map!
69
70
71
72
73
74
|
# File 'opal/opal/corelib/set.rb', line 69
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
76
77
78
79
80
81
82
83
|
# File 'opal/opal/corelib/set.rb', line 76
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
85
86
87
|
# File 'opal/opal/corelib/set.rb', line 85
def compare_by_identity?
@hash.respond_to?(:compare_by_identity?) && @hash.compare_by_identity?
end
|
#delete(o) ⇒ Object
89
90
91
92
|
# File 'opal/opal/corelib/set.rb', line 89
def delete(o)
@hash.delete(o)
self
end
|
94
95
96
97
98
99
|
# File 'opal/opal/corelib/set.rb', line 94
def delete?(o)
if include?(o)
delete(o)
self
end
end
|
#delete_if ⇒ Object
101
102
103
104
105
106
107
|
# File 'opal/opal/corelib/set.rb', line 101
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
234
235
236
|
# File 'opal/opal/corelib/set.rb', line 234
def disjoint?(set)
!intersect?(set)
end
|
25
26
27
28
|
# File 'opal/opal/corelib/set.rb', line 25
def dup
result = self.class.new
result.merge(self)
end
|
#each(&block) ⇒ Object
144
145
146
147
148
|
# File 'opal/opal/corelib/set.rb', line 144
def each(&block)
return enum_for(:each) unless block_given?
@hash.each_key(&block)
self
end
|
150
151
152
|
# File 'opal/opal/corelib/set.rb', line 150
def empty?
@hash.empty?
end
|
#eql?(other) ⇒ Boolean
154
155
156
|
# File 'opal/opal/corelib/set.rb', line 154
def eql?(other)
@hash.eql?(other.instance_eval { @hash })
end
|
109
110
111
112
113
114
|
# File 'opal/opal/corelib/set.rb', line 109
def freeze
return self if frozen?
@hash.freeze
`$freeze(self)`
end
|
#include?(o) ⇒ Boolean
Also known as:
member?
163
164
165
|
# File 'opal/opal/corelib/set.rb', line 163
def include?(o)
@hash.include?(o)
end
|
38
39
40
|
# File 'opal/opal/corelib/set.rb', line 38
def inspect
"#<Set: {#{to_a.join(',')}}>"
end
|
#intersect?(set) ⇒ Boolean
225
226
227
228
229
230
231
232
|
# File 'opal/opal/corelib/set.rb', line 225
def intersect?(set)
`is_set(set)`
if size < set.size
any? { |o| set.include?(o) }
else
set.any? { |o| include?(o) }
end
end
|
116
117
118
119
120
|
# File 'opal/opal/corelib/set.rb', line 116
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
167
168
169
170
|
# File 'opal/opal/corelib/set.rb', line 167
def merge(enum)
enum.each { |item| add item }
self
end
|
#proper_subset?(set) ⇒ Boolean
Also known as:
<
219
220
221
222
223
|
# File 'opal/opal/corelib/set.rb', line 219
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:
>
207
208
209
210
211
|
# File 'opal/opal/corelib/set.rb', line 207
def proper_superset?(set)
`is_set(set)`
return false if size <= set.size
set.all? { |o| include?(o) }
end
|
#reject!(&block) ⇒ Object
122
123
124
125
126
127
|
# File 'opal/opal/corelib/set.rb', line 122
def reject!(&block)
return enum_for(:reject!) unless block_given?
before = size
delete_if(&block)
size == before ? nil : self
end
|
#replace(enum) ⇒ Object
172
173
174
175
176
177
|
# File 'opal/opal/corelib/set.rb', line 172
def replace(enum)
clear
merge(enum)
self
end
|
#select!(&block) ⇒ Object
Also known as:
filter!
129
130
131
132
133
134
|
# File 'opal/opal/corelib/set.rb', line 129
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
179
180
181
|
# File 'opal/opal/corelib/set.rb', line 179
def size
@hash.size
end
|
#subset?(set) ⇒ Boolean
Also known as:
<=
213
214
215
216
217
|
# File 'opal/opal/corelib/set.rb', line 213
def subset?(set)
`is_set(set)`
return false if set.size < size
all? { |o| set.include?(o) }
end
|
#subtract(enum) ⇒ Object
183
184
185
186
|
# File 'opal/opal/corelib/set.rb', line 183
def subtract(enum)
enum.each { |item| delete item }
self
end
|
#superset?(set) ⇒ Boolean
Also known as:
>=
201
202
203
204
205
|
# File 'opal/opal/corelib/set.rb', line 201
def superset?(set)
`is_set(set)`
return false if size < set.size
set.all? { |o| include?(o) }
end
|
238
239
240
|
# File 'opal/opal/corelib/set.rb', line 238
def to_a
@hash.keys
end
|
#|(enum) ⇒ Object
Also known as:
+, union
188
189
190
191
192
193
|
# File 'opal/opal/corelib/set.rb', line 188
def |(enum)
unless enum.respond_to? :each
::Kernel.raise ::ArgumentError, 'value must be enumerable'
end
dup.merge(enum)
end
|