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.



19
20
21
# File 'lib/racc/iset.rb', line 19

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

Instance Attribute Details

#setObject (readonly)

Returns the value of attribute set.



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

def set
  @set
end

Instance Method Details

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



29
30
31
# File 'lib/racc/iset.rb', line 29

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

#[]=(key, val) ⇒ Object



33
34
35
# File 'lib/racc/iset.rb', line 33

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

#add(i) ⇒ Object



25
26
27
# File 'lib/racc/iset.rb', line 25

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

#clearObject



83
84
85
# File 'lib/racc/iset.rb', line 83

def clear
  @set.clear
end

#delete(key) ⇒ Object



55
56
57
58
59
# File 'lib/racc/iset.rb', line 55

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

#dupObject



87
88
89
# File 'lib/racc/iset.rb', line 87

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

#each(&block) ⇒ Object



61
62
63
# File 'lib/racc/iset.rb', line 61

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

#empty?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/racc/iset.rb', line 79

def empty?
  @set.nitems == 0
end

#sizeObject



75
76
77
# File 'lib/racc/iset.rb', line 75

def size
  @set.nitems
end

#to_aObject



65
66
67
# File 'lib/racc/iset.rb', line 65

def to_a
  @set.compact
end

#to_sObject Also known as: inspect



69
70
71
# File 'lib/racc/iset.rb', line 69

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

#update(other) ⇒ Object



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

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



50
51
52
53
# File 'lib/racc/iset.rb', line 50

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