From c276c45801b788840924551732e030e80cc4fc36 Mon Sep 17 00:00:00 2001 From: sveinungf Date: Sat, 2 Dec 2023 18:35:42 +0100 Subject: [PATCH] The cell types and Formula are now records --- .../Benchmarks/MixedDataTypeCells.cs | 1 + SpreadCheetah/Cell.cs | 22 +------------------ .../Boolean/BooleanCellValueWriter.cs | 3 --- .../CellValueWriters/CellValueWriter.cs | 2 -- .../CellValueWriters/NullValueWriterBase.cs | 3 --- .../Number/DoubleCellValueWriter.cs | 3 --- .../Number/FloatCellValueWriter.cs | 3 --- .../Number/IntegerCellValueWriter.cs | 3 --- .../CellValueWriters/StringCellValueWriter.cs | 3 --- .../Time/DateTimeCellValueWriter.cs | 3 --- SpreadCheetah/DataCell.cs | 19 +--------------- SpreadCheetah/Formula.cs | 20 +---------------- SpreadCheetah/StyledCell.cs | 20 +---------------- 13 files changed, 5 insertions(+), 100 deletions(-) diff --git a/SpreadCheetah.Benchmark/Benchmarks/MixedDataTypeCells.cs b/SpreadCheetah.Benchmark/Benchmarks/MixedDataTypeCells.cs index 647663c0..58faf38c 100644 --- a/SpreadCheetah.Benchmark/Benchmarks/MixedDataTypeCells.cs +++ b/SpreadCheetah.Benchmark/Benchmarks/MixedDataTypeCells.cs @@ -4,6 +4,7 @@ namespace SpreadCheetah.Benchmark.Benchmarks; +[SimpleJob(RuntimeMoniker.Net48)] [SimpleJob(RuntimeMoniker.Net70)] [SimpleJob(RuntimeMoniker.Net80)] [MemoryDiagnoser] diff --git a/SpreadCheetah/Cell.cs b/SpreadCheetah/Cell.cs index 0667e523..76842f40 100644 --- a/SpreadCheetah/Cell.cs +++ b/SpreadCheetah/Cell.cs @@ -7,7 +7,7 @@ namespace SpreadCheetah; /// The content can either be a value, or a formula with an optional cached value. /// Style IDs are created with . /// -public readonly struct Cell : IEquatable +public readonly record struct Cell { internal DataCell DataCell { get; } @@ -251,24 +251,4 @@ public Cell(Formula formula, bool cachedValue, StyleId? styleId = null) : this(n public Cell(Formula formula, bool? cachedValue, StyleId? styleId = null) : this(new DataCell(cachedValue), formula, styleId) { } - - /// - public bool Equals(Cell other) - { - return DataCell.Equals(other.DataCell) - && EqualityComparer.Default.Equals(Formula, other.Formula) - && EqualityComparer.Default.Equals(StyleId, other.StyleId); - } - - /// - public override bool Equals(object? obj) => obj is Cell other && Equals(other); - - /// - public override int GetHashCode() => HashCode.Combine(DataCell, Formula, StyleId); - - /// Determines whether two instances have the same value. - public static bool operator ==(in Cell left, in Cell right) => left.Equals(right); - - /// Determines whether two instances have different values. - public static bool operator !=(in Cell left, in Cell right) => !left.Equals(right); } diff --git a/SpreadCheetah/CellValueWriters/Boolean/BooleanCellValueWriter.cs b/SpreadCheetah/CellValueWriters/Boolean/BooleanCellValueWriter.cs index 7a6557b4..8f1ffa6c 100644 --- a/SpreadCheetah/CellValueWriters/Boolean/BooleanCellValueWriter.cs +++ b/SpreadCheetah/CellValueWriters/Boolean/BooleanCellValueWriter.cs @@ -160,7 +160,4 @@ public override bool WriteFormulaStartElement(StyleId? styleId, DefaultStyling? /// public override bool CanWriteValuePieceByPiece(in DataCell cell) => false; public override bool WriteValuePieceByPiece(in DataCell cell, SpreadsheetBuffer buffer, ref int valueIndex) => true; - - public override bool Equals(in CellValue value, in CellValue other) => true; - public override int GetHashCodeFor(in CellValue value) => 0; } diff --git a/SpreadCheetah/CellValueWriters/CellValueWriter.cs b/SpreadCheetah/CellValueWriters/CellValueWriter.cs index 4fa9e679..a95bd212 100644 --- a/SpreadCheetah/CellValueWriters/CellValueWriter.cs +++ b/SpreadCheetah/CellValueWriters/CellValueWriter.cs @@ -30,8 +30,6 @@ internal abstract class CellValueWriter public abstract bool WriteValuePieceByPiece(in DataCell cell, SpreadsheetBuffer buffer, ref int valueIndex); public abstract bool TryWriteEndElement(SpreadsheetBuffer buffer); public abstract bool TryWriteEndElement(in Cell cell, SpreadsheetBuffer buffer); - public abstract bool Equals(in CellValue value, in CellValue other); - public abstract int GetHashCodeFor(in CellValue value); protected static bool TryWriteCellStartWithReference(CellWriterState state, Span bytes, ref int bytesWritten) { diff --git a/SpreadCheetah/CellValueWriters/NullValueWriterBase.cs b/SpreadCheetah/CellValueWriters/NullValueWriterBase.cs index 9063a7d3..e29be0c3 100644 --- a/SpreadCheetah/CellValueWriters/NullValueWriterBase.cs +++ b/SpreadCheetah/CellValueWriters/NullValueWriterBase.cs @@ -139,7 +139,4 @@ protected static bool WriteFormulaStartElement(int? styleId, CellWriterState sta /// public override bool CanWriteValuePieceByPiece(in DataCell cell) => false; public override bool WriteValuePieceByPiece(in DataCell cell, SpreadsheetBuffer buffer, ref int valueIndex) => true; - - public override bool Equals(in CellValue value, in CellValue other) => true; - public override int GetHashCodeFor(in CellValue value) => 0; } diff --git a/SpreadCheetah/CellValueWriters/Number/DoubleCellValueWriter.cs b/SpreadCheetah/CellValueWriters/Number/DoubleCellValueWriter.cs index d71b46a8..c587afa6 100644 --- a/SpreadCheetah/CellValueWriters/Number/DoubleCellValueWriter.cs +++ b/SpreadCheetah/CellValueWriters/Number/DoubleCellValueWriter.cs @@ -8,7 +8,4 @@ protected override bool TryWriteValue(in DataCell cell, Span destination, { return Utf8Formatter.TryFormat(cell.NumberValue.DoubleValue, destination, out bytesWritten); } - - public override bool Equals(in CellValue value, in CellValue other) => value.DoubleValue == other.DoubleValue; - public override int GetHashCodeFor(in CellValue value) => value.DoubleValue.GetHashCode(); } diff --git a/SpreadCheetah/CellValueWriters/Number/FloatCellValueWriter.cs b/SpreadCheetah/CellValueWriters/Number/FloatCellValueWriter.cs index 88eb5cb5..721de7e6 100644 --- a/SpreadCheetah/CellValueWriters/Number/FloatCellValueWriter.cs +++ b/SpreadCheetah/CellValueWriters/Number/FloatCellValueWriter.cs @@ -8,7 +8,4 @@ protected override bool TryWriteValue(in DataCell cell, Span destination, { return Utf8Formatter.TryFormat(cell.NumberValue.FloatValue, destination, out bytesWritten); } - - public override bool Equals(in CellValue value, in CellValue other) => value.FloatValue == other.FloatValue; - public override int GetHashCodeFor(in CellValue value) => value.FloatValue.GetHashCode(); } diff --git a/SpreadCheetah/CellValueWriters/Number/IntegerCellValueWriter.cs b/SpreadCheetah/CellValueWriters/Number/IntegerCellValueWriter.cs index a1c0fb0e..097161ac 100644 --- a/SpreadCheetah/CellValueWriters/Number/IntegerCellValueWriter.cs +++ b/SpreadCheetah/CellValueWriters/Number/IntegerCellValueWriter.cs @@ -8,7 +8,4 @@ protected override bool TryWriteValue(in DataCell cell, Span destination, { return Utf8Formatter.TryFormat(cell.NumberValue.IntValue, destination, out bytesWritten); } - - public override bool Equals(in CellValue value, in CellValue other) => value.IntValue == other.IntValue; - public override int GetHashCodeFor(in CellValue value) => value.IntValue.GetHashCode(); } diff --git a/SpreadCheetah/CellValueWriters/StringCellValueWriter.cs b/SpreadCheetah/CellValueWriters/StringCellValueWriter.cs index 23cfa294..606603a2 100644 --- a/SpreadCheetah/CellValueWriters/StringCellValueWriter.cs +++ b/SpreadCheetah/CellValueWriters/StringCellValueWriter.cs @@ -222,7 +222,4 @@ public override bool WriteValuePieceByPiece(in DataCell cell, SpreadsheetBuffer { return buffer.WriteLongString(cell.StringValue, ref valueIndex); } - - public override bool Equals(in CellValue value, in CellValue other) => true; - public override int GetHashCodeFor(in CellValue value) => 0; } diff --git a/SpreadCheetah/CellValueWriters/Time/DateTimeCellValueWriter.cs b/SpreadCheetah/CellValueWriters/Time/DateTimeCellValueWriter.cs index 3f8a625c..f5614fbd 100644 --- a/SpreadCheetah/CellValueWriters/Time/DateTimeCellValueWriter.cs +++ b/SpreadCheetah/CellValueWriters/Time/DateTimeCellValueWriter.cs @@ -15,9 +15,6 @@ protected override bool TryWriteValue(in DataCell cell, Span destination, return Utf8Formatter.TryFormat(cell.NumberValue.DoubleValue, destination, out bytesWritten); } - public override bool Equals(in CellValue value, in CellValue other) => value.DoubleValue == other.DoubleValue; - public override int GetHashCodeFor(in CellValue value) => value.DoubleValue.GetHashCode(); - public override bool TryWriteCell(in DataCell cell, DefaultStyling? defaultStyling, CellWriterState state) { var defaultStyleId = defaultStyling?.DateTimeStyleId; diff --git a/SpreadCheetah/DataCell.cs b/SpreadCheetah/DataCell.cs index 4bbad133..d0bbcbfd 100644 --- a/SpreadCheetah/DataCell.cs +++ b/SpreadCheetah/DataCell.cs @@ -6,7 +6,7 @@ namespace SpreadCheetah; /// /// Represents the value and data type for a worksheet cell. /// -public readonly struct DataCell : IEquatable +public readonly record struct DataCell { private readonly CellValueWriter? _writer; @@ -158,21 +158,4 @@ public DataCell(bool? value) : this() } private static CellValueWriter GetBooleanWriter(bool value) => value ? CellValueWriter.TrueBoolean : CellValueWriter.FalseBoolean; - - /// - public bool Equals(DataCell other) => Writer == other.Writer - && string.Equals(StringValue, other.StringValue, StringComparison.Ordinal) - && Writer.Equals(NumberValue, other.NumberValue); - - /// - public override bool Equals(object? obj) => obj is DataCell cell && Equals(cell); - - /// - public override int GetHashCode() => HashCode.Combine(StringValue, Writer, Writer.GetHashCodeFor(NumberValue)); - - /// Determines whether two instances have the same value. - public static bool operator ==(in DataCell left, in DataCell right) => left.Equals(right); - - /// Determines whether two instances have different values. - public static bool operator !=(in DataCell left, in DataCell right) => !left.Equals(right); } diff --git a/SpreadCheetah/Formula.cs b/SpreadCheetah/Formula.cs index 0a366ef9..ffcaffa8 100644 --- a/SpreadCheetah/Formula.cs +++ b/SpreadCheetah/Formula.cs @@ -5,7 +5,7 @@ namespace SpreadCheetah; /// /// Represents a formula for a worksheet cell. /// -public readonly struct Formula : IEquatable +public readonly record struct Formula { internal string FormulaText { get; } @@ -17,22 +17,4 @@ public Formula(string? formulaText) { FormulaText = formulaText != null ? WebUtility.HtmlEncode(formulaText) : string.Empty; } - - /// - public bool Equals(Formula other) - { - return string.Equals(FormulaText, other.FormulaText, StringComparison.Ordinal); - } - - /// - public override bool Equals(object? obj) => obj is Formula formula && Equals(formula); - - /// - public override int GetHashCode() => HashCode.Combine(FormulaText); - - /// Determines whether two instances have the same value. - public static bool operator ==(Formula left, Formula right) => left.Equals(right); - - /// Determines whether two instances have different values. - public static bool operator !=(Formula left, Formula right) => !left.Equals(right); } diff --git a/SpreadCheetah/StyledCell.cs b/SpreadCheetah/StyledCell.cs index 0ba12045..9a1eb503 100644 --- a/SpreadCheetah/StyledCell.cs +++ b/SpreadCheetah/StyledCell.cs @@ -6,7 +6,7 @@ namespace SpreadCheetah; /// Represents the value and an optional style for a worksheet cell. /// Style IDs are created with . /// -public readonly struct StyledCell : IEquatable +public readonly record struct StyledCell { internal DataCell DataCell { get; } @@ -164,22 +164,4 @@ public StyledCell(bool? value, StyleId? styleId) DataCell = new DataCell(value); StyleId = styleId; } - - /// - public bool Equals(StyledCell other) - { - return DataCell.Equals(other.DataCell) && EqualityComparer.Default.Equals(StyleId, other.StyleId); - } - - /// - public override bool Equals(object? obj) => obj is StyledCell other && Equals(other); - - /// - public override int GetHashCode() => HashCode.Combine(DataCell, StyleId); - - /// Determines whether two instances have the same value. - public static bool operator ==(in StyledCell left, in StyledCell right) => left.Equals(right); - - /// Determines whether two instances have different values. - public static bool operator !=(in StyledCell left, in StyledCell right) => !left.Equals(right); }