Move RingBuffer to VoiDPlugins.Library

This commit is contained in:
X9VoiD 2020-12-18 10:22:50 +08:00
parent 0ead2cb731
commit 4831722dc4
8 changed files with 30 additions and 80 deletions

View file

@ -3,6 +3,7 @@ using System.Linq;
using System.Numerics;
using MathNet.Numerics;
using OpenTabletDriver.Plugin;
using VoiDPlugins.Library;
namespace VoiDPlugins.Filter.MeL.Core
{

View file

@ -1,6 +1,7 @@
using System;
using System.Numerics;
using MathNet.Numerics;
using VoiDPlugins.Library;
namespace VoiDPlugins.Filter.MeL.Core
{
@ -13,7 +14,7 @@ namespace VoiDPlugins.Filter.MeL.Core
private bool AddTimeSeriesPoint(Vector2 point, DateTime time)
{
this.timeSeriesPoints.Insert(new TimeSeriesPoint(point, time));
return this.timeSeriesPoints.Filled;
return this.timeSeriesPoints.IsFilled;
}
private double[] ConstructTimeDesignMatrix()

View file

@ -1,76 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
namespace VoiDPlugins.Filter.MeL.Core
{
internal class RingBuffer<T> : IEnumerable<T>
{
public int Size { private set; get; }
public bool Filled { private set; get; }
private T[] dataStream;
private int head;
public RingBuffer(int size)
{
this.Size = size;
this.dataStream = new T[size];
}
public void Insert(T item)
{
this.dataStream[this.head++] = item;
if (this.head == this.Size)
{
this.head = 0;
this.Filled = true;
}
}
public T PeekFirst()
{
var currentPosition = this.head;
return this.dataStream[currentPosition];
}
public T PeekLast()
{
var last = Math.Abs(this.head - 1);
return this.dataStream[last];
}
public void Clear()
{
this.dataStream = new T[this.Size];
this.head = 0;
}
public IEnumerator<T> GetEnumerator()
{
if (this.head == 0)
{
foreach (var item in this.dataStream)
{
yield return item;
}
}
else
{
foreach (var item in this.dataStream[this.head..^0])
{
yield return item;
}
foreach (var item in this.dataStream[0..this.head])
{
yield return item;
}
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}

View file

@ -13,6 +13,7 @@
<ItemGroup>
<PackageReference Include="MathNet.Numerics" Version="4.11.0" />
<ProjectReference Include="../../.modules/OpenTabletDriver/OpenTabletDriver.Plugin/OpenTabletDriver.Plugin.csproj" />
<ProjectReference Include="../../VoiDPlugins.Library/VoiD/VoiD.csproj" />
</ItemGroup>
</Project>

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Numerics;
using OpenTabletDriver.Plugin.Attributes;
using OpenTabletDriver.Plugin.Tablet;
using VoiDPlugins.Library;
namespace VoiDPlugins.Filter
{

View file

@ -12,6 +12,7 @@
<ItemGroup>
<ProjectReference Include="../../.modules/OpenTabletDriver/OpenTabletDriver.Plugin/OpenTabletDriver.Plugin.csproj" />
<ProjectReference Include="../../VoiDPlugins.Library/VoiD/VoiD.csproj" />
</ItemGroup>
</Project>

View file

@ -2,9 +2,9 @@ using System;
using System.Collections;
using System.Collections.Generic;
namespace VoiDPlugins.Filter
namespace VoiDPlugins.Library
{
internal class RingBuffer<T> : IEnumerable<T>
public class RingBuffer<T> : IEnumerable<T>
{
public int Size { private set; get; }
public bool IsFilled { private set; get; }
@ -46,7 +46,7 @@ namespace VoiDPlugins.Filter
this.head = 0;
}
public IEnumerator<T> GetEnumerator()
IEnumerator<T> RingGetEnumerator()
{
if (this.head == 0)
{
@ -68,6 +68,14 @@ namespace VoiDPlugins.Filter
}
}
public IEnumerator<T> GetEnumerator()
{
if (!this.IsFilled)
return (IEnumerator<T>)dataStream.GetEnumerator();
else
return RingGetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();

View file

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net5</TargetFrameworks>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<DebugType>none</DebugType>
<DebugSymbols>false</DebugSymbols>
<CopyOutputSymbolsToOutputDirectory>false</CopyOutputSymbolsToOutputDirectory>
</PropertyGroup>
</Project>