Skip to content

Commit

Permalink
Merge pull request #70 from koculu/69-enhancement-add-serializers-and…
Browse files Browse the repository at this point in the history
…-comparers-for-unsigned-primitive-types

Enhancement: add serializers and comparers for unsigned primitive types
  • Loading branch information
koculu authored Aug 11, 2024
2 parents 81a6347 + 3263c74 commit dc4f6bf
Show file tree
Hide file tree
Showing 28 changed files with 137 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/ZoneTree/Comparers/ByteArrayComparerAscending.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public int Compare(in Memory<byte> x, in Memory<byte> y)
var spanY = y.Span;
for (var i = 0; i < len; ++i)
{
var r = spanX[i] - spanY[i];
var r = spanX[i].CompareTo(spanY[i]);
if (r < 0)
return -1;
if (r > 0)
Expand Down
2 changes: 1 addition & 1 deletion src/ZoneTree/Comparers/ByteArrayComparerDescending.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public int Compare(in Memory<byte> x, in Memory<byte> y)
var spanY = y.Span;
for (var i = 0; i < len; ++i)
{
var r = spanY[i] - spanX[i];
var r = spanY[i].CompareTo(spanX[i]);
if (r < 0)
return -1;
if (r > 0)
Expand Down
2 changes: 1 addition & 1 deletion src/ZoneTree/Comparers/ByteComparerAscending.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ public sealed class ByteComparerAscending : IRefComparer<byte>
{
public int Compare(in byte x, in byte y)
{
return x - y;
return x.CompareTo(y);
}
}
2 changes: 1 addition & 1 deletion src/ZoneTree/Comparers/CharComparerAscending.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ public sealed class CharComparerAscending : IRefComparer<char>
{
public int Compare(in char x, in char y)
{
return x - y;
return x.CompareTo(y);
}
}
5 changes: 1 addition & 4 deletions src/ZoneTree/Comparers/DateTimeComparerAscending.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ public sealed class DateTimeComparerAscending : IRefComparer<DateTime>
{
public int Compare(in DateTime x, in DateTime y)
{
var r = x.Ticks - y.Ticks;
if (r == 0)
return 0;
return r < 0 ? -1 : 1;
return x.CompareTo(y);
}
}
5 changes: 1 addition & 4 deletions src/ZoneTree/Comparers/DateTimeComparerDescending.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ public sealed class DateTimeComparerDescending : IRefComparer<DateTime>
{
public int Compare(in DateTime x, in DateTime y)
{
var r = y.Ticks - x.Ticks;
if (r == 0)
return 0;
return r < 0 ? -1 : 1;
return y.CompareTo(x);
}
}
5 changes: 1 addition & 4 deletions src/ZoneTree/Comparers/DecimalComparerAscending.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ public sealed class DecimalComparerAscending : IRefComparer<decimal>
{
public int Compare(in decimal x, in decimal y)
{
var r = x - y;
if (r == 0)
return 0;
return r < 0 ? -1 : 1;
return x.CompareTo(y);
}
}
9 changes: 9 additions & 0 deletions src/ZoneTree/Comparers/DecimalComparerDescending.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Tenray.ZoneTree.Comparers;

public sealed class DecimalComparerDescending : IRefComparer<decimal>
{
public int Compare(in decimal x, in decimal y)
{
return y.CompareTo(x);
}
}
5 changes: 1 addition & 4 deletions src/ZoneTree/Comparers/DoubleComparerAscending.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ public sealed class DoubleComparerAscending : IRefComparer<double>
{
public int Compare(in double x, in double y)
{
var r = x - y;
if (r == 0)
return 0;
return r < 0 ? -1 : 1;
return x.CompareTo(y);
}
}
7 changes: 2 additions & 5 deletions src/ZoneTree/Comparers/DoubleComparerDescending.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
namespace Tenray.ZoneTree.Comparers;

public sealed class DoubleComparerDescending: IRefComparer<double>
public sealed class DoubleComparerDescending : IRefComparer<double>
{
public int Compare(in double x, in double y)
{
var r = y - x;
if (r == 0)
return 0;
return r < 0 ? -1 : 1;
return y.CompareTo(x);
}
}
4 changes: 2 additions & 2 deletions src/ZoneTree/Comparers/Int16ComparerAscending.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ public sealed class Int16ComparerAscending : IRefComparer<short>
{
public int Compare(in short x, in short y)
{
return x - y;
return x.CompareTo(y);
}
}
}
4 changes: 2 additions & 2 deletions src/ZoneTree/Comparers/Int16ComparerDescending.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ public sealed class Int16ComparerDescending : IRefComparer<short>
{
public int Compare(in short x, in short y)
{
return y - x;
return y.CompareTo(x);
}
}
}
2 changes: 1 addition & 1 deletion src/ZoneTree/Comparers/Int32ComparerAscending.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ public sealed class Int32ComparerAscending : IRefComparer<int>
{
public int Compare(in int x, in int y)
{
return x - y;
return x.CompareTo(y);
}
}
2 changes: 1 addition & 1 deletion src/ZoneTree/Comparers/Int32ComparerDescending.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ public sealed class Int32ComparerDescending : IRefComparer<int>
{
public int Compare(in int x, in int y)
{
return y - x;
return y.CompareTo(x);
}
}
5 changes: 1 addition & 4 deletions src/ZoneTree/Comparers/Int64ComparerAscending.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ public sealed class Int64ComparerAscending : IRefComparer<long>
{
public int Compare(in long x, in long y)
{
var r = x - y;
if (r == 0)
return 0;
return r < 0 ? -1 : 1;
return x.CompareTo(y);
}
}
7 changes: 2 additions & 5 deletions src/ZoneTree/Comparers/Int64ComparerDescending.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ public sealed class Int64ComparerDescending : IRefComparer<long>
{
public int Compare(in long x, in long y)
{
var r = y - x;
if (r == 0)
return 0;
return r < 0 ? -1 : 1;
return y.CompareTo(x);
}
}
}
9 changes: 9 additions & 0 deletions src/ZoneTree/Comparers/UInt16ComparerAscending.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Tenray.ZoneTree.Comparers;

