Skip to content

Commit

Permalink
Merge pull request #2 from ledjon-behluli/fix-concurrent-tuple-to-string
Browse files Browse the repository at this point in the history
Fix concurrent tuple ToString & GetEnumerator
  • Loading branch information
ledjon-behluli authored Jul 22, 2023
2 parents 11289a6 + 2b628eb commit e707fd8
Show file tree
Hide file tree
Showing 42 changed files with 420 additions and 88 deletions.
2 changes: 1 addition & 1 deletion src/OrleanSpaces/Helpers/TemplateHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ public static bool Matches<T, TTuple, TTemplate>(this TTemplate template, TTuple

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ToString<T>(T?[] fields) where T : unmanaged
=> $"({string.Join(", ", fields.Select(field => field is null ? "{NULL}" : field.ToString()))})";
=> fields is null ? "()" : $"({string.Join(", ", fields.Select(field => field is null ? "{NULL}" : field.ToString()))})";
}
2 changes: 1 addition & 1 deletion src/OrleanSpaces/Helpers/TupleHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,5 +244,5 @@ static int GetCharCount(int tupleLength, int maxFieldCharLength)

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ToString<T>(T[] fields) where T : unmanaged
=> $"({string.Join(", ", fields)})";
=> fields is null ? "()" : $"({string.Join(", ", fields)})";
}
14 changes: 8 additions & 6 deletions src/OrleanSpaces/Tuples/SpaceTuple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,14 @@ public bool Equals(SpaceTuple other)
return true;
}

public override int GetHashCode() => fields.GetHashCode();
public override string ToString() => $"({string.Join(", ", fields)})";
public override int GetHashCode() => fields?.GetHashCode() ?? 0;
public override string ToString() => $"({string.Join(", ", fields ?? Array.Empty<object>())})";

/// <summary>
/// Returns an enumerator to enumerate over the fields of this tuple.
/// </summary>
public ReadOnlySpan<object>.Enumerator GetEnumerator() => new ReadOnlySpan<object>(fields).GetEnumerator();
public ReadOnlySpan<object>.Enumerator GetEnumerator() =>
(fields is null ? ReadOnlySpan<object>.Empty : new ReadOnlySpan<object>(fields)).GetEnumerator();
}

/// <summary>
Expand Down Expand Up @@ -224,11 +225,12 @@ public bool Equals(SpaceTemplate other)
return true;
}

public override int GetHashCode() => fields.GetHashCode();
public override string ToString() => $"({string.Join(", ", fields.Select(field => field ?? "{NULL}"))})";
public override int GetHashCode() => fields?.GetHashCode() ?? 0;
public override string ToString() => fields is null ? "()" : $"({string.Join(", ", fields.Select(field => field ?? "{NULL}"))})";

/// <summary>
/// Returns an enumerator to enumerate over the fields of this tuple.
/// </summary>
public ReadOnlySpan<object?>.Enumerator GetEnumerator() => new ReadOnlySpan<object?>(fields).GetEnumerator();
public ReadOnlySpan<object?>.Enumerator GetEnumerator() =>
(fields is null ? ReadOnlySpan<object?>.Empty : new ReadOnlySpan<object?>(fields)).GetEnumerator();
}
10 changes: 6 additions & 4 deletions src/OrleanSpaces/Tuples/Specialized/BoolTuple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,13 @@ public bool Equals(BoolTuple other)
return marshaller.TryParallelEquals(out bool result) ? result : this.SequentialEquals<bool, BoolTuple>(other);
}

public override int GetHashCode() => fields.GetHashCode();
public override int GetHashCode() => fields?.GetHashCode() ?? 0;
public override string ToString() => TupleHelpers.ToString(fields);

static BoolTuple ISpaceFactory<bool, BoolTuple>.Create(bool[] fields) => new(fields);

public ReadOnlySpan<bool>.Enumerator GetEnumerator() => new ReadOnlySpan<bool>(fields).GetEnumerator();
public ReadOnlySpan<bool>.Enumerator GetEnumerator() =>
(fields is null ? ReadOnlySpan<bool>.Empty : new ReadOnlySpan<bool>(fields)).GetEnumerator();

