internal Enumerator(GrowingCollectionSegment <T> head) { Validate.NotNull(head, nameof(head)); _head = _currentSegment = head; _headOffset = _currentSegmentOffset = head.LocalCount; _count = _headOffset + (_head.NextSegment == null ? 0 : _head.NextSegment.GlobalCount); }
/// <summary>Adds an item to the collection.</summary> /// <param name="item">Item to be added.</param> public void Add(T item) { GrowingCollectionSegment <T> currHead = Volatile.Read(ref _dataHead); bool added = currHead.TryAdd(item); while (!added) { var newHead = new GrowingCollectionSegment <T>(currHead); Concurrent.CompareExchangeResult(ref _dataHead, newHead, currHead); GrowingCollectionSegment <T> updatedHead = Interlocked.CompareExchange(ref _dataHead, newHead, currHead); added = updatedHead.TryAdd(item); } }
public GrowingCollectionSegment(GrowingCollectionSegment <T> nextSegment) { if (nextSegment == null) { throw new ArgumentNullException( nameof(nextSegment), $"{nextSegment} may not be null; if there is no {nextSegment}, use another ctor overload."); } _segmentSize = nextSegment.SegmentSize; _nextSegment = nextSegment; _nextSegmentGlobalCount = nextSegment.GlobalCount; _data = new T[_segmentSize]; _localCount = 0; }
public GrowingCollectionSegment(int segmentSize) { if (segmentSize < 1 || 1024 < segmentSize) { throw new ArgumentOutOfRangeException( nameof(segmentSize), $"{segmentSize} must be in range 1..1024, but the specified value is {segmentSize}."); } _segmentSize = segmentSize; _nextSegment = null; _nextSegmentGlobalCount = 0; _data = new T[segmentSize]; _localCount = 0; }
/// <summary>Move to the next element in the underlying colection.</summary> /// <returns>The next element in the underlying collection.</returns> public bool MoveNext() { if (_currentSegmentOffset == 0) { if (_currentSegment.NextSegment == null) { return(false); } else { _currentSegment = _currentSegment.NextSegment; _currentSegmentOffset = _currentSegment.LocalCount - 1; return(true); } } else { _currentSegmentOffset--; return(true); } }
/// <summary>Initializes a new instance of the <see cref="GrowingCollection{T}"/> class. /// </summary> /// <param name="segmentSize">The collection will grow in chunks of <c>segmentSize</c> items.</param> public GrowingCollection(int segmentSize) : base(segmentSize) { _dataHead = this; }
/// <summary>Initializes a new instance of the <see cref="GrowingCollection{T}"/> class. /// </summary> public GrowingCollection() : base(DefaultSegmentSize) { _dataHead = this; }
/// <summary>Restarts this enumerator to the same state as it was created in.</summary> public void Reset() { _currentSegment = _head; _currentSegmentOffset = _headOffset; }