Esempio n. 1
0
        // 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));
            }
        }
        // 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;
            }
        }
        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;
        }
Esempio n. 4
0
        // 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++;
            }
        }
Esempio n. 5
0
        // 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++;
        }
Esempio n. 6
0
        // 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);
        }
Esempio n. 7
0
        // 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++;
        }
        //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;
        }