class Bijection

Bijection is a container similar to a Hash with unique values. {en.wikipedia.org/wiki/Bijection}

Bijection associates each non-nil unique x in a set X, with a non-nil unique y in a set Y.

Please send (welcome) feedback and bug reports to my {github.com/ryanberckmans github}.

@example

b = Bijection.new
b.add 5, 7 # associates 5 in X with 7 in Y
b.size
 => 1
b.add 5, 2 # raises, 5 already in X
b.add "foo", 7 # raises, 7 already in Y
b.add "bar", 5 # OK; 5 was not yet in Y
b.size
 => 2
d = b.domain # d == [5, "bar"]
r = b.range  # r == [7, 5] # i.e., not the "same" 5 in domain
b.each_x { |x| puts x }
 => 5
 => "bar"
b.each_y { |y| puts y }
 => 7
 => 5 # i.e., not the "same" 5 that's in X
b.each_pair { |x,y| .. }
x = b.get_x 7 # x == 5
y = b.get_y 5 # y == 7
is_nil = b.get_x "returns nil if not found" # is_nil == nil
b.inverse! # X and Y are now swapped; all as above with x and y swapped

Constants

VERSION

Public Class Methods

new() click to toggle source
# File lib/bijection.rb, line 35
def initialize
  @X = {} # @X[x] -> y
  @Y = {} # @Y[y] -> x
end

Public Instance Methods

add( x, y ) click to toggle source

add (x,y) to the bijection set (X,Y) @param x the non-nil object associated with y @param y the non-nil object associated with x @note x must be unique in X; y must be unique in Y @return [self]

# File lib/bijection.rb, line 51
def add( x, y )
  raise "Bijection: x may not be nil" if x == nil
  raise "Bijection: y may not be nil" if y == nil
  raise "Bijection: #{x.to_s} already present in domain set X" if @X.key? x
  raise "Bijection: #{y.to_s} already present in range set Y" if @Y.key? y
  @X[x] = y
  @Y[y] = x
  self
end
delete_by_x( x ) click to toggle source

given x, delete (x,y) and return y @param (see get_x) @example

b = Bijection.new
x = 2 ; y = 3
b.add x, y
y = nil
while true do y = b.delete_by_x x ; b.add x, y end

@return (see get_x)

# File lib/bijection.rb, line 126
def delete_by_x( x )
  y = @X[x]
  @X.delete x
  @Y.delete y
  y
end
delete_by_y( y ) click to toggle source

given y, delete (x,y) and return x @param (see get_y) @return (see get_y) see example delete_by_x

# File lib/bijection.rb, line 137
def delete_by_y( y )
  x = @Y[y]
  @Y.delete y
  @X.delete x
  x
end
domain() click to toggle source

get the domain set X as an Array @return [Array]

# File lib/bijection.rb, line 63
def domain
  @X.keys
end
each_pair() { |x,y| ... } click to toggle source

@yield [x,y] each (x,y) in sets X and Y @return [nil]

# File lib/bijection.rb, line 112
def each_pair
  @X.each_pair { |x,y| yield x,y } if block_given?
  nil
end
each_x() { |x| ... } click to toggle source

@yield [x] each x in domain set X @return [nil]

# File lib/bijection.rb, line 98
def each_x
  @X.each_key { |x| yield x } if block_given?
  nil
end
each_y() { |y| ... } click to toggle source

@yield [y] each y in range set Y @return [nil]

# File lib/bijection.rb, line 105
def each_y
  @Y.each_key { |y| yield y } if block_given?
  nil
end
get_x( y ) click to toggle source

get the x associated with y @param y in domain set Y @return the x associated with y, or nil if y not in range Y

# File lib/bijection.rb, line 85
def get_x( y )
  @Y[y]
end
get_y( x ) click to toggle source

get the y associated with x @param x in domain set X @return the y associated with x, or nil if x not in domain X

# File lib/bijection.rb, line 92
def get_y( x )
  @X[x]
end
inverse!() click to toggle source

swap domain X and range Y of this @return [nil]

# File lib/bijection.rb, line 75
def inverse!
  temp = @X
  @X = @Y
  @Y = temp
  nil
end
range() click to toggle source

get the range set Y as an Array @return [Array]

# File lib/bijection.rb, line 69
def range
  @Y.keys
end
size() click to toggle source

returns the number of pairs in the bijection @return [Fixnum]

# File lib/bijection.rb, line 42
def size
  @X.size
end