Skip to content

Commit

Permalink
Merge pull request #680 from carbon/hashcodes
Browse files Browse the repository at this point in the history
Optimize Equals & GetHashCode methods within PixelFormats
  • Loading branch information
JimBobSquarePants authored Aug 21, 2018
2 parents 485395d + 760dfcc commit f11263f
Show file tree
Hide file tree
Showing 16 changed files with 17 additions and 72 deletions.
5 changes: 1 addition & 4 deletions src/ImageSharp/PixelFormats/Alpha8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,7 @@ public override string ToString()

/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode()
{
return this.PackedValue.GetHashCode();
}
public override int GetHashCode() => this.PackedValue.GetHashCode();

/// <summary>
/// Packs a <see cref="float"/> into a byte.
Expand Down
7 changes: 1 addition & 6 deletions src/ImageSharp/PixelFormats/Argb32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,12 +362,7 @@ public override string ToString()

/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode()
{
int hash = HashHelpers.Combine(this.R.GetHashCode(), this.G.GetHashCode());
hash = HashHelpers.Combine(hash, this.B.GetHashCode());
return HashHelpers.Combine(hash, this.A.GetHashCode());
}
public override int GetHashCode() => this.Argb.GetHashCode();

/// <summary>
/// Gets the <see cref="Vector4"/> representation without normalizing to [0, 1]
Expand Down
5 changes: 1 addition & 4 deletions src/ImageSharp/PixelFormats/Bgr565.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,7 @@ public override string ToString()

/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode()
{
return this.PackedValue.GetHashCode();
}
public override int GetHashCode() => this.PackedValue.GetHashCode();

/// <summary>
/// Packs the <see cref="float"/> components into a <see cref="ushort"/>.
Expand Down
10 changes: 2 additions & 8 deletions src/ImageSharp/PixelFormats/Bgra32.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.

using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -105,19 +104,14 @@ public uint PackedValue
/// <inheritdoc/>
public bool Equals(Bgra32 other)
{
return this.R == other.R && this.G == other.G && this.B == other.B && this.A == other.A;
return this.Bgra == other.Bgra;
}

/// <inheritdoc/>
public override bool Equals(object obj) => obj is Bgra32 other && this.Equals(other);

/// <inheritdoc/>
public override int GetHashCode()
{
int hash = HashHelpers.Combine(this.R.GetHashCode(), this.G.GetHashCode());
hash = HashHelpers.Combine(hash, this.B.GetHashCode());
return HashHelpers.Combine(hash, this.A.GetHashCode());
}
public override int GetHashCode() => this.Bgra.GetHashCode();

/// <summary>
/// Gets the <see cref="Vector4"/> representation without normalizing to [0, 1]
Expand Down
5 changes: 1 addition & 4 deletions src/ImageSharp/PixelFormats/Bgra4444.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,7 @@ public override string ToString()

/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode()
{
return this.PackedValue.GetHashCode();
}
public override int GetHashCode() => this.PackedValue.GetHashCode();

/// <summary>
/// Packs the <see cref="float"/> components into a <see cref="ushort"/>.
Expand Down
5 changes: 1 addition & 4 deletions src/ImageSharp/PixelFormats/Bgra5551.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,7 @@ public override string ToString()
/// </summary>
/// <returns>The hash code for the packed vector.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode()
{
return this.PackedValue.GetHashCode();
}
public override int GetHashCode() => this.PackedValue.GetHashCode();

/// <summary>
/// Packs the <see cref="float"/> components into a <see cref="ushort"/>.
Expand Down
5 changes: 1 addition & 4 deletions src/ImageSharp/PixelFormats/Byte4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,7 @@ public bool Equals(Byte4 other)

/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode()
{
return this.PackedValue.GetHashCode();
}
public override int GetHashCode() => this.PackedValue.GetHashCode();

/// <summary>
/// Returns a string representation of the current instance.
Expand Down
5 changes: 1 addition & 4 deletions src/ImageSharp/PixelFormats/HalfSingle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,7 @@ public override string ToString()

/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode()
{
return this.PackedValue.GetHashCode();
}
public override int GetHashCode() => this.PackedValue.GetHashCode();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Vector4 ToByteScaledVector4()
Expand Down
5 changes: 1 addition & 4 deletions src/ImageSharp/PixelFormats/HalfVector2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,7 @@ public override string ToString()

/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode()
{
return this.PackedValue.GetHashCode();
}
public override int GetHashCode() => this.PackedValue.GetHashCode();

/// <inheritdoc />
public override bool Equals(object obj)
Expand Down
5 changes: 1 addition & 4 deletions src/ImageSharp/PixelFormats/HalfVector4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,7 @@ public override string ToString()

/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode()
{
return this.PackedValue.GetHashCode();
}
public override int GetHashCode() => this.PackedValue.GetHashCode();

/// <inheritdoc />
public override bool Equals(object obj)
Expand Down
5 changes: 1 addition & 4 deletions src/ImageSharp/PixelFormats/NormalizedByte2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,7 @@ public bool Equals(NormalizedByte2 other)

/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode()
{
return this.PackedValue.GetHashCode();
}
public override int GetHashCode() => this.PackedValue.GetHashCode();

/// <inheritdoc />
public override string ToString()
Expand Down
5 changes: 1 addition & 4 deletions src/ImageSharp/PixelFormats/NormalizedByte4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,7 @@ public bool Equals(NormalizedByte4 other)

/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode()
{
return this.PackedValue.GetHashCode();
}
public override int GetHashCode() => this.PackedValue.GetHashCode();

/// <inheritdoc />
public override string ToString()
Expand Down
7 changes: 1 addition & 6 deletions src/ImageSharp/PixelFormats/Rgba32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -437,12 +437,7 @@ public override string ToString()

/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode()
{
int hash = HashHelpers.Combine(this.R.GetHashCode(), this.G.GetHashCode());
hash = HashHelpers.Combine(hash, this.B.GetHashCode());
return HashHelpers.Combine(hash, this.A.GetHashCode());
}
public override int GetHashCode() => this.Rgba.GetHashCode();

/// <summary>
/// Gets the <see cref="Vector4"/> representation without normalizing to [0, 1]
Expand Down
5 changes: 1 addition & 4 deletions src/ImageSharp/PixelFormats/Rgba64.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,6 @@ public override string ToString()

/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode()
{
return this.PackedValue.GetHashCode();
}
public override int GetHashCode() => this.PackedValue.GetHashCode();
}
}
5 changes: 1 addition & 4 deletions src/ImageSharp/PixelFormats/Short2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,7 @@ public bool Equals(Short2 other)

/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode()
{
return this.PackedValue.GetHashCode();
}
public override int GetHashCode() => this.PackedValue.GetHashCode();

/// <inheritdoc />
public override string ToString()
Expand Down
5 changes: 1 addition & 4 deletions src/ImageSharp/PixelFormats/Short4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,7 @@ public bool Equals(Short4 other)
/// </summary>
/// <returns>Hash code for the instance.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode()
{
return this.PackedValue.GetHashCode();
}
public override int GetHashCode() => this.PackedValue.GetHashCode();

/// <summary>
/// Returns a string representation of the current instance.
Expand Down

0 comments on commit f11263f

Please sign in to comment.