DD.WellWorkover.Cloud/AsbCloudApp/CyclicArray.cs

195 lines
5.2 KiB
C#
Raw Permalink Normal View History

using System.Linq;
2022-11-03 16:57:41 +05:00
2024-08-19 10:01:07 +05:00
namespace System.Collections.Generic;
/// <summary>
/// Цикличный массив
/// </summary>
/// <typeparam name="T"></typeparam>
public class CyclicArray<T> : IEnumerable<T>
2022-11-03 16:57:41 +05:00
{
2024-08-19 10:01:07 +05:00
readonly T[] array;
int used, current = -1;
2022-11-03 16:57:41 +05:00
/// <summary>
2024-08-19 10:01:07 +05:00
/// constructor
2022-11-03 16:57:41 +05:00
/// </summary>
2024-08-19 10:01:07 +05:00
/// <param name="capacity"></param>
public CyclicArray(int capacity)
2022-11-03 16:57:41 +05:00
{
2024-08-19 10:01:07 +05:00
array = new T[capacity];
}
/// <summary>
/// Количество элементов в массиве
/// </summary>
public int Count => used;
/// <summary>
/// Добавить новый элемент<br/>
/// Если capacity достигнуто, то вытеснит самый первый элемент
/// </summary>
/// <param name="item"></param>
public void Add(T item)
{
current = (++current) % array.Length;
array[current] = item;
if (used < array.Length)
used++;
UpdatedInvoke(current, item);
}
2022-11-03 16:57:41 +05:00
2024-08-19 10:01:07 +05:00
/// <summary>
/// Добавить новые элементы.<br/>
/// Если capacity достигнуто, то вытеснит самые первые элементы.<br/>
/// Не вызывает Updated!
/// </summary>
/// <param name="items"></param>
public void AddRange(IEnumerable<T> items)
{
var capacity = array.Length;
var newItems = items.TakeLast(capacity).ToArray();
if (newItems.Length == capacity)
2022-11-03 16:57:41 +05:00
{
2024-08-19 10:01:07 +05:00
Array.Copy(newItems, array, capacity);
current = capacity - 1;
2022-11-03 16:57:41 +05:00
}
2024-08-19 10:01:07 +05:00
else
2022-11-03 16:57:41 +05:00
{
2024-08-19 10:01:07 +05:00
current = (++current) % capacity;
var countToEndOfArray = capacity - current;
if (newItems.Length <= countToEndOfArray)
2022-11-03 16:57:41 +05:00
{
2024-08-19 10:01:07 +05:00
Array.Copy(newItems, 0, array, current, newItems.Length);
current += newItems.Length - 1;
2022-11-03 16:57:41 +05:00
}
else
{
2024-08-19 10:01:07 +05:00
var firstStepLength = countToEndOfArray;
Array.Copy(newItems, 0, array, current, firstStepLength);
var secondStepCount = newItems.Length - firstStepLength;
Array.Copy(newItems, firstStepLength, array, 0, secondStepCount);
current = secondStepCount - 1;
2022-11-03 16:57:41 +05:00
}
}
2024-08-19 10:01:07 +05:00
if (used < capacity)
2022-11-03 16:57:41 +05:00
{
2024-08-19 10:01:07 +05:00
used += newItems.Length;
used = used > capacity ? capacity : used;
2022-11-03 16:57:41 +05:00
}
2024-08-19 10:01:07 +05:00
}
2022-11-03 16:57:41 +05:00
2024-08-19 10:01:07 +05:00
/// <summary>
/// Индекс
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public T this[int index]
{
get
2022-11-03 16:57:41 +05:00
{
2024-08-19 10:01:07 +05:00
if (used == 0)
throw new IndexOutOfRangeException();
2022-11-03 16:57:41 +05:00
2024-08-19 10:01:07 +05:00
var i = (current + 1 + index) % used;
return array[i];
}
set
2022-11-03 16:57:41 +05:00
{
2024-08-19 10:01:07 +05:00
var devider = used > 0 ? used : array.Length;
var i = (current + 1 + index) % devider;
array[i] = value;
UpdatedInvoke(current, value);
2022-11-03 16:57:41 +05:00
}
2024-08-19 10:01:07 +05:00
}
2022-11-03 16:57:41 +05:00
2024-08-19 10:01:07 +05:00
/// <summary>
/// событие на изменение элемента в массиве
/// </summary>
public event EventHandler<(int index, T value)>? Updated;
private void UpdatedInvoke(int index, T value)
{
Updated?.Invoke(this, (index, value));
}
2022-11-03 16:57:41 +05:00
2024-08-19 10:01:07 +05:00
/// <summary>
/// Агрегирование значения по всему массиву
/// </summary>
/// <typeparam name="Tout"></typeparam>
/// <param name="func"></param>
/// <param name="startValue"></param>
/// <returns></returns>
public Tout Aggregate<Tout>(Func<T, Tout, Tout> func, Tout startValue)
{
Tout result = startValue;
for (int i = 0; i < used; i++)
result = func(this[i], result);
return result;
}
2022-11-03 16:57:41 +05:00
2024-08-19 10:01:07 +05:00
/// <inheritdoc/>
public IEnumerator<T> GetEnumerator()
=> new CyclycListEnumerator<T>(array, current, used);
2022-11-03 16:57:41 +05:00
2024-08-19 10:01:07 +05:00
/// <inheritdoc/>
IEnumerator IEnumerable.GetEnumerator()
=> GetEnumerator();
class CyclycListEnumerator<Te> : IEnumerator<Te>
{
private readonly Te[] array;
private readonly int used;
private readonly int first;
private int current = -1;
public CyclycListEnumerator(Te[] array, int first, int used)
{
this.array = new Te[array.Length];
array.CopyTo(this.array, 0);
this.used = used;
this.first = first;
}
2022-11-03 16:57:41 +05:00
2024-08-19 10:01:07 +05:00
public Te Current
{
get
2022-11-03 16:57:41 +05:00
{
2024-08-19 10:01:07 +05:00
if (IsCurrentOk())
2022-11-03 16:57:41 +05:00
{
2024-08-19 10:01:07 +05:00
var i = (current + first + 1) % used;
return array[i];
2022-11-03 16:57:41 +05:00
}
2024-08-19 10:01:07 +05:00
else
return default!;
2022-11-03 16:57:41 +05:00
}
2024-08-19 10:01:07 +05:00
}
2022-11-03 16:57:41 +05:00
2024-08-19 10:01:07 +05:00
object? IEnumerator.Current => Current;
2022-11-03 16:57:41 +05:00
2024-08-19 10:01:07 +05:00
public void Dispose() {; }
2022-11-03 16:57:41 +05:00
2024-08-19 10:01:07 +05:00
private bool IsCurrentOk() => current >= 0 && current < used;
2022-11-03 16:57:41 +05:00
2024-08-19 10:01:07 +05:00
public bool MoveNext()
{
if (current < used)
current++;
return IsCurrentOk();
2022-11-03 16:57:41 +05:00
}
2024-08-19 10:01:07 +05:00
public void Reset()
2022-11-03 16:57:41 +05:00
{
current = -1;
}
}
2024-08-19 10:01:07 +05:00
/// <summary>
/// Очистить весь массив
/// </summary>
public void Clear()
{
used = 0;
current = -1;
}
2023-02-27 10:33:59 +05:00
}