/// <summary> /// Gets the node value associated with the specified path. /// </summary> /// <param name="enumerable">An enumerable object that specifies the path of the node to get.</param> /// <param name="value">When this method returns, contains the node value associated with the specified path, if the key is found; otherwise, the default value for the type of the value parameter. This parameter is passed uninitialized.</param> /// <returns>True if the tree contains the specified node; otherwise, false.</returns> public bool TryGetValue(IEnumerable <TKey> enumerable, out TValue value) { PrefixTreeNode <TKey, TValue> currentNode = this.GetMatchedNode(enumerable); bool hasValue = currentNode != null && currentNode.HasValue; value = hasValue ? currentNode.Value : default(TValue); return(hasValue); }
/// <summary> /// Sets the node value associated with the specified path if that node doesn't exist. /// </summary> /// <param name="enumerable">An enumerable object that specifies the path of the node to set.</param> /// <param name="value">The value of the node to set.</param> /// <returns>True if the set operation is success; otherwise, false. This method returns false if the specified node is already existed in the tree.</returns> public bool TrySetValue(IEnumerable <TKey> enumerable, TValue value) { PrefixTreeNode <TKey, TValue> currentNode = this.GetMatchedNode(enumerable, true); if (currentNode.HasValue) { return(false); } currentNode.Value = value; return(true); }
private PrefixTreeNode <TKey, TValue> GetMatchedNode(IEnumerable <TKey> enumerable, bool createNew) { PrefixTreeNode <TKey, TValue> currentNode = null; foreach (KeyValuePair <TKey, PrefixTreeNodeCollection <TKey, TValue> > pair in this.PrefixNodePath(enumerable)) { if (!pair.Value.ContainsKey(pair.Key)) { if (!createNew) { return(null); } pair.Value[pair.Key] = new PrefixTreeNode <TKey, TValue>(); } currentNode = pair.Value[pair.Key]; } return(currentNode); }
/// <summary> /// Applies the longest prefix match algorithm to the given enumerable object. /// </summary> /// <param name="enumerable">An enumerable to find a longest matching in the prefix tree.</param> /// <returns>A <see cref="PrefixMatch<TKey, TValue>"/> that represents the result of the longest prefix matching.</returns> public PrefixMatch <TKey, TValue> LongestPrefixMatch(IEnumerable <TKey> enumerable) { PrefixTreeNode <TKey, TValue> longestNode = null; int currentLength = 1, longestLength = 0; foreach (KeyValuePair <TKey, PrefixTreeNodeCollection <TKey, TValue> > pair in this.PrefixNodePath(enumerable)) { if (!pair.Value.ContainsKey(pair.Key)) { break; } if (pair.Value[pair.Key].HasValue) { longestNode = pair.Value[pair.Key]; longestLength = currentLength; } currentLength++; } return(new PrefixMatch <TKey, TValue>(longestNode, longestLength)); }
/// <summary> /// Determines whether the tree contains the specified node. /// </summary> /// <param name="enumerable">An enumerable object that specifies the path of the node.</param> /// <returns>True if the tree contains the specified node; otherwise, false.</returns> public bool HasValue(IEnumerable <TKey> enumerable) { PrefixTreeNode <TKey, TValue> currentNode = this.GetMatchedNode(enumerable); return(currentNode != null && currentNode.HasValue); }
public override bool TryGetValue(TKey key, out PrefixTreeNode <TKey, TValue> value) { return(this.collection.TryGetValue(key, out value)); }
public override void Add(TKey key, PrefixTreeNode <TKey, TValue> value) { this.collection.Add(key, value); }
internal PrefixMatch(PrefixTreeNode <TKey, TValue> node, int length) { this.node = node; this.length = length; }
/// <summary> /// Sets the node value associated with the specified path. /// </summary> /// <param name="enumerable">An enumerable object that specifies the path of the node to set.</param> /// <param name="value">The value of the node to set.</param> public void SetValue(IEnumerable <TKey> enumerable, TValue value) { PrefixTreeNode <TKey, TValue> currentNode = this.GetMatchedNode(enumerable, true); currentNode.Value = value; }
/// <summary> /// Gets the node value associated with the specified path. /// </summary> /// <param name="enumerable">An enumerable object that specifies the path of the node to get.</param> /// <returns>The node value associated with the specified path</returns> public TValue GetValue(IEnumerable <TKey> enumerable) { PrefixTreeNode <TKey, TValue> currentNode = this.GetMatchedNode(enumerable); return(currentNode.Value); }