Created
February 17, 2012 06:36
-
-
Save anavarro9731/1851315 to your computer and use it in GitHub Desktop.
remove for circular buffer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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