Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

All constants are variables.


devel / comp.lang.c++ / Lambda passed through a std::sort recursion

SubjectAuthor
o Lambda passed through a std::sort recursionBonita Montero

1
Lambda passed through a std::sort recursion

<ujcq3c$3pgf8$1@raubtier-asyl.eternal-september.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!news.1d4.us!usenet.goja.nl.eu.org!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!raubtier-asyl.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Lambda passed through a std::sort recursion
Date: Sun, 19 Nov 2023 12:04:18 +0100
Organization: A noiseless patient Spider
Lines: 41
Message-ID: <ujcq3c$3pgf8$1@raubtier-asyl.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 19 Nov 2023 11:04:12 -0000 (UTC)
Injection-Info: raubtier-asyl.eternal-september.org; posting-host="5188d23c02781bdaeb3c144e123c43f6";
logging-data="3981800"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18kUHe61vtK6ezWoqOhYBpvsGq5PLVfsWM="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:mP/iYjrsuEmVq/C+vVT6Rae1XEA=
Content-Language: de-DE
 by: Bonita Montero - Sun, 19 Nov 2023 11:04 UTC

I was interested in whether my C++ implementations pass the lambda you
supply with std::sort through the recursion of sort by coping. libstdc++
(g++) and libc++ (clang++) copy the lambda at each recursion level. MSVC
stores the lambda only once.

#include <iostream>
#include <vector>
#include <random>
#include <unordered_map>
#include <climits>
#include <algorithm>

using namespace std;

int main( int argc, char **argv )
{ mt19937_64 mt;
uniform_int_distribution<int> uid( INT_MAX, INT_MAX );
vector<int> vi;
vi.reserve( 1'000 );
for( size_t i = vi.capacity(); i--; )
vi.emplace_back( uid( mt ) );
unordered_map<void const *, size_t> addresses;
bool invert = argc > 1;
sort( vi.begin(), vi.end(),
[&, invert = invert]( int lhs, int rhs )
{
++addresses[&invert];
if( !invert )
return lhs < rhs;
else
return rhs < lhs;
} );
for( auto &pCount : addresses )
cout << pCount.first << ": " << pCount.second << endl;
}

I think for std::sort it would be the best to store the complete
state the lambda refers to as a static or thread_local variable
that the lambda itself doesn't need any state to be copied that
there's actually no parameter passed through the recursion.


devel / comp.lang.c++ / Lambda passed through a std::sort recursion

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor