Trie<TValue> Class
Definition
Represents a prefix tree (trie) that maps string keys to values and supports efficient prefix queries.
public sealed class Trie<TValue> : IReadOnlyCollection<KeyValuePair<string, TValue>>, IEnumerable<KeyValuePair<string, TValue>>, IEnumerable
Type Parameters
TValueThe type of the values associated with each key.
- Inheritance
-
Trie<TValue>
- Implements
- Inherited Members
- Extension Methods
Remarks
A Trie<TValue> stores keys as paths of characters, so operations cost time proportional to the length of the key rather than to the number of stored keys, and it answers prefix questions (StartsWith(string), KeysWithPrefix(string)) without scanning unrelated keys. Character transitions are keyed by the IEqualityComparer<T> supplied at construction, allowing ordinal or case-insensitive matching.
The empty string is a valid key. Enumeration order - whether through GetEnumerator(), KeysWithPrefix(string), or ItemsWithPrefix(string) - is unspecified in this version. The trie is not thread-safe for concurrent mutation.
Constructors
Trie()
Initializes a new instance of the Trie<TValue> class that is empty and uses ordinal character comparison.
public Trie()
Trie(IEnumerable<KeyValuePair<string, TValue>>, IEqualityComparer<char>?)
Initializes a new instance of the Trie<TValue> class containing the specified key/value pairs.
public Trie(IEnumerable<KeyValuePair<string, TValue>> items, IEqualityComparer<char>? charComparer = null)
Parameters
itemsIEnumerable<KeyValuePair<string, TValue>>The key/value pairs to add.
charComparerIEqualityComparer<char>The comparer used to match characters, or null to use Default.
Exceptions
- ArgumentNullException
- ArgumentException
A duplicate key is supplied.
Trie(IEqualityComparer<char>?)
Initializes a new instance of the Trie<TValue> class that is empty and uses the specified character comparer.
public Trie(IEqualityComparer<char>? charComparer)
Parameters
charComparerIEqualityComparer<char>The comparer used to match characters, or null to use Default.
Properties
Comparer
Gets the comparer used to match characters during key lookup.
public IEqualityComparer<char> Comparer { get; }
Property Value
- IEqualityComparer<char>
The character comparer supplied at construction, or the default comparer.
Count
Gets the number of keys stored in the trie.
public int Count { get; }
Property Value
- int
The number of stored keys.
this[string]
Gets or sets the value associated with the specified key.
public TValue this[string key] { get; set; }
Parameters
keystringThe key whose value is retrieved or assigned.
Property Value
- TValue
The value associated with
key.
Exceptions
- ArgumentNullException
keyis null.- KeyNotFoundException
The key does not exist when read.
Methods
Add(ReadOnlySpan<char>, TValue)
Adds the specified key and value to the trie.
public void Add(ReadOnlySpan<char> key, TValue value)
Parameters
keyReadOnlySpan<char>The key to add.
valueTValueThe value to associate with the key.
Exceptions
- ArgumentException
The key already exists.
Add(string, TValue)
Adds the specified key and value to the trie.
public void Add(string key, TValue value)
Parameters
keystringThe key to add.
valueTValueThe value to associate with the key.
Exceptions
- ArgumentNullException
keyis null.- ArgumentException
The key already exists.
Clear()
Removes all keys from the trie.
public void Clear()
ContainsKey(ReadOnlySpan<char>)
Determines whether the trie contains the specified key.
public bool ContainsKey(ReadOnlySpan<char> key)
Parameters
keyReadOnlySpan<char>The key to locate.
Returns
ContainsKey(string)
Determines whether the trie contains the specified key.
public bool ContainsKey(string key)
Parameters
keystringThe key to locate.
Returns
Exceptions
- ArgumentNullException
keyis null.
GetEnumerator()
Returns an enumerator that iterates over the key/value pairs of the trie.
public Trie<TValue>.Enumerator GetEnumerator()
Returns
- Trie<TValue>.Enumerator
A struct enumerator that lazily walks the trie's entries and fails fast on modification.
ItemsWithPrefix(string)
Returns the key/value pairs in the trie whose keys begin with the specified prefix.
public IEnumerable<KeyValuePair<string, TValue>> ItemsWithPrefix(string prefix)
Parameters
prefixstringThe prefix to match.
Returns
- IEnumerable<KeyValuePair<string, TValue>>
A lazily evaluated sequence of matching key/value pairs, in unspecified order.
Remarks
The sequence is fail-fast: it is invalidated by any structural modification of the trie, and continuing to iterate after a modification throws InvalidOperationException.
Exceptions
- ArgumentNullException
prefixis null.- InvalidOperationException
The trie was modified after enumeration began.
KeysWithPrefix(string)
Returns the keys in the trie that begin with the specified prefix.
public IEnumerable<string> KeysWithPrefix(string prefix)
Parameters
prefixstringThe prefix to match.
Returns
- IEnumerable<string>
A lazily evaluated sequence of matching keys, in unspecified order.
Remarks
The sequence is fail-fast: it is invalidated by any structural modification of the trie, and continuing to iterate after a modification throws InvalidOperationException.
Exceptions
- ArgumentNullException
prefixis null.- InvalidOperationException
The trie was modified after enumeration began.
Remove(ReadOnlySpan<char>)
Removes the specified key from the trie.
public bool Remove(ReadOnlySpan<char> key)
Parameters
keyReadOnlySpan<char>The key to remove.
Returns
Remove(string)
Removes the specified key from the trie.
public bool Remove(string key)
Parameters
keystringThe key to remove.
Returns
Exceptions
- ArgumentNullException
keyis null.
Set(string, TValue)
Adds or updates the value associated with the specified key.
public void Set(string key, TValue value)
Parameters
keystringThe key to add or update.
valueTValueThe value to associate with the key.
Exceptions
- ArgumentNullException
keyis null.
StartsWith(ReadOnlySpan<char>)
Determines whether any key in the trie begins with the specified prefix.
public bool StartsWith(ReadOnlySpan<char> prefix)
Parameters
prefixReadOnlySpan<char>The prefix to test.
Returns
StartsWith(string)
Determines whether any key in the trie begins with the specified prefix.
public bool StartsWith(string prefix)
Parameters
prefixstringThe prefix to test.
Returns
Exceptions
- ArgumentNullException
prefixis null.
TryAdd(string, TValue)
Attempts to add the specified key and value to the trie.
public bool TryAdd(string key, TValue value)
Parameters
keystringThe key to add.
valueTValueThe value to associate with the key.
Returns
Exceptions
- ArgumentNullException
keyis null.
TryGetValue(ReadOnlySpan<char>, out TValue)
Attempts to get the value associated with the specified key.
public bool TryGetValue(ReadOnlySpan<char> key, out TValue value)
Parameters
keyReadOnlySpan<char>The key to locate.
valueTValueWhen this method returns, contains the value if found; otherwise, the default value.
Returns
TryGetValue(string, out TValue)
Attempts to get the value associated with the specified key.
public bool TryGetValue(string key, out TValue value)
Parameters
keystringThe key to locate.
valueTValueWhen this method returns, contains the value if found; otherwise, the default value.
Returns
Exceptions
- ArgumentNullException
keyis null.
Explicit Interface Implementations
IEnumerable<KeyValuePair<string, TValue>>.GetEnumerator()
Returns an enumerator that iterates through the collection.
IEnumerator<KeyValuePair<string, TValue>> IEnumerable<KeyValuePair<string, TValue>>.GetEnumerator()
Returns
- IEnumerator<KeyValuePair<string, TValue>>
An enumerator that can be used to iterate through the collection.
IEnumerable.GetEnumerator()
Returns an enumerator that iterates through a collection.
IEnumerator IEnumerable.GetEnumerator()
Returns
- IEnumerator
An IEnumerator object that can be used to iterate through the collection.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |