Skip to content
This repository has been archived by the owner on Sep 6, 2023. It is now read-only.

Fix and cleanup GetHashCode and Equals #797

Merged
merged 7 commits into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.ValueTuple.4.5.0\lib\netstandard1.0\System.ValueTuple.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand All @@ -62,6 +65,7 @@
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resources\Unmanaged\x86\CAIR.exe" />
<None Include="packages.config" />
<None Include="Resources\Unmanaged\x86\GPL.txt" />
<EmbeddedResource Include="Resources\Unmanaged\x86\pthreadVSE2.dll" />
<None Include="Resources\Unmanaged\x86\ReadMe.txt" />
Expand Down
34 changes: 7 additions & 27 deletions src/ImageProcessor.Plugins.Cair/Imaging/ContentAwareResizeLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,15 @@ public OutputType OutputType
public int Timeout { get; set; } = 60000;

/// <summary>
/// Returns a value that indicates whether the specified object is an
/// <see cref="ContentAwareResizeLayer"/> object that is equivalent to
/// this <see cref="ContentAwareResizeLayer"/> object.
/// Determines whether the specified <see cref="System.Object" />, is equal to this instance.
/// </summary>
/// <param name="obj">
/// The object to test.
/// </param>
/// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
/// <returns>
/// True if the given object is an <see cref="ContentAwareResizeLayer"/> object that is equivalent to
/// this <see cref="ContentAwareResizeLayer"/> object; otherwise, false.
/// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
public override bool Equals(object obj)
ronaldbarendse marked this conversation as resolved.
Show resolved Hide resolved
{
ContentAwareResizeLayer resizeLayer = obj as ContentAwareResizeLayer;

if (resizeLayer == null)
if (!(obj is ContentAwareResizeLayer resizeLayer))
ronaldbarendse marked this conversation as resolved.
Show resolved Hide resolved
{
return false;
}
Expand All @@ -118,24 +111,11 @@ public override bool Equals(object obj)
}

/// <summary>
/// Returns the hash code for this instance.
/// Returns a hash code for this instance.
/// </summary>
/// <returns>
/// A 32-bit signed integer that is the hash code for this instance.
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns>
public override int GetHashCode()
{
unchecked
{
int hashCode = (int)this.ConvolutionType;
hashCode = (hashCode * 397) ^ (int)this.EnergyFunction;
hashCode = (hashCode * 397) ^ this.Parallelize.GetHashCode();
hashCode = (hashCode * 397) ^ (int)this.OutputType;
hashCode = (hashCode * 397) ^ this.Timeout;
hashCode = (hashCode * 397) ^ this.Size.GetHashCode();
hashCode = (hashCode * 397) ^ (this.WeightPath != null ? this.WeightPath.GetHashCode() : 0);
return hashCode;
}
}
public override int GetHashCode() => (this.ConvolutionType, this.EnergyFunction, this.Parallelize, this.OutputType, this.Timeout, this.Size, this.WeightPath).GetHashCode();
}
}
4 changes: 4 additions & 0 deletions src/ImageProcessor.Plugins.Cair/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="System.ValueTuple" version="4.5.0" targetFramework="net452" />
</packages>
6 changes: 6 additions & 0 deletions src/ImageProcessor/ImageProcessor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.ValueTuple.4.5.0\lib\netstandard1.0\System.ValueTuple.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand Down Expand Up @@ -202,5 +205,8 @@
<Compile Include="Processors\Watermark.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
37 changes: 14 additions & 23 deletions src/ImageProcessor/Imaging/Colors/CmykColor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,54 +247,45 @@ public override string ToString()
}

/// <summary>
/// Indicates whether this instance and a specified object are equal.
/// Determines whether the specified <see cref="System.Object" />, is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
/// <returns>
/// true if <paramref name="obj"/> and this instance are the same type and represent the same value; otherwise, false.
/// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
/// <param name="obj">Another object to compare to. </param>
public override bool Equals(object obj) => obj is CmykColor cmykColor && this.Equals(cmykColor);

/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.</returns>
public bool Equals(CmykColor other)
{
Color thisColor = this;
Color otherColor = other;
return thisColor.Equals(otherColor);
}
/// <returns>
/// true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.
/// </returns>
public bool Equals(CmykColor other) => ((Color)this).Equals((Color)other);

/// <summary>
/// Returns the hash code for this instance.
/// Returns a hash code for this instance.
/// </summary>
/// <returns>
/// A 32-bit signed integer that is the hash code for this instance.
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns>
public override int GetHashCode()
{
Color thisColor = this;
return thisColor.GetHashCode();
}
public override int GetHashCode() => ((Color)this).GetHashCode();

/// <summary>
/// Checks the range of the given value to ensure that it remains within the acceptable boundaries.
/// </summary>
/// <param name="value">
/// The value to check.
/// </param>
/// <param name="value">The value to check.</param>
/// <returns>
/// The sanitized <see cref="float"/>.
/// The sanitized <see cref="float" />.
/// </returns>
private static float Clamp(float value) => ImageMaths.Clamp(value, 0, 100);

