Skip to content

Commit

Permalink
fixed and improved positional eq and hashing; extra tests and bench
Browse files Browse the repository at this point in the history
  • Loading branch information
protobufel committed Jun 2, 2017
1 parent 50a2149 commit 9a7dfdf
Show file tree
Hide file tree
Showing 25 changed files with 400 additions and 623 deletions.
11 changes: 6 additions & 5 deletions MultiKeyMap/MultiKeyMap.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
<RepositoryUrl>https://github.com/protobufel/multikeymapcsharp</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>c# library multikey dictionary</PackageTags>
<PackageReleaseNotes>1. Exposed internal equality comparer extensions and the reference equality comparer via EqualityComparerExtensions static class.
2. Tweaked some benchmarks</PackageReleaseNotes>
<PackageReleaseNotes>1. Improved optimized positional search
2. Tweaked some benchmarks
3. Added extra tests mirroring benchmarks</PackageReleaseNotes>
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<SignAssembly>False</SignAssembly>
<AssemblyVersion>4.2.0.0</AssemblyVersion>
<FileVersion>4.2.0.0</FileVersion>
<Version>4.2.0</Version>
<AssemblyVersion>4.2.10.0</AssemblyVersion>
<FileVersion>4.2.10.0</FileVersion>
<Version>4.2.10</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
42 changes: 24 additions & 18 deletions MultiKeyMap/PositionMask/KeyMasks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal class KeyMask<T, K> : IKeyMask<T, K> where K : IEnumerable<T>
{
public KeyMask(K key)
{
if (!(key is System.ValueType) && (key == null)) throw new ArgumentNullException("key");
if (!(key is ValueType) && (key == null)) throw new ArgumentNullException("key");
Key = key;
}

Expand Down Expand Up @@ -90,7 +90,7 @@ public SubKeyMask(T subKey) : this(subKey, NonPositional)

public SubKeyMask(T subKey, int position)
{
if (!(subKey is System.ValueType) && (subKey == null)) throw new ArgumentNullException("key");
if (!(subKey is ValueType) && (subKey == null)) throw new ArgumentNullException("key");
SubKey = subKey;
Position = position;
}
Expand All @@ -101,14 +101,7 @@ public SubKeyMask(T subKey, int position)

public override int GetHashCode()
{
int hash = 13;

unchecked
{
hash = (hash + ((Position == NonPositional) ? NonPositionalHashCode : Position.GetHashCode())) * 47 + SubKey.GetHashCode();
}

return hash;
return KeyMaskExtensions.ShiftAndWrap(Position.GetHashCode(), 2) ^ SubKey.GetHashCode();
}

public bool Equals(ISubKeyMask<T> other)
Expand All @@ -123,12 +116,7 @@ public bool Equals(ISubKeyMask<T> other)
return true;
}

if (GetType() != other.GetType())
{
return false;
}

return Equals(SubKey, other.SubKey) && (Position == other.Position);
return (Position == other.Position) && Equals(SubKey, other.SubKey);
}

public override bool Equals(object obj)
Expand Down Expand Up @@ -184,15 +172,33 @@ public SubKeyMaskComparer(IEqualityComparer<T> subKeyComparer)

public override bool Equals(ISubKeyMask<T> x, ISubKeyMask<T> y)
{
return SubKeyComparer.Equals(x.SubKey, y.SubKey);
if (ReferenceEquals(x, null))
{
return false;
}

if (ReferenceEquals(x, y))
{
return true;
}

return (x.Position == y.Position) && SubKeyComparer.Equals(x.SubKey, y.SubKey);
}

public override int GetHashCode(ISubKeyMask<T> obj)
{
return obj?.GetHashCode() ?? 0;
return ShiftAndWrap(obj.Position.GetHashCode(), 2) ^ SubKeyComparer.GetHashCode(obj.SubKey);
}
}

internal static int ShiftAndWrap(int value, int positions)
{
positions = positions & 0x1F;
uint number = BitConverter.ToUInt32(BitConverter.GetBytes(value), 0);
uint wrapped = number >> (32 - positions);
return BitConverter.ToInt32(BitConverter.GetBytes((number << positions) | wrapped), 0);
}

public static IEqualityComparer<IKeyMask<T, K>> ToKeyMaskComparer<T, K>(this IEqualityComparer<K> keyComparer) where K : IEnumerable<T>
{
return new KeyMaskComparer<T, K>(keyComparer);
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 9a7dfdf

Please sign in to comment.