Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

But maybe we don't really need that... -- Larry Wall in <199709011851.LAA07101@wall.org>


devel / comp.lang.c++ / Trying to solve how to do a std::unordered_map<std::pair<..., ...>, ...>

SubjectAuthor
* Trying to solve how to do a std::unordered_map<std::pair<..., ...>, ...>Aaron Gray
`* Re: Trying to solve how to do a std::unordered_map<std::pair<...,Bonita Montero
 `* Re: Trying to solve how to do a std::unordered_map<std::pair<...,Bonita Montero
  `* Re: Trying to solve how to do a std::unordered_map<std::pair<...,Aaron Gray
   `* Re: Trying to solve how to do a std::unordered_map<std::pair<...,Bonita Montero
    `* Re: Trying to solve how to do a std::unordered_map<std::pair<...,Aaron Gray
     `* Re: Trying to solve how to do a std::unordered_map<std::pair<...,Bonita Montero
      `- Re: Trying to solve how to do a std::unordered_map<std::pair<...,Aaron Gray

1
Trying to solve how to do a std::unordered_map<std::pair<..., ...>, ...>

<d10b9603-7028-4045-94cf-60bac570af20n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2035&group=comp.lang.c%2B%2B#2035

  copy link   Newsgroups: comp.lang.c++
X-Received: by 2002:a05:6214:1910:b0:65a:fea1:5924 with SMTP id er16-20020a056214191000b0065afea15924mr141336qvb.8.1696193975695;
Sun, 01 Oct 2023 13:59:35 -0700 (PDT)
X-Received: by 2002:a05:6870:8c2f:b0:1d6:9f52:b461 with SMTP id
ec47-20020a0568708c2f00b001d69f52b461mr3662493oab.6.1696193975405; Sun, 01
Oct 2023 13:59:35 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c++
Date: Sun, 1 Oct 2023 13:59:34 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=81.174.241.153; posting-account=kL25OwoAAADaEwZmFnPXDuYNUKiH5X5u
NNTP-Posting-Host: 81.174.241.153
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <d10b9603-7028-4045-94cf-60bac570af20n@googlegroups.com>
Subject: Trying to solve how to do a std::unordered_map<std::pair<..., ...>, ...>
From: aaronngray@gmail.com (Aaron Gray)
Injection-Date: Sun, 01 Oct 2023 20:59:35 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 1333
 by: Aaron Gray - Sun, 1 Oct 2023 20:59 UTC

I am trying to work out how to do a :-

class Type {
public:
std::string name;
....
};

std::unordered_map<std::pair<const Type *, const Type*>, bool, std::hash<Type>> subTypeMemorization;

Godbolt attempt xxample :-

https://godbolt.org/z/r4YrG841E

Many thanks in advance,

Aaron

Re: Trying to solve how to do a std::unordered_map<std::pair<..., ...>, ...>

<ufe8u4$2tl9e$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2040&group=comp.lang.c%2B%2B#2040

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Trying to solve how to do a std::unordered_map<std::pair<...,
...>, ...>
Date: Mon, 2 Oct 2023 13:18:29 +0200
Organization: A noiseless patient Spider
Lines: 44
Message-ID: <ufe8u4$2tl9e$1@dont-email.me>
References: <d10b9603-7028-4045-94cf-60bac570af20n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 2 Oct 2023 11:18:28 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="bb8cbd45ad1a17b9153633bfe066831e";
logging-data="3069230"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18IXsXXg3x+YjxgOABSGEfPbA/hFKA6/I8="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:D5sGOe9WMgXL+X5dysftrfj/oOo=
In-Reply-To: <d10b9603-7028-4045-94cf-60bac570af20n@googlegroups.com>
Content-Language: de-DE
 by: Bonita Montero - Mon, 2 Oct 2023 11:18 UTC

Am 01.10.2023 um 22:59 schrieb Aaron Gray:
> I am trying to work out how to do a :-
>
> class Type {
> public:
> std::string name;
> ....
> };
> std::unordered_map<std::pair<const Type *, const Type*>, bool, std::hash<Type>> subTypeMemorization;
> Godbolt attempt xxample :-
>
> https://godbolt.org/z/r4YrG841E

You must specialize the hasher yourself:

#include <string>
#include <utility>
#include <unordered_map>
#include <functional>

using namespace std;

struct Type
{ string name;
};

template<>
struct hash<pair<Type *, Type *>>
{ size_t operator ()( pair<Type *, Type *> const &key )
{
size_t hash = 0;
hash ^= key.first ? std::hash<string>()( key.first->name ) :
std::hash<void *>()( nullptr );
hash ^= key.second ? std::hash<string>()( key.first->name ) :
std::hash<void *>()( nullptr );
return hash;
}
};

unordered_map<pair<Type *, Type *>, bool> map;

Re: Trying to solve how to do a std::unordered_map<std::pair<..., ...>, ...>

<ufe90j$2tl9e$2@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2041&group=comp.lang.c%2B%2B#2041

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Trying to solve how to do a std::unordered_map<std::pair<...,
...>, ...>
Date: Mon, 2 Oct 2023 13:19:49 +0200
Organization: A noiseless patient Spider
Lines: 50
Message-ID: <ufe90j$2tl9e$2@dont-email.me>
References: <d10b9603-7028-4045-94cf-60bac570af20n@googlegroups.com>
<ufe8u4$2tl9e$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 2 Oct 2023 11:19:47 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="bb8cbd45ad1a17b9153633bfe066831e";
logging-data="3069230"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19gxe8hirgsTw59ZZAMc+Nr0OxTUtq6GgY="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:FK2+u3nYYsjxjPtIDcTZ5+IRpKo=
In-Reply-To: <ufe8u4$2tl9e$1@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Mon, 2 Oct 2023 11:19 UTC

Am 02.10.2023 um 13:18 schrieb Bonita Montero:
> Am 01.10.2023 um 22:59 schrieb Aaron Gray:
>> I am trying to work out how to do a :-
>>
>> class Type {
>> public:
>>      std::string name;
>>      ....
>> };
>> std::unordered_map<std::pair<const Type *, const Type*>, bool,
>> std::hash<Type>> subTypeMemorization;
>> Godbolt attempt xxample :-
>>
>>      https://godbolt.org/z/r4YrG841E
>
> You must specialize the hasher yourself:
>
> #include <string>
> #include <utility>
> #include <unordered_map>
> #include <functional>
>
> using namespace std;
>
> struct Type
> {
>     string name;
> };
>
> template<>
> struct hash<pair<Type *, Type *>>
> {
>     size_t operator ()( pair<Type *, Type *> const &key )
>     {
>         size_t hash = 0;
>         hash ^= key.first ? std::hash<string>()( key.first->name ) :
> std::hash<void *>()( nullptr );
>         hash ^= key.second ? std::hash<string>()( key.first->name ) :
> std::hash<void *>()( nullptr );
hash ^= key.second ? std::hash<string>()( key.second->name )
: std::hash<void *>()( nullptr );

>         return hash;
>     }
> };
>
> unordered_map<pair<Type *, Type *>, bool> map;
>
>

Re: Trying to solve how to do a std::unordered_map<std::pair<..., ...>, ...>

<40409df3-5c3b-4c60-83a6-34ccf60ed63cn@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2047&group=comp.lang.c%2B%2B#2047

  copy link   Newsgroups: comp.lang.c++
X-Received: by 2002:a05:620a:2620:b0:76f:1b38:e73d with SMTP id z32-20020a05620a262000b0076f1b38e73dmr147281qko.10.1696248608071;
Mon, 02 Oct 2023 05:10:08 -0700 (PDT)
X-Received: by 2002:a05:6830:22d2:b0:6bd:b74:2dab with SMTP id
q18-20020a05683022d200b006bd0b742dabmr3301838otc.2.1696248607778; Mon, 02 Oct
2023 05:10:07 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!1.us.feeder.erje.net!feeder.erje.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c++
Date: Mon, 2 Oct 2023 05:10:07 -0700 (PDT)
In-Reply-To: <ufe90j$2tl9e$2@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=81.174.241.153; posting-account=kL25OwoAAADaEwZmFnPXDuYNUKiH5X5u
NNTP-Posting-Host: 81.174.241.153
References: <d10b9603-7028-4045-94cf-60bac570af20n@googlegroups.com>
<ufe8u4$2tl9e$1@dont-email.me> <ufe90j$2tl9e$2@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <40409df3-5c3b-4c60-83a6-34ccf60ed63cn@googlegroups.com>
Subject: Re: Trying to solve how to do a std::unordered_map<std::pair<...,
...>, ...>
From: aaronngray@gmail.com (Aaron Gray)
Injection-Date: Mon, 02 Oct 2023 12:10:08 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 1542
 by: Aaron Gray - Mon, 2 Oct 2023 12:10 UTC

On Monday, 2 October 2023 at 12:20:05 UTC+1, Bonita Montero wrote:
> > You must specialize the hasher yourself:

Bonita,

Great, I found another solution on Stack Overflow but will look into this one as it looks cleaner and more cannonical, thanks a lot.

https://stackoverflow.com/posts/32685618/revisions

Regards,

Aaron

Re: Trying to solve how to do a std::unordered_map<std::pair<..., ...>, ...>

<ufel6v$3034l$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2049&group=comp.lang.c%2B%2B#2049

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Trying to solve how to do a std::unordered_map<std::pair<...,
...>, ...>
Date: Mon, 2 Oct 2023 16:48:01 +0200
Organization: A noiseless patient Spider
Lines: 12
Message-ID: <ufel6v$3034l$1@dont-email.me>
References: <d10b9603-7028-4045-94cf-60bac570af20n@googlegroups.com>
<ufe8u4$2tl9e$1@dont-email.me> <ufe90j$2tl9e$2@dont-email.me>
<40409df3-5c3b-4c60-83a6-34ccf60ed63cn@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 2 Oct 2023 14:47:59 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="bb8cbd45ad1a17b9153633bfe066831e";
logging-data="3148949"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+eCvghlN3DcCMk5sA2TnsAODV35qoqU18="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:SUyOEmHtZzCTEoAiQvuS/abz1OY=
Content-Language: de-DE
In-Reply-To: <40409df3-5c3b-4c60-83a6-34ccf60ed63cn@googlegroups.com>
 by: Bonita Montero - Mon, 2 Oct 2023 14:48 UTC

Am 02.10.2023 um 14:10 schrieb Aaron Gray:

> Bonita,
> Great, I found another solution on Stack Overflow but will look into this one as it looks cleaner and more cannonical, thanks a lot.
> https://stackoverflow.com/posts/32685618/revisions

The problem I have with that solution for you is that it will hash
the Type-pointer values of first and second and not the both strings
themselfes. This may lead to different hashes for identical strings
I'm hashing either the strings, or if either Pointer is nullptr, the
nullptr-hash.

Re: Trying to solve how to do a std::unordered_map<std::pair<..., ...>, ...>

<69a4c0e8-8da1-4101-b424-259c6aebd7c9n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2050&group=comp.lang.c%2B%2B#2050

  copy link   Newsgroups: comp.lang.c++
X-Received: by 2002:a05:622a:ed2:b0:419:8627:f6df with SMTP id df18-20020a05622a0ed200b004198627f6dfmr4045qtb.3.1696268130875;
Mon, 02 Oct 2023 10:35:30 -0700 (PDT)
X-Received: by 2002:a05:6808:199b:b0:3a7:392a:7405 with SMTP id
bj27-20020a056808199b00b003a7392a7405mr5985835oib.2.1696268130620; Mon, 02
Oct 2023 10:35:30 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c++
Date: Mon, 2 Oct 2023 10:35:30 -0700 (PDT)
In-Reply-To: <ufel6v$3034l$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=81.174.241.153; posting-account=kL25OwoAAADaEwZmFnPXDuYNUKiH5X5u
NNTP-Posting-Host: 81.174.241.153
References: <d10b9603-7028-4045-94cf-60bac570af20n@googlegroups.com>
<ufe8u4$2tl9e$1@dont-email.me> <ufe90j$2tl9e$2@dont-email.me>
<40409df3-5c3b-4c60-83a6-34ccf60ed63cn@googlegroups.com> <ufel6v$3034l$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <69a4c0e8-8da1-4101-b424-259c6aebd7c9n@googlegroups.com>
Subject: Re: Trying to solve how to do a std::unordered_map<std::pair<...,
...>, ...>
From: aaronngray@gmail.com (Aaron Gray)
Injection-Date: Mon, 02 Oct 2023 17:35:30 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 2172
 by: Aaron Gray - Mon, 2 Oct 2023 17:35 UTC

On Monday, 2 October 2023 at 15:48:18 UTC+1, Bonita Montero wrote:
> Am 02.10.2023 um 14:10 schrieb Aaron Gray:
>
> > Bonita,
> > Great, I found another solution on Stack Overflow but will look into this one as it looks cleaner and more cannonical, thanks a lot.
> > https://stackoverflow.com/posts/32685618/revisions
> The problem I have with that solution for you is that it will hash
> the Type-pointer values of first and second and not the both strings
> themselfes. This may lead to different hashes for identical strings
> I'm hashing either the strings, or if either Pointer is nullptr, the
> nullptr-hash.

Hash functions are not my strong point but I can see you are covering the domain consistently in your code where the Stack Overflow example is almost random and probably overlapping input domains.

Thanks for pointing that out !

Aaron

Re: Trying to solve how to do a std::unordered_map<std::pair<..., ...>, ...>

<ufgsh0$3h5ob$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2059&group=comp.lang.c%2B%2B#2059

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!news.hispagatos.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Trying to solve how to do a std::unordered_map<std::pair<...,
...>, ...>
Date: Tue, 3 Oct 2023 13:05:07 +0200
Organization: A noiseless patient Spider
Lines: 26
Message-ID: <ufgsh0$3h5ob$1@dont-email.me>
References: <d10b9603-7028-4045-94cf-60bac570af20n@googlegroups.com>
<ufe8u4$2tl9e$1@dont-email.me> <ufe90j$2tl9e$2@dont-email.me>
<40409df3-5c3b-4c60-83a6-34ccf60ed63cn@googlegroups.com>
<ufel6v$3034l$1@dont-email.me>
<69a4c0e8-8da1-4101-b424-259c6aebd7c9n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 3 Oct 2023 11:05:05 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="485eca7af7972ec2e41e655d59aee6d8";
logging-data="3708683"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/rB+twKqjqRD7QXbchbwzblrCFLNV77D4="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:ndt+13L4y1nxJZnQlzNHD11g4bY=
In-Reply-To: <69a4c0e8-8da1-4101-b424-259c6aebd7c9n@googlegroups.com>
Content-Language: de-DE
 by: Bonita Montero - Tue, 3 Oct 2023 11:05 UTC

Am 02.10.2023 um 19:35 schrieb Aaron Gray:

> Thanks for pointing that out !

Maybe that should be the final solution:

template<>
struct hash<pair<Type*, Type*>>
{ size_t operator ()(pair<Type*, Type*> const& key)
{
size_t h = 0;
std::hash<string_view> hsv;
auto extend = [&]( auto, Type *pT ) { h ^= hsv( pT ?
string_view( pT->name.begin(), pT->name.end() ) : string_view( "" ) ); };
extend( false_type(), key.first );
extend( true_type(), key.second );
return h;
}
};

I use lambdas to save code. To force them to be inlined I make
them generic and call each instntiation only once. To instantiate
the lambda above twice I call them with different bool_constant<X>
types.

Re: Trying to solve how to do a std::unordered_map<std::pair<..., ...>, ...>

<af3b34ca-ff40-4e86-8bc1-395dc4beebecn@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2070&group=comp.lang.c%2B%2B#2070

  copy link   Newsgroups: comp.lang.c++
X-Received: by 2002:a05:622a:307:b0:412:1cbf:fb41 with SMTP id q7-20020a05622a030700b004121cbffb41mr39756qtw.0.1696451184022;
Wed, 04 Oct 2023 13:26:24 -0700 (PDT)
X-Received: by 2002:a05:6808:18a3:b0:3ae:2710:cf87 with SMTP id
bi35-20020a05680818a300b003ae2710cf87mr1926274oib.7.1696451183841; Wed, 04
Oct 2023 13:26:23 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c++
Date: Wed, 4 Oct 2023 13:26:23 -0700 (PDT)
In-Reply-To: <ufgsh0$3h5ob$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=81.174.241.153; posting-account=kL25OwoAAADaEwZmFnPXDuYNUKiH5X5u
NNTP-Posting-Host: 81.174.241.153
References: <d10b9603-7028-4045-94cf-60bac570af20n@googlegroups.com>
<ufe8u4$2tl9e$1@dont-email.me> <ufe90j$2tl9e$2@dont-email.me>
<40409df3-5c3b-4c60-83a6-34ccf60ed63cn@googlegroups.com> <ufel6v$3034l$1@dont-email.me>
<69a4c0e8-8da1-4101-b424-259c6aebd7c9n@googlegroups.com> <ufgsh0$3h5ob$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <af3b34ca-ff40-4e86-8bc1-395dc4beebecn@googlegroups.com>
Subject: Re: Trying to solve how to do a std::unordered_map<std::pair<...,
...>, ...>
From: aaronngray@gmail.com (Aaron Gray)
Injection-Date: Wed, 04 Oct 2023 20:26:24 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 2299
 by: Aaron Gray - Wed, 4 Oct 2023 20:26 UTC

On Tuesday, 3 October 2023 at 12:05:24 UTC+1, Bonita Montero wrote:
> Am 02.10.2023 um 19:35 schrieb Aaron Gray:
>
> > Thanks for pointing that out !
> Maybe that should be the final solution:
>
> template<>
> struct hash<pair<Type*, Type*>>
> {
> size_t operator ()(pair<Type*, Type*> const& key)
> {
> size_t h = 0;
> std::hash<string_view> hsv;
> auto extend = [&]( auto, Type *pT ) { h ^= hsv( pT ?
> string_view( pT->name.begin(), pT->name.end() ) : string_view( "" ) ); };
> extend( false_type(), key.first );
> extend( true_type(), key.second );
> return h;
> }
> };
>
> I use lambdas to save code. To force them to be inlined I make
> them generic and call each instntiation only once. To instantiate
> the lambda above twice I call them with different bool_constant<X>
> types.

I am not sure about this. I prefer your previous example.

Regards,

Aaron

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor