working on hash(), also for tensor

This commit is contained in:
jiri committed 2026-11-26 20:35:58 +01:00
1 parent 8b2b413db7
commit ed96821e0a
9 files changed
+126 -1

No files matched your search

+15 -1
View File
@@ -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<T>::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<NRVec<int>,double> a;
std::unordered_map<NRPerm<int>,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.
+1
View File
@@ -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<T>::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<typename T> std::ostream & operator<<(std::ostream &s, const Tensor<T> &x)}{text output: the shape, the names, then one line per stored element with its flat indices and value.}
\lafunc{template<typename T> std::istream & operator>>(std::istream &s, Tensor<T> &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}.}
+1
View File
@@ -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<typename X> 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}
%=====================================================================