Skip to content

Instantly share code, notes, and snippets.

@anavarro9731
Created February 17, 2012 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anavarro9731/1851315 to your computer and use it in GitHub Desktop.
Save anavarro9731/1851315 to your computer and use it in GitHub Desktop.
remove for circular buffer
/// <summary>
/// Removes an item from the collection. If the item removed occurs at or before the current position the current position is moved back by one.
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public bool Remove(T item)
{
if (Contains(item))
{
T[] _newStore = new T[_capacity];
int? skippedAt = null;
EqualityComparer<T> comparer = EqualityComparer<T>.Default;
for (int i = 0; i < _size; i++)
{
_newStore[(skippedAt != null ? i - 1 : i)] = _store[i];
if (comparer.Equals(_store[i], item))
{
skippedAt = i;
continue;
}
}
_size--;
if (_start >= skippedAt && _start > 0)
{
_start--;
}
_store = _newStore;
_version++;
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment