Class: Set

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

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#to_json, #to_set

Constructor Details

#initialize(enum = nil, &block) ⇒ Set

Returns a new instance of Set

Raises:

  • (ArgumentError)


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

def initialize(enum = nil, &block)
  @hash = Hash.new

  return if enum.nil?
  raise ArgumentError, 'value must be enumerable' unless Enumerable === enum

  if block
    enum.each { |item| add block.call(item) }
  else
    merge(enum)
  end
end

Class Method Details

.[](*ary) ⇒ Object



5
6
7
# File 'opal/stdlib/set.rb', line 5

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

Instance Method Details

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



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

def -(enum)
  unless enum.respond_to? :each
    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/stdlib/set.rb', line 40

def ==(other)
  if self.equal?(other)
    true
  elsif other.instance_of?(self.class)
    @hash == other.instance_variable_get(:@hash)
  elsif other.is_a?(Set) && self.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/stdlib/set.rb', line 52

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

#add?(o) ⇒ Boolean

Returns:



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

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

#classify(&block) ⇒ Object



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

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



120
121
122
123
# File 'opal/stdlib/set.rb', line 120

def clear
  @hash.clear
  self
end

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



68
69
70
71
72
73
# File 'opal/stdlib/set.rb', line 68

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

#delete(o) ⇒ Object



76
77
78
79
# File 'opal/stdlib/set.rb', line 76

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

#delete?(o) ⇒ Boolean

Returns:



81
82
83
84
85
86
87
88
# File 'opal/stdlib/set.rb', line 81

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

#delete_ifObject



90
91
92
93
94
95
96
# File 'opal/stdlib/set.rb', line 90

def delete_if
  block_given? or return enum_for(__method__)
  # @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

#dupObject



22
23
24
25
# File 'opal/stdlib/set.rb', line 22

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

#each(&block) ⇒ Object



106
107
108
109
110
# File 'opal/stdlib/set.rb', line 106

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

#empty?Boolean

Returns:



112
113
114
# File 'opal/stdlib/set.rb', line 112

def empty?
  @hash.empty?
end

#eql?(other) ⇒ Boolean

Returns:



116
117
118
# File 'opal/stdlib/set.rb', line 116

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

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

Returns:



125
126
127
# File 'opal/stdlib/set.rb', line 125

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

#inspectObject



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

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

#merge(enum) ⇒ Object



130
131
132
133
# File 'opal/stdlib/set.rb', line 130

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

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

Returns:



183
184
185
186
187
# File 'opal/stdlib/set.rb', line 183

def proper_subset?(set)
  set.is_a?(Set) or raise ArgumentError, "value must be a set"
  return false if set.size <= size
  all? { |o| set.include?(o) }
end

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

Returns:



167
168
169
170
171
# File 'opal/stdlib/set.rb', line 167

def proper_superset?(set)
  set.is_a?(Set) or raise ArgumentError, "value must be a set"
  return false if size <= set.size
  set.all? { |o| include?(o) }
end

#replace(enum) ⇒ Object



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

def replace(enum)
  clear
  merge(enum)

  self
end

#sizeObject Also known as: length



142
143
144
# File 'opal/stdlib/set.rb', line 142

def size
  @hash.size
end

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

Returns:



175
176
177
178
179
# File 'opal/stdlib/set.rb', line 175

def subset?(set)
  set.is_a?(Set) or raise ArgumentError, "value must be a set"
  return false if set.size < size
  all? { |o| set.include?(o) }
end

#subtract(enum) ⇒ Object



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

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

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

Returns:



159
160
161
162
163
# File 'opal/stdlib/set.rb', line 159

def superset?(set)
  set.is_a?(Set) or raise ArgumentError, "value must be a set"
  return false if size < set.size
  set.all? { |o| include?(o) }
end

#to_aObject



194
195
196
# File 'opal/stdlib/set.rb', line 194

def to_a
  @hash.keys
end

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



152
153
154
155
156
157
# File 'opal/stdlib/set.rb', line 152

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