Пример #1
0
        public void AddTest()
        {
            var array = new NodeArray <IExpression>();

            array.Add(new LiteralExpression("original-1"));
            array.Add(new LiteralExpression("original-2"));

            Assert.Equal(2, array.Length);
            Assert.Equal("original-1", array[0].Cast <LiteralExpression>().Text);
            Assert.Equal("original-2", array[1].Cast <LiteralExpression>().Text);
        }
Пример #2
0
        public void InsertTest()
        {
            var array = new NodeArray <IExpression>();

            array.Add(new LiteralExpression("original-1"));
            array.Add(new LiteralExpression("original-2"));

            array.Insert(1, new LiteralExpression("insert-1"));
            array.Insert(0, new LiteralExpression("insert-0"));

            Assert.Equal(4, array.Length);
            Assert.Equal("insert-0", array[0].Cast <LiteralExpression>().Text);
            Assert.Equal("original-1", array[1].Cast <LiteralExpression>().Text);
            Assert.Equal("insert-1", array[2].Cast <LiteralExpression>().Text);
            Assert.Equal("original-2", array[3].Cast <LiteralExpression>().Text);
        }
Пример #3
0
            public BasicBlockStub(Variable var, BasicBlock block)
            {
                prev = new ArrayList();
                next = new ArrayList();

                isEmpty = true;
                foreach (ManageVar node in var.UsersArray)
                {
                    isEmpty &= (node.Options[BasicBlock.BASIC_BLOCK_OPTION] as BasicBlock) != block;
                }

                usageArray = new NodeArray();
                if (!isEmpty)
                {
                    foreach (Node node in block.Body)
                    {
                        if (node is ManageVar)
                        {
                            ManageVar usage = node as ManageVar;
                            if (usage.Var == var)
                            {
                                usageArray.Add(usage);
                            }
                        }
                    }
                }
            }
Пример #4
0
        private bool performVariableAliasesRemoval()
        {
            bool result = false;

            foreach (Variable v in mbb.Variables)
            {
                if (v.UsersArray.Count == 1)
                {
                    Node varUseNode = v.UsersArray[0];
                    if (varUseNode is LoadVar)
                    {
                        BasicBlock block    = varUseNode.Options[BasicBlock.BASIC_BLOCK_OPTION] as BasicBlock;
                        Node       nextNode = varUseNode.Next;
                        while (nextNode is DuplicateStackTop &&
                               nextNode.Options[BasicBlock.BASIC_BLOCK_OPTION] == block)
                        {
                            nextNode = nextNode.Next;
                        }

                        if (nextNode is StoreVar &&
                            nextNode.Options[BasicBlock.BASIC_BLOCK_OPTION] == block)
                        {
                            LoadVar  ldNode = varUseNode as LoadVar;
                            StoreVar stNode = nextNode as StoreVar;
                            Variable var = ldNode.Var, alias = stNode.Var;

                            if (var != alias && var.Type.Equals(alias.Type))
                            {
                                result = true;
                                replaceNodeByPop(stNode);
                                stNode.RemoveFromGraph();

                                NodeArray aliasUsageList = new NodeArray();
                                foreach (Node node in alias.UsersArray)
                                {
                                    aliasUsageList.Add(node);
                                }

                                foreach (ManageVar node in aliasUsageList)
                                {
                                    node.Var = var;
                                }
                            }
                        }
                    }
                }
            }

            return(result);
        }
