// Removes a range of elements from this list. // public void RemoveRange(int index, int count) { if (index < 0) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); } if (count < 0) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); } if (_size - index < count) { ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen); } #if DEBUG Contract.EndContractBlock(); #endif if (count > 0) { int i = _size; _size -= count; if (index < _size) { ClusteredArray <T> .Copy(_items, index + count, _items, index, _size - index); } ClusteredArray <T> .Clear(_items, _size, count); _version++; } }
//Changelog: Used ClusteredArray's internal CopyFrom method. public ClusteredMemoryStream(byte[] buffer, int index, int count, bool writable, bool publiclyVisible) { if (buffer == null) { throw new ArgumentNullException("buffer", ResourceHelper.GetResourceString("ArgumentNull_Buffer")); } if (index < 0) { throw new ArgumentOutOfRangeException("index", ResourceHelper.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (count < 0) { throw new ArgumentOutOfRangeException("count", ResourceHelper.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (buffer.Length - index < count) { throw new ArgumentException(ResourceHelper.GetResourceString("Argument_InvalidOffLen")); } #if DEBUG Contract.EndContractBlock(); #endif _buffer = new ClusteredArray <byte>(buffer.Length); _buffer.CopyFrom(buffer, 0, 0, buffer.Length); _origin = _position = index; _length = _capacity = index + count; _writable = writable; _exposable = publiclyVisible; // Can GetBuffer return the array? _expandable = false; _isOpen = true; }
public static void Clear(ClusteredArray <T> array, int index, int length) { if (index < 0 || index > array._length) { throw new ArgumentOutOfRangeException("index"); } if (length < 0 || index + length > array._length) { throw new ArgumentOutOfRangeException("length"); } int _lengthThreshold = array._lengthThreshold; int superStartIndex = (index / _lengthThreshold); int superEndIndex = ((index + length) / _lengthThreshold); int chunkIndex = index % _lengthThreshold; if (superStartIndex == superEndIndex) { Array.Clear(array._chunks[superStartIndex], index % _lengthThreshold, length); return; } for (int i = superStartIndex; i <= superEndIndex; i++) { int toBeCleared = i == superStartIndex ? _lengthThreshold - chunkIndex : i == superEndIndex ? length % _lengthThreshold : 0; Array.Clear(array._chunks[i], i == superStartIndex ? chunkIndex : 0, toBeCleared); length -= toBeCleared; } }
public override void WriteByte(byte value) { if (!_isOpen) { __Error.StreamIsClosed(); } EnsureWriteable(); if (_position >= _length) { int newLength = _position + 1; bool mustZero = _position > _length; if (newLength >= _capacity) { bool allocatedNewArray = EnsureCapacity(newLength); if (allocatedNewArray) { mustZero = false; } } if (mustZero) { ClusteredArray <byte> .Clear(_buffer, _length, _position - _length); } _length = newLength; } _buffer[_position++] = value; }
// Sets the length of the stream to a given value. The new // value must be nonnegative and less than the space remaining in // the array, Int32.MaxValue - origin // Origin is 0 in all cases other than a MemoryStream created on // top of an existing array and a specific starting offset was passed // into the MemoryStream constructor. The upper bounds prevents any // situations where a stream may be created on top of an array then // the stream is made longer than the maximum possible length of the // array (Int32.MaxValue). // public override void SetLength(long value) { if (value < 0 || value > Int32.MaxValue) { throw new ArgumentOutOfRangeException("value", ResourceHelper.GetResourceString("ArgumentOutOfRange_StreamLength")); } #if DEBUG Contract.Ensures(_length - _origin == value); Contract.EndContractBlock(); #endif EnsureWriteable(); // Origin wasn't publicly exposed above. #if DEBUG Contract.Assert(MemStreamMaxLength == Int32.MaxValue); // Check parameter validation logic in this method if this fails. #endif if (value > (Int32.MaxValue - _origin)) { throw new ArgumentOutOfRangeException("value", ResourceHelper.GetResourceString("ArgumentOutOfRange_StreamLength")); } int newLength = _origin + (int)value; bool allocatedNewArray = EnsureCapacity(newLength); if (!allocatedNewArray && newLength > _length) { ClusteredArray <byte> .Clear(_buffer, _length, newLength - _length); } _length = newLength; if (_position > newLength) { _position = newLength; } }
// Removes all Objects from the queue. /// <include file='doc\Queue.uex' path='docs/doc[@for="Queue.Clear"]/*' /> public virtual void Clear() { ClusteredArray <T> oldArray = null; if (_transaction != null) { oldArray = _array; _array = new ClusteredArray <T>(0); } else if (_head < _tail) { ClusteredArray <T> .Clear(_array, _head, _size); } else { ClusteredArray <T> .Clear(_array, _head, _array.Length - _head); ClusteredArray <T> .Clear(_array, 0, _tail); } _head = 0; _tail = 0; _size = 0; _version++; if (_transaction != null) { _transaction.AddRollbackOperation(new ClearRollbackOperation(this, oldArray)); } }
public ClusteredList <T> GetRange(int index, int count) { if (index < 0) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); } if (count < 0) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); } if (_size - index < count) { ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen); } #if DEBUG Contract.Ensures(Contract.Result <ClusteredList <T> >() != null); Contract.EndContractBlock(); #endif ClusteredList <T> list = new ClusteredList <T>(count); ClusteredArray <T> .Copy(_items, index, list._items, 0, count); list._size = count; return(list); }
public object Clone() { ClusteredArray <T> newarray = new ClusteredArray <T>(_length); Copy(this, 0, newarray, 0, _length); return(newarray); }
// Inserts the elements of the given collection at a given index. If // required, the capacity of the list is increased to twice the previous // capacity or the new size, whichever is larger. Ranges may be added // to the end of the list by setting index to the List's size. // public void InsertRange(int index, IEnumerable <T> collection) { if (collection == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection); } if ((uint)index > (uint)_size) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_Index); } #if DEBUG Contract.EndContractBlock(); #endif ICollection <T> c = collection as ICollection <T>; if (c != null) { int count = c.Count; if (count > 0) { EnsureCapacity(_size + count); if (index < _size) { ClusteredArray <T> .Copy(_items, index, _items, index + count, _size - index); } // If we're inserting a List into itself, we want to be able to deal with that. if (this == c) { // Copy first part of _items to insert location ClusteredArray <T> .Copy(_items, 0, _items, index, index); // Copy last part of _items back to inserted location ClusteredArray <T> .Copy(_items, index + count, _items, index * 2, _size - index); } else { T[] itemsToInsert = new T[count]; c.CopyTo(itemsToInsert, 0); _items.CopyFrom(itemsToInsert, 0, index, count); } _size += count; } } else { using (IEnumerator <T> en = collection.GetEnumerator()) { while (en.MoveNext()) { Insert(index++, en.Current); } } } _version++; }
public static void Copy(ClusteredArray <T> source, int sourceIndex, ClusteredArray <T> destination, int destinationIndex, int length) { if (source == null) { throw new ArgumentNullException("source"); } if (destination == null) { throw new ArgumentNullException("destination"); } if (sourceIndex > source.Length) { throw new ArgumentOutOfRangeException("sourceIndex"); } if (destinationIndex > destination.Length) { throw new ArgumentOutOfRangeException("destinationIndex"); } if ((sourceIndex + length) > source.Length) { throw new ArgumentOutOfRangeException("length"); } if ((destinationIndex + length) > destination.Length) { throw new ArgumentOutOfRangeException("length"); } int lengthThreshold = source._lengthThreshold; VirtualIndex srcIndex = new VirtualIndex(source._lengthThreshold, sourceIndex); VirtualIndex dstIndex = new VirtualIndex(source._lengthThreshold, destinationIndex); int dataCopied = 0; while (dataCopied < length) { T[] srcArray = source._chunks[srcIndex.YIndex]; T[] dstArray = destination._chunks[dstIndex.YIndex]; int dataRemaining = length - dataCopied; int dstSpaceLeft = dstArray.Length - dstIndex.XIndex; int data2Copy = dataRemaining > dstSpaceLeft ? dstSpaceLeft : dataRemaining; int srcSpaceLeft = srcArray.Length - srcIndex.XIndex; if (data2Copy > srcSpaceLeft) { data2Copy = srcSpaceLeft; } Array.Copy(srcArray, srcIndex.XIndex, dstArray, dstIndex.XIndex, data2Copy); dataCopied += data2Copy; srcIndex.IncrementBy(data2Copy); dstIndex.IncrementBy(data2Copy); } }
// Clears the contents of List. public void Clear() { if (_size > 0) { ClusteredArray <T> .Clear(_items, 0, _size); // Don't need to doc this but we clear the elements so that the gc can reclaim the references. _size = 0; } _version++; }
// Creates a queue with room for capacity objects. The default grow factor // is used. // /// <include file='doc\Queue.uex' path='docs/doc[@for="Queue.Queue1"]/*' /> public ClusteredQueue(int capacity) { if (capacity < 0) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.capacity); } _array = new ClusteredArray <T>(capacity); _head = 0; _tail = 0; _size = 0; }
// Removes the element at the given index. The size of the list is // decreased by one. // public void RemoveAt(int index) { #if DEBUG Contract.EndContractBlock(); #endif _size--; if (index < _size) { ClusteredArray <T> .Copy(_items, index + 1, _items, index, _size - index); } _items[_size] = default(T); _version++; }
public ClusteredMemoryStream(ClusteredArray <byte> buffer) { if (buffer == null) { throw new ArgumentNullException("buffer", ResourceHelper.GetResourceString("ArgumentNull_Buffer")); } #if DEBUG Contract.EndContractBlock(); #endif _buffer = buffer; _length = _capacity = buffer.Length; _writable = true; _exposable = false; _origin = 0; _isOpen = true; }
public ClusteredMemoryStream(IList buffers, bool writable) { int position = 0; _buffer = new ClusteredArray <byte>(0); foreach (byte[] bytes in buffers) { _buffer.CopyFrom(bytes, 0, position, bytes.Length); position += bytes.Length; } _length = _capacity = position; _writable = writable; _expandable = false; _origin = 0; _isOpen = true; }
// Creates a queue with room for capacity objects with the given grow factor // /// <include file='doc\Queue.uex' path='docs/doc[@for="Queue.Queue1"]/*' /> public ClusteredQueue(int capacity, float growFactor) { if (capacity < 0) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.capacity); } if (!(growFactor >= 1.0 && growFactor <= 10.0)) { throw new ArgumentOutOfRangeException("growFactor", ResourceHelper.GetResourceString("ArgumentOutOfRange_QueueGrowFactor")); } _array = new ClusteredArray <T>(capacity); _head = 0; _tail = 0; _size = 0; _GrowFactor = (int)(growFactor * 100); }
public ClusteredMemoryStream(int capacity) { if (capacity < 0) { throw new ArgumentOutOfRangeException("capacity", ResourceHelper.GetResourceString("ArgumentOutOfRange_NegativeCapacity")); } #if DEBUG Contract.EndContractBlock(); #endif _buffer = new ClusteredArray <byte>(capacity); _capacity = capacity; _expandable = true; _writable = true; _exposable = true; _origin = 0; // Must be 0 for byte[]'s created by MemoryStream _isOpen = true; }
// This method removes all items which matches the predicate. // The complexity is O(n). public int RemoveAll(Predicate <T> match) { if (match == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match); } #if DEBUG Contract.Ensures(Contract.Result <int>() >= 0); Contract.Ensures(Contract.Result <int>() <= Contract.OldValue(Count)); Contract.EndContractBlock(); #endif int freeIndex = 0; // the first free slot in items array // Find the first item which needs to be removed. while (freeIndex < _size && !match(_items[freeIndex])) { freeIndex++; } if (freeIndex >= _size) { return(0); } int current = freeIndex + 1; while (current < _size) { // Find the first item which needs to be kept. while (current < _size && match(_items[current])) { current++; } if (current < _size) { // copy item to the free slot. _items[freeIndex++] = _items[current++]; } } ClusteredArray <T> .Clear(_items, freeIndex, _size - freeIndex); int result = _size - freeIndex; _size = freeIndex; _version++; return(result); }
public ClusteredList(int capacity) { if (capacity < 0) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.capacity, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); } #if DEBUG Contract.EndContractBlock(); #endif if (capacity == 0) { _items = _emptyArray; } else { _items = new ClusteredArray <T>(capacity); } }
// Removes all Objects from the queue. /// <include file='doc\Queue.uex' path='docs/doc[@for="Queue.Clear"]/*' /> public virtual void Clear() { if (_head < _tail) { ClusteredArray <T> .Clear(_array, _head, _size); } else { ClusteredArray <T> .Clear(_array, _head, _array.Length - _head); ClusteredArray <T> .Clear(_array, 0, _tail); } _head = 0; _tail = 0; _size = 0; _version++; }
// Fills a Queue with the elements of an ICollection. Uses the enumerator // to get each of the elements. // /// <include file='doc\Queue.uex' path='docs/doc[@for="Queue.Queue3"]/*' /> public ClusteredQueue(IEnumerable <T> collection) { if (collection == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection); } _array = new ClusteredArray <T>(_DefaultCapacity); _size = 0; _version = 0; using (IEnumerator <T> en = collection.GetEnumerator()) { while (en.MoveNext()) { Enqueue(en.Current); } } }
// Constructs a List, copying the contents of the given collection. The // size and capacity of the new list will both be equal to the size of the // given collection. // public ClusteredList(IEnumerable <T> collection) { if (collection == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection); } #if DEBUG Contract.EndContractBlock(); #endif ICollection <T> c = collection as ICollection <T>; if (c != null) { int count = c.Count; if (count == 0) { _items = _emptyArray; } else { _items = new ClusteredArray <T>(count); AddRange(c); } } else { _size = 0; _items = _emptyArray; // This enumerable could be empty. Let Add allocate a new array, if needed. // Note it will also go to _defaultCapacity first, not 1, then 2, etc. using (IEnumerator <T> en = collection.GetEnumerator()) { while (en.MoveNext()) { Add(en.Current); } } } }
// Inserts an element into this list at a given index. The size of the list // is increased by one. If required, the capacity of the list is doubled // before inserting the new element. // public void Insert(int index, T item) { // Note that insertions at the end are legal. if ((uint)index > (uint)_size) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_ListInsert); } #if DEBUG Contract.EndContractBlock(); #endif if (_size == _items.Length) { EnsureCapacity(_size + 1); } if (index < _size) { ClusteredArray <T> .Copy(_items, index, _items, index + 1, _size - index); } _items[index] = item; _size++; _version++; }
// PRIVATE Grows or shrinks the buffer to hold capacity objects. Capacity // must be >= _size. private void SetCapacity(int capacity) { ClusteredArray <T> newarray = new ClusteredArray <T>(capacity); if (_size > 0) { if (_head < _tail) { ClusteredArray <T> .Copy(_array, _head, newarray, 0, _size); } else { ClusteredArray <T> .Copy(_array, _head, newarray, 0, _array.Length - _head); ClusteredArray <T> .Copy(_array, 0, newarray, _array.Length - _head, _tail); } } _array = newarray; _head = 0; _tail = (_size == capacity) ? 0 : _size; _version++; }
public ClearRollbackOperation(ClusteredQueue <T> parent, ClusteredArray <T> items) { _items = items; _parent = parent; }
// Creates a queue with room for capacity objects. The default initial // capacity and grow factor are used. /// <include file='doc\Queue.uex' path='docs/doc[@for="Queue.Queue"]/*' /> public ClusteredQueue() { _array = _emptyArray; }
public ClusteredList() { _items = _emptyArray; }
//Changelog: Used ClusteredArray's internal CopyFrom method. public override void Write(byte[] buffer, int offset, int count) { if (buffer == null) { throw new ArgumentNullException("buffer", ResourceHelper.GetResourceString("ArgumentNull_Buffer")); } if (offset < 0) { throw new ArgumentOutOfRangeException("offset", ResourceHelper.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (count < 0) { throw new ArgumentOutOfRangeException("count", ResourceHelper.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (buffer.Length - offset < count) { throw new ArgumentException(ResourceHelper.GetResourceString("Argument_InvalidOffLen")); } #if DEBUG Contract.EndContractBlock(); #endif if (!_isOpen) { __Error.StreamIsClosed(); } EnsureWriteable(); int i = _position + count; // Check for overflow if (i < 0) { throw new IOException(ResourceHelper.GetResourceString("IO.IO_StreamTooLong")); } if (i > _length) { bool mustZero = _position > _length; if (i > _capacity) { bool allocatedNewArray = EnsureCapacity(i); if (allocatedNewArray) { mustZero = false; } } if (mustZero) { ClusteredArray <byte> .Clear(_buffer, _length, i - _length); } _length = i; } if (count <= 8) { int byteCount = count; while (--byteCount >= 0) { _buffer[_position + byteCount] = buffer[offset + byteCount]; } } else { _buffer.CopyFrom(buffer, offset, _position, count); } _position = i; }