Пример #1
0
 private void VerifyNodesHaveCommonParent(DualLinkNode <TEntity> node)
 {
     if (!ReferenceEquals(Parent, node.Parent))
     {
         throw new InvalidOperationException("Cannot attach nodes with different parents");
     }
 }
Пример #2
0
        public void PlaceBefore(IDualLinkNode <TEntity> sourceNode, IDualLinkNode <TEntity> targetNode)
        {
            DualLinkNode <TEntity> source = FindNode(sourceNode);
            DualLinkNode <TEntity> target = FindNode(targetNode);

            if (source != target)
            {
                target.AttachBefore(source);
                RealignEnds();
            }
        }
Пример #3
0
        public DualLinkNode <TEntity> DetachBefore()
        {
            DualLinkNode <TEntity> currentPrevious = _previous;

            _previous = null;
            if (currentPrevious != null)
            {
                currentPrevious._next = null;
            }
            return(currentPrevious);
        }
Пример #4
0
        public DualLinkNode <TEntity> DetachAfter()
        {
            DualLinkNode <TEntity> currentNext = _next;

            _next = null;
            if (currentNext != null)
            {
                currentNext._previous = null;
            }
            return(currentNext);
        }
Пример #5
0
        public IDualLinkNode <TEntity> AddAfter(IDualLinkNode <TEntity> suppliedNode, TEntity entity)
        {
            DualLinkNode <TEntity> sourceNode = FindNode(suppliedNode);
            DualLinkNode <TEntity> newNode    = new DualLinkNode <TEntity>(this, entity);

            if (sourceNode != null)
            {
                newNode.AttachAfter(sourceNode);
            }
            AddElement(newNode);

            return(newNode);
        }
Пример #6
0
 private void AddElement(DualLinkNode <TEntity> node)
 {
     Nodes.Add(node, node);
     if (First == null)
     {
         First = node;
     }
     if (Last == null)
     {
         Last = node;
     }
     RealignEnds();
 }
Пример #7
0
        public void AttachAfter(DualLinkNode <TEntity> node)
        {
            VerifyNodesHaveCommonParent(node);
            CollapseCurrent();
            DualLinkNode <TEntity> targetNext = node._next;

            node._next = this;
            if (targetNext != null)
            {
                targetNext._previous = this;
            }
            _previous = node;
            _next     = targetNext;
        }
Пример #8
0
        public void AttachBefore(DualLinkNode <TEntity> node)
        {
            VerifyNodesHaveCommonParent(node);
            CollapseCurrent();
            DualLinkNode <TEntity> targetPrevious = node._previous;

            node._previous = this;
            if (targetPrevious != null)
            {
                targetPrevious._next = this;
            }
            _previous = targetPrevious;
            _next     = node;
        }
Пример #9
0
        private void CollapseCurrent()
        {
            if (_previous != null)
            {
                _previous._next = _next;
            }
            if (_next != null)
            {
                _next._previous = _previous;
            }

            _previous = null;
            _next     = null;
        }
Пример #10
0
        public List <TEntity> TrimAfter(IDualLinkNode <TEntity> node)
        {
            DualLinkNode <TEntity> trimNode = FindNode(node);
            DualLinkNode <TEntity> detached = trimNode.DetachAfter();

            Last = trimNode;

            List <TEntity>          trimmedValues = new List <TEntity>();
            IDualLinkNode <TEntity> currentNode   = detached;

            while (currentNode != null)
            {
                Nodes.Remove(currentNode);
                trimmedValues.Add(currentNode.Value);
                currentNode = currentNode.Next;
            }
            return(trimmedValues);
        }
Пример #11
0
        public TEntity Remove(IDualLinkNode <TEntity> node)
        {
            DualLinkNode <TEntity> targetNode = FindNode(node);

            if (targetNode == First)
            {
                First = targetNode.Next;
            }
            if (targetNode == Last)
            {
                Last = targetNode.Previous;
            }
            targetNode.DetachCompletely();
            Nodes.Remove(node);
            RealignEnds();

            return(targetNode.Value);
        }
Пример #12
0
        public List <TEntity> TrimBefore(IDualLinkNode <TEntity> node)
        {
            DualLinkNode <TEntity> trimNode = FindNode(node);
            DualLinkNode <TEntity> detached = trimNode.DetachBefore();

            First = trimNode;

            List <TEntity>          trimmedValues = new List <TEntity>();
            IDualLinkNode <TEntity> currentNode   = detached;

            while (currentNode != null)
            {
                Nodes.Remove(currentNode);
                trimmedValues.Add(currentNode.Value);
                currentNode = currentNode.Previous;
            }
            trimmedValues.Reverse();
            return(trimmedValues);
        }