/* * Process Hacker - * circular buffer * * Copyright (C) 2009 wj32 * * This file is part of Process Hacker. * * Process Hacker is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Process Hacker is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Process Hacker. If not, see . */ /* If enabled, the sizes of circular buffers are limited to (and rounded up to) * powers of two, increasing performance. */ #define POWER_OF_TWO_SIZE using System; using System.Collections.Generic; namespace ProcessHacker.Common { /// /// Provides methods for manipulating a circular buffer. A circular buffer /// is a fixed-size array where old elements will be automatically deleted /// as new elements are added. /// /// /// This data structure is not thread-safe. You must provide your own /// synchronization if more than one thread reads from or writes to the /// buffer. /// /// /// Ten-element circular buffer: /// Data array: [4] [3] [2] [1] [0] [9] [8] [7] [6] [5] /// ^ most recent data /// ^ index /// public class CircularBuffer : IList { private int _size; #if POWER_OF_TWO_SIZE private int _sizeMinusOne; #endif private int _count; private int _index; private T[] _data; /// /// Creates a new circular buffer of the specified size. /// /// The size of the buffer. public CircularBuffer(int size) { /* * [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] * ^ _index */ #if POWER_OF_TWO_SIZE _size = size.RoundUpTwo(); _sizeMinusOne = _size - 1; #else _size = size; #endif _count = 0; _index = 0; _data = new T[_size]; } public CircularBuffer(System.IO.Stream s) { byte[] buf = new byte[sizeof(int)]; s.Read(buf, 0, 4); _size = buf.ToInt32(); #if POWER_OF_TWO_SIZE _sizeMinusOne = _size - 1; #endif s.Read(buf, 0, 4); _count = buf.ToInt32(); s.Read(buf, 0, 4); _index = buf.ToInt32(); _data = new T[_size]; int i = 0; int length = Buffer.ByteLength(_data); buf = new byte[256]; while (length > 0) { int readLength = length > buf.Length ? buf.Length : length; s.Read(buf, 0, readLength); length -= readLength; Buffer.BlockCopy(buf, 0, _data, i, readLength); i += readLength; } } /// /// Gets or sets an element in the buffer. This is guaranteed to /// never throw an exception. /// /// /// A zero-based index into the buffer. Index 0 contains the /// most recently added item, and higher positive indicies /// access less recent items. Index -1 contains the least recently /// added item, and lower negative indicies access more recent /// items. /// public T this[int index] { get { /* * For example, if _index = 6 and index = 5: * * [5] [4] [3] [2] [1] [0] [9] [8] [7] [6] * ^ _index * ^ (_index + index) mod _size = 11 mod 10 = 1 */ #if POWER_OF_TWO_SIZE // See the comment in Add for more details on power-of-two modulus. return _data[(_index + index) & _sizeMinusOne]; #else // See the comment in Add for more details on modulus. return _data[(((_index + index) % _size) + _size) % _size]; #endif } set { #if POWER_OF_TWO_SIZE // See the comment in Add for more details on power-of-two modulus. _data[(_index + index) & _sizeMinusOne] = value; #else // See the comment in Add for more details. _data[(((_index + index) % _size) + _size) % _size] = value; #endif } } /// /// Gets the number of elements stored in the buffer. /// public int Count { get { return _count; } private set { _count = value; } } /// /// Gets the maximum number of elements that can be stored in /// the buffer. /// public int Size { get { return _size; } } /// /// Adds an element to the buffer. If the maximum buffer size /// has been reached, the least recently added element will /// be erased by the new element. /// /// The element to add. public void Add(T value) { /* * To add an item to the circular buffer the index is * decremented and a modulus is performed on it to ensure * it is not negative. * * [5] [4] [3] [2] [1] [0] [9] [8] [7] [6] * ^ _index (6) * When the new element x is added: * [5] [4] [3] [2] [1] [x] [9] [8] [7] [6] * ^ _index (5) * * Another example: * [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] * ^ _index (0) * When the new element x is added: * [9] [8] [7] [6] [5] [4] [3] [2] [1] [x] * ^ _index (9) * = -1 mod 10 = 9 */ #if POWER_OF_TWO_SIZE /* Power-of-two modulus can be performed quickly by ANDing the * value by the power-of-two minus one. This even works for * negative numbers. */ _data[_index = ((_index - 1) & _sizeMinusOne)] = value; #else /* The C# modulus operator produces a result which has the * same sign as the dividend. For circular array access, * we want the result to have the same sign as the divisor. * We do this by using r = ((i % t) + t) % t where i is * the index (possibly negative) and t is the size of the * array. */ _data[_index = (((_index - 1) % _size) + _size) % _size] = value; #endif if (_count < _size) _count++; } /// /// Resizes the circular buffer. /// /// The new maximum buffer size. public void Resize(int newSize) { newSize = newSize.RoundUpTwo(); // If we're not actually resizing the thing... if (newSize == _size) return; T[] newArray = new T[newSize]; int tailSize = (_size - _index) % _size; int headSize = this.Count - tailSize; /* * The tail contains the most recent data. * [3] [2] [1] [0] [ ] [8] [7] [6] [5] [4] * [ ... head ... ] [ ..... tail ..... ] * ^ _index (5) * tailSize = _size - _index = 5 * headSize = _count - tailSize = 9 - 5 = 4 */ // If the new buffer is bigger than the current one. if (newSize > _size) { /* * Copy the tail, then the head. * This means that the tail will now be at the front. * [8] [7] [6] [5] [4] [3] [2] [1] [0] [ ] [ ] [ ] * [ ..... tail ..... ][ ... head ... ] * ^ _index (0) */ Array.Copy(_data, _index, newArray, 0, tailSize); Array.Copy(_data, 0, newArray, tailSize, headSize); _index = 0; } // If the new buffer is smaller than the current one. else if (newSize < _size) { // If the new buffer is smaller than (or equal to) the tail size. if (tailSize >= newSize) { /* * Copy only a part of the tail because we don't have enough room. * [8] [7] [6] * [ . tail . ] * ^ _index (0) */ Array.Copy(_data, _index, newArray, 0, newSize); _index = 0; } // If the new buffer is bigger than the tail size. else { /* * Copy the tail in full, then copy a part of the head. * [8] [7] [6] [5] [4] [3] [2] * [ ..... tail ..... ][ head ] * ^ _index (0) */ Array.Copy(_data, _index, newArray, 0, tailSize); Array.Copy(_data, 0, newArray, tailSize, newSize - tailSize); _index = 0; } // The number of elements obviously can't be bigger than the // buffer size. if (this.Count > newSize) this.Count = newSize; } _data = newArray; _size = newSize; _sizeMinusOne = _size - 1; } public void Save(System.IO.Stream s) { s.Write(_size.GetBytes(), 0, sizeof(int)); s.Write(_count.GetBytes(), 0, sizeof(int)); s.Write(_index.GetBytes(), 0, sizeof(int)); int i = 0; int length = Buffer.ByteLength(_data); byte[] buf = new byte[256]; while (length > 0) { int writeLength = length > buf.Length ? buf.Length : length; Buffer.BlockCopy(_data, i, buf, 0, writeLength); length -= writeLength; s.Write(buf, 0, writeLength); i += writeLength; } } /// /// Converts the buffer to an array. /// /// public T[] ToArray() { T[] newArray = new T[this.Count]; this.CopyTo(newArray, 0); return newArray; } #region IList Members /// /// Gets the index of the specified element in the array. /// /// The element to search for. /// A positive index if the element was found. Otherwise, -1. public int IndexOf(T item) { for (int i = 0; i < this.Count; i++) if (this[i].Equals(item)) return i; return -1; } /// /// This method is not supported. /// public void Insert(int index, T item) { throw new NotSupportedException(); } /// /// This method is not supported. /// public void RemoveAt(int index) { throw new NotSupportedException(); } #endregion #region ICollection Members /// /// Clears the buffer. /// public void Clear() { // Just set the number of elements to zero. this.Count = 0; } /// /// Gets whether the buffer contains the specified element. /// /// The element to search for. /// Whether the element is present. public bool Contains(T item) { return this.IndexOf(item) != -1; } /// /// Copies the elements of the buffer to the specified array. /// /// The array to copy to. /// The index of the destination array at which to begin copying. public void CopyTo(T[] array, int arrayIndex) { /* * We have to make sure we don't copy unused elements. * [2] [1] [0] [ ] [ ] [ ] [6] [5] [4] [3] * [ . head . ] [ ... tail ... ] * ^ _index (6) * tailSize = _size - _index = 10 - 6 = 4 * headSize = _count - tailSize = 7 - 4 = 3 */ int tailSize = _size - _index; int headSize = this.Count - tailSize; // Copy the tail, then the head. Array.Copy(_data, _index, array, arrayIndex, tailSize); Array.Copy(_data, 0, array, arrayIndex + tailSize, headSize); } /// /// Gets whether the buffer is read-only. /// public bool IsReadOnly { get { return false; } } /// /// This method is not supported. /// public bool Remove(T item) { throw new NotSupportedException(); } #endregion #region IEnumerable Members /// /// Gets an enumerator for the buffer. /// public IEnumerator GetEnumerator() { for (int i = 0; i < this.Count; i++) yield return this[i]; } #endregion #region IEnumerable Members /// /// Gets an enumerator for the buffer. /// System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { for (int i = 0; i < this.Count; i++) yield return this[i]; } #endregion } }