Ejemplo n.º 1
0
 internal void CloneItems(BuilderElements builders)
 {
     foreach (GrammarBuilderBase item in builders.Items)
     {
         _items.Add(item);
     }
 }
Ejemplo n.º 2
0
        public override bool Equals(object obj)
        {
            BuilderElements refObj = obj as BuilderElements;

            if (refObj == null)
            {
                return(false);
            }

            // Easy out if the number of elements do not match
            if (refObj.Count != Count || refObj.Items.Count != Items.Count)
            {
                return(false);
            }

            // Deep recursive search for equality
            for (int i = 0; i < Items.Count; i++)
            {
                if (!Items[i].Equals(refObj.Items[i]))
                {
                    return(false);
                }
            }
            return(true);
        }
Ejemplo n.º 3
0
 private void RemoveDictionaryElements(SortedDictionary <int, Collection <BuilderElements> > dict)
 {
     foreach (GrammarBuilderBase item in Items)
     {
         BuilderElements builderElements = item as BuilderElements;
         if (builderElements != null)
         {
             builderElements.RemoveDictionaryElements(dict);
             dict[builderElements.Count].Remove(builderElements);
         }
     }
 }
Ejemplo n.º 4
0
        internal override int CalcCount(BuilderElements parent)
        {
            base.CalcCount(parent);
            int num = 1;

            foreach (GrammarBuilderBase item in Items)
            {
                num += item.CalcCount(this);
            }
            Count = num;
            return(num);
        }
Ejemplo n.º 5
0
        internal override int CalcCount(BuilderElements parent)
        {
            base.CalcCount(parent);
            int c = 1;

            foreach (GrammarBuilderBase item in Items)
            {
                c += item.CalcCount(this);
            }
            Count = c;

            return(c);
        }
Ejemplo n.º 6
0
        protected void Optimize(Collection <RuleElement> newRules)
        {
            SortedDictionary <int, Collection <BuilderElements> > sortedDictionary = new SortedDictionary <int, Collection <BuilderElements> >();

            GetDictionaryElements(sortedDictionary);
            int[] array = new int[sortedDictionary.Keys.Count];
            int   num   = array.Length - 1;

            foreach (int key in sortedDictionary.Keys)
            {
                array[num--] = key;
            }
            for (int i = 0; i < array.Length && array[i] >= 3; i++)
            {
                Collection <BuilderElements> collection = sortedDictionary[array[i]];
                for (int j = 0; j < collection.Count; j++)
                {
                    RuleElement    ruleElement    = null;
                    RuleRefElement ruleRefElement = null;
                    for (int k = j + 1; k < collection.Count; k++)
                    {
                        if (collection[j] == null || !collection[j].Equals(collection[k]))
                        {
                            continue;
                        }
                        BuilderElements builderElements = collection[k];
                        BuilderElements parent          = builderElements.Parent;
                        if (builderElements is SemanticKeyElement)
                        {
                            parent.Items[parent.Items.IndexOf(builderElements)] = collection[j];
                        }
                        else
                        {
                            if (ruleElement == null)
                            {
                                ruleElement = new RuleElement(builderElements, "_");
                                newRules.Add(ruleElement);
                            }
                            if (ruleRefElement == null)
                            {
                                ruleRefElement = new RuleRefElement(ruleElement);
                                collection[j].Parent.Items[collection[j].Parent.Items.IndexOf(collection[j])] = ruleRefElement;
                            }
                            parent.Items[builderElements.Parent.Items.IndexOf(builderElements)] = ruleRefElement;
                        }
                        builderElements.RemoveDictionaryElements(sortedDictionary);
                        collection[k] = null;
                    }
                }
            }
        }
Ejemplo n.º 7
0
 private void GetDictionaryElements(SortedDictionary <int, Collection <BuilderElements> > dict)
 {
     foreach (GrammarBuilderBase item in Items)
     {
         BuilderElements builderElements = item as BuilderElements;
         if (builderElements != null)
         {
             if (!dict.ContainsKey(builderElements.Count))
             {
                 dict.Add(builderElements.Count, new Collection <BuilderElements>());
             }
             dict[builderElements.Count].Add(builderElements);
             builderElements.GetDictionaryElements(dict);
         }
     }
 }
