Example #1
0
 public override void Apply(ITreeVisitor tp)
 {
     foreach (Tree tree in this)
     {
         tp.VisitTree(tree);
     }
 }
Example #2
0
        /* applies a TreeVisitor to all projections (including the node itself) of a node in a Tree.
         *  Does nothing if head is not in root.
         * @return the maximal projection of head in root.
         */
        public static Tree ApplyToProjections(ITreeVisitor v, Tree head, Tree root, IHeadFinder hf)
        {
            Tree projection = head;
            Tree parent     = projection.Parent(root);

            if (parent == null && projection != root)
            {
                return(null);
            }
            v.VisitTree(projection);
            if (projection == root)
            {
                return(root);
            }
            while (hf.DetermineHead(parent) == projection)
            {
                projection = parent;
                v.VisitTree(projection);
                if (projection == root)
                {
                    return(root);
                }
                parent = projection.Parent(root);
            }
            return(projection);
        }
Example #3
0
 /// <summary>Apply the TreeVisitor tp to all trees in the Treebank.</summary>
 /// <param name="tp">A class that implements the TreeVisitor interface</param>
 public override void Apply(ITreeVisitor tp)
 {
     foreach (Tree parseTree in parseTrees)
     {
         tp.VisitTree(parseTree);
     }
 }
Example #4
0
        public void Traverse(ITreeVisitor <T> visitor)
        {
            Left?.Traverse(visitor);

            visitor.Visit(this);

            Right?.Traverse(visitor);
        }
Example #5
0
 void VisitSingleThread(ITreeVisitor treeVisitor, ITrieNodeResolver trieNodeResolver, TrieVisitContext visitContext)
 {
     // single threaded route
     for (int i = 0; i < BranchesCount; i++)
     {
         VisitChild(i, GetChild(trieNodeResolver, i), trieNodeResolver, treeVisitor, visitContext);
     }
 }
Example #6
0
        /// <summary>
        /// Dispatches the current tree instance to the tree visitor's Visit method.
        /// </summary>
        /// <param name="visitor">Visitor to accept the tree.</param>
        /// <returns>Result of the visit.</returns>
        ITree ITree.Accept(ITreeVisitor visitor)
        {
            if (visitor == null)
            {
                throw new ArgumentNullException(nameof(visitor));
            }

            return(visitor.Visit(this));
        }
Example #7
0
 public void InOrder(IBinaryNode <K, V> Subroot, ITreeVisitor <K, V> v)
 {
     if (Subroot == null)
     {
         return;
     }
     InOrder(Subroot.Left, v);
     v.Visit(Subroot);
     InOrder(Subroot.Right, v);
 }
Example #8
0
        public void Unknown_node_with_missing_data_can_accept_visitor()
        {
            ITreeVisitor     visitor = Substitute.For <ITreeVisitor>();
            TrieVisitContext context = new();
            TrieNode         node    = new(NodeType.Unknown);

            node.Accept(visitor, NullTrieNodeResolver.Instance, context);

            visitor.Received().VisitMissingNode(node.Keccak, context);
        }
Example #9
0
        public void Extension_can_accept_visitors()
        {
            ITreeVisitor     visitor = Substitute.For <ITreeVisitor>();
            TrieVisitContext context = new();
            TrieNode         ignore  = TrieNodeFactory.CreateLeaf(HexPrefix.Leaf("ccc"), Array.Empty <byte>());
            TrieNode         node    = TrieNodeFactory.CreateExtension(HexPrefix.Extension("aa"), ignore);

            node.Accept(visitor, NullTrieNodeResolver.Instance, context);

            visitor.Received().VisitExtension(node, context);
        }
Example #10
0
        /* ==== VISITOR PATTERN ==== */

        /**
         * Visitor pattern acceptance method.
         *
         * @param visitor
         *            The visitor traveling this tree
         */
        public void accept(ITreeVisitor visitor)
        {
            visitor.visit(this);

            IEnumerator en = children.GetEnumerator();

            while (en.MoveNext())
            {
                ((TreeElement)en.Current).accept(visitor);
            }
        }
