diff --git a/doc/concepts.tex b/doc/concepts.tex index 70c5d29..822eb9f 100644 --- a/doc/concepts.tex +++ b/doc/concepts.tex @@ -351,4 +351,5 @@ 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. +modified afterwards without \code{copyonwrite()}. \code{std::hash} is specialized also for \code{Tensor} (by its +\code{hash()} without the names of the indices). diff --git a/doc/tensor.tex b/doc/tensor.tex index 33561a9..a85e86c 100644 --- a/doc/tensor.tex +++ b/doc/tensor.tex @@ -319,6 +319,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{bool operator==(const Tensor \&rhs) const; bool operator!=(const Tensor \&rhs) const}{Equality: the same shape, exactly the same data (bit by bit, as for vectors) and the same names of the indices; a named tensor is not equal to an unnamed one with the same data. This is an equivalence relation, so that tensors can be the keys of \code{std::unordered\_map} and \code{std::unordered\_set} (\code{std::hash} is specialized for \code{Tensor}). It differs from the test of compatibility in the arithmetic operators, which compares the names only if both tensors are named. Shapes and names are compared field by field and as strings (not by \code{memcmp}, which was wrong for names with undefined characters after the terminating zero).} \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/t.cc b/t.cc index 315182f..5220ff1 100644 --- a/t.cc +++ b/t.cc @@ -4940,6 +4940,18 @@ 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 "< t4(t), t5(t2); +cout <<"tensor equal to its copy "<<(t==t4)<<", to the named copy "<<(t==t2)<<", named copies equal "<<(t2==t5)<<", other shape "<<(t==t3)<,int> ttable; +ttable[t]=1; +ttable[t2]=2; +ttable[t4]=3; +cout <<"hash table with tensors as keys: size "< f(n); f.randomize(1.); diff --git a/tensor.h b/tensor.h index a6db38d..6274597 100644 --- a/tensor.h +++ b/tensor.h @@ -136,7 +136,7 @@ class LA_traits { static bool is_plaindata() {return true;}; static void copyonwrite(INDEXNAME& x) {}; typedef INDEXNAME normtype; - static inline int gencmp(const INDEXNAME *a, const INDEXNAME *b, int n) {return memcmp(a,b,n*sizeof(INDEXNAME));}; + static inline int gencmp(const INDEXNAME *a, const INDEXNAME *b, int n) {for(int i=0; i { static bool is_plaindata() {return true;}; static void copyonwrite(INDEXGROUP& x) {}; typedef INDEXGROUP normtype; - static inline int gencmp(const INDEXGROUP *a, const INDEXGROUP *b, int n) {return memcmp(a,b,n*sizeof(INDEXGROUP));}; + static inline int gencmp(const INDEXGROUP *a, const INDEXGROUP *b, int n) {for(int i=0; i>(std::istream &s, Tensor &x); }//namespace + +//hash() (without the names of the indices) as the hash function of the standard library, see the note in vec.h +namespace std { +template struct hash > {size_t operator()(const LA::Tensor &x) const {return x.hash();}}; +} #endif