Example #1
0
        internal ForkNode(ForkNode parent)
        {
            Debug.Assert(parent != null);
            Debug.Assert(parent != this);

            LookAheadQueue = parent.LookAheadQueue;
            MasterScanner = parent.MasterScanner;

            Children = new List<ForkNode>();
            Parent = parent;
        }
Example #2
0
        public ForkableScanner Fork()
        {
            if (m_node.MasterScanner == null)
            {
                throw new ObjectDisposedException(null);
            }

            var forked = m_node.State.Fork(2);

            m_node = forked[0];

            return new ForkableScanner(forked[1]);
        }
Example #3
0
        public void Join(ForkableScanner child)
        {
            CodeContract.RequiresArgumentNotNull(child, "child");
            CodeContract.Requires(child.m_node.MasterScanner != null, "The scanner to join with has been closed");
            CodeContract.Requires(child.m_node.Parent == m_node.Parent, "child", "The scanner to join does not share the parent node with current scanner");

            var parent = m_node.Parent;

            //swap "this" with "child"
            var temp = m_node;
            m_node = child.m_node;
            child.m_node = temp;

            //close other children
            foreach (var otherChild in parent.Children.ToArray())
            {
                if (otherChild != m_node)
                {
                    otherChild.Close();
                }
            }
        }
Example #4
0
        public override ForkNode[] Fork(int count)
        {
            Debug.Assert(Node.Children.Count >= 2);
            ForkNode[] results = new ForkNode[count];

            for (int i = 0; i < count; i++)
            {
                results[i] = new ForkNode(Node);
                results[i].State = new HeadState(results[i]);
                Node.Children.Add(results[i]);
            }

            //no switching state

            return results;
        }
Example #5
0
 public TailState(ForkNode node) : base(node) { }
Example #6
0
 public abstract void Remove(ForkNode child);
Example #7
0
 protected NodeState(ForkNode node)
 {
     
     Node = node;
 }
Example #8
0
 public override void Remove(ForkNode child)
 {
     Debug.Assert(Node.Children.Count == 0);
     throw new NotSupportedException();
 }
Example #9
0
        public override ForkNode[] Fork(int count)
        {
            Debug.Assert(count >= 2);
            Debug.Assert(Node.Children.Count == 0);

            ForkNode[] results = new ForkNode[count];

            for (int i = 0; i < count; i++)
            {
                results[i] = new ForkNode(Node);
                results[i].State = new HeadState(results[i]);
                Node.Children.Add(results[i]);
            }

            //switch to TailState after fork
            Node.State = new TailState(this);

            return results;
        }
Example #10
0
        private void Forward(ForkNode child)
        {
            //1. add node's children to its parent's children list
            //2. remove node from its parent's children list

            ForkNode parent = Node.Parent;
            parent.Children.Add(child);
            child.Offset += Node.Offset;

            Debug.Assert(parent != child);
            child.Parent = parent;

            Node.Offset = 0;
            Node.Children.Clear();
            parent.Children.Remove(Node);

            //destroy Node
            Node.Destroy();
        }
Example #11
0
        public override void Remove(ForkNode child)
        {
            //remove child from Node
            bool succ = Node.Children.Remove(child);
            Debug.Assert(succ);
            Debug.Assert(Node.Children.Count > 0);

            //check if forwarding necessory
            if (Node.Children.Count == 1)
            {
                ForkNode onlyChild = Node.Children[0];

                //forward to onlyChild
                Forward(onlyChild);
            }
        }
Example #12
0
 public BranchState(ForkNode node) : base(node) { }
Example #13
0
 public HeadState(ForkNode node) : base(node) { }
Example #14
0
        void Forward(ForkNode child)
        {
            // dequeue "offset" times
            // destroy Node
            // change child's state to TailHead or Tail

            Debug.Assert(Node.Offset == 0);

            for (int i = 0; i < child.Offset; i++)
            {
                Node.LookAheadQueue.Dequeue();
            }
            child.Offset = 0;

            Node.Children.Clear();
            Node.Destroy();

            Debug.Assert(child.State is HeadState || child.State is BranchState, "Invalid child state when forwarding");

            child.Parent = null;
            if (child.State is HeadState)
            {
                child.State = new TailHeadState(child);
            }
            else
            {
                child.State = new TailState(child);
            }
        }
Example #15
0
        internal static ForkableScanner Create(Scanner masterScanner)
        {
            ForkNode node = new ForkNode();
            node.State = new TailHeadState(node);
            node.MasterScanner = masterScanner;

            return new ForkableScanner(node);
        }
Example #16
0
 private ForkableScanner(ForkNode node)
 {
     m_node = node;
 }