struct Pf::BidiMap(K, V)
- Pf::BidiMap(K, V)
- Struct
- Value
- Object
Overview
A thread-safe, persistent, unordered bidirectional map.
See also: Map
.
Included Modules
Defined in:
permafrost/bidi_map.crConstructors
-
.new(enumerable : Enumerable(Tuple(K, V))) : BidiMap(K, V)
Returns a map with mappings from an enumerable of key-value pairs.
-
.new : BidiMap(K, V)
Returns a new empty
BidiMap
.
Class Method Summary
-
.assoc(key : K, value : V) : BidiMap(K, V)
A shorthand for
new.assoc
.
Instance Method Summary
-
#==(other : self)
Returns
true
if the bidirectional maps are equal. -
#assoc(key : K, value : V) : BidiMap(K, V)
Returns a copy of
self
that contains the mapping of key to value. -
#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. -
#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. -
#each(& : Tuple(K, V) -> ) : Nil
Yields each key-value pair to the block.
-
#empty? : Bool
Returns
true
if this map contains no mappings. -
#has_key_for?(value) : Bool
Returns
true
if this map contains a mapping with the given value. -
#has_value_for?(key) : Bool
Returns
true
if this map contains a mapping with the given key. -
#hash(hasher)
See
Object#hash(hasher)
. - #inspect(io)
-
#key_for(value : V) : K | Nil
Returns the key mapped to the given value.
-
#key_for?(value : V) : K | Nil
Returns the key mapped to the given value, or nil if there is no such key.
- #pretty_print(pp) : Nil
-
#same?(other : BidiMap(K, V)) : Bool
Returns
true
ifself
and other refer to the same map in memory. -
#size : Int32
Returns the number of mappings.
- #to_s(io)
-
#value_for(key : K) : V | Nil
Returns the value mapped to the given key.
-
#value_for?(key : K) : V | Nil
Returns the value mapped to the given key, or nil if there is no such value.
Instance methods inherited from module Enumerable({K, V})
to_pf_bidi
to_pf_bidi,
to_pf_map(& : {K, V} -> Tuple(K, V)) : Pf::Map(K, V) forall K, Vto_pf_map to_pf_map, to_pf_set : Pf::Set({K, V}) to_pf_set
Constructor Detail
Returns a map with mappings from an enumerable of key-value pairs.
Returns a new empty BidiMap
.
bidi = Pf::BidiMap(String, Int32).new
bidi.empty? # => true
Class Method Detail
Instance Method Detail
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}
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}
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}
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
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
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
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
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
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
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