Enhanced C#
Loyc library documentation
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
Properties | Public Member Functions | Static Public Member Functions | List of all members
Loyc.Collections.MMap< K, V > Class Template Reference

A dictionary class built on top of InternalSet<KeyValuePair<K,V>>. More...


Source file:
Inheritance diagram for Loyc.Collections.MMap< K, V >:
Loyc.Collections.MapOrMMap< K, V > Loyc.Collections.IDictionaryEx< K, V > Loyc.ICloneable< out T > Loyc.Collections.IAddRange< in T > Loyc.Collections.ISetOperations< in T, in InSetT, out OutSetT > Loyc.Collections.ICount Loyc.ICloneable< out T > Loyc.Collections.IDictionaryImpl< K, V > Loyc.Collections.IIndexed< in K, out V > Loyc.Collections.ITryGet< in K, out V >

Remarks

A dictionary class built on top of InternalSet<KeyValuePair<K,V>>.

Template Parameters
K
V

Benchmarks show that this class is not as fast as the standard Dictionary{K,V} in most cases. however, it does have some advantages:

The documentation of InternalSet{T} describes how the data structure works.

Properties

new KeyCollection< K, V > Keys [get]
 
new ValueCollection< K, V > Values [get]
 
new V this[K key] [get, set]
 
bool IsReadOnly [get]
 
- Properties inherited from Loyc.Collections.MapOrMMap< K, V >
bool IsEmpty [get]
 
IEqualityComparer< K > KeyComparer [get]
 
InternalSet< KeyValuePair< K, V > > FrozenInternalSet [get]
 
this[K key] [get]
 
this[K key, V defaultValue] [get]
 Retrieves the value associated with the specified key, or returns defaultValue if the key is not found. More...
 
int Count [get]
 
IEnumerable< K > Keys [get]
 
IEnumerable< V > Values [get]
 
- Properties inherited from Loyc.Collections.IIndexed< in K, out V >
this[K key] [get]
 Gets the value associated with the specified key. More...
 
- Properties inherited from Loyc.Collections.IDictionaryEx< K, V >
new V this[K key] [get, set]
 
new ICollection< K > Keys [get]
 
new ICollection< V > Values [get]
 
- Properties inherited from Loyc.Collections.IIndexedSink< in K, in V >
this[K key] [set]
 
- Properties inherited from Loyc.Collections.ISource< out T >
new int Count [get]
 Gets the number of items in the collection. More...
 
- Properties inherited from Loyc.Collections.ICount
int Count [get]
 Gets the number of items in the collection. More...
 
- Properties inherited from Loyc.Collections.IIsEmpty
bool IsEmpty [get]
 
- Properties inherited from Loyc.Collections.IMIndexed< in K, V >
new V this[K key] [get, set]
 Gets the value associated with the specified key. More...
 

Public Member Functions

 MMap (IEqualityComparer< K >?comparer)
 Creates an empty map with the specified key comparer. More...
 
 MMap (IEnumerable< KeyValuePair< K, V >> copy)
 Creates a map with the specified elements. More...
 
 MMap (IEnumerable< KeyValuePair< K, V >> copy, IEqualityComparer< K >?comparer)
 Creates a map with the specified elements and key comparer. More...
 
void Add (K key, V value)
 
bool Remove (K key)
 
void Add (KeyValuePair< K, V > item)
 
void Clear ()
 
bool Remove (KeyValuePair< K, V > item)
 Removes a pair from the map. More...
 
virtual MMap< K, V > Clone ()
 Creates a copy of this map in O(1) time, by marking the current root node as frozen. More...
 
int AddRange (MMap< K, V > data, bool replaceIfPresent=true)
 Merges the contents of the specified map into this map. More...
 
int AddRange (IEnumerable< KeyValuePair< K, V >> data, bool replaceIfPresent=true)
 Merges the contents of the specified sequence into this map. More...
 
int AddRange (IEnumerable< KeyValuePair< K, V >> data, DictEditMode mode)
 Merges the contents of the specified sequence into this map. More...
 
bool GetAndEdit (ref K key, ref V value, DictEditMode mode)
 Combines a get and change operation into a single method call. You rarely need to call this method directly; the following extension methods are based on it: DictionaryExt.SwapIfPresent, DictionaryExt.AddIfNotPresent, DictionaryExt.AddOrGetExisting, DictionaryExt.ReplaceIfPresent, DictionaryExt.SetAndGet.