Example #11
0
        public void Unknown_node_with_missing_data_can_accept_visitor()
        {
            ITreeVisitor     visitor = Substitute.For <ITreeVisitor>();
            TrieVisitContext context = new TrieVisitContext();
            PatriciaTree     tree    = new PatriciaTree();
            TrieNode         node    = new TrieNode(NodeType.Unknown);

            node.Accept(visitor, tree, context);

            visitor.Received().VisitMissingNode(node.Keccak, context);
        }
Example #12
0
 /// <summary>
 /// Applies the TreeVisitor, but only to the trees that pass the
 /// filter.
 /// </summary>
 /// <remarks>
 /// Applies the TreeVisitor, but only to the trees that pass the
 /// filter.  Applies the visitor to a copy of the tree.
 /// </remarks>
 /// <param name="tv">A class that can process trees.</param>
 public override void Apply(ITreeVisitor tv)
 {
     foreach (Tree t in treebank)
     {
         if (!filter.Test(t))
         {
             continue;
         }
         Tree tmpT = t.DeepCopy();
         tv.VisitTree(tmpT);
     }
 }
Example #13
0
 /// <summary>Applies the TreeVisitor to to all trees in the Treebank.</summary>
 /// <param name="tv">A class that can process trees.</param>
 public override void Apply(ITreeVisitor tv)
 {
     foreach (Tree t in tb)
     {
         Tree tmpT = t.DeepCopy();
         if (transformer != null)
         {
             tmpT = transformer.TransformTree(tmpT);
         }
         tv.VisitTree(tmpT);
     }
 }
Example #14
0
        public void Leaf_with_simple_account_can_accept_visitors()
        {
            ITreeVisitor     visitor = Substitute.For <ITreeVisitor>();
            TrieVisitContext context = new();
            Account          account = new(100);
            AccountDecoder   decoder = new();
            TrieNode         node    = TrieNodeFactory.CreateLeaf(HexPrefix.Leaf("aa"), decoder.Encode(account).Bytes);

            node.Accept(visitor, NullTrieNodeResolver.Instance, context);

            visitor.Received().VisitLeaf(node, context, node.Value);
        }
Example #15
0
        public void Leaf_with_contract_without_storage_and_empty_code_can_accept_visitors()
        {
            ITreeVisitor     visitor = Substitute.For <ITreeVisitor>();
            TrieVisitContext context = new();
            Account          account = new(1, 100, Keccak.EmptyTreeHash, Keccak.OfAnEmptyString);
            AccountDecoder   decoder = new();
            TrieNode         node    = TrieNodeFactory.CreateLeaf(HexPrefix.Leaf("aa"), decoder.Encode(account).Bytes);

            node.Accept(visitor, NullTrieNodeResolver.Instance, context);

            visitor.Received().VisitLeaf(node, context, node.Value);
        }
Example #16
0
        public void Accept(ITreeVisitor visitor, Keccak stateRoot)
        {
            if (visitor == null)
            {
                throw new ArgumentNullException(nameof(visitor));
            }
            if (stateRoot == null)
            {
                throw new ArgumentNullException(nameof(stateRoot));
            }

            _stateProvider.Accept(visitor, stateRoot);
        }
Example #17
0
            public void Treverse(ITreeVisitor visitor)
            {
                if (left != null)
                {
                    left.Treverse(visitor);
                }

                visitor.Visit(key);

                if (right != null)
                {
                    right.Treverse(visitor);
                }
            }
