コード例 #1
0
ファイル: LcrsTrie.cs プロジェクト: CodeFork/resin
        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);
            }
        }
コード例 #2
0
ファイル: LcrsTrie.cs プロジェクト: mpvyard/resin
        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);
            }
        }
コード例 #3
0
ファイル: LcrsTrie.cs プロジェクト: mpvyard/resin
        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);
                }
            }
        }
コード例 #4
0
ファイル: InnerDawgNode.cs プロジェクト: bluelight1324/Eliza
        /// <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);
            }
        }
コード例 #5
0
ファイル: LcrsTrie.cs プロジェクト: gbrian/resin
        public int GetWeight()
        {
            var count = 1;

            if (LeftChild != null)
            {
                count = count + LeftChild.GetWeight();
            }

            if (RightSibling != null)
            {
                count = count + RightSibling.GetWeight();
            }

            return(count);
        }
コード例 #6
0
ファイル: LcrsTrie.cs プロジェクト: mpvyard/resin
        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);
                }
            }
        }
コード例 #7
0
ファイル: LcrsTrie.cs プロジェクト: light88/resin
        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;
                }
            }
        }
コード例 #8
0
ファイル: LcrsTrie.cs プロジェクト: gbrian/resin
        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);
            }
        }
コード例 #9
0
ファイル: LcrsTrie.cs プロジェクト: CodeFork/resin
        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);
                }
            }
        }
コード例 #10
0
ファイル: LcrsTrie.cs プロジェクト: gbrian/resin
        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);
                }
            }
        }
コード例 #11
0
ファイル: LcrsTrie.cs プロジェクト: CodeFork/resin
        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);
                }
            }
        }
コード例 #12
0
 /// <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));
 }
コード例 #13
0
ファイル: InnerDawgNode.cs プロジェクト: bluelight1324/Eliza
 /// <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(),
     });
 }
コード例 #14
0
        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);
                        }
                    }
                }
            }
        }