Parameters
keySpecifies the key that you want to search for in the map. Some implementations will update the key with the version of it found in the dictionary (although the new key is "equal" to the old key, it may be a different object); otherwise the key is left unchanged.
valueIf the key is found, the old value is saved in this parameter. Otherwise, it is left unchanged.
modeThe specific behavior of this method depends on this. See DictEditMode to understand its effect.
Returns
True if the pair's key ALREADY existed, false if not.
This method exists because some collections can optimize certain combinations of operations, avoiding the two traversals through the data structure that would be required by the IDictionary interface. This method shall not throw when the key is null, unless the AddIfNotPresent bit is set in mode and the dictionary does not support a null key.
 
bool AddOrFind (ref KeyValuePair< K, V > pair, bool replaceIfPresent)
 For internal use. Adds a pair to the map if the key is not present, retrieves the existing key-value pair if the key is present, and optionally replaces the existing pair with a new pair. More...
 
Maybe< V > GetAndRemove (K key)
 Gets the value associated with the specified key, then removes the pair with that key from the dictionary. More...
 
bool GetAndRemove (ref KeyValuePair< K, V > pair)
 Gets the pair associated with pair.Key, then removes the pair with that key from the dictionary. More...
 
MMap< K, V > With (K key, V value, bool replaceIfPresent=true)
 
MMap< K, V > With (KeyValuePair< K, V > item)
 
MMap< K, V > Without (K key)
 
MMap< K, V > Union (MapOrMMap< K, V > other)
 
MMap< K, V > Union (MapOrMMap< K, V > other, bool replaceWithValuesFromOther)
 
MMap< K, V > Intersect (MapOrMMap< K, V > other)
 
MMap< K, V > Except (MapOrMMap< K, V > other)
 
MMap< K, V > Xor (MapOrMMap< K, V > other)
 
Map< K, V > AsImmutable ()
 
- Public Member Functions inherited from Loyc.Collections.MapOrMMap< K, V >
bool ContainsKey (K key)
 
bool TryGetValue (K key, out V value)
 
bool Contains (KeyValuePair< K, V > item)
 
void CopyTo (KeyValuePair< K, V >[] array, int arrayIndex)
 
InternalSet< KeyValuePair< K,
V > >.Enumerator 
GetEnumerator ()
 
TryGetValue (K key, V defaultValue)
 Synonym for this[key, defaultValue]. More...
 
virtual long CountMemory (int sizeOfPair)
 Measures the total size of all objects allocated to this collection, in bytes, including the size of this object itself; see InternalSet{T}.CountMemory. More...
 
- Public Member Functions inherited from Loyc.Collections.IDictionaryEx< K, V >
new bool ContainsKey (K key)
 
new bool TryGetValue (K key, out V value)
 
- Public Member Functions inherited from Loyc.Collections.IAddRange< in T >
void AddRange (IEnumerable< T > e)
 
void AddRange (IReadOnlyCollection< T > s)
 
- Public Member Functions inherited from Loyc.Collections.ISetOperations< in T, in InSetT, out OutSetT >
OutSetT With (T item)
 
OutSetT Without (T item)
 
OutSetT Union (InSetT other)
 
OutSetT Intersect (InSetT other)
 
OutSetT Except (InSetT other)
 
OutSetT Xor (InSetT other)
 

Static Public Member Functions

static operator Map< K, V > (MMap< K, V > copy)
 

Additional Inherited Members

- Protected Member Functions inherited from Loyc.Collections.MapOrMMap< K, V >
 MapOrMMap (IEqualityComparer< K >?comparer)
 
 MapOrMMap (IEnumerable< KeyValuePair< K, V >> list)
 
 MapOrMMap (IEnumerable< KeyValuePair< K, V >> list, IEqualityComparer< K >?comparer)
 
- Protected static fields inherited from Loyc.Collections.MapOrMMap< K, V >
static readonly
EqualityComparer< V > 
DefaultValueComparer = EqualityComparer<V>.Default
 

Constructor & Destructor Documentation

Loyc.Collections.MMap< K, V >.MMap ( IEqualityComparer< K >?  comparer)
inline