Ejemplo n.º 8
0
        private void RemoveDictionaryElements(SortedDictionary <int, Collection <BuilderElements> > dict)
        {
            // Recursive search from a matching subtree
            foreach (GrammarBuilderBase item in Items)
            {
                BuilderElements current = item as BuilderElements;

                // Go deeper if the number of children is greater the element to compare against.
                if (current != null)
                {
                    // Recursively remove all elements
                    current.RemoveDictionaryElements(dict);

                    dict[current.Count].Remove(current);
                }
            }
        }
Ejemplo n.º 9
0
        private void GetDictionaryElements(SortedDictionary <int, Collection <BuilderElements> > dict)
        {
            // Recursive search from a matching subtree
            foreach (GrammarBuilderBase item in Items)
            {
                BuilderElements current = item as BuilderElements;

                // Go deeper if the number of children is greater the element to compare against.
                if (current != null)
                {
                    if (!dict.ContainsKey(current.Count))
                    {
                        dict.Add(current.Count, new Collection <BuilderElements>());
                    }
                    dict[current.Count].Add(current);

                    current.GetDictionaryElements(dict);
                }
            }
        }
Ejemplo n.º 10
0
        public override bool Equals(object obj)
        {
            BuilderElements builderElements = obj as BuilderElements;

            if (builderElements == null)
            {
                return(false);
            }
            if (builderElements.Count != Count || builderElements.Items.Count != Items.Count)
            {
                return(false);
            }
            for (int i = 0; i < Items.Count; i++)
            {
                if (!Items[i].Equals(builderElements.Items[i]))
                {
                    return(false);
                }
            }
            return(true);
        }
Ejemplo n.º 11
0
 internal virtual int CalcCount(BuilderElements parent)
 {
     Marked = false;
     Parent = parent;
     return(Count);
 }
Ejemplo n.º 12
0
        /// <summary>
        /// Optimization for a element tree
        /// </summary>
        protected void Optimize(Collection <RuleElement> newRules)
        {
            // Create an dictionary of [Count of elements, list of elements]
            SortedDictionary <int, Collection <BuilderElements> > dict = new();

            GetDictionaryElements(dict);

            // The dictionary is sorted from the smallest buckets to the largest.
            // Revert the order in the keys arrays
            int[] keys = new int[dict.Keys.Count];

            int index = keys.Length - 1;

            foreach (int key in dict.Keys)
            {
                keys[index--] = key;
            }

            // Look for each bucket from the largest to the smallest
            for (int i = 0; i < keys.Length && keys[i] >= 3; i++)
            {
                Collection <BuilderElements> gb = dict[keys[i]];
                for (int j = 0; j < gb.Count; j++)
                {
                    RuleElement    newRule = null;
                    RuleRefElement ruleRef = null;
                    for (int k = j + 1; k < gb.Count; k++)
                    {
                        if (gb[j] != null && gb[j].Equals(gb[k]))
                        {
                            BuilderElements current = gb[k];
                            BuilderElements parent  = current.Parent;
                            if (current is SemanticKeyElement)
                            // if current is already a ruleref. There is no need to create a new one
                            {
                                // Simply set the ruleref of the current element to the ruleref of the org element.
                                parent.Items[parent.Items.IndexOf(current)] = gb[j];
                            }
                            else
                            {
                                // Create a rule to store the common elements
                                if (newRule == null)
                                {
                                    newRule = new RuleElement(current, "_");
                                    newRules.Add(newRule);
                                }

                                // Create a ruleref and attach the
                                if (ruleRef == null)
                                {
                                    ruleRef = new RuleRefElement(newRule);
                                    gb[j].Parent.Items[gb[j].Parent.Items.IndexOf(gb[j])] = ruleRef;
                                }
                                parent.Items[current.Parent.Items.IndexOf(current)] = ruleRef;
                            }
                            //
                            current.RemoveDictionaryElements(dict);
                            gb[k] = null;
                        }
                    }
                }
            }
        }
Ejemplo n.º 13
0
 internal override int CalcCount(BuilderElements parent)
 {
     _rule = null;
     return(base.CalcCount(parent));
 }
Ejemplo n.º 14
0
 internal override int CalcCount(BuilderElements parent)
 {
     // clear any existing value
     _rule = null;
     return(base.CalcCount(parent));
 }