Class: Set

Inherits:
Object show all
Includes:
Enumerable
Defined in:
opal/opal/corelib/set.rb

Overview

helpers: freeze Portions Copyright (c) 2002-2013 Akinori MUSHA [email protected]

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.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'opal/opal/corelib/set.rb', line 10

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

.[](*ary) ⇒ Object



6
7
8
# File 'opal/opal/corelib/set.rb', line 6

def self.[](*ary)
  new(ary)
end

Instance Method Details

#-(enum) ⇒ Object Also known as: difference



28
29
30
31
32
33
34
# File 'opal/opal/corelib/set.rb', line 28

def -(enum)
  unless enum.respond_to? :each
    ::Kernel.raise ::ArgumentError, 'value must be enumerable'
  end

  dup.subtract(enum)
end

#==(other) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'opal/opal/corelib/set.rb', line 40

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: <<



52
53
54
55
# File 'opal/opal/corelib/set.rb', line 52

def add(o)
  @hash[o] = true
  self
end

#add?(o) ⇒ Boolean

Returns:



134
135
136
137
138
139
140
# File 'opal/opal/corelib/set.rb', line 134

def add?(o)
  if include?(o)
    nil
  else
    add(o)
  end
end

#classify(&block) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'opal/opal/corelib/set.rb', line 57

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

#clearObject



156
157
158
159
# File 'opal/opal/corelib/set.rb', line 156

def clear
  @hash.clear
  self
end

#collect!(&block) ⇒ Object Also known as: map!



67
68
69
70
71
72
# File 'opal/opal/corelib/set.rb', line 67

def collect!(&block)
  return enum_for(:collect!) unless block_given?
  result = self.class.new
  each { |item| result << yield(item) }
  replace result
end

#compare_by_identityObject



74
75
76
77
78
79
80
81
# File 'opal/opal/corelib/set.rb', line 74

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

Returns:



83
84
85
# File 'opal/opal/corelib/set.rb', line 83

def compare_by_identity?
  @hash.respond_to?(:compare_by_identity?) && @hash.compare_by_identity?
end

#delete(o) ⇒ Object



87
88
89
90
# File 'opal/opal/corelib/set.rb', line 87

def delete(o)
  @hash.delete(o)
  self
end

#delete?(o) ⇒ Boolean

Returns:



92
93
94
95
96
97
# File 'opal/opal/corelib/set.rb', line 92

def delete?(o)
  if include?(o)
    delete(o)
    self
  end
end

#delete_ifObject



99
100
101
102
103
104
105
# File 'opal/opal/corelib/set.rb', line 99

def delete_if
  return enum_for(:delete_if) unless block_given?
  # @hash.delete_if should be faster, but using it breaks the order
  # of enumeration in subclasses.
  select { |o| yield o }.each { |o| @hash.delete(o) }
  self
end

#disjoint?(set) ⇒ Boolean

Returns:



232
233
234
# File 'opal/opal/corelib/set.rb', line 232

def disjoint?(set)
  !intersect?(set)
end

#dupObject



23
24
25
26
# File 'opal/opal/corelib/set.rb', line 23

def dup
  result = self.class.new
  result.merge(self)
end

#each(&block) ⇒ Object



142
143
144
145
146
# File 'opal/opal/corelib/set.rb', line 142

def each(&block)
  return enum_for(:each) unless block_given?
  @hash.each_key(&block)
  self
end

#empty?Boolean

Returns:



148
149
150
# File 'opal/opal/corelib/set.rb', line 148

def empty?
  @hash.empty?
end

#eql?(other) ⇒ Boolean

Returns:



152
153
154
# File 'opal/opal/corelib/set.rb', line 152

def eql?(other)
  @hash.eql?(other.instance_eval { @hash })
end

#freezeObject



107
108
109
110
111
112
# File 'opal/opal/corelib/set.rb', line 107

def freeze
  return self if frozen?

  @hash.freeze
  `$freeze(self)`
end

#include?(o) ⇒ Boolean Also known as: member?

Returns:



161
162
163
# File 'opal/opal/corelib/set.rb', line 161

def include?(o)
  @hash.include?(o)
end

#inspectObject



36
37
38
# File 'opal/opal/corelib/set.rb', line 36

def inspect
  "#<Set: {#{to_a.join(',')}}>"
end

#intersect?(set) ⇒ Boolean

Returns:



223
224
225
226
227
228
229
230
# File 'opal/opal/corelib/set.rb', line 223

def intersect?(set)
  `is_set(set)`
  if size < set.size
    any? { |o| set.include?(o) }
  else
    set.any? { |o| include?(o) }
  end
end

#keep_ifObject



114
115
116
117
118
# File 'opal/opal/corelib/set.rb', line 114

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



165
166
167
168
# File 'opal/opal/corelib/set.rb', line 165

def merge(enum)
  enum.each { |item| add item }
  self
end

#proper_subset?(set) ⇒ Boolean Also known as: <

Returns:



217
218
219
220
221
# File 'opal/opal/corelib/set.rb', line 217

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: >

Returns:



205
206
207
208
209
# File 'opal/opal/corelib/set.rb', line 205

def proper_superset?(set)
  `is_set(set)`
  return false if size <= set.size
  set.all? { |o| include?(o) }
end

#reject!(&block) ⇒ Object



120
121
122
123
124
125
# File 'opal/opal/corelib/set.rb', line 120

def reject!(&block)
  return enum_for(:reject!) unless block_given?
  before = size
  delete_if(&block)
  size == before ? nil : self
end

#replace(enum) ⇒ Object



170
171
172
173
174
175
# File 'opal/opal/corelib/set.rb', line 170

def replace(enum)
  clear
  merge(enum)

  self
end

#select!(&block) ⇒ Object Also known as: filter!



127
128
129
130
131
132
# File 'opal/opal/corelib/set.rb', line 127

def select!(&block)
  return enum_for(:select!) unless block_given?
  before = size
  keep_if(&block)
  size == before ? nil : self
end

#sizeObject Also known as: length



177
178
179
# File 'opal/opal/corelib/set.rb', line 177

def size
  @hash.size
end

#subset?(set) ⇒ Boolean Also known as: <=

Returns:



211
212
213
214
215
# File 'opal/opal/corelib/set.rb', line 211

def subset?(set)
  `is_set(set)`
  return false if set.size < size
  all? { |o| set.include?(o) }
end

#subtract(enum) ⇒ Object



181
182
183
184
# File 'opal/opal/corelib/set.rb', line 181

def subtract(enum)
  enum.each { |item| delete item }
  self
end

#superset?(set) ⇒ Boolean Also known as: >=

Returns:



199
200
201
202
203
# File 'opal/opal/corelib/set.rb', line 199

def superset?(set)
  `is_set(set)`
  return false if size < set.size
  set.all? { |o| include?(o) }
end

#to_aObject



236
237
238
# File 'opal/opal/corelib/set.rb', line 236

def to_a
  @hash.keys
end

#|(enum) ⇒ Object Also known as: +, union



186
187
188
189
190
191
# File 'opal/opal/corelib/set.rb', line 186

def |(enum)
  unless enum.respond_to? :each
    ::Kernel.raise ::ArgumentError, 'value must be enumerable'
  end
  dup.merge(enum)
end