forked from ddrilling/AsbCloudServer
Add CyclycArray
This commit is contained in:
parent
c38730b7aa
commit
a575c1163d
194
AsbCloudApp/CyclycArray.cs
Normal file
194
AsbCloudApp/CyclycArray.cs
Normal file
@ -0,0 +1,194 @@
|
||||
#nullable enable
|
||||
using System.Linq;
|
||||
|
||||
namespace System.Collections.Generic
|
||||
{
|
||||
/// <summary>
|
||||
/// Цикличный массив
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public class CyclycArray<T> : IEnumerable<T>
|
||||
{
|
||||
readonly T[] array;
|
||||
int used, current = -1;
|
||||
|
||||
/// <summary>
|
||||
/// constructor
|
||||
/// </summary>
|
||||
/// <param name="capacity"></param>
|
||||
public CyclycArray(int capacity)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
Array.Copy(newItems, array, capacity);
|
||||
current = capacity - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
current = (++current) % capacity;
|
||||
var countToEndOfArray = capacity - current;
|
||||
if (newItems.Length <= countToEndOfArray)
|
||||
{
|
||||
Array.Copy(newItems, 0, array, current, newItems.Length);
|
||||
current += newItems.Length - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
if (used < capacity)
|
||||
{
|
||||
used += newItems.Length;
|
||||
used = used > capacity ? capacity : used;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Индекс
|
||||
/// </summary>
|
||||
/// <param name="index"></param>
|
||||
/// <returns></returns>
|
||||
public T this[int index]
|
||||
{
|
||||
get
|
||||
{
|
||||
var i = (current + 1 + index) % used;
|
||||
return array[i];
|
||||
}
|
||||
set
|
||||
{
|
||||
var i = (current + 1 + index) % used;
|
||||
array[i] = value;
|
||||
UpdatedInvoke(current, value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// событие на изменение элемента в массиве
|
||||
/// </summary>
|
||||
public event EventHandler<(int index, T value)>? Updated;
|
||||
private void UpdatedInvoke(int index, T value)
|
||||
{
|
||||
Updated?.Invoke(this, (index, value));
|
||||
}
|
||||
|
||||
/// <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;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
=> new CyclycListEnumerator<T>(array, current, used);
|
||||
|
||||
/// <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;
|
||||
}
|
||||
|
||||
public Te Current
|
||||
{
|
||||
get
|
||||
{
|
||||
if (IsCurrentOk())
|
||||
{
|
||||
var i = (current + first + 1) % used;
|
||||
return array[i];
|
||||
}
|
||||
else
|
||||
return default!;
|
||||
}
|
||||
}
|
||||
|
||||
object? IEnumerator.Current => Current;
|
||||
|
||||
public void Dispose() {; }
|
||||
|
||||
private bool IsCurrentOk() => current >= 0 && current < used;
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
if (current < used)
|
||||
current++;
|
||||
return IsCurrentOk();
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
current = -1;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Очистить весь массив
|
||||
/// </summary>
|
||||
public void Clear()
|
||||
{
|
||||
used = 0;
|
||||
current = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
#nullable disable
|
23
AsbCloudInfrastructure/Services/SAUB/TelemetryDataCache.cs
Normal file
23
AsbCloudInfrastructure/Services/SAUB/TelemetryDataCache.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using AsbCloudDb.Model;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections;
|
||||
using System;
|
||||
using AsbCloudApp.Services;
|
||||
|
||||
#nullable enable
|
||||
namespace AsbCloudInfrastructure.Services.SAUB
|
||||
{
|
||||
public class TelemetryDataCache
|
||||
{
|
||||
private readonly ConcurrentDictionary<int, Dictionary<int, IEnumerable>> caches;
|
||||
private static TelemetryDataCache? instance;
|
||||
|
||||
public TelemetryDataCache(IAsbCloudDbContext db)
|
||||
{
|
||||
caches = new ();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
#nullable disable
|
@ -18,6 +18,8 @@ using System.Threading.Tasks;
|
||||
namespace ConsoleApp1
|
||||
{
|
||||
|
||||
|
||||
|
||||
class Program
|
||||
{
|
||||
private static AsbCloudDbContext db = ServiceFactory.Context;
|
||||
@ -25,12 +27,21 @@ namespace ConsoleApp1
|
||||
// use ServiceFactory to make services
|
||||
static void Main(/*string[] args*/)
|
||||
{
|
||||
var h = new Hashtable();
|
||||
h.Add("name", 1);
|
||||
h.Add("name2", "66");
|
||||
var v = h["v"];
|
||||
|
||||
var s = System.Text.Json.JsonSerializer.Serialize(h);
|
||||
var set1 = new int[15].Select((v,i) => i.ToString());
|
||||
var set2 = new int[5].Select((v, i) => (i + 1000).ToString());
|
||||
var set3 = new int[5].Select((v, i) => (i + 2000).ToString());
|
||||
var set4 = new int[5].Select((v, i) => (i + 4000).ToString());
|
||||
var set5 = new int[8].Select((v, i) => (i + 5000).ToString());
|
||||
var set6 = new int[3].Select((v, i) => (i + 6000).ToString());
|
||||
|
||||
var ca = new CyclycArray<string>(10);
|
||||
ca.AddRange(set1);
|
||||
ca.AddRange(set2);
|
||||
ca.AddRange(set3);
|
||||
ca.AddRange(set4);
|
||||
ca.AddRange(set5);
|
||||
ca.AddRange(set6);
|
||||
|
||||
Console.WriteLine($"total time: ms");
|
||||
Console.ReadLine();
|
||||
|
@ -65,7 +65,7 @@ namespace ConsoleApp1
|
||||
=> new TelemetryTracker(CacheDb, ConfigurationService);
|
||||
|
||||
public static TelemetryService MakeTelemetryService()
|
||||
=> new TelemetryService(Context, MakeTelemetryTracker(), TimezoneService, CacheDb);
|
||||
=> new TelemetryService(Context, MakeTelemetryTracker(), TimezoneService);
|
||||
|
||||
public static WellService MakeWellService()
|
||||
=> new WellService(Context, CacheDb, MakeTelemetryService(), TimezoneService);
|
||||
|
Loading…
Reference in New Issue
Block a user