struct

Pf::BidiMap(K, V)

Inherits Enumerable / Struct / Value / Object

A thread-safe, persistent, unordered bidirectional map.

See also: Map.

Constructors

new(enumerable : Enumerable(Tuple(K, V))) : BidiMap(K, V)

Returns a map with mappings from an enumerable of key-value pairs.

Source
new

Returns a new empty BidiMap.

bidi = Pf::BidiMap(String, Int32).new
bidi.empty? # => true
Source

Class methods

assoc(key : K, value : V) : BidiMap(K, V)

A shorthand for new.assoc.

Source

Instance methods

==(other : self)

Returns true if the bidirectional maps are equal.

Source
assoc(key : K, value : V) : BidiMap(K, V)

Returns a copy of self that contains the mapping of key to value. and of value to key.

Supports value equality.

bidi = Pf::BidiMap(String, Int32).new
bidi.assoc("hello", 100) # => Pf::BidiMap{"hello" <=> 100}
Source
dissoc_by_key(key : K) : BidiMap(K, V)

Returns a copy of self which is guaranteed not to have a mapping with the given key.

bidi = Pf::BidiMap.assoc(:foo, 100).assoc(:bar, 200)
bidi.dissoc_by_key(:foo) # => Pf::BidiMap{:bar <=> 200}
Source
dissoc_by_value(value : V) : BidiMap(K, V)

Returns a copy of self which is guaranteed not to have a mapping with the given value.

bidi = Pf::BidiMap.assoc(:foo, 100).assoc(:bar, 200)
bidi.dissoc_by_value(200) # => Pf::BidiMap{:foo <=> 100}
Source
each

Yields each key-value pair to the block.

Source
empty?

Returns true if this map contains no mappings.

Source
has_key_for?(value) : Bool

Returns true if this map contains a mapping with the given value.

bidi = Pf::BidiMap.assoc(:foo, 100).assoc(:bar, 200)
bidi.has_key_for?(100) # => true
bidi.has_key_for?(200) # => true
bidi.has_key_for?(300) # => false
Source
has_value_for?(key) : Bool

Returns true if this map contains a mapping with the given key.

bidi = Pf::BidiMap.assoc(:foo, 100).assoc(:bar, 200)
bidi.has_value_for?(:foo) # => true
bidi.has_value_for?(:bar) # => true
bidi.has_value_for?(:baz) # => false
Source
hash(hasher)

See Object#hash(hasher).

Source
inspect(io)
Source
key_for(value : V) : K | Nil

Returns the key mapped to the given value. If there is no such key raises KeyError.

bidi = Pf::BidiMap.assoc(:foo, 100).assoc(:bar, 200)
bidi.key_for(100) # => :foo
bidi.key_for(200) # => :bar
bidi.key_for(300) # raises KeyError
Source
key_for?(value : V) : K | Nil

Returns the key mapped to the given value, or nil if there is no such key.

bidi = Pf::BidiMap.assoc(:foo, 100).assoc(:bar, 200)
bidi.key_for?(100) # => :foo
bidi.key_for?(200) # => :bar
bidi.key_for?(300) # => nil
Source
pretty_print(pp) : Nil
Source
same?(other : BidiMap(K, V)) : Bool

Returns true if self and other refer to the same map in memory.

Due to the way BidiMap is implemented, this method can be used as a cheap way to detect changes.

bidi1 = Pf::BidiMap.assoc(:foo, 100).assoc(:bar, 200)
bidi2 = bidi1.assoc(:foo, 100)
bidi1.same?(bidi2) # => true
Source
size

Returns the number of mappings.

Source
to_s(io)
Source
value_for(key : K) : V | Nil

Returns the value mapped to the given key. If there is no such value raises KeyError.

bidi = Pf::BidiMap.assoc(:foo, 100).assoc(:bar, 200)
bidi.value_for(:foo) # => 100
bidi.value_for(:bar) # => 200
bidi.value_for(:baz) # raises KeyError
Source
value_for?(key : K) : V | Nil

Returns the value mapped to the given key, or nil if there is no such value.

bidi = Pf::BidiMap.assoc(:foo, 100).assoc(:bar, 200)
bidi.value_for?(:foo) # => 100
bidi.value_for?(:bar) # => 200
bidi.value_for?(:baz) # => nil
Source