[PATCH][_Hashtable] Fix hash code cache usage
Jonathan Wakely
jwakely.gcc@gmail.com
Thu Mar 13 09:54:04 GMT 2025
On Thu, 13 Mar 2025 at 09:24, Florian Weimer <fweimer@redhat.com> wrote:
>
> * Jonathan Wakely:
>
> > On Thu, 13 Mar 2025 at 06:50, Florian Weimer <fweimer@redhat.com> wrote:
> >>
> >> * François Dumont:
> >>
> >> > + // Get hash code for a node that comes from another _Hashtable.
> >> > + // Reuse a cached hash code if the hash function is stateless,
> >> > + // otherwise recalculate it using our own hash function.
> >> > + __hash_code
> >> > + _M_hash_code_ext(const __node_value_type& __from) const
> >> > + {
> >> > + if constexpr (__and_<__hash_cached, is_empty<_Hash>>::value)
> >> > + return __from._M_hash_code;
> >> > + else
> >> > + return this->_M_hash_code(_ExtractKey{}(__from._M_v()));
> >> > + }
> >>
> >> Does C++ support stateful hash functions? I don't think so, and I don't
> >> see it documented as a GNU extension, either.
> >
> > It does, yes. That's why the hash function isn't required to be
> > default constructible, and has to be stored in the container and why
> > doing swap on two containers has to swap the hash functions as well.
>
> Interesting. I have trouble reconciling this with the Cpp17Hash
> requirement that “The value” h(k) “shall depend only on the argument k
> for the duration of the program.”
That's for a given value of the type, h. For any two values of the
type h and h2, it's not required that h2(k) == h(k).
More information about the Libstdc++
mailing list