public void AddNode(ThreadSafeNode <T> threadSafeNode) { bool lockTaken = false; try { spinLock.Enter(ref lockTaken); if (firstNode == null) { firstNode = threadSafeNode; } else { threadSafeNode.previousNode = lastNode; lastNode.nextNode = threadSafeNode; } lastNode = threadSafeNode; itemCount++; } finally { if (lockTaken) { spinLock.Exit(false); } } }
public bool RemoveNode(ThreadSafeNode <T> threadSafeNode) { bool lockTaken = false; try { spinLock.Enter(ref lockTaken); itemCount--; if (threadSafeNode == firstNode) { firstNode = threadSafeNode.nextNode; } if (threadSafeNode == lastNode) { lastNode = threadSafeNode.previousNode; } if (threadSafeNode.previousNode != null) { threadSafeNode.previousNode.nextNode = threadSafeNode.nextNode; } if (threadSafeNode.nextNode != null) { threadSafeNode.nextNode.previousNode = threadSafeNode.previousNode; } } finally { if (lockTaken) { spinLock.Exit(false); } } return(true); }
ThreadSafeNode <T> getNodeAtIndex(int index) { if (!warningIssuedIndex) { Log.Warning("Using an index on ThreadSafeLinkedList will lead to errors!"); warningIssuedIndex = true; } int i = 0; //ThreadSafeNode<T> lastCheckedNode = null; ThreadSafeNode <T> currentNode = firstNode; while (currentNode != null) { if (i == index) { return(currentNode); } i++; currentNode = currentNode.nextNode; } //Log.ErrorOnce("index at: " + i + ", last index checked:" + index + ", node count:" + itemCount, 488564); return(lastNode); throw new ArgumentOutOfRangeException(); }
public void InsertNode(int index, ThreadSafeNode <T> newNode) { if (!warningIssuedIndex) { Log.Warning("Using an index on ThreadSafeLinkedList will lead to errors!"); warningIssuedIndex = true; } int i = 1; //not using 0, since it is doing insertAfter instead of insertBefore bool lockTaken = false; try { ThreadSafeNode <T> insertAfterNode_nextNode = null; spinLock.Enter(ref lockTaken); ThreadSafeNode <T> insertAfterNode = firstNode; if (index <= 0) { newNode.nextNode = firstNode; firstNode = newNode; } else { foreach (ThreadSafeNode <T> threadSafeNode in this) { insertAfterNode = threadSafeNode; if (i == index) { break; } i++; } newNode.previousNode = insertAfterNode; if (insertAfterNode_nextNode == null) { Log.Error("Never null"); } newNode.nextNode = insertAfterNode.nextNode; insertAfterNode.nextNode = newNode; } if (index >= itemCount) { lastNode = newNode; } else { newNode.nextNode.previousNode = newNode; } itemCount++; } finally { if (lockTaken) { spinLock.Exit(false); } } }
public bool MoveNext() { if (firstMove) { currentNode = currentNode?.nextNode; } firstMove = true; return(currentNode != null); }
public bool ContainsNode(ThreadSafeNode <T> node) { ThreadSafeNode <T> currentNode = firstNode; while (currentNode != null) { if (currentNode.Equals(node)) { return(true); } currentNode = currentNode.nextNode; } return(false); }
public ThreadSafeNode <T> getFirstNodeContaining(T item) { ThreadSafeNode <T> currentNode = firstNode; while (currentNode != null) { if (currentNode.value.Equals(item)) { return(currentNode); } currentNode = currentNode.nextNode; } return(null); }
public new int RemoveAll(Predicate <T> predicate) { int totalRemoved = 0; ThreadSafeNode <T> currentNode = firstNode; while (currentNode != null) { if (predicate(currentNode.value)) { RemoveNode(currentNode); totalRemoved++; } currentNode = currentNode.nextNode; } return(totalRemoved); }
public new void Clear() { bool lockTaken = false; try { spinLock.Enter(ref lockTaken); itemCount = 0; firstNode = null; lastNode = null; } finally { if (lockTaken) { spinLock.Exit(false); } } }
public new bool Remove(T item) { if (!warningIssuedRemove) { Log.Warning("Calling ThreadSafeLinkedList.Remove(T) is not optimal and will remove the first occurance. ThreadSafeLinkedList.RemoveNode(ThreadSafeNode<T>) is preferred."); warningIssuedRemove = true; } ThreadSafeNode <T> currentNode = firstNode; while (currentNode != null) { if (currentNode.value.Equals(item)) { itemCount--; RemoveNode(currentNode); } currentNode = currentNode.nextNode; } return(true); }
public void InsertBefore(ThreadSafeNode <T> nextNode, ThreadSafeNode <T> newNode) { bool lockTaken = false; try { spinLock.Enter(ref lockTaken); newNode.nextNode = nextNode; newNode.previousNode = nextNode.previousNode; nextNode.previousNode = newNode; if (firstNode == nextNode) { firstNode = newNode; } itemCount++; } finally { if (lockTaken) { spinLock.Exit(false); } } }
public ThreadSafeNode(T obj, ThreadSafeNode <T> previous, ThreadSafeNode <T> next) { value = obj; previousNode = previous; nextNode = next; }
public ThreadSafeNode(T obj, ThreadSafeNode <T> previous) { value = obj; previousNode = previous; }
internal Enumerator(ThreadSafeLinkedList <T> threadSafeLinkedList) { list = threadSafeLinkedList; currentNode = threadSafeLinkedList.firstNode; firstMove = false; }
public void Reset() { currentNode = list.firstNode; firstMove = false; }
//T IEnumerator<T>.Current => Current.value; public void Dispose() { currentNode = null; }