Пример #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;
                }
            }
        }
Пример #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));
        }
Пример #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);
            }
        }
Пример #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);
            }
Пример #5
0
 public void Reset()
 {
     _current = _start;
 }
Пример #6
0
 public LinkedListEnumerator(ImmutableLinkedList <T> current)
 {
     _start = current;
 }