private void DepthFirst(string traveled, IList <char> state, IList <Word> compressed) { var copy = new List <char>(state); if (Value != char.MinValue) { state.Add(Value); } if (EndOfWord) { var value = traveled + new string(state.ToArray()); var word = new Word(value, WordCount, PostingsAddress, Postings); compressed.Add(word); } if (LeftChild != null) { LeftChild.DepthFirst(traveled, state, compressed); } if (RightSibling != null) { RightSibling.DepthFirst(traveled, copy, compressed); } }
private void DepthFirst(string traveled, IList <char> state, IList <Word> words, bool surpassedLbound = false, string lbound = null, string ubound = null) { var copy = new List <char>(state); if (Value != char.MinValue) { state.Add(Value); } if (EndOfWord) { var word = traveled + new string(state.ToArray()); if ((surpassedLbound && word != lbound) || word != ubound) { words.Add(new Word(word, PostingsAddress)); } } if (LeftChild != null) { LeftChild.DepthFirst(traveled, state, words); } if (RightSibling != null) { RightSibling.DepthFirst(traveled, copy, words); } }
private void WithinEditDistanceDepthFirst( string word, string state, List <Word> words, int depth, int maxEdits, IDistanceResolver distanceResolver, bool stop = false) { var reachedMin = maxEdits == 0 || depth >= word.Length - 1 - maxEdits; var reachedDepth = depth >= word.Length - 1; var reachedMax = depth >= word.Length + maxEdits; if (!reachedMax && !stop) { string test; if (depth == state.Length) { test = state + Value; } else { test = state.ReplaceOrAppend(depth, Value); } if (reachedMin) { if (distanceResolver.IsValid(Value, depth)) { if (EndOfWord) { if (distanceResolver.GetDistance(word, test) <= maxEdits) { words.Add(new Word(test, PostingsAddress)); } } } else { stop = true; } } else { distanceResolver.Put(Value, depth); } // Go left (deep) if (LeftChild != null) { LeftChild.WithinEditDistanceDepthFirst(word, test, words, depth + 1, maxEdits, distanceResolver, stop); } // Go right (wide) if (RightSibling != null) { RightSibling.WithinEditDistanceDepthFirst(word, state, words, depth, maxEdits, distanceResolver); } } }
/// <summary> /// Gets the subtree hash code. /// </summary> /// <returns>A hash code for this tree, suitable for use in hashing algorithms and data structures like a hash table. </returns> public virtual int GetSubtreeHashCode() { unchecked { var hashCode = EqualityComparer <TKey> .Default.GetHashCode(Key); hashCode += (hashCode * 397) ^ (LeftChild != null ? LeftChild.GetSubtreeHashCode() : 0); hashCode += (hashCode * 397) ^ (RightSibling != null ? RightSibling.GetSubtreeHashCode() : 0); return(hashCode); } }
public int GetWeight() { var count = 1; if (LeftChild != null) { count = count + LeftChild.GetWeight(); } if (RightSibling != null) { count = count + RightSibling.GetWeight(); } return(count); }
public IEnumerable <LcrsTrie> AllNodesDepthFirst() { yield return(this); if (LeftChild != null) { foreach (var node in LeftChild.AllNodesDepthFirst()) { yield return(node); } } if (RightSibling != null) { foreach (var node in RightSibling.AllNodesDepthFirst()) { yield return(node); } } }
public IEnumerable<LcrsTrie> NodesPreOrder() { yield return this; if (LeftChild != null) { foreach (var node in LeftChild.NodesPreOrder()) { yield return node; } } if (RightSibling != null) { foreach (var node in RightSibling.NodesPreOrder()) { yield return node; } } }
private void DepthFirst(string traveled, IList <char> state, IList <Word> compressed) { var copy = new List <char>(state); state.Add(Value); if (EndOfWord) { compressed.Add(new Word(traveled + new string(state.ToArray()))); } if (LeftChild != null) { LeftChild.DepthFirst(traveled, state, compressed); } if (RightSibling != null) { RightSibling.DepthFirst(traveled, copy, compressed); } }
public IEnumerable <LcrsTrie> EndOfWordNodes() { if (EndOfWord) { yield return(this); } if (LeftChild != null) { foreach (var node in LeftChild.EndOfWordNodes()) { yield return(node); } } if (RightSibling != null) { foreach (var node in RightSibling.EndOfWordNodes()) { yield return(node); } } }
private void WithinEditDistanceDepthFirst(string word, string state, IList <Word> compressed, int depth, int maxEdits) { var childIndex = depth + 1; string test; if (depth == state.Length) { test = state + Value; } else { test = new string(state.ReplaceOrAppend(depth, Value).Where(c => c != Char.MinValue).ToArray()); } var edits = Levenshtein.Distance(word, test); if (edits <= maxEdits) { if (EndOfWord) { compressed.Add(new Word(test)); } } if (edits <= maxEdits || test.Length < word.Length) { if (LeftChild != null) { LeftChild.WithinEditDistanceDepthFirst(word, test, compressed, childIndex, maxEdits); } if (RightSibling != null) { RightSibling.WithinEditDistanceDepthFirst(word, test, compressed, depth, maxEdits); } } }
private void WithinEditDistanceDepthFirst(string word, string state, List <Word> compressed, int depth, int maxEdits) { string test; if (depth == state.Length) { test = state + Value; } else { test = new string(state.ReplaceOrAppend(depth, Value).ToArray()); } var edits = Levenshtein.Distance(word, test); if (edits <= maxEdits) { if (EndOfWord) { compressed.Add(new Word(test, WordCount, PostingsAddress, Postings)); } } if (edits <= maxEdits || test.Length < word.Length) { if (LeftChild != null) { LeftChild.WithinEditDistanceDepthFirst(word, test, compressed, depth + 1, maxEdits); } if (RightSibling != null) { RightSibling.WithinEditDistanceDepthFirst(word, test, compressed, depth, maxEdits); } } }
/// <summary> /// Creates a new object that is a copy of the current instance. /// </summary> /// <returns> /// A new object that is a copy of this instance. /// </returns> public override object Clone() { return(new EndLinkedDawgNode <TKey, TValue>(Key, LeftChild == null? null: (IDawgNode <TKey, TValue>)LeftChild.Clone(), RightSibling == null? null: (IDawgNode <TKey, TValue>)RightSibling.Clone(), Value)); }
/// <summary> /// Creates a new object that is a copy of the current instance. /// </summary> /// <returns> /// A new object that is a copy of this instance. /// </returns> public virtual object Clone() { return(new InnerDawgNode <TKey, TValue>() { Key = Key, LeftChild = LeftChild == null ? null : (IDawgNode <TKey, TValue>)LeftChild.Clone(), RightSibling = RightSibling == null ? null : (IDawgNode <TKey, TValue>)RightSibling.Clone(), }); }
private void EnsureFullEnough(FileStream File, int i) { BNode <T, TKey> Children = DiskRead(File, Degree, ChildrenPointers[0], ContentFactory, KeyFactory); if (Children.N < (Degree / 2 - 1)) { BNode <T, TKey> LeftSibling = DiskRead(File, Degree, ChildrenPointers[i - 1], ContentFactory, KeyFactory); int LeftSiblingN = LeftSibling.N; if (i > 0) { LeftSibling = DiskRead(File, Degree, ChildrenPointers[i - 1], ContentFactory, KeyFactory); } else { LeftSibling = null; LeftSiblingN = 0; } if (N >= (Degree / 2 - 1)) { for (int j = Children.N - 1; j >= 0; j--) { Children.Keys[j + 1] = Children.Keys[j]; } if (!Children.Leaf) { for (int j = Children.N; j >= 0; j--) { Children.ChildrenPointers[j + 1] = Children.ChildrenPointers[j]; } } Children.Keys[0] = Keys[i - 1]; Keys[i - 1] = LeftSibling.Keys[LeftSiblingN - 1]; LeftSibling.Keys[LeftSiblingN - 1] = KeyFactory.CreateNull(); if (!Children.Leaf) { Children.ChildrenPointers[0] = LeftSibling.ChildrenPointers[LeftSiblingN]; LeftSibling.ChildrenPointers[LeftSiblingN] = SizesNSpecialCharacters.NullPointer; } DiskWrite(File, 65); Children.DiskWrite(File, 65); LeftSibling.DiskWrite(File, 65); } else { BNode <T, TKey> RightSibling; int RightSiblingN; if (i < N) { RightSibling = DiskRead(File, Degree, ChildrenPointers[i + 1], ContentFactory, KeyFactory); RightSiblingN = RightSibling.N; } else { RightSibling = null; RightSiblingN = 0; } if (RightSiblingN >= (Degree / 2 - 1)) { Children.Keys[Children.N] = Keys[i]; Keys[i] = RightSibling.Keys[0]; if (!Children.Leaf) { Children.ChildrenPointers[Children.N] = RightSibling.ChildrenPointers[0]; } for (int j = 1; j < RightSiblingN; j++) { RightSibling.Keys[j - 1] = RightSibling.Keys[j]; } RightSibling.Keys[RightSiblingN - 1] = KeyFactory.CreateNull(); if (!RightSibling.Leaf) { for (int j = 1; j <= RightSiblingN; j++) { RightSibling.ChildrenPointers[j - 1] = RightSibling.ChildrenPointers[j]; } RightSibling.ChildrenPointers[RightSiblingN] = SizesNSpecialCharacters.NullPointer; } DiskWrite(File, 65); Children.DiskWrite(File, 65); RightSibling.DiskWrite(File, 65); } else { if (LeftSiblingN > 0) { for (int j = Children.N - 1; j >= 0; j--) { Children.Keys[j + (Degree / 2 - 1)] = Children.Keys[j]; } if (!Children.Leaf) { for (int j = Children.N; j >= 0; j--) { Children.ChildrenPointers[j + (Degree / 2 - 1)] = Children.ChildrenPointers[j]; } } for (int j = 0; j < LeftSiblingN; j++) { Children.Keys[j] = LeftSibling.Keys[j]; LeftSibling.Keys[j] = KeyFactory.CreateNull(); } if (!Children.Leaf) { for (int j = 0; j < LeftSiblingN; j++) { Children.ChildrenPointers[j] = LeftSibling.ChildrenPointers[j]; LeftSibling.ChildrenPointers[j] = SizesNSpecialCharacters.NullPointer; } } Children.Keys[(Degree / 2 - 1)] = Keys[i - 1]; for (int j = i; j < N; j++) { Keys[j - 1] = Keys[j]; ChildrenPointers[j - 1] = ChildrenPointers[j]; } ChildrenPointers[N - 1] = ChildrenPointers[N]; Keys[N - 1] = KeyFactory.CreateNull(); ChildrenPointers[N] = SizesNSpecialCharacters.NullPointer; DiskWrite(File, 65); Children.DiskWrite(File, 65); } else { for (int j = 0; j < RightSiblingN; j++) { Children.Keys[j + Children.N + 1] = RightSibling.Keys[j]; RightSibling.Keys[j] = KeyFactory.CreateNull(); } if (!Children.Leaf) { for (int j = 0; j <= RightSiblingN; j++) { Children.ChildrenPointers[j + Children.N + 1] = RightSibling.ChildrenPointers[j]; RightSibling.ChildrenPointers[j] = SizesNSpecialCharacters.NullPointer; } } Children.Keys[(Degree / 2 - 1) - 1] = Keys[i]; for (int j = i + 1; j < N; j++) { Keys[j - 1] = Keys[j]; ChildrenPointers[j] = ChildrenPointers[j + 1]; } Keys[N - 1] = KeyFactory.CreateNull(); ChildrenPointers[N] = SizesNSpecialCharacters.NullPointer; RightSibling.DiskWrite(File, 65); DiskWrite(File, 65); Children.DiskWrite(File, 65); } } } } }