Example #18
0
        public void Extension_can_accept_visitors()
        {
            ITreeVisitor     visitor = Substitute.For <ITreeVisitor>();
            TrieVisitContext context = new TrieVisitContext();
            PatriciaTree     tree    = new PatriciaTree();
            TrieNode         ignore  = new TrieNode(NodeType.Unknown);
            TrieNode         node    = new TrieNode(NodeType.Extension);

            node.SetChild(0, ignore);

            node.Accept(visitor, tree, context);

            visitor.Received().VisitExtension(node, context);
        }
Example #19
0
        public void Leaf_with_simple_account_can_accept_visitors()
        {
            ITreeVisitor     visitor = Substitute.For <ITreeVisitor>();
            TrieVisitContext context = new TrieVisitContext();
            PatriciaTree     tree    = new PatriciaTree();
            Account          account = new Account(100);
            AccountDecoder   decoder = new AccountDecoder();
            TrieNode         node    = new TrieNode(NodeType.Leaf);

            node.Value = decoder.Encode(account).Bytes;

            node.Accept(visitor, tree, context);

            visitor.Received().VisitLeaf(node, context, node.Value);
        }
Example #20
0
        public void Branch_can_accept_visitors()
        {
            ITreeVisitor     visitor = Substitute.For <ITreeVisitor>();
            TrieVisitContext context = new();
            TrieNode         node    = new(NodeType.Branch);

            for (int i = 0; i < 16; i++)
            {
                node.SetChild(i, null);
            }

            node.Accept(visitor, NullTrieNodeResolver.Instance, context);

            visitor.Received().VisitBranch(node, context);
        }
Example #21
0
        public void Extension_with_leaf_can_be_visited()
        {
            Context      ctx     = new();
            ITreeVisitor visitor = Substitute.For <ITreeVisitor>();

            visitor.ShouldVisit(Arg.Any <Keccak>()).Returns(true);

            TrieVisitContext context = new();
            TrieNode         node    = TrieNodeFactory.CreateExtension(HexPrefix.Extension("aa"), ctx.AccountLeaf);

            node.Accept(visitor, NullTrieNodeResolver.Instance, context);

            visitor.Received().VisitExtension(node, context);
            visitor.Received().VisitLeaf(ctx.AccountLeaf, context, ctx.AccountLeaf.Value);
        }
Example #22
0
        public void Leaf_with_contract_without_storage_and_empty_code_can_accept_visitors()
        {
            ITreeVisitor     visitor = Substitute.For <ITreeVisitor>();
            TrieVisitContext context = new TrieVisitContext();
            PatriciaTree     tree    = new PatriciaTree();
            Account          account = new Account(1, 100, Keccak.EmptyTreeHash, Keccak.OfAnEmptyString);
            AccountDecoder   decoder = new AccountDecoder();
            TrieNode         node    = new TrieNode(NodeType.Leaf);

            node.Value = decoder.Encode(account).Bytes;

            node.Accept(visitor, tree, context);

            visitor.Received().VisitLeaf(node, context, node.Value);
        }
Example #23
0
        public void Branch_can_accept_visitors()
        {
            ITreeVisitor     visitor = Substitute.For <ITreeVisitor>();
            TrieVisitContext context = new TrieVisitContext();
            PatriciaTree     tree    = new PatriciaTree();
            TrieNode         node    = new TrieNode(NodeType.Branch);

            for (int i = 0; i < 16; i++)
            {
                node.SetChild(i, null);
            }

            node.Accept(visitor, tree, context);

            visitor.Received().VisitBranch(node, context);
        }
Example #24
0
                void VisitChild(int i, TrieNode?child, ITrieNodeResolver resolver, ITreeVisitor v, TrieVisitContext context)
                {
                    if (child != null)
                    {
                        child.ResolveKey(resolver, false);
                        if (v.ShouldVisit(child.Keccak !))
                        {
                            context.BranchChildIndex = i;
                            child.Accept(v, resolver, context);
                        }

                        if (child.IsPersisted)
                        {
                            UnresolveChild(i);
                        }
                    }
                }
