Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #989 from IgorStepanov/hashOf
Browse files Browse the repository at this point in the history
add object.hashOf and object.mixHash
  • Loading branch information
DmitryOlshansky committed Oct 14, 2014
2 parents 62f5b89 + f3ebd71 commit 92c2e54
Show file tree
Hide file tree
Showing 20 changed files with 55 additions and 21 deletions.
28 changes: 28 additions & 0 deletions src/object.di
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,34 @@ bool _ArrayEq(T1, T2)(T1[] a1, T2[] a2)
return true;
}

/**
Calculates the hash value of $(D arg) with $(D seed) initial value.
Result may be non-equals with $(D typeid(T).getHash(&arg))
The $(D seed) value may be used for hash chaining:
----
struct Test
{
int a;
string b;
MyObject c;
size_t toHash() const @safe pure nothrow
{
size_t hash = a.hashOf();
hash = b.hashOf(hash);
size_t h1 = c.myMegaHash();
hash = h1.hashOf(hash); //Mix two hash values
return hash;
}
}
----
*/
size_t hashOf(T)(auto ref T arg, size_t seed = 0)
{
import core.internal.hash;
return core.internal.hash.hashOf(arg, seed);
}

bool _xopEquals(in void* ptr, in void* ptr);
bool _xopCmp(in void* ptr, in void* ptr);

Expand Down
12 changes: 9 additions & 3 deletions src/object_.d
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ class TypeInfo
try
{
auto data = this.toString();
return hashOf(data.ptr, data.length);
return rt.util.hash.hashOf(data.ptr, data.length);
}
catch (Throwable)
{
Expand Down Expand Up @@ -983,7 +983,7 @@ class TypeInfo_Struct : TypeInfo
}
else
{
return hashOf(p, init().length);
return rt.util.hash.hashOf(p, init().length);
}
}

Expand Down Expand Up @@ -2768,6 +2768,12 @@ bool _ArrayEq(T1, T2)(T1[] a1, T2[] a2)
}


size_t hashOf(T)(auto ref T arg, size_t seed = 0)
{
import core.internal.hash;
return core.internal.hash.hashOf(arg, seed);
}

bool _xopEquals(in void*, in void*)
{
throw new Error("TypeInfo.equals is not implemented");
Expand Down Expand Up @@ -2834,7 +2840,7 @@ size_t getArrayHash(in TypeInfo element, in void* ptr, in size_t count) @trusted
}

if(!hasCustomToHash(element))
return hashOf(ptr, elementSize * count);
return rt.util.hash.hashOf(ptr, elementSize * count);

size_t hash = 0;
foreach(size_t i; 0 .. count)
Expand Down
2 changes: 1 addition & 1 deletion src/rt/typeinfo/ti_Acdouble.d
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TypeInfo_Ar : TypeInfo_Array
override size_t getHash(in void* p) @trusted const
{
cdouble[] s = *cast(cdouble[]*)p;
return hashOf(s.ptr, s.length * cdouble.sizeof);
return rt.util.hash.hashOf(s.ptr, s.length * cdouble.sizeof);
}

override bool equals(in void* p1, in void* p2) const
Expand Down
2 changes: 1 addition & 1 deletion src/rt/typeinfo/ti_Acfloat.d
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TypeInfo_Aq : TypeInfo_Array
override size_t getHash(in void* p) @trusted const
{
cfloat[] s = *cast(cfloat[]*)p;
return hashOf(s.ptr, s.length * cfloat.sizeof);
return rt.util.hash.hashOf(s.ptr, s.length * cfloat.sizeof);
}

override bool equals(in void* p1, in void* p2) const
Expand Down
2 changes: 1 addition & 1 deletion src/rt/typeinfo/ti_Acreal.d
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TypeInfo_Ac : TypeInfo_Array
override size_t getHash(in void* p) @trusted const
{
creal[] s = *cast(creal[]*)p;
return hashOf(s.ptr, s.length * creal.sizeof);
return rt.util.hash.hashOf(s.ptr, s.length * creal.sizeof);
}

override bool equals(in void* p1, in void* p2) const
Expand Down
2 changes: 1 addition & 1 deletion src/rt/typeinfo/ti_Adouble.d
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TypeInfo_Ad : TypeInfo_Array
override size_t getHash(in void* p) @trusted const
{
double[] s = *cast(double[]*)p;
return hashOf(s.ptr, s.length * double.sizeof);
return rt.util.hash.hashOf(s.ptr, s.length * double.sizeof);
}

override bool equals(in void* p1, in void* p2) const
Expand Down
2 changes: 1 addition & 1 deletion src/rt/typeinfo/ti_Afloat.d
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TypeInfo_Af : TypeInfo_Array
override size_t getHash(in void* p) @trusted const
{
float[] s = *cast(float[]*)p;
return hashOf(s.ptr, s.length * float.sizeof);
return rt.util.hash.hashOf(s.ptr, s.length * float.sizeof);
}

