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); }
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); } }
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); }
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; }
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; } }
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); }
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; }
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; } }
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); }
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; }
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; } }
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); } }
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); }
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); }
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; } } } }
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); } } }
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; }
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; }
public ActiveListNode <T> SortFirst(T value, Func <T, T, long> comparator) { var newNode = new ActiveListNode <T>((ActiveList <T>) this, value); return(SortFirst(newNode, comparator)); }
public void AddAfter(ActiveListNode <T> node, ActiveListNode <T> newNode) { this.AssertNode(node); this.AssertNewNode(newNode); this.InsertNodeBefore(node.next, newNode); }