/// <summary>
/// Returns a value indicating whether the current instance is empty.
/// Determines whether this instance is empty.
/// </summary>
/// <returns>
/// The true if this instance is empty; otherwise, false.
/// <c>true</c> if this instance is empty; otherwise, <c>false</c>.
/// </returns>
private bool IsEmpty()
{
Expand Down
34 changes: 8 additions & 26 deletions src/ImageProcessor/Imaging/Colors/Color32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,47 +86,29 @@ public Color32(int argb)
public Color Color => Color.FromArgb(this.A, this.R, this.G, this.B);

/// <summary>
/// Indicates whether this instance and a specified object are equal.
/// Determines whether the specified <see cref="System.Object" />, is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
/// <returns>
/// true if <paramref name="obj"/> and this instance are the same type and represent the same value; otherwise, false.
/// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
/// <param name="obj">Another object to compare to. </param>
public override bool Equals(object obj) => obj is Color32 color32 && this.Equals(color32);

/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.</returns>
public bool Equals(Color32 other) => this.Argb.Equals(other.Argb);

/// <summary>
/// Returns the hash code for this instance.
/// </summary>
/// <returns>
/// A 32-bit signed integer that is the hash code for this instance.
/// true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.
/// </returns>
public override int GetHashCode() => this.GetHashCode(this);
public bool Equals(Color32 other) => this.Argb.Equals(other.Argb);

/// <summary>
/// Returns the hash code for the given instance.
/// Returns a hash code for this instance.
/// </summary>
/// <param name="obj">
/// The instance of <see cref="Color32"/> to return the hash code for.
/// </param>
/// <returns>
/// A 32-bit signed integer that is the hash code for this instance.
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns>
private int GetHashCode(Color32 obj)
{
unchecked
{
int hashCode = obj.B.GetHashCode();
hashCode = (hashCode * 397) ^ obj.G.GetHashCode();
hashCode = (hashCode * 397) ^ obj.R.GetHashCode();
return (hashCode * 397) ^ obj.A.GetHashCode();
}
}
public override int GetHashCode() => this.Argb.GetHashCode();
ronaldbarendse marked this conversation as resolved.
Show resolved Hide resolved
}
}
27 changes: 10 additions & 17 deletions src/ImageProcessor/Imaging/Colors/HSLAColor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,37 +242,30 @@ public override string ToString()
}

/// <summary>
/// Indicates whether this instance and a specified object are equal.
/// Determines whether the specified <see cref="System.Object" />, is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
/// <returns>
/// true if <paramref name="obj"/> and this instance are the same type and represent the same value; otherwise, false.
/// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
/// <param name="obj">Another object to compare to. </param>
public override bool Equals(object obj) => obj is HslaColor hslaColor && this.Equals(hslaColor);

/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.</returns>
public bool Equals(HslaColor other)
{
Color thisColor = this;
Color otherColor = other;
return thisColor.Equals(otherColor);
}
/// <returns>
/// true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.
/// </returns>
public bool Equals(HslaColor other) => ((Color)this).Equals((Color)other);

/// <summary>
/// Returns the hash code for this instance.
/// Returns a hash code for this instance.
/// </summary>
/// <returns>
/// A 32-bit signed integer that is the hash code for this instance.
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns>
public override int GetHashCode()
{
Color thisColor = this;
return thisColor.GetHashCode();
}
public override int GetHashCode() => ((Color)this).GetHashCode();
ronaldbarendse marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Gets the color component from the given hue values.
Expand Down
27 changes: 10 additions & 17 deletions src/ImageProcessor/Imaging/Colors/RGBAColor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,36 +190,29 @@ public override string ToString()
}

/// <summary>
/// Indicates whether this instance and a specified object are equal.
/// Determines whether the specified <see cref="System.Object" />, is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
/// <returns>
/// true if <paramref name="obj"/> and this instance are the same type and represent the same value; otherwise, false.
/// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
/// <param name="obj">Another object to compare to. </param>
public override bool Equals(object obj) => obj is RgbaColor rgbaColor && this.Equals(rgbaColor);

/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.</returns>
public bool Equals(RgbaColor other)
{
Color thisColor = this;
Color otherColor = other;
return thisColor.Equals(otherColor);
}
/// <returns>
/// true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.
/// </returns>
public bool Equals(RgbaColor other) => ((Color)this).Equals((Color)other);

/// <summary>
/// Returns the hash code for this instance.
/// Returns a hash code for this instance.
/// </summary>
/// <returns>
/// A 32-bit signed integer that is the hash code for this instance.
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns>
public override int GetHashCode()
{
Color thisColor = this;
return thisColor.GetHashCode();
}
public override int GetHashCode() => ((Color)this).GetHashCode();
}
}
27 changes: 10 additions & 17 deletions src/ImageProcessor/Imaging/Colors/YCbCrColor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,37 +168,30 @@ public override string ToString()
}

/// <summary>
/// Indicates whether this instance and a specified object are equal.
/// Determines whether the specified <see cref="System.Object" />, is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
/// <returns>
/// true if <paramref name="obj"/> and this instance are the same type and represent the same value; otherwise, false.
/// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
/// <param name="obj">Another object to compare to. </param>
public override bool Equals(object obj) => obj is YCbCrColor yCbCrColor && this.Equals(yCbCrColor);

/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.</returns>
public bool Equals(YCbCrColor other)
{
Color thisColor = this;
Color otherColor = other;
return thisColor.Equals(otherColor);
}
/// <returns>
/// true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.
/// </returns>
public bool Equals(YCbCrColor other) => ((Color)this).Equals((Color)other);

/// <summary>
/// Returns the hash code for this instance.
/// Returns a hash code for this instance.
/// </summary>
/// <returns>
/// A 32-bit signed integer that is the hash code for this instance.
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns>
public override int GetHashCode()
{
Color thisColor = this;
return thisColor.GetHashCode();
}
public override int GetHashCode() => ((Color)this).GetHashCode();
ronaldbarendse marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Returns a value indicating whether the current instance is empty.
Expand Down
Loading