Skip to content

Commit

Permalink
Generic math is used
Browse files Browse the repository at this point in the history
  • Loading branch information
FreePhoenix888 committed Feb 9, 2023
1 parent 72a6739 commit 02f9f2f
Show file tree
Hide file tree
Showing 22 changed files with 61 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ namespace Platform.Data.Doublets.Sequences.Tests
{
public class BigIntegerConvertersTests
{
public ILinks<TLinkAddress> CreateLinks() => CreateLinks<TLinkAddress>(new IO.TemporaryFile());
public ILinks<TLinkAddress> CreateLinks() => CreateLinks(new IO.TemporaryFile());

public ILinks<TLinkAddress> CreateLinks<TLinkAddress>(string dataDbFilename)
public ILinks<TLinkAddress> CreateLinks(string dataDbFilename)
{
var linksConstants = new LinksConstants<TLinkAddress>(enableExternalReferencesSupport: true);
return new UnitedMemoryLinks<TLinkAddress>(new FileMappedResizableDirectMemory(dataDbFilename), UnitedMemoryLinks<TLinkAddress>.DefaultLinksSizeStep, linksConstants, IndexTreeType.Default);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public ByteConvertersTests()
_listToSequenceConverter = new BalancedVariantConverter<TLinkAddress>(Storage);
var type = Storage.GetOrCreate(TLinkAddress.CreateTruncating(1), TLinkAddress.CreateTruncating(1));
var typeIndex = type;
var unicodeSymbolType = Storage.GetOrCreate(type, Arithmetic.Increment(ref typeIndex));
var unicodeSequenceType = Storage.GetOrCreate(type, Arithmetic.Increment(ref typeIndex));
var unicodeSymbolType = Storage.GetOrCreate(type, ++typeIndex);
var unicodeSequenceType = Storage.GetOrCreate(type, ++typeIndex);
BalancedVariantConverter<TLinkAddress> balancedVariantConverter = new(Storage);
AddressToRawNumberConverter<TLinkAddress> addressToRawNumberConverter = new();
RawNumberToAddressConverter<TLinkAddress> rawNumberToAddressConverter = new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public DefaultSequenceAppenderTests(ITestOutputHelper output)
{
_output = output;
}
public static ILinks<TLinkAddress> CreateLinks() => CreateLinks<TLinkAddress>(new IO.TemporaryFile());
public static ILinks<TLinkAddress> CreateLinks() => CreateLinks(new IO.TemporaryFile());

public static ILinks<TLinkAddress> CreateLinks<TLinkAddress>(string dataDBFilename)
public static ILinks<TLinkAddress> CreateLinks(string dataDBFilename)
{
var linksConstants = new LinksConstants<TLinkAddress>(enableExternalReferencesSupport: true);
return new UnitedMemoryLinks<TLinkAddress>(new FileMappedResizableDirectMemory(dataDBFilename), UnitedMemoryLinks<TLinkAddress>.DefaultLinksSizeStep, linksConstants, IndexTreeType.Default);
Expand All @@ -48,7 +48,7 @@ public ValueCriterionMatcher(ILinks<TLinkAddress> links, TLinkAddress marker)
public void AppendArrayBug()
{
ILinks<TLinkAddress> links = CreateLinks();
var markerIndex = Arithmetic.Increment(TLinkAddress.CreateTruncating(0));
var markerIndex = TLinkAddress.CreateTruncating(1);
var meaningRoot = links.GetOrCreate(markerIndex, markerIndex);
var sequence = links.Create();
sequence = links.Update(sequence, meaningRoot, sequence);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ namespace Platform.Data.Doublets.Sequences.Tests
{
public class RationalNumbersTests
{
public ILinks<TLinkAddress> CreateLinks() => CreateLinks<TLinkAddress>(new IO.TemporaryFile());
public ILinks<TLinkAddress> CreateLinks() => CreateLinks(new IO.TemporaryFile());

public ILinks<TLinkAddress> CreateLinks<TLinkAddress>(string dataDbFilename)
public ILinks<TLinkAddress> CreateLinks(string dataDbFilename)
{
var linksConstants = new LinksConstants<TLinkAddress>(enableExternalReferencesSupport: true);
return new UnitedMemoryLinks<TLinkAddress>(new FileMappedResizableDirectMemory(dataDbFilename), UnitedMemoryLinks<TLinkAddress>.DefaultLinksSizeStep, linksConstants, IndexTreeType.Default);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ namespace Platform.Data.Doublets.Sequences.Tests
{
public class Uint64LinksExtensionsTests
{
public static ILinks<TLinkAddress> CreateLinks() => CreateLinks<TLinkAddress>(new Platform.IO.TemporaryFile());
public static ILinks<TLinkAddress> CreateLinks() => CreateLinks(new Platform.IO.TemporaryFile());

public static ILinks<TLinkAddress> CreateLinks<TLinkAddress>(string dataDBFilename)
public static ILinks<TLinkAddress> CreateLinks(string dataDBFilename)
{
var linksConstants = new LinksConstants<TLinkAddress>(enableExternalReferencesSupport: true);
return new UnitedMemoryLinks<TLinkAddress>(new FileMappedResizableDirectMemory(dataDBFilename), UnitedMemoryLinks<TLinkAddress>.DefaultLinksSizeStep, linksConstants, IndexTreeType.Default);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ private void UpdateMaxDoublet(ref Doublet<TLinkAddress> doublet, LinkFrequency<T
var frequency = data.Frequency;
var maxFrequency = _maxDoubletData.Frequency;
//if (frequency > _minFrequencyToCompress && (maxFrequency < frequency || (maxFrequency == frequency && doublet.Source + doublet.Target < /* gives better compression string data (and gives collisions quickly) */ _maxDoublet.Source + _maxDoublet.Target)))
if (frequency > _minFrequencyToCompress && (maxFrequency > frequency) || (maxFrequency == frequency && (Arithmetic.Add(doublet.Source, doublet.Target) > Arithmetic.Add(_maxDoublet.Source, _maxDoublet.Target)))) /* gives better stability and better compression on sequent data and even on rundom numbers data (but gives collisions anyway) */
if (frequency > _minFrequencyToCompress && (maxFrequency > frequency) || (maxFrequency == frequency && (doublet.Source + doublet.Target > _maxDoublet.Source + _maxDoublet.Target))) /* gives better stability and better compression on sequent data and even on rundom numbers data (but gives collisions anyway) */
{
_maxDoublet = doublet;
_maxDoubletData = data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public LinkFrequency<TLinkAddress> IncrementFrequency(ref Doublet<TLinkAddress>
data = new LinkFrequency<TLinkAddress>(TLinkAddress.One, link);
if ((link != TLinkAddress.Zero))
{
data.Frequency = Arithmetic.Add(data.Frequency, _frequencyCounter.Count(link));
data.Frequency = data.Frequency + _frequencyCounter.Count(link);
}
_doubletsCache.Add(doublet, data);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Numerics;
using System.Runtime.CompilerServices;
using Platform.Numbers;

Expand All @@ -11,7 +12,7 @@ namespace Platform.Data.Doublets.Sequences.Frequencies.Cache
/// </para>
/// <para></para>
/// </summary>
public class LinkFrequency<TLinkAddress>
public class LinkFrequency<TLinkAddress> where TLinkAddress: struct, IUnsignedNumber<TLinkAddress>
{
/// <summary>
/// <para>
Expand Down Expand Up @@ -65,7 +66,7 @@ public LinkFrequency() { }
/// <para></para>
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void IncrementFrequency() => Frequency = Arithmetic<TLinkAddress>.Increment(Frequency);
public void IncrementFrequency() => Frequency = Frequency + TLinkAddress.One;

/// <summary>
/// <para>
Expand All @@ -74,7 +75,7 @@ public LinkFrequency() { }
/// <para></para>
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void DecrementFrequency() => Frequency = Arithmetic<TLinkAddress>.Decrement(Frequency);
public void DecrementFrequency() => Frequency = Frequency - TLinkAddress.One;

/// <summary>
/// <para>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private bool VisitElement(TLinkAddress element)
{
if (element == _symbol)
{
_total = Arithmetic.Increment(_total);
_total = _total + TLinkAddress.One;
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public TotalMarkedSequenceSymbolFrequencyOneOffCounter(ILinks<TLinkAddress> link
protected override void CountSequenceSymbolFrequency(TLinkAddress link)
{
var symbolFrequencyCounter = new MarkedSequenceSymbolFrequencyOneOffCounter<TLinkAddress>(_links, _markedSequenceMatcher, link, _symbol);
_total = Arithmetic.Add(_total, symbolFrequencyCounter.Count());
_total = _total + symbolFrequencyCounter.Count();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private void CountCore(TLinkAddress link)
protected virtual void CountSequenceSymbolFrequency(TLinkAddress link)
{
var symbolFrequencyCounter = new SequenceSymbolFrequencyOneOffCounter<TLinkAddress>(_links, link, _symbol);
_total = Arithmetic.Add(_total, symbolFrequencyCounter.Count());
_total = _total + symbolFrequencyCounter.Count();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private TLinkAddress EachElementHandler(IList<TLinkAddress>? doublet)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public TLinkAddress Get(TLinkAddress sequence)
while (!_elementMatcher.IsMatched(pairOrElement))
{
pairOrElement = _links.GetTarget(pairOrElement);
height = Arithmetic.Increment(height);
height = height + TLinkAddress.One;
}
return height;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@

namespace Platform.Data.Doublets.Sequences.Numbers.Byte;

public class BytesToRawNumberSequenceConverter<TLinkAddress> : LinksDecoratorBase<TLinkAddress>, IConverter<IList<byte>, TLinkAddress> where TLinkAddress : struct, IUnsignedNumber<TLinkAddress>, IComparisonOperators<TLinkAddress, TLinkAddress, bool>
public class BytesToRawNumberSequenceConverter<TLinkAddress> : LinksDecoratorBase<TLinkAddress>, IConverter<IList<byte>, TLinkAddress> where TLinkAddress : struct, IUnsignedNumber<TLinkAddress>, IComparisonOperators<TLinkAddress, TLinkAddress, bool>, IShiftOperators<TLinkAddress, int, TLinkAddress>
{
public static readonly TLinkAddress MaximumValue = NumericType<TLinkAddress>.MaxValue;

public static readonly TLinkAddress BitMask = Bit.ShiftRight(MaximumValue, 1);
public static readonly TLinkAddress BitMask = (MaximumValue >> 1);

public static readonly int BitsSize = NumericType<TLinkAddress>.BitsSize;

Expand Down Expand Up @@ -54,7 +54,7 @@ public BytesToRawNumberSequenceConverter(ILinks<TLinkAddress> links, IConverter<
ListToSequenceConverter = listToSequenceConverter;
BalancedVariantConverter = new BalancedVariantConverter<TLinkAddress>(_links);
StringToUnicodeSequenceConverter = stringToUnicodeSequenceConverter;
Type = Arithmetic.Increment(TLinkAddress.Zero);
Type = TLinkAddress.Zero + TLinkAddress.One;
ByteArrayLengthType = _links.GetOrCreate(Type, StringToUnicodeSequenceConverter.Convert(nameof(ByteArrayLengthType)));
ByteArrayType = _links.GetOrCreate(Type, StringToUnicodeSequenceConverter.Convert(nameof(ByteArrayType)));
EmptyByteArraySequenceType = _links.GetOrCreate(Type, StringToUnicodeSequenceConverter.Convert(nameof(EmptyByteArraySequenceType)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

namespace Platform.Data.Doublets.Sequences.Numbers.Byte;

public class RawNumberSequenceToBytesConverter<TLinkAddress> : LinksDecoratorBase<TLinkAddress>, IConverter<TLinkAddress, IList<byte>> where TLinkAddress : struct, IUnsignedNumber<TLinkAddress>, IComparisonOperators<TLinkAddress, TLinkAddress, bool>
public class RawNumberSequenceToBytesConverter<TLinkAddress> : LinksDecoratorBase<TLinkAddress>, IConverter<TLinkAddress, IList<byte>> where TLinkAddress : struct, IUnsignedNumber<TLinkAddress>, IComparisonOperators<TLinkAddress, TLinkAddress, bool>, IShiftOperators<TLinkAddress, int, TLinkAddress>
{
public static readonly TLinkAddress MaximumValue = NumericType<TLinkAddress>.MaxValue;

public static readonly TLinkAddress BitMask = Bit.ShiftRight(MaximumValue, 1);
public static readonly TLinkAddress BitMask = (MaximumValue >> 1);

public static readonly int BitsSize = NumericType<TLinkAddress>.BitsSize;

Expand All @@ -46,7 +46,7 @@ public RawNumberSequenceToBytesConverter(ILinks<TLinkAddress> links, IConverter<
BalancedVariantConverter = new BalancedVariantConverter<TLinkAddress>(links);
StringToUnicodeSequenceConverteer = stringToUnicodeSequenceConverter;
UnicodeSequenceToStringConverteer = unicodeSequenceToStringConverteer;
Type = Arithmetic.Increment(TLinkAddress.Zero);
Type = TLinkAddress.Zero + TLinkAddress.One;
}

private bool IsEmptyArray(TLinkAddress array)
Expand All @@ -60,7 +60,7 @@ private bool IsEmptyArray(TLinkAddress array)
private void EnsureIsByteArrayLength(TLinkAddress byteArrayLengthAddress)
{
var source = _links.GetSource(byteArrayLengthAddress);
var type = Arithmetic.Increment(TLinkAddress.Zero);
var type = TLinkAddress.Zero + TLinkAddress.One;
var byteArrayLengthType = _links.SearchOrDefault(type, StringToUnicodeSequenceConverteer.Convert("ByteArrayLengthType"));
if (byteArrayLengthType == TLinkAddress.Zero)
{
Expand Down Expand Up @@ -156,10 +156,13 @@ public IList<byte> Convert(TLinkAddress source)
// return byteList;
}

private static byte GetByteWithNotSavedBitsAtEnd(TLinkAddress currentRawNumber, int nonSavedBits)
{
var @byte = byte.CreateTruncating(currentRawNumber);
@byte = Bit.ShiftLeft(@byte, 8 - nonSavedBits);
return @byte;
}
// private static byte GetByteWithNotSavedBitsAtEnd(TLinkAddress currentRawNumber, int nonSavedBits)
// {
// byte @byte = byte.CreateTruncating(currentRawNumber);
// var a = @byte << 1;
// var b = byte.CreateTruncating(1) << 1;
// var c = uint.CreateTruncating(1) << 1;
// @byte = (@byte << 2);
// return @byte;
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Platform.Data.Doublets.Numbers.Rational
/// </summary>
/// <seealso cref="LinksDecoratorBase{TLinkAddress}"/>
/// <seealso cref="IConverter{decimal, TLinkAddress}"/>
public class DecimalToRationalConverter<TLinkAddress> : LinksDecoratorBase<TLinkAddress>, IConverter<decimal, TLinkAddress> where TLinkAddress : struct, IUnsignedNumber<TLinkAddress>, IComparisonOperators<TLinkAddress, TLinkAddress, bool>
public class DecimalToRationalConverter<TLinkAddress> : LinksDecoratorBase<TLinkAddress>, IConverter<decimal, TLinkAddress> where TLinkAddress : struct, IUnsignedNumber<TLinkAddress>, IComparisonOperators<TLinkAddress, TLinkAddress, bool>, IBitwiseOperators<TLinkAddress, TLinkAddress, TLinkAddress>, IShiftOperators<TLinkAddress, int, TLinkAddress>
{
/// <summary>
/// <para>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Platform.Data.Doublets.Numbers.Raw
/// </summary>
/// <seealso cref="LinksDecoratorBase{TLinkAddress}"/>
/// <seealso cref="IConverter{BigInteger, TLinkAddress}"/>
public class BigIntegerToRawNumberSequenceConverter<TLinkAddress> : LinksDecoratorBase<TLinkAddress>, IConverter<BigInteger, TLinkAddress> where TLinkAddress : struct, IUnsignedNumber<TLinkAddress>, IComparisonOperators<TLinkAddress, TLinkAddress, bool>
public class BigIntegerToRawNumberSequenceConverter<TLinkAddress> : LinksDecoratorBase<TLinkAddress>, IConverter<BigInteger, TLinkAddress> where TLinkAddress : struct, IUnsignedNumber<TLinkAddress>, IComparisonOperators<TLinkAddress, TLinkAddress, bool>, IBitwiseOperators<TLinkAddress, TLinkAddress, TLinkAddress>, IShiftOperators<TLinkAddress, int, TLinkAddress>
{
/// <summary>
/// <para>
Expand All @@ -34,7 +34,7 @@ public class BigIntegerToRawNumberSequenceConverter<TLinkAddress> : LinksDecorat
/// </para>
/// <para></para>
/// </summary>
public static readonly TLinkAddress BitMask = Bit.ShiftRight(MaximumValue, 1);
public static readonly TLinkAddress BitMask = (MaximumValue >> 1);
/// <summary>
/// <para>
/// The address to number converter.
Expand Down Expand Up @@ -92,7 +92,7 @@ private List<TLinkAddress> GetRawNumberParts(BigInteger bigInteger)
do
{
var bigIntBytes = currentBigInt.ToByteArray();
var bigIntWithBitMask = Bit.And(bigIntBytes.ToStructure<TLinkAddress>(), BitMask);
var bigIntWithBitMask = (bigIntBytes.ToStructure<TLinkAddress>() & BitMask);
var rawNumber = AddressToNumberConverter.Convert(bigIntWithBitMask);
rawNumbers.Add(rawNumber);
currentBigInt >>= (NumericType<TLinkAddress>.BitsSize - 1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Net.Security;
using System.Numerics;
using System.Runtime.CompilerServices;
using Platform.Collections.Stacks;
Expand All @@ -20,8 +21,8 @@ namespace Platform.Data.Doublets.Numbers.Raw
/// <seealso cref="LinksDecoratorBase{TSource}"/>
/// <seealso cref="IConverter{TSource, TTarget}"/>
public class LongRawNumberSequenceToNumberConverter<TSource, TTarget> : LinksDecoratorBase<TSource>, IConverter<TSource, TTarget>
where TSource : struct, IUnsignedNumber<TSource>, IComparisonOperators<TSource, TSource, bool>
where TTarget : struct, IUnsignedNumber<TTarget>, IComparisonOperators<TTarget, TTarget, bool>
where TSource : struct, IUnsignedNumber<TSource>, IComparisonOperators<TSource, TSource, bool>, IBitwiseOperators<TSource, TSource, TSource>, IShiftOperators<TSource, int, TSource>
where TTarget : struct, IUnsignedNumber<TTarget>, IComparisonOperators<TTarget, TTarget, bool>, IBitwiseOperators<TTarget, TTarget, TTarget>, IShiftOperators<TTarget, int, TTarget>
{
private static readonly int _bitsPerRawNumber = NumericType<TSource>.BitsSize - 1;
private static readonly UncheckedConverter<TSource, TTarget> _sourceToTargetConverter = UncheckedConverter<TSource, TTarget>.Default;
Expand Down Expand Up @@ -74,7 +75,7 @@ public TTarget Convert(TSource source)
TTarget result = TTarget.Zero;
foreach (var element in walker.Walk(source))
{
result = Bit.Or(Bit.ShiftLeft(result, _bitsPerRawNumber), Convert(element));
result = ((result << (_bitsPerRawNumber)) | Convert(element));
}
return result;
}
Expand Down
Loading

0 comments on commit 02f9f2f

Please sign in to comment.