public ReadOnlySpan<char> AsSpan()
{
Expand Down Expand Up @@ -131,8 +132,9 @@ public BoolTemplate([AllowNull] params bool?[] fields)
public bool Matches(BoolTuple tuple) => this.Matches<bool, BoolTuple, BoolTemplate>(tuple);
public bool Equals(BoolTemplate other) => this.SequentialEquals<bool, BoolTemplate>(other);

public override int GetHashCode() => fields.GetHashCode();
public override int GetHashCode() => fields?.GetHashCode() ?? 0;
public override string ToString() => TemplateHelpers.ToString(fields);

public ReadOnlySpan<bool?>.Enumerator GetEnumerator() => new ReadOnlySpan<bool?>(fields).GetEnumerator();
public ReadOnlySpan<bool?>.Enumerator GetEnumerator() =>
(fields is null ? ReadOnlySpan<bool?>.Empty : new ReadOnlySpan<bool?>(fields)).GetEnumerator();
}
10 changes: 6 additions & 4 deletions src/OrleanSpaces/Tuples/Specialized/ByteTuple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ public bool Equals(ByteTuple other)
=> this.TryParallelEquals<byte, ByteTuple>(other, out bool result) ?
result : this.SequentialEquals<byte, ByteTuple>(other);

public override int GetHashCode() => fields.GetHashCode();
public override int GetHashCode() => fields?.GetHashCode() ?? 0;
public override string ToString() => TupleHelpers.ToString(fields);

static ByteTuple ISpaceFactory<byte, ByteTuple>.Create(byte[] fields) => new(fields);

public ReadOnlySpan<char> AsSpan() => this.AsSpan<byte, ByteTuple>(Constants.MaxFieldCharLength_Byte);
public ReadOnlySpan<byte>.Enumerator GetEnumerator() => new ReadOnlySpan<byte>(fields).GetEnumerator();
public ReadOnlySpan<byte>.Enumerator GetEnumerator() =>
(fields is null ? ReadOnlySpan<byte>.Empty : new ReadOnlySpan<byte>(fields)).GetEnumerator();
}

/// <summary>
Expand Down Expand Up @@ -98,8 +99,9 @@ public ByteTemplate([AllowNull] params byte?[] fields)
public bool Matches(ByteTuple tuple) => this.Matches<byte, ByteTuple, ByteTemplate>(tuple);
public bool Equals(ByteTemplate other) => this.SequentialEquals<byte, ByteTemplate>(other);

public override int GetHashCode() => fields.GetHashCode();
public override int GetHashCode() => fields?.GetHashCode() ?? 0;
public override string ToString() => TemplateHelpers.ToString(fields);

public ReadOnlySpan<byte?>.Enumerator GetEnumerator() => new ReadOnlySpan<byte?>(fields).GetEnumerator();
public ReadOnlySpan<byte?>.Enumerator GetEnumerator() =>
(fields is null ? ReadOnlySpan<byte?>.Empty : new ReadOnlySpan<byte?>(fields)).GetEnumerator();
}
10 changes: 6 additions & 4 deletions src/OrleanSpaces/Tuples/Specialized/CharTuple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,14 @@ public bool Equals(CharTuple other)
result : this.SequentialEquals<char, CharTuple>(other);
}

public override int GetHashCode() => fields.GetHashCode();
public override int GetHashCode() => fields?.GetHashCode() ?? 0;
public override string ToString() => TupleHelpers.ToString(fields);

static CharTuple ISpaceFactory<char, CharTuple>.Create(char[] fields) => new(fields);

public ReadOnlySpan<char> AsSpan() => this.AsSpan<char, CharTuple>(Constants.MaxFieldCharLength_Char);
public ReadOnlySpan<char>.Enumerator GetEnumerator() => new ReadOnlySpan<char>(fields).GetEnumerator();
public ReadOnlySpan<char>.Enumerator GetEnumerator() =>
(fields is null ? ReadOnlySpan<char>.Empty : new ReadOnlySpan<char>(fields)).GetEnumerator();
}