Creates an empty map with the specified key comparer.

Loyc.Collections.MMap< K, V >.MMap ( IEnumerable< KeyValuePair< K, V >>  copy)
inline

Creates a map with the specified elements.

Loyc.Collections.MMap< K, V >.MMap ( IEnumerable< KeyValuePair< K, V >>  copy,
IEqualityComparer< K >?  comparer 
)
inline

Creates a map with the specified elements and key comparer.

Member Function Documentation

bool Loyc.Collections.MMap< K, V >.AddOrFind ( ref KeyValuePair< K, V >  pair,
bool  replaceIfPresent 
)
inline

For internal use. Adds a pair to the map if the key is not present, retrieves the existing key-value pair if the key is present, and optionally replaces the existing pair with a new pair.

Parameters
pairWhen calling this method, pair.Key specifies the key that you want to search for in the map. If the key is not found then the pair is added to the map; if the key is found, the pair is replaced with the existing pair that was found in the map.
replaceIfPresentThis parameter specifies what to do if the key is found in the map. If this parameter is true, the existing pair is replaced with the specified new pair (in fact the pair in the map is swapped with the pair parameter). If this parameter is false, the existing pair is left unmodified and a copy of it is stored in the pair parameter.
Returns
True if the pair's key did NOT exist and was added, false if the key already existed.
int Loyc.Collections.MMap< K, V >.AddRange ( MMap< K, V >  data,
bool  replaceIfPresent = true 
)
inline

Merges the contents of the specified map into this map.

Parameters
replaceIfPresentIf true, values in the other collection replace values in this one. If false, the existing pairs in this map are not overwritten.
Returns
The number of items that were added.
int Loyc.Collections.MMap< K, V >.AddRange ( IEnumerable< KeyValuePair< K, V >>  data,
bool  replaceIfPresent = true 
)
inline

Merges the contents of the specified sequence into this map.

Parameters
replaceIfPresentIf true, values in the other collection replace values in this one. If false, the existing pairs in this map are not overwritten.
Returns
The number of new pairs added, whose keys didn't already exist.

Duplicates are allowed in the source data. If replaceIfPresent is true, later values take priority over earlier values, otherwise earlier values take priority.

int Loyc.Collections.MMap< K, V >.AddRange ( IEnumerable< KeyValuePair< K, V >>  data,
DictEditMode  mode 
)
inline

Merges the contents of the specified sequence into this map.

Parameters
dataPairs to merge in. Duplicates are allowed; if the ReplaceIfPresent bit is set in mode, later values take priority over earlier values, otherwise earlier values take priority.
modeSpecifies how to combine the collections.
Returns
The number of pairs that did not already exist in the collection. if the AddIfNotPresent bit is set on mode, this is the number of new pairs added.
See also
DictionaryExt.AddRange{K, V}(IDictionary{K, V}, IEnumerable{KeyValuePair{K, V}})

Implements Loyc.Collections.IDictionaryEx< K, V >.

virtual MMap<K, V> Loyc.Collections.MMap< K, V >.Clone ( )
inlinevirtual

Creates a copy of this map in O(1) time, by marking the current root node as frozen.

Implements Loyc.ICloneable< out T >.

Maybe<V> Loyc.Collections.MMap< K, V >.GetAndRemove ( key)
inline

Gets the value associated with the specified key, then removes the pair with that key from the dictionary.

Parameters
keyKey to search for.
Returns
The value that was removed. If the key is not found, the result has no value (Maybe{V}.HasValue is false).

This method shall not throw when the key is null.

Implements Loyc.Collections.IDictionaryEx< K, V >.

bool Loyc.Collections.MMap< K, V >.GetAndRemove ( ref KeyValuePair< K, V >  pair)
inline

Gets the pair associated with pair.Key, then removes the pair with that key from the dictionary.

Parameters
pairSpecifies the key to search for. On return, if the key was found, this holds both the key and value that used to be in the dictionary.
Returns
True if a pair was removed, false if not.
bool Loyc.Collections.MMap< K, V >.Remove ( KeyValuePair< K, V >  item)
inline

Removes a pair from the map.

The removal occurs only if the value provided matches the value that is already associated with the key (value comparison is performed using object.Equals()).

Returns
True if the pair was removed, false if not.

References Loyc.Collections.Remove.