Class: Racc::ISet

Inherits:
Object show all
Defined in:
lib/racc/iset.rb

Overview

An “indexed” set. All items must respond to :ident.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a = []) ⇒ ISet

Returns a new instance of ISet.



17
18
19
# File 'lib/racc/iset.rb', line 17

def initialize(a = [])
  @set = a
end

Instance Attribute Details

#setObject (readonly)

Returns the value of attribute set.



21
22
23
# File 'lib/racc/iset.rb', line 21

def set
  @set
end

Instance Method Details

#[](key) ⇒ Object Also known as: include?, key?



27
28
29
# File 'lib/racc/iset.rb', line 27

def [](key)
  @set[key.ident]
end

#[]=(key, val) ⇒ Object



31
32
33
# File 'lib/racc/iset.rb', line 31

def []=(key, val)
  @set[key.ident] = val
end

#add(i) ⇒ Object



23
24
25
# File 'lib/racc/iset.rb', line 23

def add(i)
  @set[i.ident] = i
end

#clearObject



81
82
83
# File 'lib/racc/iset.rb', line 81

def clear
  @set.clear
end

#delete(key) ⇒ Object



53
54
55
56
57
# File 'lib/racc/iset.rb', line 53

def delete(key)
  i = @set[key.ident]
  @set[key.ident] = nil
  i
end

#dupObject



85
86
87
# File 'lib/racc/iset.rb', line 85

def dup
  ISet.new(@set.dup)
end

#each(&block) ⇒ Object



59
60
61
# File 'lib/racc/iset.rb', line 59

def each(&block)
  @set.compact.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/racc/iset.rb', line 77

def empty?
  @set.nitems == 0
end

#sizeObject



73
74
75
# File 'lib/racc/iset.rb', line 73

def size
  @set.nitems
end

#to_aObject



63
64
65
# File 'lib/racc/iset.rb', line 63

def to_a
  @set.compact
end

#to_sObject Also known as: inspect



67
68
69
# File 'lib/racc/iset.rb', line 67

def to_s
  "[#{@set.compact.join(' ')}]"
end

#update(other) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/racc/iset.rb', line 38

def update(other)
  s = @set
  o = other.set
  o.each_index do |idx|
    if t = o[idx]
      s[idx] = t
    end
  end
end

#update_a(a) ⇒ Object



48
49
50
51
# File 'lib/racc/iset.rb', line 48

def update_a(a)
  s = @set
  a.each {|i| s[i.ident] = i }
end