Beispiel #1
0
        /// <summary>
        /// Add to a list in a thread safe manner
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="value"></param>
        public static void ThreadSafeAdd <T>(ref ImmutableLinkedList <T> list, T value)
        {
            if (list == null)
            {
                throw new ArgumentNullException(nameof(list));
            }

            var listValue = list;
            var newList   = listValue.Add(value);

            if (ReferenceEquals(Interlocked.CompareExchange(ref list, newList, listValue), listValue))
            {
                return;
            }

            var wait = new SpinWait();

            while (true)
            {
                wait.SpinOnce();

                listValue = list;
                newList   = listValue.Add(value);

                if (ReferenceEquals(Interlocked.CompareExchange(ref list, newList, listValue), listValue))
                {
                    return;
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Empty an immutable linked list in a thread and return the list
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list">list to empty</param>
        /// <returns></returns>
        public static ImmutableLinkedList <T> ThreadSafeEmpty <T>(ref ImmutableLinkedList <T> list)
        {
            if (list == null)
            {
                throw new ArgumentNullException(nameof(list));
            }

            return(Interlocked.Exchange(ref list, ImmutableLinkedList <T> .Empty));
        }
Beispiel #3
0
        /// <summary>
        /// Add a range to a list in a thread safe manner
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="values"></param>
        public static void ThreadSafeAddRange <T>(ref ImmutableLinkedList <T> list, IEnumerable <T> values)
        {
            if (list == null)
            {
                throw new ArgumentNullException(nameof(list));
            }
            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            foreach (var value in values)
            {
                ThreadSafeAdd(ref list, value);
            }
        }
Beispiel #4
0
            public bool MoveNext()
            {
                if (_current == null)
                {
                    if (_start.Next == null)
                    {
                        return(false);
                    }

                    _current = _start;
                }
                else if (_current.Next?.Next == null)
                {
                    return(false);
                }
                else
                {
                    _current = _current.Next;
                }

                return(true);
            }
Beispiel #5
0
 public void Reset()
 {
     _current = _start;
 }
Beispiel #6
0
 public LinkedListEnumerator(ImmutableLinkedList <T> current)
 {
     _start = current;
 }