Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions csharp/Platform.Data.Doublets/ILinksExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1599,5 +1599,54 @@ public static void ClearGarbage<TLinkAddress>(this ILinks<TLinkAddress> links, T
}

#endregion

/// <summary>
/// <para>
/// Prints the contents of the links database using the specified message handler.
/// </para>
/// <para></para>
/// </summary>
/// <typeparam name="TLinkAddress">
/// <para>The type of link address.</para>
/// <para></para>
/// </typeparam>
/// <param name="links">
/// <para>The links.</para>
/// <para></para>
/// </param>
/// <param name="messageHandler">
/// <para>The message handler for outputting formatted messages.</para>
/// <para></para>
/// </param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void PrintContents<TLinkAddress>(this ILinks<TLinkAddress> links, Action<string> messageHandler) where TLinkAddress : IUnsignedNumber<TLinkAddress>, IComparisonOperators<TLinkAddress, TLinkAddress, bool>
{
var count = links.Count();
if (count == TLinkAddress.Zero)
{
messageHandler("Database is empty.");
}
else
{
messageHandler("Contents:");
var linksTotalLength = count.ToString().Length;
var printFormatBase = new string('0', linksTotalLength);
var printFormat = $"\t[{{0:{printFormatBase}}}]: {{1:{printFormatBase}}} -> {{2:{printFormatBase}}}";

// Iterate through all existing links
var allLinks = links.All();
for (var i = 0; i < allLinks.Count; i++)
{
var link = allLinks[i];
if (link != null)
{
var linkIndex = links.GetIndex(link);
var source = links.GetSource(link);
var target = links.GetTarget(link);
messageHandler(string.Format(printFormat, linkIndex, source, target));
}
}
}
}
}
}
49 changes: 49 additions & 0 deletions experiments/TestPrintContents.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using Platform.Data.Doublets;
using Platform.Data.Doublets.Memory.United.Generic;

namespace Platform.Data.Doublets.Experiments
{
class TestPrintContents
{
static void Main()
{
Console.WriteLine("Testing PrintContents extension method...");

// Create a simple in-memory links storage
using var links = new UnitedMemoryLinks<ulong>("test.db");

var messages = new List<string>();
void CaptureMessage(string message) => messages.Add(message);

// Test with empty database
Console.WriteLine("1. Testing with empty database:");
links.PrintContents(CaptureMessage);
foreach (var message in messages)
{
Console.WriteLine($"Output: {message}");
}
Console.WriteLine();

// Clear messages and add some links
messages.Clear();

// Create some test links
var link1 = links.Create();
var link2 = links.Create();
var link3 = links.CreateAndUpdate(link1, link2);

Console.WriteLine("2. Testing with some links:");
Console.WriteLine($"Created links: {link1}, {link2}, {link3}");

links.PrintContents(CaptureMessage);
foreach (var message in messages)
{
Console.WriteLine($"Output: {message}");
}

Console.WriteLine("\nTest completed successfully!");
}
}
}
13 changes: 13 additions & 0 deletions experiments/TestPrintContents.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="../csharp/Platform.Data.Doublets/Platform.Data.Doublets.csproj"/>
</ItemGroup>
</Project>
Binary file added experiments/test.db
Binary file not shown.
Loading