/// <summary>
Expand Down Expand Up @@ -109,8 +110,9 @@ public CharTemplate([AllowNull] params char?[] fields)
public bool Matches(CharTuple tuple) => this.Matches<char, CharTuple, CharTemplate>(tuple);
public bool Equals(CharTemplate other) => this.SequentialEquals<char, CharTemplate>(other);

public override int GetHashCode() => fields.GetHashCode();
public override int GetHashCode() => fields?.GetHashCode() ?? 0;
public override string ToString() => TemplateHelpers.ToString(fields);

public ReadOnlySpan<char?>.Enumerator GetEnumerator() => new ReadOnlySpan<char?>(fields).GetEnumerator();
public ReadOnlySpan<char?>.Enumerator GetEnumerator() =>
(fields is null ? ReadOnlySpan<char?>.Empty : new ReadOnlySpan<char?>(fields)).GetEnumerator();
}
10 changes: 6 additions & 4 deletions src/OrleanSpaces/Tuples/Specialized/DateTimeOffsetTuple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,14 @@ public bool Equals(DateTimeOffsetTuple other)
result : this.SequentialEquals<DateTimeOffset, DateTimeOffsetTuple>(other);
}

public override int GetHashCode() => fields.GetHashCode();
public override int GetHashCode() => fields?.GetHashCode() ?? 0;
public override string ToString() => TupleHelpers.ToString(fields);

static DateTimeOffsetTuple ISpaceFactory<DateTimeOffset, DateTimeOffsetTuple>.Create(DateTimeOffset[] fields) => new(fields);

public ReadOnlySpan<char> AsSpan() => this.AsSpan<DateTimeOffset, DateTimeOffsetTuple>(Constants.MaxFieldCharLength_DateTimeOffset);
public ReadOnlySpan<DateTimeOffset>.Enumerator GetEnumerator() => new ReadOnlySpan<DateTimeOffset>(fields).GetEnumerator();
public ReadOnlySpan<DateTimeOffset>.Enumerator GetEnumerator() =>
(fields is null ? ReadOnlySpan<DateTimeOffset>.Empty : new ReadOnlySpan<DateTimeOffset>(fields)).GetEnumerator();
}

/// <summary>
Expand Down Expand Up @@ -99,8 +100,9 @@ public DateTimeOffsetTemplate([AllowNull] params DateTimeOffset?[] fields) =>
public bool Matches(DateTimeOffsetTuple tuple) => this.Matches<DateTimeOffset, DateTimeOffsetTuple, DateTimeOffsetTemplate>(tuple);
public bool Equals(DateTimeOffsetTemplate other) => this.SequentialEquals<DateTimeOffset, DateTimeOffsetTemplate>(other);

public override int GetHashCode() => fields.GetHashCode();
public override int GetHashCode() => fields?.GetHashCode() ?? 0;
public override string ToString() => TemplateHelpers.ToString(fields);

public ReadOnlySpan<DateTimeOffset?>.Enumerator GetEnumerator() => new ReadOnlySpan<DateTimeOffset?>(fields).GetEnumerator();
public ReadOnlySpan<DateTimeOffset?>.Enumerator GetEnumerator() =>
(fields is null ? ReadOnlySpan<DateTimeOffset?>.Empty : new ReadOnlySpan<DateTimeOffset?>(fields)).GetEnumerator();
}
10 changes: 6 additions & 4 deletions src/OrleanSpaces/Tuples/Specialized/DateTimeTuple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,14 @@ public bool Equals(DateTimeTuple other)
result : this.SequentialEquals<DateTime, DateTimeTuple>(other);
}

public override int GetHashCode() => fields.GetHashCode();
public override int GetHashCode() => fields?.GetHashCode() ?? 0;
public override string ToString() => TupleHelpers.ToString(fields);

