예제 #1
0
        public ActiveListNode <T> SortFirst(ActiveListNode <T> newNode, Func <T, T, long> comparator)
        {
            if (this.head == null)
            {
                this.InsertNodeToEmptyList(newNode);
                return(newNode);
            }

            var node = this.head;

            do
            {
                if (comparator(node.Value, newNode.Value) > 0)
                {
                    this.InsertNodeBefore(node, newNode);
                    if (node == this.head)
                    {
                        this.head = newNode;
                    }
                    return(newNode);
                }
                node = node.Next;
            } while (node != null);
            this.InsertNodeAfter(this.tail, newNode);
            this.tail = newNode;
            return(newNode);
        }
예제 #2
0
        public void CopyTo(T[] array, int index)
        {
            if (array == null)
            {
                throw new ArgumentNullException("array");
            }
            if ((index < 0) || (index > array.Length))
            {
                throw new ArgumentOutOfRangeException("bad index " + index);
            }
            if ((array.Length - index) < this.Count)
            {
                throw new ArgumentException("Not enough space");
            }
            var head = this.head;

            if (head != null)
            {
                do
                {
                    array[index++] = head.item;
                    head           = head.next;
                }while (head != this.head);
            }
        }
예제 #3
0
        public ActiveListNode <T> AddAfter(ActiveListNode <T> node, T value)
        {
            this.AssertNode(node);
            var newNode = new ActiveListNode <T>(node.list, value);

            this.InsertNodeBefore(node.next, newNode);
            return(newNode);
        }
예제 #4
0
 private void InsertNodeToEmptyList(ActiveListNode <T> newNode)
 {
     newNode.next = null;
     newNode.prev = null;
     this.head    = newNode;
     this.tail    = newNode;
     newNode.list = (ActiveList <T>) this;
     ++this.count;
     ++version;
 }
예제 #5
0
 public void AddBefore(ActiveListNode <T> node, ActiveListNode <T> newNode)
 {
     this.AssertNode(node);
     this.AssertNewNode(newNode);
     this.InsertNodeBefore(node, newNode);
     if (node == this.head)
     {
         this.head = newNode;
     }
 }
예제 #6
0
        public ActiveListNode <T> AddBefore(ActiveListNode <T> node, T value)
        {
            this.AssertNode(node);
            var newNode = new ActiveListNode <T>(node.list, value);

            this.InsertNodeBefore(node, newNode);
            if (node == this.head)
            {
                this.head = newNode;
            }
            return(newNode);
        }
예제 #7
0
        public void Clear()
        {
            var head = this.head;

            while (head != null)
            {
                ActiveListNode <T> node2 = head;
                node2.Invalidate();
                head = head.Next;
            }
            this.head  = null;
            this.count = 0;
        }
예제 #8
0
 public void AddLast(ActiveListNode <T> node)
 {
     this.AssertNewNode(node);
     if (this.head == null)
     {
         this.InsertNodeToEmptyList(node);
     }
     else
     {
         this.InsertNodeAfter(this.tail, node);
         this.tail = node;
     }
 }
예제 #9
0
        public ActiveListNode <T> AddLast(T value)
        {
            var newNode = new ActiveListNode <T>((ActiveList <T>) this, value);

            if (this.head == null)
            {
                this.InsertNodeToEmptyList(newNode);
                return(newNode);
            }
            this.InsertNodeAfter(this.tail, newNode);
            this.tail = newNode;
            return(newNode);
        }
예제 #10
0
 private void InsertNodeAfter(ActiveListNode <T> node, ActiveListNode <T> newNode)
 {
     newNode.prev = node;
     newNode.next = node.next;
     if (node.next != null)
     {
         node.next.prev = newNode;
     }
     node.next    = newNode;
     newNode.list = (ActiveList <T>) this;
     ++this.count;
     ++version;
 }
예제 #11
0
 public void AddFirst(ActiveListNode <T> node)
 {
     this.AssertNewNode(node);
     if (this.head == null)
     {
         this.InsertNodeToEmptyList(node);
     }
     else
     {
         this.InsertNodeBefore(this.head, node);
         this.head = node;
     }
 }
예제 #12
0
 internal void AssertNewNode(ActiveListNode <T> node)
 {
     if (node == null)
     {
         throw new ArgumentNullException("node");
     }
     if (node.list == this)
     {
         throw new InvalidOperationException("already in this list for " + node.Value);
     }
     if (node.list != null)
     {
         throw new InvalidOperationException("already in a different list for " + node.Value);
     }
 }