public sealed class UInt16ComparerAscending : IRefComparer<ushort>
{
public int Compare(in ushort x, in ushort y)
{
return x.CompareTo(y);
}
}
9 changes: 9 additions & 0 deletions src/ZoneTree/Comparers/UInt16ComparerDescending.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Tenray.ZoneTree.Comparers;

public sealed class UInt16ComparerDescending : IRefComparer<ushort>
{
public int Compare(in ushort x, in ushort y)
{
return y.CompareTo(x);
}
}
9 changes: 9 additions & 0 deletions src/ZoneTree/Comparers/UInt32ComparerAscending.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Tenray.ZoneTree.Comparers;

public sealed class UInt32ComparerAscending : IRefComparer<uint>
{
public int Compare(in uint x, in uint y)
{
return x.CompareTo(y);
}
}
9 changes: 9 additions & 0 deletions src/ZoneTree/Comparers/UInt32ComparerDescending.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Tenray.ZoneTree.Comparers;

public sealed class UInt32ComparerDescending : IRefComparer<uint>
{
public int Compare(in uint x, in uint y)
{
return y.CompareTo(x);
}
}
9 changes: 9 additions & 0 deletions src/ZoneTree/Comparers/UInt64ComparerAscending.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Tenray.ZoneTree.Comparers;

public sealed class UInt64ComparerAscending : IRefComparer<ulong>
{
public int Compare(in ulong x, in ulong y)
{
return x.CompareTo(y);
}
}
9 changes: 9 additions & 0 deletions src/ZoneTree/Comparers/UInt64ComparerDescending.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Tenray.ZoneTree.Comparers;

public sealed class UInt64ComparerDescending : IRefComparer<ulong>
{
public int Compare(in ulong x, in ulong y)
{
return y.CompareTo(x);
}
}
3 changes: 3 additions & 0 deletions src/ZoneTree/Core/ZoneTree.BottomSegments.Merge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ MergeResult MergeBottomSegmentsInternal(int from, int to)

