diff --git a/src/ZoneTree/Comparers/ByteArrayComparerAscending.cs b/src/ZoneTree/Comparers/ByteArrayComparerAscending.cs index 027dadf..ef496a5 100644 --- a/src/ZoneTree/Comparers/ByteArrayComparerAscending.cs +++ b/src/ZoneTree/Comparers/ByteArrayComparerAscending.cs @@ -9,7 +9,7 @@ public int Compare(in Memory x, in Memory 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) diff --git a/src/ZoneTree/Comparers/ByteArrayComparerDescending.cs b/src/ZoneTree/Comparers/ByteArrayComparerDescending.cs index 3871a0a..da3befe 100644 --- a/src/ZoneTree/Comparers/ByteArrayComparerDescending.cs +++ b/src/ZoneTree/Comparers/ByteArrayComparerDescending.cs @@ -9,7 +9,7 @@ public int Compare(in Memory x, in Memory 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) diff --git a/src/ZoneTree/Comparers/ByteComparerAscending.cs b/src/ZoneTree/Comparers/ByteComparerAscending.cs index 63af02e..44cde98 100644 --- a/src/ZoneTree/Comparers/ByteComparerAscending.cs +++ b/src/ZoneTree/Comparers/ByteComparerAscending.cs @@ -4,6 +4,6 @@ public sealed class ByteComparerAscending : IRefComparer { public int Compare(in byte x, in byte y) { - return x - y; + return x.CompareTo(y); } } diff --git a/src/ZoneTree/Comparers/CharComparerAscending.cs b/src/ZoneTree/Comparers/CharComparerAscending.cs index 9666ef5..4d3ab15 100644 --- a/src/ZoneTree/Comparers/CharComparerAscending.cs +++ b/src/ZoneTree/Comparers/CharComparerAscending.cs @@ -4,6 +4,6 @@ public sealed class CharComparerAscending : IRefComparer { public int Compare(in char x, in char y) { - return x - y; + return x.CompareTo(y); } } diff --git a/src/ZoneTree/Comparers/DateTimeComparerAscending.cs b/src/ZoneTree/Comparers/DateTimeComparerAscending.cs index c7abc5c..5e247ea 100644 --- a/src/ZoneTree/Comparers/DateTimeComparerAscending.cs +++ b/src/ZoneTree/Comparers/DateTimeComparerAscending.cs @@ -4,9 +4,6 @@ public sealed class DateTimeComparerAscending : IRefComparer { 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); } } \ No newline at end of file diff --git a/src/ZoneTree/Comparers/DateTimeComparerDescending.cs b/src/ZoneTree/Comparers/DateTimeComparerDescending.cs index 8b21b31..ca8e7a7 100644 --- a/src/ZoneTree/Comparers/DateTimeComparerDescending.cs +++ b/src/ZoneTree/Comparers/DateTimeComparerDescending.cs @@ -4,9 +4,6 @@ public sealed class DateTimeComparerDescending : IRefComparer { 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); } } \ No newline at end of file diff --git a/src/ZoneTree/Comparers/DecimalComparerAscending.cs b/src/ZoneTree/Comparers/DecimalComparerAscending.cs index 50df086..4cba0d0 100644 --- a/src/ZoneTree/Comparers/DecimalComparerAscending.cs +++ b/src/ZoneTree/Comparers/DecimalComparerAscending.cs @@ -4,9 +4,6 @@ public sealed class DecimalComparerAscending : IRefComparer { 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); } } diff --git a/src/ZoneTree/Comparers/DecimalComparerDescending.cs b/src/ZoneTree/Comparers/DecimalComparerDescending.cs new file mode 100644 index 0000000..5e3308a --- /dev/null +++ b/src/ZoneTree/Comparers/DecimalComparerDescending.cs @@ -0,0 +1,9 @@ +namespace Tenray.ZoneTree.Comparers; + +public sealed class DecimalComparerDescending : IRefComparer +{ + public int Compare(in decimal x, in decimal y) + { + return y.CompareTo(x); + } +} \ No newline at end of file diff --git a/src/ZoneTree/Comparers/DoubleComparerAscending.cs b/src/ZoneTree/Comparers/DoubleComparerAscending.cs index 27a3a8c..4482032 100644 --- a/src/ZoneTree/Comparers/DoubleComparerAscending.cs +++ b/src/ZoneTree/Comparers/DoubleComparerAscending.cs @@ -4,9 +4,6 @@ public sealed class DoubleComparerAscending : IRefComparer { 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); } } \ No newline at end of file diff --git a/src/ZoneTree/Comparers/DoubleComparerDescending.cs b/src/ZoneTree/Comparers/DoubleComparerDescending.cs index e76b218..f772d6c 100644 --- a/src/ZoneTree/Comparers/DoubleComparerDescending.cs +++ b/src/ZoneTree/Comparers/DoubleComparerDescending.cs @@ -1,12 +1,9 @@ namespace Tenray.ZoneTree.Comparers; -public sealed class DoubleComparerDescending: IRefComparer +public sealed class DoubleComparerDescending : IRefComparer { 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); } } \ No newline at end of file diff --git a/src/ZoneTree/Comparers/Int16ComparerAscending.cs b/src/ZoneTree/Comparers/Int16ComparerAscending.cs index 85ddf1c..d843d00 100644 --- a/src/ZoneTree/Comparers/Int16ComparerAscending.cs +++ b/src/ZoneTree/Comparers/Int16ComparerAscending.cs @@ -4,6 +4,6 @@ public sealed class Int16ComparerAscending : IRefComparer { public int Compare(in short x, in short y) { - return x - y; + return x.CompareTo(y); } -} \ No newline at end of file +} diff --git a/src/ZoneTree/Comparers/Int16ComparerDescending.cs b/src/ZoneTree/Comparers/Int16ComparerDescending.cs index 5c04187..5968b66 100644 --- a/src/ZoneTree/Comparers/Int16ComparerDescending.cs +++ b/src/ZoneTree/Comparers/Int16ComparerDescending.cs @@ -4,6 +4,6 @@ public sealed class Int16ComparerDescending : IRefComparer { public int Compare(in short x, in short y) { - return y - x; + return y.CompareTo(x); } -} \ No newline at end of file +} diff --git a/src/ZoneTree/Comparers/Int32ComparerAscending.cs b/src/ZoneTree/Comparers/Int32ComparerAscending.cs index 0767f8c..70a12ae 100644 --- a/src/ZoneTree/Comparers/Int32ComparerAscending.cs +++ b/src/ZoneTree/Comparers/Int32ComparerAscending.cs @@ -4,6 +4,6 @@ public sealed class Int32ComparerAscending : IRefComparer { public int Compare(in int x, in int y) { - return x - y; + return x.CompareTo(y); } } diff --git a/src/ZoneTree/Comparers/Int32ComparerDescending.cs b/src/ZoneTree/Comparers/Int32ComparerDescending.cs index 56f46fa..4ab7530 100644 --- a/src/ZoneTree/Comparers/Int32ComparerDescending.cs +++ b/src/ZoneTree/Comparers/Int32ComparerDescending.cs @@ -4,6 +4,6 @@ public sealed class Int32ComparerDescending : IRefComparer { public int Compare(in int x, in int y) { - return y - x; + return y.CompareTo(x); } } diff --git a/src/ZoneTree/Comparers/Int64ComparerAscending.cs b/src/ZoneTree/Comparers/Int64ComparerAscending.cs index 4518d1e..cf4c4aa 100644 --- a/src/ZoneTree/Comparers/Int64ComparerAscending.cs +++ b/src/ZoneTree/Comparers/Int64ComparerAscending.cs @@ -4,9 +4,6 @@ public sealed class Int64ComparerAscending : IRefComparer { 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); } } diff --git a/src/ZoneTree/Comparers/Int64ComparerDescending.cs b/src/ZoneTree/Comparers/Int64ComparerDescending.cs index a6e096c..fdbe900 100644 --- a/src/ZoneTree/Comparers/Int64ComparerDescending.cs +++ b/src/ZoneTree/Comparers/Int64ComparerDescending.cs @@ -4,9 +4,6 @@ public sealed class Int64ComparerDescending : IRefComparer { 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); } -} \ No newline at end of file +} diff --git a/src/ZoneTree/Comparers/UInt16ComparerAscending.cs b/src/ZoneTree/Comparers/UInt16ComparerAscending.cs new file mode 100644 index 0000000..3ccee75 --- /dev/null +++ b/src/ZoneTree/Comparers/UInt16ComparerAscending.cs @@ -0,0 +1,9 @@ +namespace Tenray.ZoneTree.Comparers; + +public sealed class UInt16ComparerAscending : IRefComparer +{ + public int Compare(in ushort x, in ushort y) + { + return x.CompareTo(y); + } +} \ No newline at end of file diff --git a/src/ZoneTree/Comparers/UInt16ComparerDescending.cs b/src/ZoneTree/Comparers/UInt16ComparerDescending.cs new file mode 100644 index 0000000..570ad71 --- /dev/null +++ b/src/ZoneTree/Comparers/UInt16ComparerDescending.cs @@ -0,0 +1,9 @@ +namespace Tenray.ZoneTree.Comparers; + +public sealed class UInt16ComparerDescending : IRefComparer +{ + public int Compare(in ushort x, in ushort y) + { + return y.CompareTo(x); + } +} \ No newline at end of file diff --git a/src/ZoneTree/Comparers/UInt32ComparerAscending.cs b/src/ZoneTree/Comparers/UInt32ComparerAscending.cs new file mode 100644 index 0000000..f769424 --- /dev/null +++ b/src/ZoneTree/Comparers/UInt32ComparerAscending.cs @@ -0,0 +1,9 @@ +namespace Tenray.ZoneTree.Comparers; + +public sealed class UInt32ComparerAscending : IRefComparer +{ + public int Compare(in uint x, in uint y) + { + return x.CompareTo(y); + } +} diff --git a/src/ZoneTree/Comparers/UInt32ComparerDescending.cs b/src/ZoneTree/Comparers/UInt32ComparerDescending.cs new file mode 100644 index 0000000..314e7fa --- /dev/null +++ b/src/ZoneTree/Comparers/UInt32ComparerDescending.cs @@ -0,0 +1,9 @@ +namespace Tenray.ZoneTree.Comparers; + +public sealed class UInt32ComparerDescending : IRefComparer +{ + public int Compare(in uint x, in uint y) + { + return y.CompareTo(x); + } +} diff --git a/src/ZoneTree/Comparers/UInt64ComparerAscending.cs b/src/ZoneTree/Comparers/UInt64ComparerAscending.cs new file mode 100644 index 0000000..aab080d --- /dev/null +++ b/src/ZoneTree/Comparers/UInt64ComparerAscending.cs @@ -0,0 +1,9 @@ +namespace Tenray.ZoneTree.Comparers; + +public sealed class UInt64ComparerAscending : IRefComparer +{ + public int Compare(in ulong x, in ulong y) + { + return x.CompareTo(y); + } +} \ No newline at end of file diff --git a/src/ZoneTree/Comparers/UInt64ComparerDescending.cs b/src/ZoneTree/Comparers/UInt64ComparerDescending.cs new file mode 100644 index 0000000..027828b --- /dev/null +++ b/src/ZoneTree/Comparers/UInt64ComparerDescending.cs @@ -0,0 +1,9 @@ +namespace Tenray.ZoneTree.Comparers; + +public sealed class UInt64ComparerDescending : IRefComparer +{ + public int Compare(in ulong x, in ulong y) + { + return y.CompareTo(x); + } +} \ No newline at end of file diff --git a/src/ZoneTree/Core/ZoneTree.BottomSegments.Merge.cs b/src/ZoneTree/Core/ZoneTree.BottomSegments.Merge.cs index dbcbdf6..d8700cd 100644 --- a/src/ZoneTree/Core/ZoneTree.BottomSegments.Merge.cs +++ b/src/ZoneTree/Core/ZoneTree.BottomSegments.Merge.cs @@ -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) diff --git a/src/ZoneTree/Serializers/Int16Serializer.cs b/src/ZoneTree/Serializers/Int16Serializer.cs index 2a79030..151b987 100644 --- a/src/ZoneTree/Serializers/Int16Serializer.cs +++ b/src/ZoneTree/Serializers/Int16Serializer.cs @@ -11,4 +11,4 @@ public Memory Serialize(in short entry) { return BitConverter.GetBytes(entry); } -} \ No newline at end of file +} diff --git a/src/ZoneTree/Serializers/UInt16Serializer.cs b/src/ZoneTree/Serializers/UInt16Serializer.cs new file mode 100644 index 0000000..dba5215 --- /dev/null +++ b/src/ZoneTree/Serializers/UInt16Serializer.cs @@ -0,0 +1,14 @@ +namespace Tenray.ZoneTree.Serializers; + +public sealed class UInt16Serializer : ISerializer +{ + public ushort Deserialize(Memory bytes) + { + return BitConverter.ToUInt16(bytes.Span); + } + + public Memory Serialize(in ushort entry) + { + return BitConverter.GetBytes(entry); + } +} \ No newline at end of file diff --git a/src/ZoneTree/Serializers/UInt32Serializer.cs b/src/ZoneTree/Serializers/UInt32Serializer.cs new file mode 100644 index 0000000..2c406fc --- /dev/null +++ b/src/ZoneTree/Serializers/UInt32Serializer.cs @@ -0,0 +1,14 @@ +namespace Tenray.ZoneTree.Serializers; + +public sealed class UInt32Serializer : ISerializer +{ + public uint Deserialize(Memory bytes) + { + return BitConverter.ToUInt32(bytes.Span); + } + + public Memory Serialize(in uint entry) + { + return BitConverter.GetBytes(entry); + } +} \ No newline at end of file diff --git a/src/ZoneTree/Serializers/UInt64Serializer.cs b/src/ZoneTree/Serializers/UInt64Serializer.cs new file mode 100644 index 0000000..17aff9b --- /dev/null +++ b/src/ZoneTree/Serializers/UInt64Serializer.cs @@ -0,0 +1,14 @@ +namespace Tenray.ZoneTree.Serializers; + +public sealed class UInt64Serializer : ISerializer +{ + public ulong Deserialize(Memory bytes) + { + return BitConverter.ToUInt64(bytes.Span); + } + + public Memory Serialize(in ulong entry) + { + return BitConverter.GetBytes(entry); + } +} diff --git a/src/ZoneTree/ZoneTreeFactory.cs b/src/ZoneTree/ZoneTreeFactory.cs index 3d32d5f..085bab7 100644 --- a/src/ZoneTree/ZoneTreeFactory.cs +++ b/src/ZoneTree/ZoneTreeFactory.cs @@ -364,8 +364,11 @@ void FillComparer() decimal => new DecimalComparerAscending() as IRefComparer, double => new DoubleComparerAscending() as IRefComparer, short => new Int16ComparerAscending() as IRefComparer, + ushort => new UInt16ComparerAscending() as IRefComparer, int => new Int32ComparerAscending() as IRefComparer, + uint => new UInt32ComparerAscending() as IRefComparer, long => new Int64ComparerAscending() as IRefComparer, + ulong => new UInt64ComparerAscending() as IRefComparer, Guid => new GuidComparerAscending() as IRefComparer, _ => null }; @@ -391,8 +394,11 @@ void FillKeySerializer() decimal => new DecimalSerializer() as ISerializer, double => new DoubleSerializer() as ISerializer, short => new Int16Serializer() as ISerializer, + ushort => new UInt16Serializer() as ISerializer, int => new Int32Serializer() as ISerializer, + uint => new UInt32Serializer() as ISerializer, long => new Int64Serializer() as ISerializer, + ulong => new UInt64Serializer() as ISerializer, Guid => new StructSerializer() as ISerializer, _ => null }; @@ -425,8 +431,11 @@ void FillValueSerializer() decimal => new DecimalSerializer() as ISerializer, double => new DoubleSerializer() as ISerializer, short => new Int16Serializer() as ISerializer, + ushort => new UInt16Serializer() as ISerializer, int => new Int32Serializer() as ISerializer, + uint => new UInt32Serializer() as ISerializer, long => new Int64Serializer() as ISerializer, + ulong => new UInt64Serializer() as ISerializer, Guid => new StructSerializer() as ISerializer, _ => null };