Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.