Exemplo n.º 1
0
        public bool Add(string[] stack, object accepts)
        {
            if (IsBaked)
            {
                throw new Exception("Cannot mould a baked tree");
            }

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

            ObjectTree parent = this;

            int counter = 0;

            do
            {
                ObjectTree i = parent.MatchKey(stack[counter++]);
                if (null == i)
                {
                    return(AddStackToObjectTree(stack, null, parent, --counter, accepts));
                }
                if (null == i.child && counter < stack.Length)
                {
                    return(AddStackToObjectTree(stack, i, null, counter, accepts));
                }
                if (counter == stack.Length)
                {
                    return(AddStackToObjectTree(stack, i, null, counter, accepts));
                }
                parent = i.child;
            }while (true);
        }
Exemplo n.º 2
0
 ObjectTree(string key)
 {
     this.key = key;
     accept   = null;
     child    = null;
     next     = null;
 }
Exemplo n.º 3
0
        public string[][] AcceptFirstStates(string[] stack)
        {
            ObjectTree pnode = GetObjectTreeAt(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)
            {
                string   r      = pnode.key;
                string[] result = new string[stack.Length + 1];
                Array.Copy(stack, result, stack.Length);
                result[stack.Length] = r;
                allres.Add(result);
                pnode = pnode.next;
            }

            return(allres.ToArray(typeof(string[])) as string[][]);
        }
Exemplo n.º 4
0
        bool AddStackToObjectTree(string[] stack, ObjectTree parent,
                                  ObjectTree prev, int counter, object accepts)
        {
            do
            {
                if (counter == stack.Length)
                {
                    parent.accept = accepts;
                    return(true);
                }
                else
                {
                    ObjectTree pnode = new ObjectTree(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);
        }
Exemplo n.º 5
0
        ObjectTree GetObjectTreeAt(string[] stack)
        {
            if (stack == null)
            {
                return(null);
            }

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

            ObjectTree parent  = this;
            int        counter = 0;

            do
            {
                ObjectTree 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);
        }
Exemplo n.º 6
0
        void MatchWild(string[] stack, string[] stacktemp, ArrayList results)
        {
            ObjectTree pn = null;
            int        i  = 0;

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

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

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

            if (substack.Length > 0)
            {
                pn = GetObjectTreeAt(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] = null;
            }
            else
            {
                pn = GetObjectTreeAt(stacktemp);
                if (pn != null)
                {
                    if (pn.IsAccepting != false)
                    {
                        results.Add(stack);
                    }
                }
            }
        }
Exemplo n.º 7
0
        public object Accepts(string[] stack)
        {
            ObjectTree i = GetObjectTreeAt(stack);

            if (null != i)
            {
                return(i.accept);
            }
            return(null);
        }
Exemplo n.º 8
0
        ObjectTree GetLastNext()
        {
            ObjectTree last = this;

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

            return(last);
        }
Exemplo n.º 9
0
        public string[][] CompleteStates(string[] stack)
        {
            string[] substack = new string[stack.Length - 1];
            Array.Copy(stack, substack, substack.Length);
            ObjectTree pnode = GetObjectTreeAt(substack);

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

            Set allres = new Set();

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

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

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

                foreach (string[] r in results)
                {
                    if (r.Length > 0 && r[0].ToLower().StartsWith(stack[stack.Length - 1].ToLower()))
                    {
                        allres.Add(r);
                    }
                }

                pnode = pnode.next;
            }

            return(allres.ToArray(typeof(string[])) as string[][]);
        }
Exemplo n.º 10
0
        ObjectTree MatchKey(string key)
        {
            ObjectTree currentnode = this;

            while (currentnode != null)
            {
                if (currentnode.key != null && key != null)
                {
                    if (currentnode.key.ToLower() == key.ToLower())
                    {
                        return(currentnode);
                    }
                }
                currentnode = currentnode.next;
            }
            return(null);
        }
Exemplo n.º 11
0
        public string[][] AcceptStates(string[] stack)
        {
            ObjectTree pnode = GetObjectTreeAt(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;

                string[]   buffer  = new string[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[][]);
        }
Exemplo n.º 12
0
        public string[] CompleteFirstStates(string[] stack)
        {
            string[] substack = new string[stack.Length - 1];
            Array.Copy(stack, substack, substack.Length);
            ObjectTree pnode = GetObjectTreeAt(substack);

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

            Set allres = new Set();

            while (pnode != null)
            {
                string r = pnode.key;

                if (r != null && r.Length > 0 && r.ToLower().StartsWith(stack[stack.Length - 1].ToLower()))
                {
                    allres.Add(r);
                }

                pnode = pnode.next;
            }

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