Beispiel #1
0
        public void Clear()
        {
            var root = PrefixTreeNode <TLetter, TWord, TKey> .MakeRoot();

            this.nodes.Clear();   // Clear the vertex collection.
            this.nodes.Add(root); // Add root element.
        }
        } // relax(...)

        T AsSingle(PrefixTreeNode <Char, String, TKey> searchResult, ref Boolean isValid)
        {
            isValid = true;
            if (searchResult.IsWord) // Perfect match.
            {
                return(this.database[searchResult.Key.Value]);
            } // if (...)
            if (searchResult.SynonymKeys.Count == 1) // Extrapolated perfect match.
            {
                return(this.database[searchResult.SynonymKeys.First()]);
            } // if (...)

            isValid = false;
            return(default(T));
        } // as_single(...)
Beispiel #3
0
        /** Adds a non-station child with specified link value and parent to the vertex collection \p global_vertex_collection. */
        public Boolean TryEmplaceChild(TLetter letter, List <PrefixTreeNode <TLetter, TWord, TKey> > nodeCollection, ref Int32 childIndex)
        {
            // Check if there is already a child with the same \c letter.
            if (this.childrenIndices.ContainsKey(letter))
            {
                return(false);
            }

            // Create a new child if not.
            var nextStop = new PrefixTreeNode <TLetter, TWord, TKey>(letter);

            childIndex = nodeCollection.Count;

            nextStop.selfIndex   = childIndex;
            nextStop.parentIndex = this.selfIndex;

            this.childrenIndices.Add(letter, childIndex); // Add the child to the current vertex.

            // Warning: invalidates this pointer.
            nodeCollection.Add(nextStop); // Store the new vertex in the railway repository.
            return(true);
        } // try_emplace_child(...)
        } // as_single(...)

        T AsSingle(PrefixTreeNode <Char, String, TKey> searchResult, Func <T, Boolean> filter, ref Boolean isValid)
        {
            if (Object.ReferenceEquals(filter, null))
            {
                return(this.AsSingle(searchResult, ref isValid));
            }

            isValid = true;
            if (searchResult.IsWord) // Perfect match.
            {
                var maybe = this.database[searchResult.Key.Value];
                if (filter(maybe))
                {
                    return(maybe);
                }
            } // if (...)
            var countSynonyms = 0;
            T   lastSynonym   = default(T);

            foreach (TKey synonymKey in searchResult.SynonymKeys)
            {
                var maybe = this.database[synonymKey];
                if (filter(maybe))
                {
                    ++countSynonyms;
                    lastSynonym = maybe;
                } // if (...)
            }     // for (...)
            if (countSynonyms == 1)
            {
                return(lastSynonym);
            }

            isValid = false;
            return(default(T));
        } // as_single(...)