From 35d6f5f67dd225399ce2ed1770018460cacd61ae Mon Sep 17 00:00:00 2001 From: FreePhoenix888 Date: Sat, 18 Feb 2023 23:17:24 +0600 Subject: [PATCH 1/7] Use generics, generic math --- .../Unicode/UnicodeMap.cs | 61 ++++++++++--------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/csharp/Platform.Data.Doublets.Sequences/Unicode/UnicodeMap.cs b/csharp/Platform.Data.Doublets.Sequences/Unicode/UnicodeMap.cs index 998b5fb..33a05bb 100644 --- a/csharp/Platform.Data.Doublets.Sequences/Unicode/UnicodeMap.cs +++ b/csharp/Platform.Data.Doublets.Sequences/Unicode/UnicodeMap.cs @@ -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; @@ -15,7 +16,7 @@ namespace Platform.Data.Doublets.Sequences.Unicode /// /// /// - public class UnicodeMap + public class UnicodeMap where TLinkAddress : struct, IUnsignedNumber, IComparisonOperators { /// /// @@ -23,22 +24,22 @@ public class UnicodeMap /// /// /// - public static readonly ulong FirstCharLink = 1; + public static readonly TLinkAddress FirstCharLink = TLinkAddress.One; /// /// /// The max value. /// /// /// - public static readonly ulong LastCharLink = FirstCharLink + char.MaxValue; + public static readonly TLinkAddress LastCharLink = FirstCharLink + TLinkAddress.CreateTruncating(char.MaxValue); /// /// /// The max value. /// /// /// - public static readonly ulong MapSize = 1 + char.MaxValue; - private readonly ILinks _links; + public static readonly TLinkAddress MapSize = TLinkAddress.One + TLinkAddress.CreateTruncating(char.MaxValue); + private readonly ILinks _links; private bool _initialized; /// @@ -52,7 +53,7 @@ public class UnicodeMap /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public UnicodeMap(ILinks links) => _links = links; + public UnicodeMap(ILinks links) => _links = links; /// /// @@ -69,9 +70,9 @@ public class UnicodeMap /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static UnicodeMap InitNew(ILinks links) + public static UnicodeMap InitNew(ILinks links) { - var map = new UnicodeMap(links); + var map = new UnicodeMap(links); map.Init(); return map; } @@ -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(); @@ -130,11 +131,11 @@ public void Init() /// /// /// - /// The ulong + /// The TLinkAddress /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ulong FromCharToLink(char character) => (ulong)character + 1; + public static TLinkAddress FromCharToLink(char character) => TLinkAddress.CreateTruncating(character) + TLinkAddress.One; /// /// @@ -151,7 +152,7 @@ public void Init() /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static char FromLinkToChar(ulong link) => (char)(link - 1); + public static char FromLinkToChar(TLinkAddress link) => (char)(object)(link - TLinkAddress.One); /// /// @@ -168,7 +169,7 @@ public void Init() /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool IsCharLink(ulong link) => link <= MapSize; + public static bool IsCharLink(TLinkAddress link) => link <= MapSize; /// /// @@ -185,7 +186,7 @@ public void Init() /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static string FromLinksToString(IList linksList) + public static string FromLinksToString(IList linksList) { var sb = new StringBuilder(); for (int i = 0; i < linksList.Count; i++) @@ -214,7 +215,7 @@ public static string FromLinksToString(IList linksList) /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static string FromSequenceLinkToString(ulong link, ILinks links) + public static string FromSequenceLinkToString(TLinkAddress link, ILinks links) { var sb = new StringBuilder(); if (links.Exists(link)) @@ -240,11 +241,11 @@ public static string FromSequenceLinkToString(ulong link, ILinks links) /// /// /// - /// The ulong array + /// The TLinkAddress array /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ulong[] FromCharsToLinkArray(char[] chars) => FromCharsToLinkArray(chars, chars.Length); + public static TLinkAddress[] FromCharsToLinkArray(char[] chars) => FromCharsToLinkArray(chars, chars.Length); /// /// @@ -265,10 +266,10 @@ public static string FromSequenceLinkToString(ulong link, ILinks links) /// /// [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]); @@ -291,10 +292,10 @@ public static ulong[] FromCharsToLinkArray(char[] chars, int count) /// /// [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]); @@ -317,9 +318,9 @@ public static ulong[] FromStringToLinkArray(string sequence) /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static List FromStringToLinkArrayGroups(string sequence) + public static List FromStringToLinkArrayGroups(string sequence) { - var result = new List(); + var result = new List(); var offset = 0; while (offset < sequence.Length) { @@ -332,8 +333,8 @@ public static List 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++) { @@ -360,9 +361,9 @@ public static List FromStringToLinkArrayGroups(string sequence) /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static List FromLinkArrayToLinkArrayGroups(ulong[] array) + public static List FromLinkArrayToLinkArrayGroups(TLinkAddress[] array) { - var result = new List(); + var result = new List(); var offset = 0; while (offset < array.Length) { @@ -389,7 +390,7 @@ public static List 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++) { From 339f1d13cf11cee5ba17e0ab264ed262ab974c4a Mon Sep 17 00:00:00 2001 From: FreePhoenix888 Date: Sat, 18 Feb 2023 23:17:54 +0600 Subject: [PATCH 2/7] Add generic --- .../UInt64LinksExtensions.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/csharp/Platform.Data.Doublets.Sequences/UInt64LinksExtensions.cs b/csharp/Platform.Data.Doublets.Sequences/UInt64LinksExtensions.cs index 3c57841..7e17837 100644 --- a/csharp/Platform.Data.Doublets.Sequences/UInt64LinksExtensions.cs +++ b/csharp/Platform.Data.Doublets.Sequences/UInt64LinksExtensions.cs @@ -1,3 +1,5 @@ +using System; +using System.Numerics; using System.Runtime.CompilerServices; using Platform.Data.Doublets.Sequences.Unicode; @@ -24,6 +26,9 @@ public static class UInt64LinksExtensions /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void UseUnicode(this ILinks links) => UnicodeMap.InitNew(links); + public static void UseUnicode(this ILinks links) where TLinkAddress : struct, IUnsignedNumber, IComparisonOperators + { + UnicodeMap.InitNew(links); + } } } From 3d1425d6041078dc2ecdb86eb96839ae1ab766a6 Mon Sep 17 00:00:00 2001 From: FreePhoenix888 Date: Sat, 18 Feb 2023 23:18:26 +0600 Subject: [PATCH 3/7] Move IsGarbage and ClearGarbage to ILinks Extension and make them generic --- .../Sequences.cs | 16 ----------- .../UInt64LinksExtensions.cs | 27 +++++++++++++++++++ 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/csharp/Platform.Data.Doublets.Sequences/Sequences.cs b/csharp/Platform.Data.Doublets.Sequences/Sequences.cs index 7470305..2eddf3d 100644 --- a/csharp/Platform.Data.Doublets.Sequences/Sequences.cs +++ b/csharp/Platform.Data.Doublets.Sequences/Sequences.cs @@ -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(Links.GetLink(link)); -// Links.Unsync.Delete(link); -// ClearGarbage(contents.Source); -// ClearGarbage(contents.Target); -// } -// } -// -// #endregion // // #region Walkers // diff --git a/csharp/Platform.Data.Doublets.Sequences/UInt64LinksExtensions.cs b/csharp/Platform.Data.Doublets.Sequences/UInt64LinksExtensions.cs index 7e17837..1b3d12e 100644 --- a/csharp/Platform.Data.Doublets.Sequences/UInt64LinksExtensions.cs +++ b/csharp/Platform.Data.Doublets.Sequences/UInt64LinksExtensions.cs @@ -30,5 +30,32 @@ public static void UseUnicode(this ILinks links) whe { UnicodeMap.InitNew(links); } + + #region Garbage Collection + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsGarbage(this ILinks links, TLinkAddress link) where TLinkAddress : struct, IUnsignedNumber, IComparisonOperators + { + return !links.IsPartialPoint(link) && links.Count(links.Constants.Any, link) == TLinkAddress.Zero && links.Count(links.Constants.Any, links.Constants.Any, link) == TLinkAddress.Zero; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ClearGarbage(this ILinks links, TLinkAddress link) where TLinkAddress : struct, IUnsignedNumber, IComparisonOperators + { + ClearGarbage(links, link, links.IsGarbage); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ClearGarbage(this ILinks links, TLinkAddress link, Func getIsGarbage) where TLinkAddress : struct, IUnsignedNumber, IComparisonOperators + { + if (getIsGarbage(link)) + { + var contents = new Link(links.GetLink(link)); + links.Delete(link); + links.ClearGarbage(contents.Source); + links.ClearGarbage(contents.Target); + } + } + + #endregion } } From 2be3cf149ac9b02840cd5f41b4c008ed4dbb9987 Mon Sep 17 00:00:00 2001 From: FreePhoenix888 Date: Sat, 18 Feb 2023 23:18:42 +0600 Subject: [PATCH 4/7] Remove UInt64 from name cause class is generic now --- .../{UInt64LinksExtensions.cs => LinksExtensions.cs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename csharp/Platform.Data.Doublets.Sequences/{UInt64LinksExtensions.cs => LinksExtensions.cs} (100%) diff --git a/csharp/Platform.Data.Doublets.Sequences/UInt64LinksExtensions.cs b/csharp/Platform.Data.Doublets.Sequences/LinksExtensions.cs similarity index 100% rename from csharp/Platform.Data.Doublets.Sequences/UInt64LinksExtensions.cs rename to csharp/Platform.Data.Doublets.Sequences/LinksExtensions.cs From b61de4b0f29e1c676bc15ef1dda0bf9c83690a11 Mon Sep 17 00:00:00 2001 From: FreePhoenix888 Date: Sun, 19 Feb 2023 13:43:42 +0600 Subject: [PATCH 5/7] Move garbage collection extensions to Data.Doublets --- .../LinksExtensions.cs | 27 ------------------- 1 file changed, 27 deletions(-) diff --git a/csharp/Platform.Data.Doublets.Sequences/LinksExtensions.cs b/csharp/Platform.Data.Doublets.Sequences/LinksExtensions.cs index 1b3d12e..7e17837 100644 --- a/csharp/Platform.Data.Doublets.Sequences/LinksExtensions.cs +++ b/csharp/Platform.Data.Doublets.Sequences/LinksExtensions.cs @@ -30,32 +30,5 @@ public static void UseUnicode(this ILinks links) whe { UnicodeMap.InitNew(links); } - - #region Garbage Collection - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool IsGarbage(this ILinks links, TLinkAddress link) where TLinkAddress : struct, IUnsignedNumber, IComparisonOperators - { - return !links.IsPartialPoint(link) && links.Count(links.Constants.Any, link) == TLinkAddress.Zero && links.Count(links.Constants.Any, links.Constants.Any, link) == TLinkAddress.Zero; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void ClearGarbage(this ILinks links, TLinkAddress link) where TLinkAddress : struct, IUnsignedNumber, IComparisonOperators - { - ClearGarbage(links, link, links.IsGarbage); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void ClearGarbage(this ILinks links, TLinkAddress link, Func getIsGarbage) where TLinkAddress : struct, IUnsignedNumber, IComparisonOperators - { - if (getIsGarbage(link)) - { - var contents = new Link(links.GetLink(link)); - links.Delete(link); - links.ClearGarbage(contents.Source); - links.ClearGarbage(contents.Target); - } - } - - #endregion } } From fad9bec441b684eb0e0759078d4d5d27a7f93e4e Mon Sep 17 00:00:00 2001 From: FreePhoenix888 Date: Sun, 19 Feb 2023 14:10:39 +0600 Subject: [PATCH 6/7] Update Platform.Data.Doublets to 0.17.1 --- .../Platform.Data.Doublets.Sequences.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/csharp/Platform.Data.Doublets.Sequences/Platform.Data.Doublets.Sequences.csproj b/csharp/Platform.Data.Doublets.Sequences/Platform.Data.Doublets.Sequences.csproj index 28e9400..77d55a1 100644 --- a/csharp/Platform.Data.Doublets.Sequences/Platform.Data.Doublets.Sequences.csproj +++ b/csharp/Platform.Data.Doublets.Sequences/Platform.Data.Doublets.Sequences.csproj @@ -30,7 +30,7 @@ - + From 4b4c547b4af931a794856743276188100ee63f00 Mon Sep 17 00:00:00 2001 From: FreePhoenix888 Date: Sun, 19 Feb 2023 14:10:56 +0600 Subject: [PATCH 7/7] Add GarbageCollectionTests --- .../GarbageCollectionTests.cs | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 csharp/Platform.Data.Doublets.Sequences.Tests/GarbageCollectionTests.cs diff --git a/csharp/Platform.Data.Doublets.Sequences.Tests/GarbageCollectionTests.cs b/csharp/Platform.Data.Doublets.Sequences.Tests/GarbageCollectionTests.cs new file mode 100644 index 0000000..54ed80a --- /dev/null +++ b/csharp/Platform.Data.Doublets.Sequences.Tests/GarbageCollectionTests.cs @@ -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 LinksStorage; + public BalancedVariantConverter BalancedVariantConverter; + + public GarbageCollectionTests() + { + LinksMemory = new HeapResizableDirectMemory(); + LinksStorage = new UnitedMemoryLinks(LinksMemory); + BalancedVariantConverter = new BalancedVariantConverter(LinksStorage); + } + + [Fact] + public void FullPointsSequence() + { + TLinkAddress link1 = LinksStorage.CreatePoint(); + TLinkAddress link2 = LinksStorage.CreatePoint(); + TLinkAddress link3 = LinksStorage.CreatePoint(); + TLinkAddress sequence = BalancedVariantConverter.Convert(new List{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{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)); + } +}