struct Pf::BidiMap(K, V)

Overview

A thread-safe, persistent, unordered bidirectional map.

See also: Map.

Included Modules

Defined in:

permafrost/bidi_map.cr

Constructors

Class Method Summary

Instance Method Summary

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, V
to_pf_map
to_pf_map
, to_pf_set : Pf::Set({K, V}) to_pf_set

Constructor Detail

def self.new(enumerable : Enumerable(Tuple(K, V))) : BidiMap(K, V) #

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


[View source]
def self.new : BidiMap(K, V) #

Returns a new empty BidiMap.

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

[View source]

Class Method Detail

def self.assoc(key : K, value : V) : BidiMap(K, V) #

A shorthand for new.assoc.


[View source]

Instance Method Detail

def ==(other : self) #

Returns true if the bidirectional maps are equal.


[View source]
def 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}

[View source]
def 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}

[View source]
def 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}

[View source]
def each(& : Tuple(K, V) -> ) : Nil #

Yields each key-value pair to the block.


[View source]
def empty? : Bool #

Returns true if this map contains no mappings.


[View source]
def 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

[View source]
def 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

[View source]
def hash(hasher) #

See Object#hash(hasher).


[View source]
def inspect(io) #

[View source]
def 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

[View source]
def 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

[View source]
def pretty_print(pp) : Nil #

[View source]
def 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

[View source]
def size : Int32 #

Returns the number of mappings.


[View source]
def to_s(io) #

[View source]
def 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

[View source]
def 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

[View source]