Using Bijections

Adding and deleting key/value pairs

Once a Bijection, b, is created, we add a new key-value pair in the same manner as with a Dict:

julia> b = Bijection{Int, String}();
julia> b[1] = "hello""hello"
julia> b[2] = "bye""bye"

Notice, however, that if we add a new key with a value that already exists in the Bijection an error ensues:

julia> b[3] = "hello"ERROR: ArgumentError: inserting 3 => hello would break bijectiveness

On the contrary, if a key already has a value it can be changed by giving it a new value as long as it doesn't break bijectiveness (i.e. the new value is not already in the Bijection):

julia> b[1] = "ciao""ciao"

Accessing values from keys, and keys from values

To access a value associated with a given key, we use the same syntax as for a Dict:

julia> b[1]"ciao"
julia> b[2]"bye"

If the key is not in the Bijection an error is raised:

julia> b[3]ERROR: KeyError: key 3 not found

Since the values in a Bijection must be distinct, we can give a value as an input and retrieve its associate key. The function inverse(b,y) finds the value x such that b[x]==y. However, we provide the handy short cut b(y):

julia> b("bye")2
julia> b("ciao")1

Naturally, if the requested value is not in the Bijection an error is raised:

julia> b("hello")ERROR: KeyError: key "hello" not found

In order to access the domain and image sets of the Bijection, one can use the Base.keys and Base.values functions.

julia> keys(b)KeySet for a Dict{Int64, String} with 2 entries. Keys:
  2
  1
julia> values(b)ValueIterator for a Dict{Int64, String} with 2 entries. Values: "bye" "ciao"

The collect function returns the Bijection as an array of key-value pairs:

julia> collect(b)2-element Vector{Pair{Int64, String}}:
 2 => "bye"
 1 => "ciao"

The length function returns the number of key-value pairs:

julia> length(b)2

The isempty function returns true exactly when the Bijection contains no pairs:

julia> isempty(b)false