var writeDeletedValues = from > 0;

if (to > BottomSegments.Count)
to = BottomSegments.Count;

var mergingSegments = bottomSegments
.Select(x => x.GetSeekableIterator())
.Skip(from)
Expand Down
2 changes: 1 addition & 1 deletion src/ZoneTree/Serializers/Int16Serializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ public Memory<byte> Serialize(in short entry)
{
return BitConverter.GetBytes(entry);
}
}
}
14 changes: 14 additions & 0 deletions src/ZoneTree/Serializers/UInt16Serializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Tenray.ZoneTree.Serializers;

public sealed class UInt16Serializer : ISerializer<ushort>
{
public ushort Deserialize(Memory<byte> bytes)
{
return BitConverter.ToUInt16(bytes.Span);
}

public Memory<byte> Serialize(in ushort entry)
{
return BitConverter.GetBytes(entry);
}
}
14 changes: 14 additions & 0 deletions src/ZoneTree/Serializers/UInt32Serializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Tenray.ZoneTree.Serializers;

public sealed class UInt32Serializer : ISerializer<uint>
{
public uint Deserialize(Memory<byte> bytes)
{
return BitConverter.ToUInt32(bytes.Span);
}

public Memory<byte> Serialize(in uint entry)
{
return BitConverter.GetBytes(entry);
}
}
14 changes: 14 additions & 0 deletions src/ZoneTree/Serializers/UInt64Serializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Tenray.ZoneTree.Serializers;

public sealed class UInt64Serializer : ISerializer<ulong>
{
public ulong Deserialize(Memory<byte> bytes)
{
return BitConverter.ToUInt64(bytes.Span);
}

public Memory<byte> Serialize(in ulong entry)
{
return BitConverter.GetBytes(entry);
}
}
9 changes: 9 additions & 0 deletions src/ZoneTree/ZoneTreeFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,11 @@ void FillComparer()
decimal => new DecimalComparerAscending() as IRefComparer<TKey>,
double => new DoubleComparerAscending() as IRefComparer<TKey>,
short => new Int16ComparerAscending() as IRefComparer<TKey>,
ushort => new UInt16ComparerAscending() as IRefComparer<TKey>,
int => new Int32ComparerAscending() as IRefComparer<TKey>,
uint => new UInt32ComparerAscending() as IRefComparer<TKey>,
long => new Int64ComparerAscending() as IRefComparer<TKey>,
ulong => new UInt64ComparerAscending() as IRefComparer<TKey>,
Guid => new GuidComparerAscending() as IRefComparer<TKey>,
_ => null
};
Expand All @@ -391,8 +394,11 @@ void FillKeySerializer()
decimal => new DecimalSerializer() as ISerializer<TKey>,
double => new DoubleSerializer() as ISerializer<TKey>,
short => new Int16Serializer() as ISerializer<TKey>,
ushort => new UInt16Serializer() as ISerializer<TKey>,
int => new Int32Serializer() as ISerializer<TKey>,
uint => new UInt32Serializer() as ISerializer<TKey>,
long => new Int64Serializer() as ISerializer<TKey>,
ulong => new UInt64Serializer() as ISerializer<TKey>,
Guid => new StructSerializer<Guid>() as ISerializer<TKey>,
_ => null
};
Expand Down Expand Up @@ -425,8 +431,11 @@ void FillValueSerializer()
decimal => new DecimalSerializer() as ISerializer<TValue>,
double => new DoubleSerializer() as ISerializer<TValue>,
short => new Int16Serializer() as ISerializer<TValue>,
ushort => new UInt16Serializer() as ISerializer<TValue>,
int => new Int32Serializer() as ISerializer<TValue>,
uint => new UInt32Serializer() as ISerializer<TValue>,
long => new Int64Serializer() as ISerializer<TValue>,
ulong => new UInt64Serializer() as ISerializer<TValue>,
Guid => new StructSerializer<Guid>() as ISerializer<TValue>,
_ => null
};
Expand Down

0 comments on commit dc4f6bf

Please sign in to comment.