Class: Moon::Set

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/moon/packages/std/set.rb

Overview

Basic implementation of Set.

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Set.

Parameters:

  • enum (#each) (defaults to: nil)

7
8
9
10
11
12
13
14
# File 'lib/moon/packages/std/set.rb', line 7

def initialize(enum = nil, &block)
  @data = {}
  if block_given?
    enum.times { |i| add block.call(i) }
  else
    concat enum if enum
  end
end

Instance Method Details

#==(other) ⇒ Boolean

Determines if the Set matches the given object

Parameters:

Returns:


64
65
66
67
68
69
70
71
72
# File 'lib/moon/packages/std/set.rb', line 64

def ==(other)
  if other.equal?(self)
    true
  elsif other.is_a?(Set) || other.is_a?(Array)
    other.size == size && other.all? { |e| include?(e) }
  else
    false
  end
end

#add(*values) ⇒ Object Also known as: push, <<, unshift

Adds an object to the Set.

Parameters:


85
86
87
88
89
90
# File 'lib/moon/packages/std/set.rb', line 85

def add(*values)
  values.each do |e|
    @data[e] = true
  end
  self
end

#blank?Boolean

Determines if the Set is blank?

Returns:


40
41
42
# File 'lib/moon/packages/std/set.rb', line 40

def blank?
  empty?
end

#clearself

Clear the Set.

Returns:

  • (self)

77
78
79
80
# File 'lib/moon/packages/std/set.rb', line 77

def clear
  @data.clear
  self
end

#concat(other) ⇒ self

Appends the other to the current set

Parameters:

Returns:

  • (self)

108
109
110
111
112
113
# File 'lib/moon/packages/std/set.rb', line 108

def concat(other)
  other.each do |obj|
    add obj
  end
  self
end

#delete(value) ⇒ Object

Removes the object from the set and returns it.

Parameters:

Returns:


99
100
101
102
# File 'lib/moon/packages/std/set.rb', line 99

def delete(value)
  @data.delete value
  value
end

#each {|obj| ... } ⇒ Object

Yields each object in the Set.

Yield Parameters:


55
56
57
58
# File 'lib/moon/packages/std/set.rb', line 55

def each(&block)
  return to_enum :each unless block_given?
  @data.keys.each(&block)
end

#empty?Boolean

Determines if the Set is empty?

Returns:


33
34
35
# File 'lib/moon/packages/std/set.rb', line 33

def empty?
  @data.empty?
end

#include?(obj) ⇒ Boolean

Determins if the Set includes a object

Parameters:

Returns:


48
49
50
# File 'lib/moon/packages/std/set.rb', line 48

def include?(obj)
  @data.key?(obj)
end

#popObject

Pops an object from the set, may be random.

Returns:


125
126
127
# File 'lib/moon/packages/std/set.rb', line 125

def pop
  delete @data.keys.pop
end

#sampleObject

Returns a random object from the Set.

Returns:


118
119
120
# File 'lib/moon/packages/std/set.rb', line 118

def sample
  @data.keys.sample
end

#shiftObject

Shifts an object from the set, may be random.

Returns:


132
133
134
# File 'lib/moon/packages/std/set.rb', line 132

def shift
  delete @data.keys.shift
end

#sizeInteger

Returns the size of the Set.

Returns:


26
27
28
# File 'lib/moon/packages/std/set.rb', line 26

def size
  @data.size
end

#to_aArray<Object>

Returns an Array from the Set elements.

Returns:


19
20
21
# File 'lib/moon/packages/std/set.rb', line 19

def to_a
  @data.keys.to_a
end