Exemple #1
0
 StringTree(char key)
 {
     this.key = key;
     accept   = null;
     child    = null;
     next     = null;
 }
Exemple #2
0
        bool Add(char[] stack, object accepts)
        {
            if (IsBaked)
            {
                throw new Exception("Cannot mould a baked tree");
            }

            if (stack.Length == 0)
            {
                return(false);
            }

            StringTree parent = this;

            int counter = 0;

            do
            {
                StringTree i = parent.MatchKey(stack[counter++]);
                if (null == i)
                {
                    return(AddStackToStringTree(stack, null, parent, --counter, accepts));
                }
                if (null == i.child && counter < stack.Length)
                {
                    return(AddStackToStringTree(stack, i, null, counter, accepts));
                }
                if (counter == stack.Length)
                {
                    return(AddStackToStringTree(stack, i, null, counter, accepts));
                }
                parent = i.child;
            }while (true);
        }
Exemple #3
0
        bool AddStackToStringTree(char[] stack, StringTree parent,
                                  StringTree prev, int counter, object accepts)
        {
            do
            {
                if (counter == stack.Length)
                {
                    parent.accept = accepts;
                    return(true);
                }
                else
                {
                    StringTree pnode = new StringTree(stack[counter]);

                    if (null != parent)
                    {
                        parent.child = pnode;
                    }
                    if (null != prev)
                    {
                        prev.GetLastNext().next = pnode;
                        prev = null;
                    }
                    parent = pnode;
                }
            }while (counter++ < stack.Length);
            return(true);
        }
Exemple #4
0
        StringTree GetStringTreeAt(char[] stack)
        {
            if (stack == null)
            {
                return(null);
            }

            if (stack.Length == 0)
            {
                return(this);
            }

            StringTree parent  = this;
            int        counter = 0;

            do
            {
                StringTree i = parent.MatchKey(stack[counter++]);
                if (null == i)
                {
                    return(null);
                }
                if (null == i.child && counter < stack.Length)
                {
                    return(null);
                }
                if (counter == stack.Length)
                {
                    return(i);
                }
                parent = i.child;
            }while (true);
        }
Exemple #5
0
        void MatchWild(char[] stack, char[] stacktemp, ArrayList results)
        {
            StringTree pn = null;
            int        i  = 0;

            for (; i < stacktemp.Length; i++)
            {
                if (stacktemp[i] == '?')
                {
                    break;
                }
            }

            char[] substack = new char[stacktemp.Length - i];

            for (int j = 0; j < substack.Length; j++)
            {
                substack[j] = stacktemp[i + j];
            }

            if (substack.Length > 0)
            {
                pn = GetStringTreeAt(stacktemp);

                if (pn != null)
                {
                    pn = pn.child;
                }
                else if (i == 0)
                {
                    pn = this;
                }

                while (pn != null)
                {
                    substack[0] = pn.key;
                    //this one
                    stack[i] = pn.key;

                    pn.MatchWild(stack, substack, results);

                    pn = pn.next;
                }
                substack[0] = '\0';
            }
            else
            {
                pn = GetStringTreeAt(stacktemp);
                if (pn != null)
                {
                    if (pn.IsAccepting != false)
                    {
                        results.Add(stack);
                    }
                }
            }
        }
Exemple #6
0
        object Accepts(char[] stack)
        {
            StringTree i = GetStringTreeAt(stack);

            if (null != i)
            {
                return(i.accept);
            }
            return(null);
        }
Exemple #7
0
        StringTree GetLastNext()
        {
            StringTree last = this;

            while (last.next != null)
            {
                last = last.next;
            }

            return(last);
        }
Exemple #8
0
        StringTree MatchKey(char key)
        {
            StringTree currentnode = this;

            while (currentnode != null)
            {
                if (currentnode.key == key)
                {
                    return(currentnode);
                }
                currentnode = currentnode.next;
            }
            return(null);
        }
Exemple #9
0
        string[] AcceptStates(char[] stack)
        {
            StringTree pnode = GetStringTreeAt(stack);

            if (stack == null || stack.Length == 0)
            {
                pnode = this;
            }
            else
            {
                if (pnode == null || pnode.child == null)
                {
                    return(new string[0]);
                }
                else
                {
                    pnode = pnode.child;
                }
            }

            ArrayList allres = new ArrayList();

            while (pnode != null)
            {
                int index = pnode.AcceptStateCount;
                int size  = index;

                char[]   buffer  = new char[pnode.Depth(2)];
                string[] results = new string[size];

                pnode.MatchItem(buffer, ref index, 0, size, results);

                allres.AddRange(results);

                pnode = pnode.next;
            }

            return(allres.ToArray(typeof(string)) as string[]);
        }