static DateTimeTuple ISpaceFactory<DateTime, DateTimeTuple>.Create(DateTime[] fields) => new(fields);

public ReadOnlySpan<char> AsSpan() => this.AsSpan<DateTime, DateTimeTuple>(Constants.MaxFieldCharLength_DateTime);
public ReadOnlySpan<DateTime>.Enumerator GetEnumerator() => new ReadOnlySpan<DateTime>(fields).GetEnumerator();
public ReadOnlySpan<DateTime>.Enumerator GetEnumerator() =>
(fields is null ? ReadOnlySpan<DateTime>.Empty : new ReadOnlySpan<DateTime>(fields)).GetEnumerator();
}

/// <summary>
Expand Down Expand Up @@ -99,8 +100,9 @@ public DateTimeTemplate([AllowNull] params DateTime?[] fields) =>
public bool Matches(DateTimeTuple tuple) => this.Matches<DateTime, DateTimeTuple, DateTimeTemplate>(tuple);
public bool Equals(DateTimeTemplate other) => this.SequentialEquals<DateTime, DateTimeTemplate>(other);

public override int GetHashCode() => fields.GetHashCode();
public override int GetHashCode() => fields?.GetHashCode() ?? 0;
public override string ToString() => TemplateHelpers.ToString(fields);

public ReadOnlySpan<DateTime?>.Enumerator GetEnumerator() => new ReadOnlySpan<DateTime?>(fields).GetEnumerator();
public ReadOnlySpan<DateTime?>.Enumerator GetEnumerator() =>
(fields is null ? ReadOnlySpan<DateTime?>.Empty : new ReadOnlySpan<DateTime?>(fields)).GetEnumerator();
}
10 changes: 6 additions & 4 deletions src/OrleanSpaces/Tuples/Specialized/DecimalTuple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,14 @@ public bool Equals(DecimalTuple other)
return new Comparer(this, other).AllocateAndExecute<int, Comparer>(8 * Length); // 8 x Length, because each decimal will be decomposed into 4 ints, and we have 2 tuples to compare.
}

public override int GetHashCode() => fields.GetHashCode();
public override int GetHashCode() => fields?.GetHashCode() ?? 0;
public override string ToString() => TupleHelpers.ToString(fields);

static DecimalTuple ISpaceFactory<decimal, DecimalTuple>.Create(decimal[] fields) => new(fields);

public ReadOnlySpan<char> AsSpan() => this.AsSpan<decimal, DecimalTuple>(Constants.MaxFieldCharLength_Decimal);
public ReadOnlySpan<decimal>.Enumerator GetEnumerator() => new ReadOnlySpan<decimal>(fields).GetEnumerator();
public ReadOnlySpan<decimal>.Enumerator GetEnumerator() =>
(fields is null ? ReadOnlySpan<decimal>.Empty : new ReadOnlySpan<decimal>(fields)).GetEnumerator();

readonly struct Comparer : IBufferConsumer<int>
{
Expand Down Expand Up @@ -151,8 +152,9 @@ public DecimalTemplate([AllowNull] params decimal?[] fields)
public bool Matches(DecimalTuple tuple) => this.Matches<decimal, DecimalTuple, DecimalTemplate>(tuple);
public bool Equals(DecimalTemplate other) => this.SequentialEquals<decimal, DecimalTemplate>(other);

public override int GetHashCode() => fields.GetHashCode();
public override int GetHashCode() => fields?.GetHashCode() ?? 0;
public override string ToString() => TemplateHelpers.ToString(fields);

public ReadOnlySpan<decimal?>.Enumerator GetEnumerator() => new ReadOnlySpan<decimal?>(fields).GetEnumerator();
public ReadOnlySpan<decimal?>.Enumerator GetEnumerator() =>
(fields is null ? ReadOnlySpan<decimal?>.Empty : new ReadOnlySpan<decimal?>(fields)).GetEnumerator();
}
10 changes: 6 additions & 4 deletions src/OrleanSpaces/Tuples/Specialized/DoubleTuple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ public bool Equals(DoubleTuple other)
=> this.TryParallelEquals<double, DoubleTuple>(other, out bool result) ?
result : this.SequentialEquals<double, DoubleTuple>(other);