예제 #13
0
        public ActiveListNode <T> AddFirst(T value)
        {
            var newNode = new ActiveListNode <T>((ActiveList <T>) this, value);

            if (this.head == null)
            {
                this.InsertNodeToEmptyList(newNode);
            }
            else
            {
                this.InsertNodeBefore(this.head, newNode);
                this.head = newNode;
            }
            return(newNode);
        }
예제 #14
0
 public bool Remove(ActiveListNode <T> node)
 {
     if (node == null)
     {
         throw new ArgumentNullException("node");
     }
     if (node.list == null)
     {
         return(false);
     }
     if (node.list != this)
     {
         throw new InvalidOperationException("node belongs to a different list. null? " + (node.list == null));
     }
     this.RemoveNode(node);
     return(true);
 }
예제 #15
0
 public void AddLast(Iterable <T> list2)
 {
     if (list2 != null)
     {
         for (var current = list2.First; current != null; current = current.Next)
         {
             var newNode = new ActiveListNode <T>((ActiveList <T>) this, current.Value);
             if (this.head == null)
             {
                 this.InsertNodeToEmptyList(newNode);
             }
             else
             {
                 this.InsertNodeAfter(this.tail, newNode);
                 this.tail = newNode;
             }
         }
     }
 }
예제 #16
0
 internal void AssertNode(ActiveListNode <T> node)
 {
     if (node == null)
     {
         throw new ArgumentNullException("active list node");
     }
     if (node.list == null)
     {
         if (head == node)
         {
             throw new InvalidOperationException("node removed but head points to node " + node.Value);
         }
         if (count != 0)
         {
             var current = head;
             do
             {
                 if (current.next == node)
                 {
                     throw new InvalidOperationException("node removed but a different node next still points to node for " + node.Value);
                 }
                 if (current.prev == node)
                 {
                     throw new InvalidOperationException("node removed but a different node prev still points to node for " + node.Value);
                 }
                 current = current.Next;
             } while (current != null);
         }
         throw new InvalidOperationException("node not in the list for " + node.Value);
     }
     if (node.list != this)
     {
         if (node.list == null)
         {
             throw new InvalidOperationException("wrong list. node.list is null for " + node.Value);
         }
         if (node.list != null)
         {
             throw new InvalidOperationException("wrong list. mismatch \n node.list " + node.list.id + " " + node.list + "\n this " + this.id + " " + this);
         }
     }
 }
예제 #17
0
        public void ResortFirst(ActiveListNode <T> newNode, Func <T, T, int> comparator)
        {
            if (newNode == null)
            {
                throw new ArgumentNullException("newNode");
            }
            if (newNode.list == this)
            {
                this.RemoveNode(newNode);
            }
            if (newNode.list != null)
            {
                throw new InvalidOperationException("newNode belongs to a different list");
            }
            if (this.head == null)
            {
                this.InsertNodeToEmptyList(newNode);
                return;
            }

            var node = this.head;

            do
            {
                if (comparator(node.Value, newNode.Value) > 0)
                {
                    this.InsertNodeBefore(node, newNode);
                    if (node == this.head)
                    {
                        this.head = newNode;
                    }
                    return;
                }
                node = node.Next;
            } while (node != null);
            this.InsertNodeAfter(this.tail, newNode);
            this.tail = newNode;
            return;
        }
예제 #18
0
 private void RemoveNode(ActiveListNode <T> node)
 {
     if (node.next != null)
     {
         node.next.prev = node.prev;
     }
     if (node.prev != null)
     {
         node.prev.next = node.next;
     }
     node.Invalidate();
     if (this.head == node)
     {
         this.head = node.next;
     }
     if (this.tail == node)
     {
         this.tail = node.prev;
     }
     --this.count;
     ++version;
 }
예제 #19
0
        public ActiveListNode <T> SortFirst(T value, Func <T, T, long> comparator)
        {
            var newNode = new ActiveListNode <T>((ActiveList <T>) this, value);

            return(SortFirst(newNode, comparator));
        }
예제 #20
0
 public void AddAfter(ActiveListNode <T> node, ActiveListNode <T> newNode)
 {
     this.AssertNode(node);
     this.AssertNewNode(newNode);
     this.InsertNodeBefore(node.next, newNode);
 }