From ed96821e0aa738ce292dcbd53bcc0e0754d63d6f Mon Sep 17 00:00:00 2001 From: Jiri Pittner Date: Thu, 26 Nov 2026 20:35:58 +0100 Subject: [PATCH] working on hash(), also for tensor --- doc/concepts.tex | 16 +++++++++++++++- doc/tensor.tex | 1 + doc/utilities.tex | 1 + mat.h | 6 ++++++ miscfunc.h | 8 ++++++++ smat.h | 6 ++++++ t.cc | 48 +++++++++++++++++++++++++++++++++++++++++++++++ tensor.h | 32 +++++++++++++++++++++++++++++++ vec.h | 9 +++++++++ 9 files changed, 126 insertions(+), 1 deletion(-) diff --git a/doc/concepts.tex b/doc/concepts.tex index 822e995..70c5d29 100644 --- a/doc/concepts.tex +++ b/doc/concepts.tex @@ -306,7 +306,8 @@ void get(int fd, bool dimensions=1, bool transp=0, bool orca_format=false); For storing many objects of the same size in a scratch file, see \code{AuxStorage} (chapter~\ref{ch:utilities}). \paragraph{Hash and checksum.} Two functions characterize the contents of an object by one number; they are members of -\code{NRVec}, \code{NRMat} and \code{NRSMat} (and of the classes derived from them) and work for nested objects. +\code{NRVec}, \code{NRMat} and \code{NRSMat}, of the classes derived from them (e.g.\ \code{fourindex\_dense}) and of +\code{Tensor}, and they work for nested objects. The sparse classes do not have them. \begin{lstlisting} uint64_t hash(uint64_t h=LA_HASH_INIT) const; const typename LA_traits::scalartype checksum(const uint64_t seed=0) const; @@ -338,3 +339,16 @@ the user can replace (section~\ref{sec:utilities:random}), and on the order of a frozen: their values are meant to be stored in files and will not change in later versions. The method \code{checksum} is formally a template, so that traits of user-defined element types need not define \code{scalartype} unless \code{checksum} is called for them. + +\code{std::hash} is specialized for \code{NRVec}, \code{NRMat} and \code{NRSMat} (each in its header) by means +of \code{hash()}, so that these objects (in practice with integer elements) can be the keys of +\code{std::unordered\_map} and \code{std::unordered\_set}; the keys are compared by \code{operator==}. A specialization +of \code{std::hash} does not apply to derived classes (\code{NRVec\_from1}, \code{NRVec\_colex}, \code{NRPerm}, +\code{Partition}, \code{fourindex\_dense}, \dots); for these, and for any class with the method \code{hash()}, the +hash function \code{LA::Hash} (\code{miscfunc.h}) is given as the third template argument: +\begin{lstlisting} +std::unordered_map,double> a; +std::unordered_map,double,LA::Hash> b; +\end{lstlisting} +A key stored in such a container shares its data with the object it was copied from, which therefore must not be +modified afterwards without \code{copyonwrite()}. \code{Tensor} has no \code{operator==} and cannot be a key. diff --git a/doc/tensor.tex b/doc/tensor.tex index 84a1f7b..33561a9 100644 --- a/doc/tensor.tex +++ b/doc/tensor.tex @@ -318,6 +318,7 @@ indexing, which is the same as the matrix convention above for \code{x} built by \begin{lareference} \lafunc{void put(int fd, bool with_names=false) const; void get(int fd, bool with_names=false)}{binary I/O of shape, auxiliary arrays, data and optionally names (section~\ref{sec:concepts:io}); with \code{with_names} the names are always written (an empty list if the tensor has none), so \code{put} and \code{get} must use the same \code{with_names}.} +\lafunc{uint64\_t hash(uint64\_t h=LA\_HASH\_INIT, bool with\_names=false) const; const typename LA\_traits::scalartype checksum(const uint64\_t seed=0) const}{Hash of the exact contents and numerical checksum of the data (section~\ref{sec:concepts:io}). The shape (number of indices, symmetry, offset and range of every index group) enters both, so that tensors with the same data and different shapes differ; the names of the indices enter the hash only with \code{with\_names} and never the checksum. \code{uint64\_t shapehash(uint64\_t h) const} is the part which depends on the shape.} \lafunc{template std::ostream & operator<<(std::ostream &s, const Tensor &x)}{text output: the shape, the names, then one line per stored element with its flat indices and value.} \lafunc{template std::istream & operator>>(std::istream &s, Tensor &x)}{reads the format written by \code{<<}.} \lafunc{std::ostream & operator<<(std::ostream &s, const INDEXGROUP &x); std::istream & operator>>(std::istream &s, INDEXGROUP &x)}{\code{number symmetry offset range}.} diff --git a/doc/utilities.tex b/doc/utilities.tex index da882c9..ff32d7f 100644 --- a/doc/utilities.tex +++ b/doc/utilities.tex @@ -169,6 +169,7 @@ iteration on the polynomial $s(d,n)$ followed by an integer correction); 0 for $ \lafunc{static inline uint64\_t hash\_mix(const uint64\_t counter, const uint64\_t seed)}{Pseudorandom 64-bit number as a fixed function of its arguments (splitmix64); a generator without a state, independent of \code{la\_random.h}. It generates the weights of \code{checksum} and combines the dimensions into \code{hash} (section~\ref{sec:concepts:io}); it must never be changed.} \lafunc{static inline double checksum\_weight(const uint64\_t z)}{The number \code{z} mapped to $[-1,1)$.} \lafunc{uint64\_t hash\_bytes(const void *x, const size\_t nbytes, uint64\_t h=LA\_HASH\_INIT)}{Hash of \code{nbytes} bytes of plain data, taken by words of 8 bytes, continuing from \code{h}; used by \code{hash} of the classes for scalar element types. Frozen as well.} +\lafunc{struct Hash \{template size\_t operator()(const X \&x) const;\}}{Hash function for the containers of the standard library which calls \code{x.hash()}; for the classes without an own specialization of \code{std::hash} (section~\ref{sec:concepts:io}).} \end{lareference} %===================================================================== diff --git a/mat.h b/mat.h index 8782721..9f8e868 100644 --- a/mat.h +++ b/mat.h @@ -21,6 +21,7 @@ #ifndef _LA_MAT_H_ #define _LA_MAT_H_ #include "la_traits.h" +#include using namespace LA_Vecmat3; @@ -1829,5 +1830,10 @@ NRVECMAT_OPER2(Mat, +) NRVECMAT_OPER2(Mat, -) }//end of the LA-namespace + +//hash() as the hash function of the standard library, see the note in vec.h +namespace std { +template struct hash > {size_t operator()(const LA::NRMat &x) const {return x.hash();}}; +} #endif/* _LA_MAT_H_ */ diff --git a/miscfunc.h b/miscfunc.h index cd3e7b3..69f97a9 100644 --- a/miscfunc.h +++ b/miscfunc.h @@ -76,6 +76,14 @@ return (double)(int64_t)z*(1./9223372036854775808.); //hash of plain data continuing from the value h; the data are taken by words of 8 bytes for speed extern uint64_t hash_bytes(const void *x, const size_t nbytes, uint64_t h=LA_HASH_INIT); +//hash function for the containers of the standard library for any class which has the method hash(), +//e.g. std::unordered_map,double,LA::Hash>; std::hash itself is specialized only for NRVec, NRMat and NRSMat, +//and a specialization does not apply to the classes derived from them +struct Hash + { + template size_t operator()(const X &x) const {return x.hash();} + }; + }//namespace diff --git a/smat.h b/smat.h index e372916..ba2665f 100644 --- a/smat.h +++ b/smat.h @@ -23,6 +23,7 @@ #ifndef _LA_SMAT_H_ #define _LA_SMAT_H_ #include "la_traits.h" +#include namespace LA { #define NN2 ((size_t)nn*(nn+1)/2) @@ -1579,4 +1580,9 @@ public: }; }//namespace + +//hash() as the hash function of the standard library, see the note in vec.h +namespace std { +template struct hash > {size_t operator()(const LA::NRSMat &x) const {return x.hash();}}; +} #endif /* _LA_SMAT_H_ */ diff --git a/t.cc b/t.cc index c966b79..315182f 100644 --- a/t.cc +++ b/t.cc @@ -30,6 +30,7 @@ #include "graph.h" #include "numbers.h" #include "miscfunc.h" +#include using namespace std; @@ -4922,6 +4923,53 @@ s.randomize(1.); NRVec iv(n); for(int i=0; i t(g); +t.randomize(1.); +Tensor t2(t); +g.symmetry=1; +Tensor t3(g); +t3.data=t.data; +cout <<"tensors: equal "<<(t.hash()==t2.hash())<<", other symmetry of the same data "<<(t.hash()==t3.hash())<<", difference of their checksums "< f(n); +f.randomize(1.); +cout <<"fourindex_dense: hash "<,int> table; +for(int i=0; i<5; ++i) + { + NRVec key(3); + key[0]=i; + key[1]=i*i; + key[2]= -i; + table[key]=10*i; + } +NRVec key(3); +key[0]=3; +key[1]=9; +key[2]= -3; +cout <<"hash table with vectors as keys: size "<,int,LA::Hash> ptable; +NRPerm perm(4); +perm.identity(); +for(int i=0; i<24; ++i) + { + ptable[perm]=i; + perm.next(); + } +perm.identity(); +cout <<"hash table with permutations as keys: size "< +#include using namespace LA_Vecmat3; @@ -2994,4 +2995,12 @@ void NRVec::storesubvector_to(const NRVec &selection, NRVec &rhs) const }//namespace +//hash() as the hash function of the standard library, so that vectors (best of integer types) can be keys of +//std::unordered_map and std::unordered_set; the keys are compared by operator== +//a specialization is not inherited: for a class derived from NRVec give LA::Hash (miscfunc.h) as the hash function +//NOTE: a key shares its data with the object it was copied from, which then must not be changed without copyonwrite() +namespace std { +template struct hash > {size_t operator()(const LA::NRVec &x) const {return x.hash();}}; +} + #endif /* _LA_VEC_H_ */