Class LetterMap<T>
Maps ASCII letters to values without treating case as different. Other characters are not supported. Enumeration follows alphabetical order.
public class LetterMap<T> : IDictionary<char, T>, ICollection<KeyValuePair<char, T>>, IEnumerable<KeyValuePair<char, T>>, IEnumerable
Type Parameters
T
- Inheritance
-
LetterMap<T>
- Implements
-
IDictionary<char, T>
- Inherited Members
- Extension Methods
Constructors
LetterMap()
Creates an empty letter map.
public LetterMap()
LetterMap(params ReadOnlySpan<(char, T)>)
Creates a map from the supplied letter and value pairs.
public LetterMap(params ReadOnlySpan<(char, T)> items)
Parameters
itemsReadOnlySpan<(char, T)>The initial items to populate the map.
Properties
Count
Gets the number of unique letters currently mapped.
public int Count { get; }
Property Value
IsReadOnly
Gets a value indicating whether the ICollection<T> is read-only.
public bool IsReadOnly { get; }
Property Value
- bool
true if the ICollection<T> is read-only; otherwise, false.
this[char]
The value associated with the specified letter.
public T this[char key] { get; set; }
Parameters
keycharThe letter (A-Z or a-z).
Property Value
- T
Exceptions
- KeyNotFoundException
Thrown on retrieval if the letter is missing.
- ArgumentOutOfRangeException
Thrown if the character is not a Latin letter.
Keys
Gets an ICollection<T> containing the keys of the IDictionary<TKey, TValue>.
public ICollection<char> Keys { get; }
Property Value
- ICollection<char>
An ICollection<T> containing the keys of the object that implements IDictionary<TKey, TValue>.
PresenceMap
Gets a bitmask representing the current set of letters present in the map. Bit 0 corresponds to 'a', bit 25 to 'z'.
public uint PresenceMap { get; }
Property Value
Values
Gets an ICollection<T> containing the values in the IDictionary<TKey, TValue>.
public ICollection<T> Values { get; }
Property Value
- ICollection<T>
An ICollection<T> containing the values in the object that implements IDictionary<TKey, TValue>.
Methods
Add(char, T)
Adds a letter and value. Throws when the letter already exists.
public void Add(char key, T value)
Parameters
keycharvalueT
Add(KeyValuePair<char, T>)
Adds an item to the ICollection<T>.
public void Add(KeyValuePair<char, T> item)
Parameters
itemKeyValuePair<char, T>The object to add to the ICollection<T>.
Exceptions
- NotSupportedException
The ICollection<T> is read-only.
Clear()
Removes all items from the ICollection<T>.
public void Clear()
Exceptions
- NotSupportedException
The ICollection<T> is read-only.
Contains(KeyValuePair<char, T>)
Determines whether the ICollection<T> contains a specific value.
public bool Contains(KeyValuePair<char, T> item)
Parameters
itemKeyValuePair<char, T>The object to locate in the ICollection<T>.
Returns
- bool
true if
itemis found in the ICollection<T>; otherwise, false.
ContainsKey(char)
Returns whether the ASCII letter exists in the map.
public bool ContainsKey(char key)
Parameters
keychar
Returns
CopyTo(KeyValuePair<char, T>[], int)
Copies the elements of the ICollection<T> to an Array, starting at a particular Array index.
public void CopyTo(KeyValuePair<char, T>[] array, int arrayIndex)
Parameters
arrayKeyValuePair<char, T>[]The one-dimensional Array that is the destination of the elements copied from ICollection<T>. The Array must have zero-based indexing.
arrayIndexintThe zero-based index in
arrayat which copying begins.
Exceptions
- ArgumentNullException
arrayis null.- ArgumentOutOfRangeException
arrayIndexis less than 0.- ArgumentException
The number of elements in the source ICollection<T> is greater than the available space from
arrayIndexto the end of the destinationarray.
GetEnumerator()
Returns an enumerator that iterates through the collection.
public IEnumerator<KeyValuePair<char, T>> GetEnumerator()
Returns
- IEnumerator<KeyValuePair<char, T>>
An enumerator that can be used to iterate through the collection.
Remove(char)
Removes a letter from the map.
public bool Remove(char key)
Parameters
keychar
Returns
- bool
True if the letter was found and removed, otherwise false.
Remove(KeyValuePair<char, T>)
Removes the first occurrence of a specific object from the ICollection<T>.
public bool Remove(KeyValuePair<char, T> item)
Parameters
itemKeyValuePair<char, T>The object to remove from the ICollection<T>.
Returns
- bool
true if
itemwas successfully removed from the ICollection<T>; otherwise, false. This method also returns false ifitemis not found in the original ICollection<T>.
Exceptions
- NotSupportedException
The ICollection<T> is read-only.
ResetWith(params ReadOnlySpan<(char, T)>)
Replaces the map contents with the supplied items.
public void ResetWith(params ReadOnlySpan<(char, T)> items)
Parameters
itemsReadOnlySpan<(char, T)>A span of character-value pairs to initialize the map.
Remarks
If duplicate letters are provided in the input, the last occurrence will determine the final value for that letter.
TryGetValue(char, out T)
Tries to get the value for an ASCII letter. Returns false when it is missing.
public bool TryGetValue(char key, out T value)
Parameters
keycharvalueT