Class Mapper
Maps case insensitive names to stable indexes. Create a new mapper when the key set changes. Use GetKey(int) or GetSameKey(string) when reference comparison is useful.
public abstract class Mapper : IReadOnlyDictionary<string, int>, IReadOnlyCollection<KeyValuePair<string, int>>, IEnumerable<KeyValuePair<string, int>>, IEnumerable, IDisposable
- Inheritance
-
Mapper
- Implements
- Inherited Members
- Extension Methods
Constructors
Mapper(string[])
Maps case insensitive names to stable indexes. Create a new mapper when the key set changes. Use GetKey(int) or GetSameKey(string) when reference comparison is useful.
protected Mapper(string[] Keys)
Parameters
Keysstring[]
Fields
EmptyMapper
Instance of a Mapper without any keys.
public static readonly Mapper EmptyMapper
Field Value
_keys
The names available to a custom mapper.
protected string[] _keys
Field Value
- string[]
Properties
Count
Gets the number of unique keys defined in this schema.
public int Count { get; }
Property Value
this[ReadOnlySpan<char>]
Returns the stable index associated with the provided key.
public int this[ReadOnlySpan<char> key] { get; }
Parameters
keyReadOnlySpan<char>The case-insensitive key to locate.
Property Value
- int
The zero-based index of the key if it exists, otherwise -1.
this[string]
Returns the stable index associated with the provided key.
public int this[string key] { get; }
Parameters
keystringThe case-insensitive key to locate.
Property Value
- int
The zero-based index of the key if it exists, otherwise -1.
Keys
Gets the unique keys held by this mapper as a ReadOnlySpan<T>.
public ReadOnlySpan<string> Keys { get; }
Property Value
Values
Gets an enumerable collection that contains the values in the read-only dictionary.
public IEnumerable<int> Values { get; }
Property Value
- IEnumerable<int>
An enumerable collection that contains the values in the read-only dictionary.
Methods
ContainsKey(ReadOnlySpan<char>)
public bool ContainsKey(ReadOnlySpan<char> key)
Parameters
keyReadOnlySpan<char>
Returns
ContainsKey(string)
Determines whether the read-only dictionary contains an element that has the specified key.
public bool ContainsKey(string key)
Parameters
keystringThe key to locate.
Returns
- bool
true if the read-only dictionary contains an element that has the specified key; otherwise, false.
Exceptions
- ArgumentNullException
keyis null.
Dispose()
Releases resources owned by this mapper. This method can be called more than once.
public virtual void Dispose()
DisposeUnmanaged()
Override this method to release resources owned by a custom mapper.
protected abstract void DisposeUnmanaged()
~Mapper()
[ExcludeFromCodeCoverage]
protected ~Mapper()
GetEmptyMapper()
Provides a shared schema for instances requiring zero keys.
public static Mapper GetEmptyMapper()
Returns
GetEnumerator()
Returns an enumerator that iterates through the collection.
public IEnumerator<KeyValuePair<string, int>> GetEnumerator()
Returns
- IEnumerator<KeyValuePair<string, int>>
An enumerator that can be used to iterate through the collection.
GetIndex(char, string)
Finds the index after adding one character before value.
public int GetIndex(char prependChar, string value)
Parameters
Returns
GetIndex(ReadOnlySpan<char>)
Returns the stable index associated with the provided key.
public abstract int GetIndex(ReadOnlySpan<char> key)
Parameters
keyReadOnlySpan<char>The case-insensitive key to locate.
Returns
- int
The zero-based index of the key if it exists, otherwise -1.
GetIndex(string)
Returns the stable index associated with the provided key.
public abstract int GetIndex(string key)
Parameters
keystringThe case-insensitive key to locate.
Returns
- int
The zero-based index of the key if it exists, otherwise -1.
GetKey(int)
Gets the stored name at the given index.
public string GetKey(int ind)
Parameters
indintThe index to read.
Returns
- string
The stored string instance.
GetMapper(IEnumerable<string>)
Creates a mapper for the supplied names.
public static Mapper GetMapper(IEnumerable<string> keys)
Parameters
keysIEnumerable<string>The collection of keys to index.
Returns
- Mapper
A mapper that preserves the first occurrence of each name.
Remarks
Input order is preserved. If duplicate keys (case-insensitive) are present, only the first occurrence is stored, and its position becomes the fixed index for that key.
GetMapper(params Span<string>)
Creates a mapper for the supplied names.
public static Mapper GetMapper(params Span<string> keys)
Parameters
Returns
- Mapper
A mapper that preserves the first occurrence of each name.
Remarks
Input order is preserved. If duplicate keys (case-insensitive) are present, only the first occurrence is stored, and its position becomes the fixed index for that key.
GetOneKeyMapper(string)
Creates a mapper containing one name at index zero.
public static Mapper GetOneKeyMapper(string key)
Parameters
keystringThe sole key in the schema.
Returns
- Mapper
A mapper containing the supplied name.
GetSameKey(ReadOnlySpan<char>)
Gets the stored string instance that matches the name.
public string GetSameKey(ReadOnlySpan<char> key)
Parameters
keyReadOnlySpan<char>The name to find.
Returns
GetSameKey(string)
Gets the stored string instance that matches the name.
public string GetSameKey(string key)
Parameters
keystringThe name to find.
Returns
GetTwoKeyMapper(string, string)
Creates a mapper containing up to two names.
public static Mapper GetTwoKeyMapper(string key1, string key2)
Parameters
Returns
- Mapper
A mapper containing the distinct supplied names.
Remarks
If the keys are identical (case-insensitive), this returns a single-key mapper to ensure the index remains stable at 0.
IsAllAscii(string[])
Checks if all strings in the array contain only ASCII characters.
public static bool IsAllAscii(string[] keys)
Parameters
keysstring[]
Returns
TryGetValue(ReadOnlySpan<char>, out int)
public bool TryGetValue(ReadOnlySpan<char> key, out int value)
Parameters
keyReadOnlySpan<char>valueint
Returns
TryGetValue(string, out int)
Gets the value that is associated with the specified key.
public bool TryGetValue(string key, out int value)
Parameters
keystringThe key to locate.
valueintWhen this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the type of the
valueparameter. This parameter is passed uninitialized.
Returns
- bool
true if the object that implements the IReadOnlyDictionary<TKey, TValue> interface contains an element that has the specified key; otherwise, false.
Exceptions
- ArgumentNullException
keyis null.