internal IndexQueryTreeNode Optimize()

in src/Trinity.TSL/Trinity.TSL.CodeTemplates/LINQ/IndexQueryTreeNode.cs [38:109]


        internal IndexQueryTreeNode Optimize()
        {
            if (children != null)
            {
                List<IndexQueryTreeNode> new_child_list = new List<IndexQueryTreeNode>();
                foreach (var child in children)
                {
                    var new_child = child.Optimize();
                    if (new_child != null)
                        new_child_list.Add(new_child);
                }
                this.children = new_child_list;
            }

            if (type == NodeType.QUERY || type == NodeType.UNIVERSE || type == NodeType.EMPTY)
                return this;

            if (children.Count == 0)
                return null;

            IndexQueryTreeNode universe = null;
            IndexQueryTreeNode empty    = null;
            IndexQueryTreeNode normal   = null;

            foreach (var child in children)
            {
                if (child.type == NodeType.EMPTY)
                    empty    = child;
                else if (child.type == NodeType.UNIVERSE)
                    universe = child;
                else
                    normal   = child;
            }

            switch (type)
            {
                case NodeType.NOT:
                    /* Drop NOT queries */
                    if (children[0].type == NodeType.NOT)
                        return children[0].children[0];
                    else
                        return null;
                case NodeType.XNOR:
                case NodeType.XOR:
                    /* Not implemented. */
                    return null;
                case NodeType.AND:
                    {
                        if (empty != null)
                            return empty;
                        if (universe != null)
                            return normal;
                        if (children.Count == 1)
                            return children[0];
                        else
                            return this;
                    }
                case NodeType.OR:
                    {
                        if (empty != null)
                            return normal;
                        if (universe != null)
                            return universe;
                        if (children.Count == 1)
                            return children[0];
                        else
                            return this;
                    }
                default:
                    throw new Exception("Internal error T5011.");
            }
        }