/// <summary> /// Traverse the trie, computing indexes. /// </summary> /// <param name="n"> </param> /// <param name="data"> </param> private bool IndexWalker(TrieNode n, object data) { if (n.Parent != null) { this[n.Byte].Add(new WeakReference(n)); } return true; }
/// <summary> /// Finds a node in the given sub-tree. /// </summary> /// <param name="key">The key to search on, where key[0] corresponds to a child of startAt.</param> /// <param name="startAt">The node to search under</param> /// <param name="create">Create nodes that don't exist, while searching.</param> /// <returns>The node if found. If the node doesn't exist and create is true, the node created; otherwise null.</returns> protected virtual TrieNode FindNode(byte[] key, TrieNode startAt, bool create) { TrieNode current = startAt; byte b; for (int i=0; (i<key.Length) && (current != null); i++) { b = key[i]; current = current[b, create]; } return current; }
/// <summary> /// Copy the keys from the sub-tree into an ArrayList. /// </summary> /// <param name="n"> </param> /// <param name="data"> </param> /// <param name="key"> </param> private bool CopyWalker(TrieNode n, object data, ByteStack key) { if (n.Value != null) { ArrayList al = (ArrayList) data; al.Add((byte[]) key); if (al.Count >= m_maxResults) { return false; } } return true; }
[Test] public void Test_Main() { System.Text.Encoding ENC = System.Text.Encoding.Default; TrieNode n = new TrieNode(null, 0); byte[] key = ENC.GetBytes("test"); TrieNode current = n; for (int i=0; i<key.Length; i++) { byte b = key[i]; current = current[b, true]; } current.Value = "foo"; Assert.AreEqual(ENC.GetString(key), ENC.GetString(current.Key)); }
public bool MoveNext() { if (m_pos.Count <= 0) { return false; } m_current = (TrieNode) m_pos.Pop(); foreach (TrieNode e in m_current) { m_pos.Push(e); } if (m_current.Value != null) { return true; } return MoveNext(); }
/// <summary> /// Deletes all nodes. /// </summary> public void Clear() { m_root = new TrieNode(null, 0); m_count = 0; }
/// <summary> /// Find all of the keys. /// </summary> /// <param name="n"> </param> /// <param name="data"> </param> /// <param name="key"> </param> private bool KeyWalker(TrieNode n, object data, ByteStack key) { if (n.Value != null) { ArrayList al = (ArrayList) data; al.Add((byte[]) key); } return true; }
/// <summary> /// Perform the given function on every element of the trie. Perl's map() operator. /// </summary> /// <param name="w">The function to call</param> /// <param name="data">Extra data to pass along to the function.</param> /// <param name="current">What node are we currently on?</param> protected void Traverse(TrieWalker w, object data, TrieNode current) { if (! w(current, data)) { return; } foreach (TrieNode e in current) { Traverse(w, data, e); } }
/// <summary> /// Perform the given function on every element of the trie. Perl's map() operator. /// </summary> /// <param name="w">The function to call</param> /// <param name="data">Extra data to pass along to the function.</param> /// <param name="current">What node are we currently on?</param> /// <param name="key">A stack holding the current key value</param> protected void Traverse(TrieKeyWalker w, object data, TrieNode current, ByteStack key) { if (!w(current, data, key)) { return; } foreach (TrieNode e in current) { key.Push(e.Byte); Traverse(w, data, e, key); key.Pop(); } }
/// <summary> /// Adds a child to this node. /// </summary> /// <param name="key">The key for the child.</param> /// <returns>The child noded added to this node.</returns> public virtual TrieNode Add(byte key) { TrieNode e = new TrieNode(this, key); this[key] = e; return e; }
/// <summary> /// Create a new node /// </summary> /// <param name="parent">The parent of the new node</param> /// <param name="key">The byte for this node</param> public TrieNode(TrieNode parent, byte key) { m_parent = parent; m_key = key; }
public TrieNodeEnumerator(TrieNode n) { m_node = n; Reset(); }