Enhanced C#
Language of your choice: library documentation
Properties | Public Member Functions | Protected Member Functions | List of all members
Loyc.Collections.Bijection< K1, K2 > Class Template Reference

A bijection is a one-to-one function and its inverse. It is implemented with a pair of dictionaries, one that maps K1 to K2 and another that maps K2 to K1. More...


Source file:
Inheritance diagram for Loyc.Collections.Bijection< K1, K2 >:
Loyc.Collections.IDictionaryImpl< K1, K2 >

Remarks

A bijection is a one-to-one function and its inverse. It is implemented with a pair of dictionaries, one that maps K1 to K2 and another that maps K2 to K1.

Template Parameters
K1Default key.
K2Inverse key.

The Bijection object that you first create is a dictionary from K1 to K2. Call the Inverse property to get the inverse dictionary from K2 back to K1. Whenever you modify one of the dictionaries, the other dictionary is also modified in the same way.

For example, if you do this:

var map = new Bijection<int,string>() { { 1, "one" }, {2, "two"} };
map.Add(3, "three");
map.Inverse.Add("four", 4);
map.Inverse.Remove("two");

Then the bijection will contain three pairs: {1, "one"}, {3, "three"} and {4, "four"}. The inverse bijection object map.Inverse is "first- class" and can be used just like the original map. The two interfaces, map and map.Inverse, are linked to each other, so map.Inverse.Inverse == map.

This collection itself is not safe for multithreaded access, even if it is constructed out of two ConcurrentDictionary{K,V} objects.

Properties

Bijection< K2, K1 > Inverse [get]
 Returns the inverse dictionary. Note: this.Inverse.Inverse == this. More...
 
ICollection< K1 > Keys [get]
 
ICollection< K2 > Values [get]
 
K2 this[K1 key] [get, set]
 
int Count [get]
 
bool IsReadOnly [get]
 

Public Member Functions

 Bijection ()
 Constructs a bijection out of two Dictionary<TKey,TValue> objects. More...
 
 Bijection (int capacity)
 Constructs a bijection out of two Dictionary<TKey,TValue> objects, each with the specified capacity. More...
 
 Bijection (IReadOnlyCollection< KeyValuePair< K1, K2 >> input)
 Constructs a bijection out of two Dictionary<TKey,TValue> objects, copying the specified initial contents, and using the initial Count as the capacity. More...
 
 Bijection (IEnumerable< KeyValuePair< K1, K2 >> input, int capacity=0)
 Constructs a bijection out of two Dictionary<TKey,TValue> objects, copying the specified initial contents. More...
 
 Bijection (IDictionary< K1, K2 > cur, IDictionary< K2, K1 > inv)
 Constructs a bijection out of two existing dictionaries. More...
 
void Add (K1 key, K2 value)
 
bool ContainsKey (K1 key)
 
bool Remove (K1 key)
 
bool TryGetValue (K1 key, out K2 value)
 
void Add (KeyValuePair< K1, K2 > item)
 
void Clear ()
 
bool Contains (KeyValuePair< K1, K2 > item)
 
void CopyTo (KeyValuePair< K1, K2 >[] array, int arrayIndex)
 
bool Remove (KeyValuePair< K1, K2 > item)
 
IEnumerator< KeyValuePair< K1, K2 > > GetEnumerator ()
 

Protected Member Functions

 Bijection (IDictionary< K1, K2 > cur, Bijection< K2, K1 > other)
 

Constructor & Destructor Documentation

◆ Bijection() [1/5]

Loyc.Collections.Bijection< K1, K2 >.Bijection ( )
inline

Constructs a bijection out of two Dictionary<TKey,TValue> objects.

◆ Bijection() [2/5]

Loyc.Collections.Bijection< K1, K2 >.Bijection ( int  capacity)
inline

Constructs a bijection out of two Dictionary<TKey,TValue> objects, each with the specified capacity.

◆ Bijection() [3/5]

Loyc.Collections.Bijection< K1, K2 >.Bijection ( IReadOnlyCollection< KeyValuePair< K1, K2 >>  input)
inline

Constructs a bijection out of two Dictionary<TKey,TValue> objects, copying the specified initial contents, and using the initial Count as the capacity.

Exceptions
KeyAlreadyExistsExceptionThe input had a duplicate key or value.

The bijection is mutable even though the input is not.

◆ Bijection() [4/5]

Loyc.Collections.Bijection< K1, K2 >.Bijection ( IEnumerable< KeyValuePair< K1, K2 >>  input,
int  capacity = 0 
)
inline

Constructs a bijection out of two Dictionary<TKey,TValue> objects, copying the specified initial contents.

Exceptions
KeyAlreadyExistsExceptionThe input had a duplicate key or value.

◆ Bijection() [5/5]

Loyc.Collections.Bijection< K1, K2 >.Bijection ( IDictionary< K1, K2 >  cur,
IDictionary< K2, K1 >  inv 
)
inline

Constructs a bijection out of two existing dictionaries.

To save time, the constructor does not verify that the two dictionaries are a proper bijection; instead it works "on the honor system", and only checks to ensure that the two dictionaries have the same Count. The two dictionaries must already be a bijection (one-to-one maps into each other) and should not be modified after they are attached to this object. Of course, you can supply two empty dictionaries of any type.

Property Documentation

◆ Inverse

Bijection<K2, K1> Loyc.Collections.Bijection< K1, K2 >.Inverse
get

Returns the inverse dictionary. Note: this.Inverse.Inverse == this.