-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
043e6f6
commit 8d53513
Showing
4 changed files
with
169 additions
and
31 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
#region Copyright & License Information | ||
/* | ||
* Copyright 2007-2022 The OpenRA Developers (see AUTHORS) | ||
* This file is part of OpenRA, which is free software. It is made | ||
* available to you 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. For more | ||
* information, see COPYING. | ||
*/ | ||
#endregion | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace OpenRA.Primitives | ||
{ | ||
/// <summary>Fixed size rorating buffer backed by an array.</summary> | ||
public class RingBuffer<T> : ICollection<T>, IEnumerable<T> | ||
{ | ||
readonly IComparer<T> comparer; | ||
readonly T[] values; | ||
int start; | ||
int count; | ||
|
||
public int Capacity => values.Length; | ||
public int Count => count; | ||
public bool IsReadOnly => false; | ||
|
||
public RingBuffer(int capacity, IComparer<T> comparer) | ||
{ | ||
this.comparer = comparer; | ||
values = new T[capacity]; | ||
start = 0; | ||
count = 0; | ||
} | ||
|
||
public RingBuffer(int capacity) | ||
: this(capacity, Comparer<T>.Default) { } | ||
|
||
public void Add(T value) | ||
{ | ||
values[(start + count) % values.Length] = value; | ||
if (count < values.Length) | ||
count++; | ||
else | ||
start = (start + 1) % values.Length; | ||
} | ||
|
||
public void Clear() | ||
{ | ||
Array.Clear(values, 0, values.Length); | ||
count = 0; | ||
} | ||
|
||
public bool Contains(T value) | ||
{ | ||
var capacity = values.Length; | ||
var end = start + count; | ||
for (var i = start; i < end; ++i) | ||
if (comparer.Compare(values[i % capacity], value) == 0) | ||
return true; | ||
|
||
return false; | ||
} | ||
|
||
public void CopyTo(T[] array, int arrayIndex) | ||
{ | ||
if (array == null) | ||
throw new ArgumentNullException("Target array is null"); | ||
|
||
if (arrayIndex < 0) | ||
throw new ArgumentNullException("Index is less than 0"); | ||
|
||
if (arrayIndex + count >= array.Length) | ||
throw new ArgumentException("Invalid array capactiy"); | ||
|
||
var di = arrayIndex; | ||
var end = start + count; | ||
var capacity = values.Length; | ||
for (var i = start; i < end; ++i) | ||
array[di++] = values[i % capacity]; | ||
} | ||
|
||
public bool Remove(T value) | ||
{ | ||
var capacity = values.Length; | ||
var end = start + count; | ||
for (var i = start; i < end; ++i) | ||
if (comparer.Compare(values[i % capacity], value) == 0) | ||
{ | ||
end--; | ||
for (var j = i; j < end; ++j) | ||
values[j % capacity] = values[(j + 1) % capacity]; | ||
|
||
count--; | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public T this[int pos] | ||
{ | ||
get => values[(start + pos) % values.Length]; | ||
|
||
set | ||
{ | ||
if (pos >= count) | ||
throw new ArgumentException($"Index out of bounds: {pos}"); | ||
|
||
values[(start + pos) % values.Length] = value; | ||
} | ||
} | ||
|
||
public T First() | ||
{ | ||
if (count == 0) | ||
throw new ArgumentException("Empty buffer"); | ||
|
||
return values[start]; | ||
} | ||
|
||
public T Last() | ||
{ | ||
if (count == 0) | ||
throw new ArgumentException("Empty buffer"); | ||
|
||
return values[(start + count - 1) % values.Length]; | ||
} | ||
|
||
public IEnumerator<T> GetEnumerator() | ||
{ | ||
var initState = start + count; | ||
var index = start; | ||
for (var i = 0; i < count; i++) | ||
{ | ||
if (start + count != initState) | ||
throw new InvalidOperationException("Collection was modified; enumeration operation may not execute"); | ||
yield return values[i % values.Length]; | ||
} | ||
} | ||
|
||
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); } | ||
} | ||
} |
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
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
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