class I8::Surjection

Definitions

Public Class Methods

[](pairs = {}) click to toggle source

Class Methods

# File lib/nrser/labs/i8/surjection.rb, line 41
def self.[] pairs = {}
  new pairs
end
alloc(hash) click to toggle source
# File lib/nrser/labs/i8/surjection.rb, line 46
def self.alloc hash
  allocate.tap { |instance| instance.instance_variable_set :@hash, hash }
end
new(pairs = {}) click to toggle source

Construction

# File lib/nrser/labs/i8/surjection.rb, line 54
def initialize pairs = {}
  @hash = I8::Hash[
    pairs.each_with_object( {} ) { |(keys, value), hash|
      set = I8::Set.new keys
      if hash.key? value
        hash[value] |= set
      else
        hash[value] = set
      end
    }
  ]

  @hash.values.combination( 2 ).each do |a, b|
    ( a & b ).unless( :empty? ) { |intersection|
      raise NRSER::ConflictError.new \
        "Sets", a, "and", b, "are not disjoint, sharing", intersection
    }
  end
end

Public Instance Methods

[](key;) click to toggle source

@see call

# File lib/nrser/labs/i8/surjection.rb, line 88
def []  key;  call key; end
call(arg) click to toggle source

Instance Methods

# File lib/nrser/labs/i8/surjection.rb, line 78
def call arg
  @hash.each { |value, set| return value if set.include?( arg ) }
  nil
end
codomain() click to toggle source

The codomain of the surjection (or “values” in Ruby-Hash-y lingo).

@return [I8::Set]

# File lib/nrser/labs/i8/surjection.rb, line 157
def codomain
  I8::Set.new @hash.keys
end
domain() click to toggle source

The domain of the surjection (or “keys” in Ruby-Hash-y lingo).

@return [I8::Set]

# File lib/nrser/labs/i8/surjection.rb, line 126
def domain
  @hash.each_value.reduce I8::Set.empty, :|
end
get(key;) click to toggle source

@see call

# File lib/nrser/labs/i8/surjection.rb, line 86
def get key;  call key; end
has_key?(key;) click to toggle source

@see key?

# File lib/nrser/labs/i8/surjection.rb, line 146
def has_key?  key; key? key; end
has_value?(value;) click to toggle source

@see value?

# File lib/nrser/labs/i8/surjection.rb, line 179
def has_value? value; value? value; end
include?(key;) click to toggle source

@see key?

# File lib/nrser/labs/i8/surjection.rb, line 148
def include?  key; key? key; end
inspect() click to toggle source
# File lib/nrser/labs/i8/surjection.rb, line 182
def inspect
  to_s_with :inspect
end
key?(key) click to toggle source

The Ruby-Hash-y way of finding out if an object is in the {#domain}.

Functionally the same as `surjection.domain.include? obj`.

@param [Object] key @return [Boolean]

# File lib/nrser/labs/i8/surjection.rb, line 138
def key? key
  # domain.include? key
  @hash.each_value.any? { |key_set| key_set.include? key }
end
member?(key;) click to toggle source

@see key?

# File lib/nrser/labs/i8/surjection.rb, line 150
def member?   key; key? key; end
put(arg, value) click to toggle source
# File lib/nrser/labs/i8/surjection.rb, line 91
def put arg, value
  if domain.include?( arg ) && call( arg ) != value
    raise NRSER::ConflictError.new "Already mapping", arg, "to", call( arg )
  end

  if value? value
    # We already have a set of keys mapping to the value
    if @hash[value].include? arg
      # And it already has the key, so we can just return this instance
      self
    else
      # The key set we have for the value does not have the key in it.
      #
      # We need to
      #
      # 1.  Create a new hash with the key in the value's key set.
      # 2.  Allocate a new surjection.
      # 3.  set that as it's hash.
      #
      self.class.alloc @hash.put( value ) { |set| set.add arg }
    end
  else
    # We don't have a set of keys for that value
    self.class.alloc @hash.put( value, I8::Set[ arg ] )
  end
end
store(*args, █) click to toggle source

The standard aliases (as methods for easy subclass overrides)

# File lib/nrser/labs/i8/surjection.rb, line 119
def store *args, █ put *args, █ end
to_s() click to toggle source
# File lib/nrser/labs/i8/surjection.rb, line 187
def to_s
  to_s_with :to_s
end
value?(value) click to toggle source

Is `value` in the surjection's {#codomain}?

@param [Object] value @return [Boolean]

# File lib/nrser/labs/i8/surjection.rb, line 172
def value? value
  @hash.key? value
end
values() click to toggle source

@see codomain

# File lib/nrser/labs/i8/surjection.rb, line 164
def values; codomain; end

Protected Instance Methods

to_s_with(method_name) click to toggle source
# File lib/nrser/labs/i8/surjection.rb, line 195
def to_s_with method_name
  "#{ self.class }[" +
  @hash.each_pair.map { |value, set|
    "{#{set.map(&method_name).join(', ')}}=>#{value.send method_name}"
  }.join( ', ' ) +
  ']'
end