public override int GetHashCode() => fields.GetHashCode();
public override int GetHashCode() => fields?.GetHashCode() ?? 0;
public override string ToString() => TupleHelpers.ToString(fields);

static DoubleTuple ISpaceFactory<double, DoubleTuple>.Create(double[] fields) => new(fields);

public ReadOnlySpan<char> AsSpan() => this.AsSpan<double, DoubleTuple>(Constants.MaxFieldCharLength_Double);
public ReadOnlySpan<double>.Enumerator GetEnumerator() => new ReadOnlySpan<double>(fields).GetEnumerator();
public ReadOnlySpan<double>.Enumerator GetEnumerator() =>
(fields is null ? ReadOnlySpan<double>.Empty : new ReadOnlySpan<double>(fields)).GetEnumerator();
}

/// <summary>
Expand Down Expand Up @@ -98,8 +99,9 @@ public DoubleTemplate([AllowNull] params double?[] fields)
public bool Matches(DoubleTuple tuple) => this.Matches<double, DoubleTuple, DoubleTemplate>(tuple);
public bool Equals(DoubleTemplate other) => this.SequentialEquals<double, DoubleTemplate>(other);

public override int GetHashCode() => fields.GetHashCode();
public override int GetHashCode() => fields?.GetHashCode() ?? 0;
public override string ToString() => TemplateHelpers.ToString(fields);

public ReadOnlySpan<double?>.Enumerator GetEnumerator() => new ReadOnlySpan<double?>(fields).GetEnumerator();
public ReadOnlySpan<double?>.Enumerator GetEnumerator() =>
(fields is null ? ReadOnlySpan<double?>.Empty : new ReadOnlySpan<double?>(fields)).GetEnumerator();
}
10 changes: 6 additions & 4 deletions src/OrleanSpaces/Tuples/Specialized/FloatTuple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ public bool Equals(FloatTuple other)
=> this.TryParallelEquals<float, FloatTuple>(other, out bool result) ?
result : this.SequentialEquals<float, FloatTuple>(other);

public override int GetHashCode() => fields.GetHashCode();
public override int GetHashCode() => fields?.GetHashCode() ?? 0;
public override string ToString() => TupleHelpers.ToString(fields);

static FloatTuple ISpaceFactory<float, FloatTuple>.Create(float[] fields) => new(fields);

public ReadOnlySpan<char> AsSpan() => this.AsSpan<float, FloatTuple>(Constants.MaxFieldCharLength_Float);
public ReadOnlySpan<float>.Enumerator GetEnumerator() => new ReadOnlySpan<float>(fields).GetEnumerator();
public ReadOnlySpan<float>.Enumerator GetEnumerator() =>
(fields is null ? ReadOnlySpan<float>.Empty : new ReadOnlySpan<float>(fields)).GetEnumerator();
}

/// <summary>
Expand Down Expand Up @@ -98,8 +99,9 @@ public FloatTemplate([AllowNull] params float?[] fields)
public bool Matches(FloatTuple tuple) => this.Matches<float, FloatTuple, FloatTemplate>(tuple);
public bool Equals(FloatTemplate other) => this.SequentialEquals<float, FloatTemplate>(other);

public override int GetHashCode() => fields.GetHashCode();
public override int GetHashCode() => fields?.GetHashCode() ?? 0;
public override string ToString() => TemplateHelpers.ToString(fields);

public ReadOnlySpan<float?>.Enumerator GetEnumerator() => new ReadOnlySpan<float?>(fields).GetEnumerator();
public ReadOnlySpan<float?>.Enumerator GetEnumerator() =>
(fields is null ? ReadOnlySpan<float?>.Empty : new ReadOnlySpan<float?>(fields)).GetEnumerator();
}
10 changes: 6 additions & 4 deletions src/OrleanSpaces/Tuples/Specialized/GuidTuple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,14 @@ public bool Equals(GuidTuple other)
return true;
}

