Class: Bijection
- Inherits:
-
Object
- Object
- Bijection
- Defined in:
- lib/bijection.rb
Overview
Bijection is a container similar to a Hash with unique values. http://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.
Constant Summary collapse
- VERSION =
"0.1.0"
Instance Method Summary collapse
-
#add(x, y) ⇒ self
add (x,y) to the bijection set (X,Y).
-
#delete_by_x(x) ⇒ Object
given x, delete (x,y) and return y.
-
#delete_by_y(y) ⇒ Object
given y, delete (x,y) and return x see example #delete_by_x.
-
#domain ⇒ Array
get the domain set X as an Array.
- #each_pair {|x, y| ... } ⇒ nil
- #each_x {|x| ... } ⇒ nil
- #each_y {|y| ... } ⇒ nil
-
#get_x(y) ⇒ Object
get the x associated with y.
-
#get_y(x) ⇒ Object
get the y associated with x.
-
#initialize ⇒ Bijection
constructor
A new instance of Bijection.
-
#inverse! ⇒ nil
swap domain X and range Y of this.
-
#range ⇒ Array
get the range set Y as an Array.
-
#size ⇒ Fixnum
returns the number of pairs in the bijection.
Constructor Details
#initialize ⇒ Bijection
Returns a new instance of Bijection.
35 36 37 38 |
# File 'lib/bijection.rb', line 35 def initialize @X = {} # @X[x] -> y @Y = {} # @Y[y] -> x end |
Instance Method Details
#add(x, y) ⇒ self
x must be unique in X; y must be unique in Y
add (x,y) to the bijection set (X,Y)
51 52 53 54 55 56 57 58 59 |
# 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) ⇒ Object
given x, delete (x,y) and return y
126 127 128 129 130 131 |
# 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) ⇒ Object
given y, delete (x,y) and return x see example #delete_by_x
137 138 139 140 141 142 |
# File 'lib/bijection.rb', line 137 def delete_by_y( y ) x = @Y[y] @Y.delete y @X.delete x x end |
#domain ⇒ Array
get the domain set X as an Array
63 64 65 |
# File 'lib/bijection.rb', line 63 def domain @X.keys end |
#each_pair {|x, y| ... } ⇒ nil
112 113 114 115 |
# 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| ... } ⇒ nil
98 99 100 101 |
# File 'lib/bijection.rb', line 98 def each_x @X.each_key { |x| yield x } if block_given? nil end |
#each_y {|y| ... } ⇒ nil
105 106 107 108 |
# File 'lib/bijection.rb', line 105 def each_y @Y.each_key { |y| yield y } if block_given? nil end |
#get_x(y) ⇒ Object
get the x associated with y
85 86 87 |
# File 'lib/bijection.rb', line 85 def get_x( y ) @Y[y] end |
#get_y(x) ⇒ Object
get the y associated with x
92 93 94 |
# File 'lib/bijection.rb', line 92 def get_y( x ) @X[x] end |
#inverse! ⇒ nil
swap domain X and range Y of this
75 76 77 78 79 80 |
# File 'lib/bijection.rb', line 75 def inverse! temp = @X @X = @Y @Y = temp nil end |
#range ⇒ Array
get the range set Y as an Array
69 70 71 |
# File 'lib/bijection.rb', line 69 def range @Y.keys end |
#size ⇒ Fixnum
returns the number of pairs in the bijection
42 43 44 |
# File 'lib/bijection.rb', line 42 def size @X.size end |