Example #25
0
        public void Extension_with_leaf_can_be_visited()
        {
            ITreeVisitor visitor = Substitute.For <ITreeVisitor>();

            visitor.ShouldVisit(Arg.Any <Keccak>()).Returns(true);

            TrieVisitContext context = new TrieVisitContext();
            PatriciaTree     tree    = new PatriciaTree();
            TrieNode         node    = new TrieNode(NodeType.Extension);

            node.SetChild(0, _accountLeaf);

            node.Accept(visitor, tree, context);

            visitor.Received().VisitExtension(node, context);
            visitor.Received().VisitLeaf(_accountLeaf, context, _accountLeaf.Value);
        }
Example #26
0
        internal void Accept(ITreeVisitor visitor, ITrieNodeResolver nodeResolver, TrieVisitContext trieVisitContext)
        {
            try
            {
                ResolveNode(nodeResolver);
            }
            catch (TrieException)
            {
                visitor.VisitMissingNode(Keccak, trieVisitContext);
                return;
            }

            ResolveKey(nodeResolver, trieVisitContext.Level == 0);

            switch (NodeType)
            {
            case NodeType.Branch:
            {
Example #27
0
 void VisitMultiThread(ITreeVisitor treeVisitor, ITrieNodeResolver trieNodeResolver, TrieVisitContext visitContext, TrieNode?[] children)
 {
     // multithreaded route
     Parallel.For(0, BranchesCount, i =>
         {
             visitContext.Semaphore.Wait();
             try
             {
                 // we need to have separate context for each thread as context tracks level and branch child index
                 TrieVisitContext childContext = visitContext.Clone();
                 VisitChild(i, children[i], trieNodeResolver, treeVisitor, childContext);
             }
             finally
             {
                 visitContext.Semaphore.Release();
             }
         });
 }
Example #28
0
        public void Branch_with_children_can_be_visited()
        {
            Context      ctx     = new();
            ITreeVisitor visitor = Substitute.For <ITreeVisitor>();

            visitor.ShouldVisit(Arg.Any <Keccak>()).Returns(true);

            TrieVisitContext context = new();
            TrieNode         node    = new(NodeType.Branch);

            for (int i = 0; i < 16; i++)
            {
                node.SetChild(i, ctx.AccountLeaf);
            }

            node.Accept(visitor, NullTrieNodeResolver.Instance, context);

            visitor.Received().VisitBranch(node, context);
            visitor.Received(16).VisitLeaf(ctx.AccountLeaf, context, ctx.AccountLeaf.Value);
        }
Example #29
0
        public void Accept(ITreeVisitor visitor)
        {
            var mapTree = new Dictionary <int, IList <ITreeNode> >();
            var mapRes  = visitor.Visit(this, mapTree);

            //---- Print the map ----

            IList <ITreeNode> listNodes = null;

            for (int level = 1; level <= mapTree.Count; level++)
            {
                Console.Out.WriteLine("Level" + level);
                mapRes.TryGetValue(level, out listNodes);

                foreach (var treeNode in listNodes)
                {
                    Console.Out.WriteLine("Node Path: " + TreeHelper.PrintPath(treeNode.Path(), '-'));
                }
            }
        }
Example #30
0
        public void Branch_with_children_can_be_visited()
        {
            ITreeVisitor visitor = Substitute.For <ITreeVisitor>();

            visitor.ShouldVisit(Arg.Any <Keccak>()).Returns(true);

            TrieVisitContext context = new TrieVisitContext();
            PatriciaTree     tree    = new PatriciaTree();
            TrieNode         node    = new TrieNode(NodeType.Branch);

            for (int i = 0; i < 16; i++)
            {
                node.SetChild(i, _accountLeaf);
            }

            node.Accept(visitor, tree, context);

            visitor.Received().VisitBranch(node, context);
            visitor.Received(16).VisitLeaf(_accountLeaf, context, _accountLeaf.Value);
        }