Example #1
0
        /// <summary>
        /// Gets or sets an item to the DArray.
        /// </summary>
        /// <param name="index">Index of the item.</param>
        /// <returns>Item at the given index.</returns>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is not a valid index. Use
        /// <see cref="CanGet"/> to make sure the index is valid.</exception>
        public T this[int index]
        {
            get
            {
                // Check for if out of range
                if (index > _highestIndex || index < 0)
                {
                    throw new ArgumentOutOfRangeException("index");
                }

                // Return the item at the index
                return(_buffer[index]);
            }
            set
            {
                // Check if desired value exceeds the current buffer size - if so, resize
                if (index >= _buffer.Length)
                {
                    ResizeBuffer(Math.Max(_buffer.Length * 2, index + 1));
                }

                // If greater than the highest index, update the new highest index
                if (index > _highestIndex)
                {
                    if (_trackFree)
                    {
                        // Since we created a new value of the last highest value, we will
                        // need to push all indices skipped into the stack of free indices
                        for (var i = _highestIndex + 1; i < index; i++)
                        {
                            _freeIndices.Push(i);
                        }
                    }
                    _highestIndex = index;
                }

                // Set the item and increase the version
                _isIndexUsed[index] = true;
                _buffer[index]      = value;
                _version++;

                // Notify any listeners an item has been added
                OnItemAdded(value, index);

                if (ItemAdded != null)
                {
                    ItemAdded.Raise(this, new DArrayEventArgs <T>(value, index));
                }
            }
        }
 /// <summary>
 /// Raises the ItemAdded event</summary>
 /// <param name="e">Event args</param>
 protected virtual void OnItemAdded(ItemInsertedEventArgs <T> e)
 {
     ItemAdded.Raise(this, e);
 }