protected virtual List <TValue> SearchDeep(string query, int position) { TrieNodeBase <TValue> nextNode = GetChildOrNull(query, position); return(nextNode != null ? nextNode.Retrieve(query, position + nextNode.KeyLength) : new List <TValue>()); }
public void Add(string key, int position, TValue value) { if (key == null) { throw new ArgumentNullException("key"); } if (EndOfString(position, key)) { AddValue(value); return; } TrieNodeBase <TValue> child = GetOrCreateChild(key[position]); child.Add(key, position + 1, value); }
/// <summary> /// if key exists, update it's values /// if not it will add a new key with this values /// </summary> /// <param name="key"></param> /// <param name="position"></param> /// <param name="values"></param> public void Update(string key, int position, TValue[] values) { if (key == null) { throw new ArgumentNullException("key"); } if (EndOfString(position, key)) { UpdateValues(values); return; } TrieNodeBase <TValue> child = GetOrCreateChild(key[position]); child.Update(key, position + 1, values); }
/// <summary> /// Remove the value belongs to a key, if key exists /// </summary> /// <param name="key"></param> /// <param name="position"></param> /// <returns>boolean indicaties if the function shall continue with deletion process</returns> public bool Remove(string key, int position) { if (key == null) { throw new ArgumentNullException("key"); } if (EndOfString(position, key)) { // check if the key is part of longer key if (BelongsToLongerKey()) { // if true remove only the value RemoveValue(); // prevent the backward removal of the key return(false); } // the key is either a unique key or contains a shorter key // return true so the function can continue deleting return(true); } // had to use stack approach since no backward approach // replace this with get or null child and check if null return false TrieNodeBase <TValue> child = GetChildOrNull(key, position); if (child is null) { return(false); } bool removeRecursively = child.Remove(key, position + 1); // check if a delete signal from the previous call if (removeRecursively) { RemoveChild(key, position); // discontinue the delete process if a subkey has a value or if it contains another key return(!HasValue() && !BelongsToLongerKey()); } return(false); }