Пример #5
0
        private bool performUnnecessaryStoringRemoval()
        {
            bool result = false;

            bool            containsProtectedBlock = false;
            MethodBodyBlock mbb = Entry.Body[0] as MethodBodyBlock;

            foreach (Node node in mbb.ChildArray)
            {
                containsProtectedBlock |= node is ProtectedBlock;
            }

            if (containsProtectedBlock)
            {
                Hashtable varFlags = new Hashtable();
                foreach (BasicBlock block in blockList)
                {
                    int       i;
                    NodeArray body = block.Body;

                    if (body.Count > 0)
                    {
                        Node lastNode    = body[body.Count - 1];
                        bool initialFlag =
                            lastNode is Leave && lastNode.Parent is MethodBodyBlock;

                        foreach (Variable var in mbb.Variables)
                        {
                            varFlags[var] = initialFlag;
                        }

                        for (i = body.Count - 1; i >= 0; i--)
                        {
                            Node node = body[i];

                            if (node is LoadVar || node is StoreVar)
                            {
                                Variable var = (node as ManageVar).Var;
                                if (varIsNotReferenced(var))
                                {
                                    bool flag = (bool)(varFlags[var]);

                                    if (node is LoadVar && flag)
                                    {
                                        varFlags[var] = false;
                                    }
                                    else if (node is StoreVar && !flag)
                                    {
                                        varFlags[var] = true;
                                    }
                                    else if (node is StoreVar && flag)
                                    {
                                        result = true;
                                        replaceNodeByPop(node);
                                        node.RemoveFromGraph();
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                foreach (Variable var in mbb.Variables)
                {
                    if (varIsNotReferenced(var))
                    {
                        VarUsage  usage         = analyseVariable(var);
                        NodeArray nodesToRemove = new NodeArray();

                        foreach (ManageVar node in var.UsersArray)
                        {
                            if (node is StoreVar)
                            {
                                StoreVar storer = node as StoreVar;
                                if (!usage.IsUsed(storer))
                                {
                                    nodesToRemove.Add(storer);
                                }
                            }
                        }

                        foreach (StoreVar storer in nodesToRemove)
                        {
                            result = true;
                            replaceNodeByPop(storer);
                            storer.RemoveFromGraph();
                        }
                    }
                }
            }

            return(result);
        }
Пример #6
0
 internal void AddNode(Node node)
 {
     body.Add(node);
     node.Options[BASIC_BLOCK_OPTION] = this;
 }
Пример #7
0
        private bool performLeaveReproduction()
        {
            bool result = false;

            ArrayList leaveBlocks = new ArrayList();

            foreach (BasicBlock block in blockList)
            {
                int count = block.Body.Count;

                if (count > 0 && count <= 2)
                {
                    Node lastNode = block.Body[count - 1];
                    if (lastNode is Leave && lastNode.Parent is MethodBodyBlock)
                    {
                        leaveBlocks.Add(block);
                    }
                }
            }

            ArrayList leaveBlocksToRemove = new ArrayList();

            foreach (BasicBlock block in leaveBlocks)
            {
                Node opt = (block.Body.Count == 2) ? block.Body[0] : null;

                ArrayList prevBlocksToRemove = new ArrayList();
                foreach (BasicBlock b in block.Prev)
                {
                    if (b.Next.Count == 1 && b.Body.Count > 0)
                    {
                        NodeArray body     = b.Body;
                        Node      lastNode = body[body.Count - 1];

                        if (!(lastNode is Branch))
                        {
                            result = true;
                            prevBlocksToRemove.Add(b);

                            if (opt != null)
                            {
                                Node n = opt.Clone();
                                n.Options[BasicBlock.BASIC_BLOCK_OPTION] = b;
                                body.Add(n);
                                lastNode = lastNode.Next = n;
                            }

                            Node l = new Leave();
                            l.Options[BasicBlock.BASIC_BLOCK_OPTION] = b;
                            body.Add(l);
                            lastNode.Next = l;
                        }
                    }
                }

                foreach (BasicBlock b in prevBlocksToRemove)
                {
                    b.Next.Remove(block);
                    block.Prev.Remove(b);
                }

                if (block.Prev.Count == 0)
                {
                    leaveBlocksToRemove.Add(block);
                }
            }

            foreach (BasicBlock block in leaveBlocksToRemove)
            {
                blockList.Remove(block);
            }

            return(result);
        }
Пример #8
0
        private bool performConstantAliasesRemoval()
        {
            bool result = false;

            foreach (Variable v in mbb.Variables)
            {
                if (varIsNotReferenced(v) && v.Kind == VariableKind.Local)
                {
                    Node varUseNode = null;
                    int  count      = 0;
                    foreach (Node node in v.UsersArray)
                    {
                        if (node is StoreVar)
                        {
                            varUseNode = node;
                            count++;
                        }
                    }

                    if (count == 1)
                    {
                        BasicBlock block    = varUseNode.Options[BasicBlock.BASIC_BLOCK_OPTION] as BasicBlock;
                        Node       prevNode = varUseNode.PrevArray[0];
                        while (prevNode is DuplicateStackTop &&
                               prevNode.Options[BasicBlock.BASIC_BLOCK_OPTION] == block)
                        {
                            prevNode = prevNode.PrevArray[0];
                        }

                        if (prevNode is LoadConst &&
                            prevNode.Options[BasicBlock.BASIC_BLOCK_OPTION] == block)
                        {
                            result = true;

                            StoreVar  stNode = varUseNode as StoreVar;
                            LoadConst ldNode = prevNode as LoadConst;

                            NodeArray aliasUsageList = new NodeArray();
                            foreach (Node node in stNode.Var.UsersArray)
                            {
                                if (node is LoadVar)
                                {
                                    aliasUsageList.Add(node);
                                }
                            }

                            replaceNodeByPop(stNode);
                            stNode.RemoveFromGraph();

                            foreach (LoadVar node in aliasUsageList)
                            {
                                Node       n   = ldNode.Clone();
                                BasicBlock blk = node.Options[BasicBlock.BASIC_BLOCK_OPTION] as BasicBlock;
                                n.Options[BasicBlock.BASIC_BLOCK_OPTION] = blk;
                                blk.Body[blk.Body.IndexOf(node)]         = n;
                                node.ReplaceByNode(n);
                                n.Next = node.Next;
                                node.RemoveFromGraph();
                            }
                        }
                    }
                }
            }

            return(result);
        }
Пример #9
0
    public static void PropagateInfluence(Node node, float strength, float team = 1)
    {
        float Normalize(float x)
        {
            return(x / strength);
        }

        //var hits = Physics.OverlapSphere(node.transform.position, strength);
        NodeArray open   = new NodeArray();
        NodeArray closed = new NodeArray();
        Dictionary <Node, float> weightList = new Dictionary <Node, float>();

        open.Add(node);
        float initWeight = node.getWeight();

        node.setWeight(team);

        Dictionary <Node, int> depthMap = new Dictionary <Node, int>();

        depthMap.Add(node, 0);

        while (open.Count > 0)
        {
            var myNode = open[0];
            open.Remove(myNode);
            if (!closed.Contains(myNode))
            {
                closed.Add(myNode);
            }

            var connections = GetConnections(myNode);

            foreach (Connection connection in connections)
            {
                if (!closed.Contains(connection.to) && Vector3.Distance(connection.to.transform.position, node.transform.position) < strength)
                {
                    int depth = depthMap[myNode] + 1;
                    depthMap[connection.to] = depth;
                    closed.Add(connection.to);
                    weightList.Add(connection.to, connection.to.getWeight());
                    if (!open.Contains(connection.to))
                    {
                        open.Add(connection.to);
                    }
                    connection.to.setWeight(myNode.getWeight() * Mathf.Pow(Mathf.Abs(Normalize(strength - (depth * myNode.NodeSizeMultiplier))), .5f));
                }
            }
        }

        node.setWeight(initWeight + node.getWeight());

        foreach (var myPair in weightList)
        {
            var myNode   = myPair.Key;
            var myWeight = myPair.Value;
            myNode.setWeight(myWeight + myNode.getWeight());
        }

        foreach (var myNode in closed)
        {
            if (!myNode)
            {
                continue;
            }
            myNode.Colorize(Eval(myNode.getWeight()));
        }

        //InputManager instance = FindObjectOfType<InputManager>();
        //instance.CallColorize(closed);

        //foreach (var hit in hits)
        //{
        //    Node myNode = hit.GetComponentInParent<Node>();
        //    if (!myNode) continue;
        //    myNode.setWeight( Normalize (strength - Mathf.Clamp(Vector3.Distance(myNode.transform.position, node.transform.position),0,strength)) * team + myNode.getWeight());
        //    hit.GetComponent<MeshRenderer>().material.color = Eval(myNode.getWeight());
        //}
    }
Пример #10
0
        static void Main(string[] args)
        {
            IArray array = new NodeArray();

            Console.WriteLine(array.ToString());

            array.AddAtPosition(1, 0);
            Console.WriteLine(array.ToString());

            array.AddAtPosition(2, 0);
            Console.WriteLine(array.ToString());

            array.AddAtPosition(3, 0);
            Console.WriteLine(array.ToString());

            array.AddAtPosition(4, 1);
            Console.WriteLine(array.ToString());

            array.AddAtPosition(5, 3);
            Console.WriteLine(array.ToString());

            array.AddAtPosition(6, 4);
            Console.WriteLine(array.ToString());

            array.AddAtPosition(0, 6);
            Console.WriteLine(array.ToString());

            array.Add(1);
            array.Add(6);
            array.Add(5);
            array.Add(2);
            array.Add(4);
            array.Add(3);
            Console.WriteLine(array.ToString());

            array.Remove(10);
            Console.WriteLine(array.ToString());

            array.Remove(1);
            Console.WriteLine(array.ToString());

            array.RemoveAtPosition(6);
            Console.WriteLine(array.ToString());

            array.Clear();
            Console.WriteLine(array.ToString());

            array.Add(1);
            array.Add(2);
            array.Add(3);
            array.Add(4);
            array.Add(5);
            Console.WriteLine(array.ToString());

            array.Reverse();
            Console.WriteLine(array.ToString());
        }