123456789_123456789_123456789_123456789_123456789_

Class: TypeProf::Utils::Set

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
self, Enumerable, StructuralEquality
Inherits: Object
Defined in: lib/typeprof/utils.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(tbl) ⇒ Set

[ GitHub ]

  
# File 'lib/typeprof/utils.rb', line 31

def initialize(tbl)
  @tbl = tbl
  @tbl.freeze
end

Class Method Details

.[](*values)

[ GitHub ]

  
# File 'lib/typeprof/utils.rb', line 23

def self.[](*values)
  tbl = {}
  values.each do |v|
    tbl[v] = true
  end
  new(tbl)
end

Instance Attribute Details

#tbl (readonly)

[ GitHub ]

  
# File 'lib/typeprof/utils.rb', line 21

attr_reader :tbl

Instance Method Details

#add(new_val)

[ GitHub ]

  
# File 'lib/typeprof/utils.rb', line 52

def add(new_val)
  tbl = @tbl.dup
  tbl[new_val] = true
  Set.new(tbl)
end

#each(&blk)

[ GitHub ]

  
# File 'lib/typeprof/utils.rb', line 36

def each(&blk)
  @tbl.each_key(&blk)
end

#include?(elem) ⇒ Boolean

[ GitHub ]

  
# File 'lib/typeprof/utils.rb', line 77

def include?(elem)
  @tbl[elem]
end

#inspect

[ GitHub ]

  
# File 'lib/typeprof/utils.rb', line 71

def inspect
  s = []
  each {|v| s << v.inspect }
  "{#{ s.join(", ") }}"
end

#intersection(other)

[ GitHub ]

  
# File 'lib/typeprof/utils.rb', line 81

def intersection(other)
  tbl = {}
  h = 0
  each do |elem|
    if other.include?(elem)
      tbl << elem
      h ^= elem.hash
    end
  end
  Set.new(tbl, h)
end

#map(&blk)

[ GitHub ]

  
# File 'lib/typeprof/utils.rb', line 62

def map(&blk)
  tbl = {}
  each do |elem|
    v = yield(elem)
    tbl[v] = true
  end
  Set.new(tbl)
end

#size

[ GitHub ]

  
# File 'lib/typeprof/utils.rb', line 58

def size
  @tbl.size
end

#sum(other)

[ GitHub ]

  
# File 'lib/typeprof/utils.rb', line 42

def sum(other)
  if @tbl.size == 0
    other
  elsif other.tbl.size == 0
    self
  else
    Set.new(@tbl.merge(other.tbl))
  end
end