public override int GetHashCode() => fields.GetHashCode();
public override int GetHashCode() => fields?.GetHashCode() ?? 0;
public override string ToString() => TupleHelpers.ToString(fields);

static GuidTuple ISpaceFactory<Guid, GuidTuple>.Create(Guid[] fields) => new(fields);

public ReadOnlySpan<char> AsSpan() => this.AsSpan<Guid, GuidTuple>(Constants.MaxFieldCharLength_Guid);
public ReadOnlySpan<Guid>.Enumerator GetEnumerator() => new ReadOnlySpan<Guid>(fields).GetEnumerator();
public ReadOnlySpan<Guid>.Enumerator GetEnumerator() =>
(fields is null ? ReadOnlySpan<Guid>.Empty : new ReadOnlySpan<Guid>(fields)).GetEnumerator();
}

/// <summary>
Expand Down Expand Up @@ -123,8 +124,9 @@ public GuidTemplate([AllowNull] params Guid?[] fields)
public bool Matches(GuidTuple tuple) => this.Matches<Guid, GuidTuple, GuidTemplate>(tuple);
public bool Equals(GuidTemplate other) => this.SequentialEquals<Guid, GuidTemplate>(other);

public override int GetHashCode() => fields.GetHashCode();
public override int GetHashCode() => fields?.GetHashCode() ?? 0;
public override string ToString() => TemplateHelpers.ToString(fields);

public ReadOnlySpan<Guid?>.Enumerator GetEnumerator() => new ReadOnlySpan<Guid?>(fields).GetEnumerator();
public ReadOnlySpan<Guid?>.Enumerator GetEnumerator() =>
(fields is null ? ReadOnlySpan<Guid?>.Empty : new ReadOnlySpan<Guid?>(fields)).GetEnumerator();
}
10 changes: 6 additions & 4 deletions src/OrleanSpaces/Tuples/Specialized/HugeTuple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,14 @@ public bool Equals(HugeTuple other)
return new Comparer(this, other).AllocateAndExecute<byte, Comparer>(2 * Constants.ByteCount_Int128 * Length);
}

public override int GetHashCode() => fields.GetHashCode();
public override int GetHashCode() => fields?.GetHashCode() ?? 0;
public override string ToString() => TupleHelpers.ToString(fields);

static HugeTuple ISpaceFactory<Int128, HugeTuple>.Create(Int128[] fields) => new(fields);

public ReadOnlySpan<char> AsSpan() => this.AsSpan<Int128, HugeTuple>(Constants.MaxFieldCharLength_Huge);
public ReadOnlySpan<Int128>.Enumerator GetEnumerator() => new ReadOnlySpan<Int128>(fields).GetEnumerator();
public ReadOnlySpan<Int128>.Enumerator GetEnumerator() =>
(fields is null ? ReadOnlySpan<Int128>.Empty : new ReadOnlySpan<Int128>(fields)).GetEnumerator();

readonly struct Comparer : IBufferConsumer<byte>
{
Expand Down Expand Up @@ -147,8 +148,9 @@ public HugeTemplate([AllowNull] params Int128?[] fields)
public bool Matches(HugeTuple tuple) => this.Matches<Int128, HugeTuple, HugeTemplate>(tuple);
public bool Equals(HugeTemplate other) => this.SequentialEquals<Int128, HugeTemplate>(other);

public override int GetHashCode() => fields.GetHashCode();
public override int GetHashCode() => fields?.GetHashCode() ?? 0;
public override string ToString() => TemplateHelpers.ToString(fields);

public ReadOnlySpan<Int128?>.Enumerator GetEnumerator() => new ReadOnlySpan<Int128?>(fields).GetEnumerator();
public ReadOnlySpan<Int128?>.Enumerator GetEnumerator() =>
(fields is null ? ReadOnlySpan<Int128?>.Empty : new ReadOnlySpan<Int128?>(fields)).GetEnumerator();
}
Loading

0 comments on commit e707fd8

Please sign in to comment.