override bool equals(in void* p1, in void* p2) const
Expand Down
2 changes: 1 addition & 1 deletion src/rt/typeinfo/ti_Ag.d
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class TypeInfo_Ag : TypeInfo_Array
override size_t getHash(in void* p) @trusted const
{
byte[] s = *cast(byte[]*)p;
return hashOf(s.ptr, s.length * byte.sizeof);
return rt.util.hash.hashOf(s.ptr, s.length * byte.sizeof);
}

override bool equals(in void* p1, in void* p2) const
Expand Down
2 changes: 1 addition & 1 deletion src/rt/typeinfo/ti_Aint.d
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class TypeInfo_Ai : TypeInfo_Array
override size_t getHash(in void* p) @trusted const
{
int[] s = *cast(int[]*)p;
return hashOf(s.ptr, s.length * int.sizeof);
return rt.util.hash.hashOf(s.ptr, s.length * int.sizeof);
}

override bool equals(in void* p1, in void* p2) const
Expand Down
2 changes: 1 addition & 1 deletion src/rt/typeinfo/ti_Along.d
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TypeInfo_Al : TypeInfo_Array
override size_t getHash(in void* p) @trusted const
{
long[] s = *cast(long[]*)p;
return hashOf(s.ptr, s.length * long.sizeof);
return rt.util.hash.hashOf(s.ptr, s.length * long.sizeof);
}

override bool equals(in void* p1, in void* p2) const
Expand Down
2 changes: 1 addition & 1 deletion src/rt/typeinfo/ti_Areal.d
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TypeInfo_Ae : TypeInfo_Array
override size_t getHash(in void* p) @trusted const
{
real[] s = *cast(real[]*)p;
return hashOf(s.ptr, s.length * real.sizeof);
return rt.util.hash.hashOf(s.ptr, s.length * real.sizeof);
}

override bool equals(in void* p1, in void* p2) const
Expand Down
2 changes: 1 addition & 1 deletion src/rt/typeinfo/ti_Ashort.d
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TypeInfo_As : TypeInfo_Array
override size_t getHash(in void* p) @trusted const
{
short[] s = *cast(short[]*)p;
return hashOf(s.ptr, s.length * short.sizeof);
return rt.util.hash.hashOf(s.ptr, s.length * short.sizeof);
}

override bool equals(in void* p1, in void* p2) const
Expand Down
2 changes: 1 addition & 1 deletion src/rt/typeinfo/ti_cdouble.d
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class TypeInfo_r : TypeInfo

override size_t getHash(in void* p)
{
return hashOf(p, cdouble.sizeof);
return rt.util.hash.hashOf(p, cdouble.sizeof);
}

override bool equals(in void* p1, in void* p2)
Expand Down
2 changes: 1 addition & 1 deletion src/rt/typeinfo/ti_cfloat.d
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class TypeInfo_q : TypeInfo

override size_t getHash(in void* p)
{
return hashOf(p, cfloat.sizeof);
return rt.util.hash.hashOf(p, cfloat.sizeof);
}

override bool equals(in void* p1, in void* p2)
Expand Down
2 changes: 1 addition & 1 deletion src/rt/typeinfo/ti_creal.d
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class TypeInfo_c : TypeInfo

override size_t getHash(in void* p)
{
return hashOf(p, creal.sizeof);
return rt.util.hash.hashOf(p, creal.sizeof);
}

override bool equals(in void* p1, in void* p2)
Expand Down
2 changes: 1 addition & 1 deletion src/rt/typeinfo/ti_delegate.d
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class TypeInfo_D : TypeInfo

override size_t getHash(in void* p)
{
return hashOf(p, dg.sizeof);
return rt.util.hash.hashOf(p, dg.sizeof);
}

override bool equals(in void* p1, in void* p2)
Expand Down
2 changes: 1 addition & 1 deletion src/rt/typeinfo/ti_double.d
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class TypeInfo_d : TypeInfo

override size_t getHash(in void* p)
{
return hashOf(p, double.sizeof);
return rt.util.hash.hashOf(p, double.sizeof);
}

override bool equals(in void* p1, in void* p2)
Expand Down
2 changes: 1 addition & 1 deletion src/rt/typeinfo/ti_long.d
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class TypeInfo_l : TypeInfo

override size_t getHash(in void* p)
{
return hashOf(p, long.sizeof);
return rt.util.hash.hashOf(p, long.sizeof);
}

override bool equals(in void* p1, in void* p2)
Expand Down
2 changes: 1 addition & 1 deletion src/rt/typeinfo/ti_real.d
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class TypeInfo_e : TypeInfo

override size_t getHash(in void* p)
{
return hashOf(p, real.sizeof);
return rt.util.hash.hashOf(p, real.sizeof);
}

override bool equals(in void* p1, in void* p2)
Expand Down
2 changes: 1 addition & 1 deletion src/rt/typeinfo/ti_ulong.d
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class TypeInfo_m : TypeInfo

override size_t getHash(in void* p)
{
return hashOf(p, ulong.sizeof);
return rt.util.hash.hashOf(p, ulong.sizeof);
}

override bool equals(in void* p1, in void* p2)
Expand Down

0 comments on commit 92c2e54

Please sign in to comment.