Skip to content

Commit

Permalink
The cell types and Formula are now records
Browse files Browse the repository at this point in the history
  • Loading branch information
sveinungf committed Dec 2, 2023
1 parent 8f4a33a commit c276c45
Show file tree
Hide file tree
Showing 13 changed files with 5 additions and 100 deletions.
1 change: 1 addition & 0 deletions SpreadCheetah.Benchmark/Benchmarks/MixedDataTypeCells.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace SpreadCheetah.Benchmark.Benchmarks;

[SimpleJob(RuntimeMoniker.Net48)]
[SimpleJob(RuntimeMoniker.Net70)]
[SimpleJob(RuntimeMoniker.Net80)]
[MemoryDiagnoser]
Expand Down
22 changes: 1 addition & 21 deletions SpreadCheetah/Cell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <see cref="Spreadsheet.AddStyle(Style)"/>.
/// </summary>
public readonly struct Cell : IEquatable<Cell>
public readonly record struct Cell
{
internal DataCell DataCell { get; }

Expand Down Expand Up @@ -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)
{
}

/// <inheritdoc/>
public bool Equals(Cell other)
{
return DataCell.Equals(other.DataCell)
&& EqualityComparer<Formula?>.Default.Equals(Formula, other.Formula)
&& EqualityComparer<StyleId?>.Default.Equals(StyleId, other.StyleId);
}

/// <inheritdoc/>
public override bool Equals(object? obj) => obj is Cell other && Equals(other);

/// <inheritdoc/>
public override int GetHashCode() => HashCode.Combine(DataCell, Formula, StyleId);

/// <summary>Determines whether two instances have the same value.</summary>
public static bool operator ==(in Cell left, in Cell right) => left.Equals(right);

/// <summary>Determines whether two instances have different values.</summary>
public static bool operator !=(in Cell left, in Cell right) => !left.Equals(right);
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,4 @@ public override bool WriteFormulaStartElement(StyleId? styleId, DefaultStyling?
/// </summary>
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;
}
2 changes: 0 additions & 2 deletions SpreadCheetah/CellValueWriters/CellValueWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte> bytes, ref int bytesWritten)
{
Expand Down
3 changes: 0 additions & 3 deletions SpreadCheetah/CellValueWriters/NullValueWriterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,4 @@ protected static bool WriteFormulaStartElement(int? styleId, CellWriterState sta
/// </summary>
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,4 @@ protected override bool TryWriteValue(in DataCell cell, Span<byte> 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();
}
3 changes: 0 additions & 3 deletions SpreadCheetah/CellValueWriters/Number/FloatCellValueWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,4 @@ protected override bool TryWriteValue(in DataCell cell, Span<byte> 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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,4 @@ protected override bool TryWriteValue(in DataCell cell, Span<byte> 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();
}
3 changes: 0 additions & 3 deletions SpreadCheetah/CellValueWriters/StringCellValueWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ protected override bool TryWriteValue(in DataCell cell, Span<byte> 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;
Expand Down
19 changes: 1 addition & 18 deletions SpreadCheetah/DataCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace SpreadCheetah;
/// <summary>
/// Represents the value and data type for a worksheet cell.
/// </summary>
public readonly struct DataCell : IEquatable<DataCell>
public readonly record struct DataCell
{
private readonly CellValueWriter? _writer;

Expand Down Expand Up @@ -158,21 +158,4 @@ public DataCell(bool? value) : this()
}

private static CellValueWriter GetBooleanWriter(bool value) => value ? CellValueWriter.TrueBoolean : CellValueWriter.FalseBoolean;

/// <inheritdoc/>
public bool Equals(DataCell other) => Writer == other.Writer
&& string.Equals(StringValue, other.StringValue, StringComparison.Ordinal)
&& Writer.Equals(NumberValue, other.NumberValue);

/// <inheritdoc/>
public override bool Equals(object? obj) => obj is DataCell cell && Equals(cell);

/// <inheritdoc/>
public override int GetHashCode() => HashCode.Combine(StringValue, Writer, Writer.GetHashCodeFor(NumberValue));

/// <summary>Determines whether two instances have the same value.</summary>
public static bool operator ==(in DataCell left, in DataCell right) => left.Equals(right);

/// <summary>Determines whether two instances have different values.</summary>
public static bool operator !=(in DataCell left, in DataCell right) => !left.Equals(right);
}
20 changes: 1 addition & 19 deletions SpreadCheetah/Formula.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace SpreadCheetah;
/// <summary>
/// Represents a formula for a worksheet cell.
/// </summary>
public readonly struct Formula : IEquatable<Formula>
public readonly record struct Formula
{
internal string FormulaText { get; }

Expand All @@ -17,22 +17,4 @@ public Formula(string? formulaText)
{
FormulaText = formulaText != null ? WebUtility.HtmlEncode(formulaText) : string.Empty;
}

/// <inheritdoc/>
public bool Equals(Formula other)
{
return string.Equals(FormulaText, other.FormulaText, StringComparison.Ordinal);
}

/// <inheritdoc/>
public override bool Equals(object? obj) => obj is Formula formula && Equals(formula);

/// <inheritdoc/>
public override int GetHashCode() => HashCode.Combine(FormulaText);

/// <summary>Determines whether two instances have the same value.</summary>
public static bool operator ==(Formula left, Formula right) => left.Equals(right);

/// <summary>Determines whether two instances have different values.</summary>
public static bool operator !=(Formula left, Formula right) => !left.Equals(right);
}
20 changes: 1 addition & 19 deletions SpreadCheetah/StyledCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace SpreadCheetah;
/// Represents the value and an optional style for a worksheet cell.
/// Style IDs are created with <see cref="Spreadsheet.AddStyle(Style)"/>.
/// </summary>
public readonly struct StyledCell : IEquatable<StyledCell>
public readonly record struct StyledCell
{
internal DataCell DataCell { get; }

Expand Down Expand Up @@ -164,22 +164,4 @@ public StyledCell(bool? value, StyleId? styleId)
DataCell = new DataCell(value);
StyleId = styleId;
}

/// <inheritdoc/>
public bool Equals(StyledCell other)
{
return DataCell.Equals(other.DataCell) && EqualityComparer<StyleId?>.Default.Equals(StyleId, other.StyleId);
}

/// <inheritdoc/>
public override bool Equals(object? obj) => obj is StyledCell other && Equals(other);

/// <inheritdoc/>
public override int GetHashCode() => HashCode.Combine(DataCell, StyleId);

/// <summary>Determines whether two instances have the same value.</summary>
public static bool operator ==(in StyledCell left, in StyledCell right) => left.Equals(right);

/// <summary>Determines whether two instances have different values.</summary>
public static bool operator !=(in StyledCell left, in StyledCell right) => !left.Equals(right);
}

0 comments on commit c276c45

Please sign in to comment.