Example #1
0
        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);
                }
            }
        }
Example #2
0
        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);
        }
Example #3
0
        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();
        }
Example #4
0
        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);
                }
            }
        }
Example #5
0
 public bool MoveNext()
 {
     if (firstMove)
     {
         currentNode = currentNode?.nextNode;
     }
     firstMove = true;
     return(currentNode != null);
 }
Example #6
0
        public bool ContainsNode(ThreadSafeNode <T> node)
        {
            ThreadSafeNode <T> currentNode = firstNode;

            while (currentNode != null)
            {
                if (currentNode.Equals(node))
                {
                    return(true);
                }
                currentNode = currentNode.nextNode;
            }
            return(false);
        }
Example #7
0
        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);
        }
Example #8
0
        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);
        }
Example #9
0
        public new void Clear()
        {
            bool lockTaken = false;

            try
            {
                spinLock.Enter(ref lockTaken);
                itemCount = 0;
                firstNode = null;
                lastNode  = null;
            }
            finally
            {
                if (lockTaken)
                {
                    spinLock.Exit(false);
                }
            }
        }
Example #10
0
        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);
        }
Example #11
0
        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);
                }
            }
        }
Example #12
0
 public ThreadSafeNode(T obj, ThreadSafeNode <T> previous, ThreadSafeNode <T> next)
 {
     value        = obj;
     previousNode = previous;
     nextNode     = next;
 }
Example #13
0
 public ThreadSafeNode(T obj, ThreadSafeNode <T> previous)
 {
     value        = obj;
     previousNode = previous;
 }
Example #14
0
 internal Enumerator(ThreadSafeLinkedList <T> threadSafeLinkedList)
 {
     list        = threadSafeLinkedList;
     currentNode = threadSafeLinkedList.firstNode;
     firstMove   = false;
 }
Example #15
0
 public void Reset()
 {
     currentNode = list.firstNode;
     firstMove   = false;
 }
Example #16
0
            //T IEnumerator<T>.Current => Current.value;

            public void Dispose()
            {
                currentNode = null;
            }