Commit a68c27be authored by Davis King's avatar Davis King

Added a hash() for matrices.

parent f338f6b7
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "matrix_expressions.h" #include "matrix_expressions.h"
#include "matrix_math_functions.h" #include "matrix_math_functions.h"
#include "matrix_op.h" #include "matrix_op.h"
#include "../general_hash/murmur_hash3.h"
namespace dlib namespace dlib
...@@ -3966,6 +3967,22 @@ namespace dlib ...@@ -3966,6 +3967,22 @@ namespace dlib
return matrix_op<op>(op(m.ref())); return matrix_op<op>(op(m.ref()));
} }
// ----------------------------------------------------------------------------------------
template <typename T, long NR, long NC, typename MM, typename L>
uint32 hash (
const matrix<T,NR,NC,MM,L>& item,
uint32 seed = 0
)
{
DLIB_ASSERT_HAS_STANDARD_LAYOUT(T);
if (item.size() == 0)
return 0;
else
return murmur_hash3(&item(0,0), sizeof(T)*item.size(), seed);
}
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
} }
......
...@@ -667,10 +667,11 @@ namespace dlib ...@@ -667,10 +667,11 @@ namespace dlib
long NR, long NR,
long NC, long NC,
typename MM, typename MM,
typename U typename U,
typename L
> >
void set_all_elements ( void set_all_elements (
matrix<T,NR,NC,MM>& m, matrix<T,NR,NC,MM,L>& m,
U value U value
); );
/*! /*!
...@@ -690,6 +691,27 @@ namespace dlib ...@@ -690,6 +691,27 @@ namespace dlib
(This allows you to easily force a matrix_exp to fully evaluate) (This allows you to easily force a matrix_exp to fully evaluate)
!*/ !*/
// ----------------------------------------------------------------------------------------
template <
typename T,
long NR,
long NC,
typename MM,
typename L
>
uint32 hash (
const matrix<T,NR,NC,MM,L>& item,
uint32 seed = 0
);
/*!
ensures
- returns a 32bit hash of the data stored in item.
- Each value of seed results in a different hash function being used.
(e.g. hash(item,0) should generally not be equal to hash(item,1))
- uses the murmur_hash3() routine to compute the actual hash.
!*/
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
// if matrix_exp contains non-complex types (e.g. float, double) // if matrix_exp contains non-complex types (e.g. float, double)
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <cstdlib> #include <cstdlib>
#include <ctime> #include <ctime>
#include <dlib/hash.h> #include <dlib/hash.h>
#include <dlib/matrix.h>
#include "tester.h" #include "tester.h"
...@@ -32,6 +33,9 @@ namespace ...@@ -32,6 +33,9 @@ namespace
{ {
std::string str1 = "some random string"; std::string str1 = "some random string";
std::wstring str2 = L"another String!"; std::wstring str2 = L"another String!";
matrix<unsigned char> mat(2,2);
mat = 1,2,3,4;
std::vector<unsigned char> v(4); std::vector<unsigned char> v(4);
v[0] = 'c'; v[0] = 'c';
...@@ -48,22 +52,26 @@ namespace ...@@ -48,22 +52,26 @@ namespace
dlog << LINFO << "hash(str2): "<< hash(str2); dlog << LINFO << "hash(str2): "<< hash(str2);
dlog << LINFO << "hash(v): "<< hash(v); dlog << LINFO << "hash(v): "<< hash(v);
dlog << LINFO << "hash(m): "<< hash(m); dlog << LINFO << "hash(m): "<< hash(m);
dlog << LINFO << "hash(mat): "<< hash(mat);
DLIB_TEST(hash(str1) == 1073638390); DLIB_TEST(hash(str1) == 1073638390);
DLIB_TEST(hash(str2) == 2413364589); DLIB_TEST(hash(str2) == 2413364589);
DLIB_TEST(hash(v) == 4054789286); DLIB_TEST(hash(v) == 4054789286);
DLIB_TEST(hash(m) == 2865512303); DLIB_TEST(hash(m) == 2865512303);
DLIB_TEST(hash(mat) == 1043635621);
DLIB_TEST(murmur_hash3(&str1[0], str1.size(), 0) == 1073638390); DLIB_TEST(murmur_hash3(&str1[0], str1.size(), 0) == 1073638390);
dlog << LINFO << "hash(str1,1): "<< hash(str1,1); dlog << LINFO << "hash(str1,1): "<< hash(str1,1);
dlog << LINFO << "hash(str2,2): "<< hash(str2,2); dlog << LINFO << "hash(str2,2): "<< hash(str2,2);
dlog << LINFO << "hash(v,3): "<< hash(v,3); dlog << LINFO << "hash(v,3): "<< hash(v,3);
dlog << LINFO << "hash(m,3): "<< hash(m,4); dlog << LINFO << "hash(m,4): "<< hash(m,4);
dlog << LINFO << "hash(mat,5): "<< hash(mat,5);
DLIB_TEST(hash(str1,1) == 2977753747); DLIB_TEST(hash(str1,1) == 2977753747);
DLIB_TEST(hash(str2,2) == 3656927287); DLIB_TEST(hash(str2,2) == 3656927287);
DLIB_TEST(hash(v,3) == 2127112268); DLIB_TEST(hash(v,3) == 2127112268);
DLIB_TEST(hash(m,4) == 4200495810); DLIB_TEST(hash(m,4) == 4200495810);
DLIB_TEST(hash(mat,5) == 2380427865);
DLIB_TEST(murmur_hash3(&str1[0], str1.size(), 1) == 2977753747); DLIB_TEST(murmur_hash3(&str1[0], str1.size(), 1) == 2977753747);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment