Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Csharp/feature/add garbage collection tests #29

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System.Collections.Generic;
using Platform.Data.Doublets.Memory.United.Generic;
using Platform.Data.Doublets.Sequences.Converters;
using Platform.Memory;
using TLinkAddress = System.UInt64;
using Xunit;

namespace Platform.Data.Doublets.Sequences.Tests;

public class GarbageCollectionTests
{
public IResizableDirectMemory LinksMemory;
public ILinks<TLinkAddress> LinksStorage;
public BalancedVariantConverter<TLinkAddress> BalancedVariantConverter;

public GarbageCollectionTests()
{
LinksMemory = new HeapResizableDirectMemory();
LinksStorage = new UnitedMemoryLinks<TLinkAddress>(LinksMemory);
BalancedVariantConverter = new BalancedVariantConverter<TLinkAddress>(LinksStorage);
}

[Fact]
public void FullPointsSequence()
{
TLinkAddress link1 = LinksStorage.CreatePoint();
TLinkAddress link2 = LinksStorage.CreatePoint();
TLinkAddress link3 = LinksStorage.CreatePoint();
TLinkAddress sequence = BalancedVariantConverter.Convert(new List<TLinkAddress>{link1, link2, link3});
LinksStorage.ClearGarbage(sequence);
Assert.True(LinksStorage.Exists(link1));
Assert.True(LinksStorage.Exists(link2));
Assert.True(LinksStorage.Exists(link3));
Assert.False(LinksStorage.Exists(sequence));
}

[Fact]
public void Test()
{
TLinkAddress link1 = LinksStorage.CreatePoint();
TLinkAddress link2 = LinksStorage.CreatePoint();
TLinkAddress link3 = LinksStorage.GetOrCreate(link1, link2);
TLinkAddress link4 = LinksStorage.CreatePoint();
TLinkAddress link5 = LinksStorage.CreatePoint();
TLinkAddress link6 = LinksStorage.GetOrCreate(link4, link5);
TLinkAddress sequence = BalancedVariantConverter.Convert(new List<TLinkAddress>{link3, link6});
LinksStorage.ClearGarbage(sequence);
Assert.True(LinksStorage.Exists(link1));
Assert.True(LinksStorage.Exists(link2));
Assert.False(LinksStorage.Exists(link3));
Assert.True(LinksStorage.Exists(link4));
Assert.True(LinksStorage.Exists(link5));
Assert.False(LinksStorage.Exists(link6));
Assert.False(LinksStorage.Exists(sequence));
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using Platform.Data.Doublets.Sequences.Unicode;

Expand All @@ -24,6 +26,9 @@ public static class UInt64LinksExtensions
/// <para></para>
/// </param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void UseUnicode(this ILinks<ulong> links) => UnicodeMap.InitNew(links);
public static void UseUnicode<TLinkAddress>(this ILinks<TLinkAddress> links) where TLinkAddress : struct, IUnsignedNumber<TLinkAddress>, IComparisonOperators<TLinkAddress, TLinkAddress, bool>
{
UnicodeMap<TLinkAddress>.InitNew(links);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="Platform.Data.Doublets" Version="0.17.0" />
<PackageReference Include="Platform.Data.Doublets" Version="0.17.1" />
<PackageReference Include="Platform.Data.Sequences" Version="0.2.1" />
</ItemGroup>

Expand Down
16 changes: 0 additions & 16 deletions csharp/Platform.Data.Doublets.Sequences/Sequences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -732,22 +732,6 @@
//
// #endregion
//
// #region Garbage Collection
// [MethodImpl(MethodImplOptions.AggressiveInlining)]
// private bool IsGarbage(LinkIndex link) => link != Options.SequenceMarkerLink && !Links.Unsync.IsPartialPoint(link) && Links.Count(Constants.Any, link) == 0;
// [MethodImpl(MethodImplOptions.AggressiveInlining)]
// private void ClearGarbage(LinkIndex link)
// {
// if (IsGarbage(link))
// {
// var contents = new Link<ulong>(Links.GetLink(link));
// Links.Unsync.Delete(link);
// ClearGarbage(contents.Source);
// ClearGarbage(contents.Target);
// }
// }
//
// #endregion
//
// #region Walkers
//
Expand Down
61 changes: 31 additions & 30 deletions csharp/Platform.Data.Doublets.Sequences/Unicode/UnicodeMap.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Text;
using Platform.Data.Sequences;
Expand All @@ -15,30 +16,30 @@ namespace Platform.Data.Doublets.Sequences.Unicode
/// </para>
/// <para></para>
/// </summary>
public class UnicodeMap
public class UnicodeMap<TLinkAddress> where TLinkAddress : struct, IUnsignedNumber<TLinkAddress>, IComparisonOperators<TLinkAddress, TLinkAddress, bool>
{
/// <summary>
/// <para>
/// The first char link.
/// </para>
/// <para></para>
/// </summary>
public static readonly ulong FirstCharLink = 1;
public static readonly TLinkAddress FirstCharLink = TLinkAddress.One;
/// <summary>
/// <para>
/// The max value.
/// </para>
/// <para></para>
/// </summary>
public static readonly ulong LastCharLink = FirstCharLink + char.MaxValue;
public static readonly TLinkAddress LastCharLink = FirstCharLink + TLinkAddress.CreateTruncating(char.MaxValue);
/// <summary>
/// <para>
/// The max value.
/// </para>
/// <para></para>
/// </summary>
public static readonly ulong MapSize = 1 + char.MaxValue;
private readonly ILinks<ulong> _links;
public static readonly TLinkAddress MapSize = TLinkAddress.One + TLinkAddress.CreateTruncating(char.MaxValue);
private readonly ILinks<TLinkAddress> _links;
private bool _initialized;

/// <summary>
Expand All @@ -52,7 +53,7 @@ public class UnicodeMap
/// <para></para>
/// </param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public UnicodeMap(ILinks<ulong> links) => _links = links;
public UnicodeMap(ILinks<TLinkAddress> links) => _links = links;

/// <summary>
/// <para>
Expand All @@ -69,9 +70,9 @@ public class UnicodeMap
/// <para></para>
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UnicodeMap InitNew(ILinks<ulong> links)
public static UnicodeMap<TLinkAddress> InitNew(ILinks<TLinkAddress> links)
{
var map = new UnicodeMap(links);
var map = new UnicodeMap<TLinkAddress>(links);
map.Init();
return map;
}
Expand Down Expand Up @@ -101,7 +102,7 @@ public void Init()
}
else
{
for (var i = FirstCharLink + 1; i <= LastCharLink; i++)
for (var i = FirstCharLink + TLinkAddress.One; i <= LastCharLink; i++)
{
// From NIL to It (NIL -> Character) transformation meaning, (or infinite amount of NIL characters before actual Character)
var createdLink = _links.CreatePoint();
Expand Down Expand Up @@ -130,11 +131,11 @@ public void Init()
/// <para></para>
/// </param>
/// <returns>
/// <para>The ulong</para>
/// <para>The TLinkAddress</para>
/// <para></para>
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong FromCharToLink(char character) => (ulong)character + 1;
public static TLinkAddress FromCharToLink(char character) => TLinkAddress.CreateTruncating(character) + TLinkAddress.One;

/// <summary>
/// <para>
Expand All @@ -151,7 +152,7 @@ public void Init()
/// <para></para>
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static char FromLinkToChar(ulong link) => (char)(link - 1);
public static char FromLinkToChar(TLinkAddress link) => (char)(object)(link - TLinkAddress.One);

/// <summary>
/// <para>
Expand All @@ -168,7 +169,7 @@ public void Init()
/// <para></para>
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsCharLink(ulong link) => link <= MapSize;
public static bool IsCharLink(TLinkAddress link) => link <= MapSize;

/// <summary>
/// <para>
Expand All @@ -185,7 +186,7 @@ public void Init()
/// <para></para>
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string FromLinksToString(IList<ulong> linksList)
public static string FromLinksToString(IList<TLinkAddress> linksList)
{
var sb = new StringBuilder();
for (int i = 0; i < linksList.Count; i++)
Expand Down Expand Up @@ -214,7 +215,7 @@ public static string FromLinksToString(IList<ulong> linksList)
/// <para></para>
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string FromSequenceLinkToString(ulong link, ILinks<ulong> links)
public static string FromSequenceLinkToString(TLinkAddress link, ILinks<TLinkAddress> links)
{
var sb = new StringBuilder();
if (links.Exists(link))
Expand All @@ -240,11 +241,11 @@ public static string FromSequenceLinkToString(ulong link, ILinks<ulong> links)
/// <para></para>
/// </param>
/// <returns>
/// <para>The ulong array</para>
/// <para>The TLinkAddress array</para>
/// <para></para>
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong[] FromCharsToLinkArray(char[] chars) => FromCharsToLinkArray(chars, chars.Length);
public static TLinkAddress[] FromCharsToLinkArray(char[] chars) => FromCharsToLinkArray(chars, chars.Length);

/// <summary>
/// <para>
Expand All @@ -265,10 +266,10 @@ public static string FromSequenceLinkToString(ulong link, ILinks<ulong> links)
/// <para></para>
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong[] FromCharsToLinkArray(char[] chars, int count)
public static TLinkAddress[] FromCharsToLinkArray(char[] chars, int count)
{
// char array to ulong array
var linksSequence = new ulong[count];
// char array to TLinkAddress array
var linksSequence = new TLinkAddress[count];
for (var i = 0; i < count; i++)
{
linksSequence[i] = FromCharToLink(chars[i]);
Expand All @@ -291,10 +292,10 @@ public static ulong[] FromCharsToLinkArray(char[] chars, int count)
/// <para></para>
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong[] FromStringToLinkArray(string sequence)
public static TLinkAddress[] FromStringToLinkArray(string sequence)
{
// char array to ulong array
var linksSequence = new ulong[sequence.Length];
// char array to TLinkAddress array
var linksSequence = new TLinkAddress[sequence.Length];
for (var i = 0; i < sequence.Length; i++)
{
linksSequence[i] = FromCharToLink(sequence[i]);
Expand All @@ -317,9 +318,9 @@ public static ulong[] FromStringToLinkArray(string sequence)
/// <para></para>
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static List<ulong[]> FromStringToLinkArrayGroups(string sequence)
public static List<TLinkAddress[]> FromStringToLinkArrayGroups(string sequence)
{
var result = new List<ulong[]>();
var result = new List<TLinkAddress[]>();
var offset = 0;
while (offset < sequence.Length)
{
Expand All @@ -332,8 +333,8 @@ public static List<ulong[]> FromStringToLinkArrayGroups(string sequence)
relativeLength++;
absoluteLength++;
}
// char array to ulong array
var innerSequence = new ulong[relativeLength];
// char array to TLinkAddress array
var innerSequence = new TLinkAddress[relativeLength];
var maxLength = offset + relativeLength;
for (var i = offset; i < maxLength; i++)
{
Expand All @@ -360,9 +361,9 @@ public static List<ulong[]> FromStringToLinkArrayGroups(string sequence)
/// <para></para>
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static List<ulong[]> FromLinkArrayToLinkArrayGroups(ulong[] array)
public static List<TLinkAddress[]> FromLinkArrayToLinkArrayGroups(TLinkAddress[] array)
{
var result = new List<ulong[]>();
var result = new List<TLinkAddress[]>();
var offset = 0;
while (offset < array.Length)
{
Expand All @@ -389,7 +390,7 @@ public static List<ulong[]> FromLinkArrayToLinkArrayGroups(ulong[] array)
}
}
// copy array
var innerSequence = new ulong[relativeLength];
var innerSequence = new TLinkAddress[relativeLength];
var maxLength = offset + relativeLength;
for (var i = offset; i < maxLength; i++)
{
Expand Down