/// <summary>
        /// Inserts a node before another node in the list.
        /// The given nodes must both be in the list, and the node to insert must be the last one in it.
        /// Otherwise an exception is thrown.
        /// </summary>
        public void InsertBefore(MutableLinkedListNode <T> node, MutableLinkedListNode <T> beforeThis)
        {
            if (node.List != this)
            {
                throw new ArgumentException("Object must already be in list before inserting.");
            }
            if (beforeThis.List != this)
            {
                throw new ArgumentException("The object to insert before must be in the same list.");
            }
            if (node != Last)
            {
                throw new ArgumentException("Inserted object must be last object in list.");
            }
            if (node == beforeThis)
            {
                throw new ArgumentException("Cannot insert object before itself.");
            }

            Last = node.Prev;
            if (beforeThis == First)
            {
                First = node;
            }

            node.Prev.Next  = null;
            node.Prev       = beforeThis.Prev;
            beforeThis.Prev = node;
            node.Next       = beforeThis;

            if (node.Prev != null)
            {
                node.Prev.Next = node;
            }
        }
        /// <summary>
        /// Adds an item to the linked list. The node used to store the item is returned.
        /// </summary>
        public MutableLinkedListNode <T> Add(T item)
        {
            var node = MutableLinkedListNode.For(item);

            Add(node);
            return(node);
        }
        /// <summary>
        /// Removes the specified node from the list.
        /// Throws an exception if the given node is not in the list.
        /// </summary>
        public void Remove(MutableLinkedListNode <T> node)
        {
            if (node.List != this)
            {
                throw new ArgumentException("Node must be in list to be removed.");
            }

            foreach (var enumerator in enumerators)
            {
                enumerator.OnObjectRemove(node);
            }

            if (node == Last)
            {
                Last = node.Prev;
            }
            if (node == First)
            {
                First = node.Next;
            }

            Count--;

            if (node.Next != null)
            {
                node.Next.Prev = node.Prev;
            }
            if (node.Prev != null)
            {
                node.Prev.Next = node.Next;
            }
            node.Next = null;
            node.Prev = null;
            node.List = null;
        }
Exemple #4
0
 public void Reset()
 {
     this.current           = null;
     this.done              = false;
     this.initialised       = false;
     this.currentWasDeleted = false;
 }
Exemple #5
0
 public bool MoveNext()
 {
     if (this.done)
     {
         return(false);
     }
     if (!this.initialised)
     {
         if (this.list.Count == 0)
         {
             return(false);
         }
         this.initialised = true;
         this.current     = this.list.First;
     }
     else
     {
         if (this.currentWasDeleted)
         {
             this.currentWasDeleted = false;
             return(true);
         }
         this.current = this.current.Next;
         if (this.current == null)
         {
             this.done = true;
             return(false);
         }
     }
     return(true);
 }
 /// <summary>
 /// Adds a given linked list node before another one already in this list.
 /// This will throw an exception if the node is already in another list, of if the node to add before is null, or not in this list.
 /// </summary>
 public void AddBefore(MutableLinkedListNode <T> newNode, MutableLinkedListNode <T> beforeThis)
 {
     if (beforeThis.List != this)
     {
         throw new ArgumentException("The object to insert before must be in the same list.");
     }
     Add(newNode);
     InsertBefore(newNode, beforeThis);
 }
Exemple #7
0
        public void OnObjectRemove(MutableLinkedListNode <T> obj)
        {
            if (obj != this.current)
            {
                return;
            }

            this.currentWasDeleted = true;
            this.current           = obj.Next;
            if (this.current == null)
            {
                this.done = true;
            }
        }
        /// <summary>
        /// Adds a linked list node to this list.
        /// If the node is already in another list, an exception is thrown.
        /// </summary>
        public void Add(MutableLinkedListNode <T> node)
        {
            if (node.List != null)
            {
                throw new ArgumentException("Node cannot already be in a list when adding it to one.", nameof(node));
            }

            node.List = this;

            if (Count == 0)
            {
                First = node;
            }
            else
            {
                node.Prev = Last;
                Last.Next = node;
            }

            Last = node;

            Count++;
        }