Sunday, January 15, 2023

Digest for comp.lang.c++@googlegroups.com - 10 updates in 3 topics

quanzhou <anhonghe@gmail.com>: Jan 15 11:37AM -0800

auto [a, b, ...] = x;
auto [a, ..., b] = y;
auto [...,a, b] = z;
 
helps to get the last member variable of the right side type.
Bonita Montero <Bonita.Montero@gmail.com>: Jan 15 04:06PM +0100

Now it's perfect:
 
#include <iostream>
#include <unordered_set>
#include <charconv>
#include <random>
#include <concepts>
 
using namespace std;
 
int main( int argc, char** argv )
{
try
{
if( argc < 4 )
return
cout << argv[0] << " n from to" << endl,
EXIT_FAILURE;
auto parse = []( char const *str, char const *err )
{
size_t value;
if( from_chars_result fcr = from_chars( str, str + strlen( str ),
value ); (bool)fcr.ec || *fcr.ptr )
throw invalid_argument( err );
return value;
};
size_t n = parse( argv[1], "wrong number of values" );
if( !n )
return EXIT_SUCCESS;
size_t
from = parse( argv[2], "wrong from-value" ),
to = parse( argv[3], "wrong to-value" );
if( from > to )
swap(from, to);
if( n - 1 > to - from )
return
cout << "n is too small" << endl,
EXIT_FAILURE;
unordered_set<size_t> values;
mt19937_64 mt;
uniform_int_distribution<size_t> uid( from, to );
if( to - from != (size_t)-1 && to - from + 1 < n / 2 )
{
values.reserve( n );
while( values.size() < n )
{
size_t value;
do
value = uid( mt );
while( values.contains( value ) );
values.emplace( value );
}
}
else
{
if( to - from == (size_t)-1 )
throw bad_alloc();
values.reserve( to - from + 1 );
size_t i = from - 1;
do
values.emplace( ++i );
while( i != to );
while( values.size() > n )
for( ; ; )
{
auto itRemove = values.find( uid( mt ) );
if( itRemove == values.end() )
continue;
values.erase( itRemove );
break;
}
}
for( size_t value : values )
cout << value << endl;
}
catch( exception const &exc )
{
return
cout << exc.what() << endl,
EXIT_FAILURE;
}
}
 
If less than half of the range of numbers is selected (n < (to - from
+ 1) / 2) the unordered map is filled up to n elements and if an element
is found that's already there another value is chosen. If more than half
of the range is selected (n >= (to - from + 1) / 2) the set is filled up
to contain the whole range and the elements are incrementally removed at
a random value.
Ralf Goertz <me@myprovider.invalid>: Jan 15 04:47PM +0100

Am Sun, 15 Jan 2023 16:06:00 +0100
 
> Now it's perfect:

[snip]
 
> more than half of the range is selected (n >= (to - from + 1) / 2)
> the set is filled up to contain the whole range and the elements are
> incrementally removed at a random value.
 
What's wrong with a clean two line solution:
 
 
#include <algorithm>
#include <iostream>
#include <random>
#include <charconv>
#include <vector>
#include <cstring>
 
using namespace std;
 
int main( int argc, char** argv )
{
try
{
if( argc < 4 )
return
cout << argv[0] << " n from to" << endl,
EXIT_FAILURE;
auto parse = []( char const *str, char const *err )
{
size_t value;
if( from_chars_result fcr = from_chars( str, str + strlen( str ),
value ); (bool)fcr.ec || *fcr.ptr )
throw invalid_argument( err );
return value;
};
size_t n = parse( argv[1], "wrong number of values" );
if( !n )
return EXIT_SUCCESS;
size_t
from = parse( argv[2], "wrong from-value" ),
to = parse( argv[3], "wrong to-value" );
if( from > to )
swap(from, to);
if( n - 1 > to - from )
return
cout << "n is too small" << endl,
EXIT_FAILURE;
mt19937_64 mt;
vector<int> values( to - from );
// ------
//only two lines to find random subset of fixed size:
fill(values.begin(), values.begin() + n, 1);
shuffle(values.begin(), values.end(), mt);
// ------
for( size_t i=0; i < values.size(); ++i )
if( values[i]) cout << i+from << endl;
}
catch( exception const &exc )
{
return
cout << exc.what() << endl,
EXIT_FAILURE;
}
}
Bonita Montero <Bonita.Montero@gmail.com>: Jan 15 05:13PM +0100

Am 15.01.2023 um 16:47 schrieb Ralf Goertz:
>> the set is filled up to contain the whole range and the elements are
>> incrementally removed at a random value.
 
> What's wrong with a clean two line solution:
 
You might have less numbers than to - from + 1 (you omitted the 1 on
allocation of the vector).
Bart <bc@freeuk.com>: Jan 15 04:27PM

On 15/01/2023 15:06, Bonita Montero wrote:
> Now it's perfect:
 
You said that last time too:
 
On 08/01/2023 05:01, Bonita Montero wrote:
> Now it's perfect:
 
I guess it's more perfect?
Bonita Montero <Bonita.Montero@gmail.com>: Jan 15 05:42PM +0100

Am 15.01.2023 um 17:27 schrieb Bart:
 
> On 08/01/2023 05:01, Bonita Montero wrote:
> > Now it's perfect:
 
> I guess it's more perfect?
 
The code I've shown yet is also perfect in terms of performance,
but not just functionally.
Malcolm McLean <malcolm.arthur.mclean@gmail.com>: Jan 15 08:57AM -0800

On Sunday, 15 January 2023 at 15:48:01 UTC, Ralf Goertz wrote:
> EXIT_FAILURE;
> }
> }
 
Where N is the range and M the number of elements to choose, it's O(N)
in space and time. So unsuitable if N is very much larger than M.
gazelle@shell.xmission.com (Kenny McCormack): Jan 15 05:11PM

In article <tq14n7$2cgbf$1@dont-email.me>,
>Now it's perfect:
 
I just need 11,780 votes. Give me a break here.
 
--
If Jeb is Charlie Brown kicking a football-pulled-away, Mitt is a '50s
housewife with a black eye who insists to her friends the roast wasn't
dry.
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>: Jan 14 04:14PM -0800

On 5/12/2022 12:16 PM, Chris M. Thomasson wrote:
> Using my experimental vector field to generate a fractal formation. Here
> is generation two:
 
> https://fractalforums.org/gallery/1612-120522191048.png
 
An experiment... The music and geometry are mine, EXCEPT, the model of
the girl:
 
A test of the Thomasson Fractal System. Other models are welcome.
https://youtu.be/n13GHyYEfLA Using some of my experimental MIDI music...
Btw, the model for the girl can be found here: https://skfb.ly/6zGMG
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>: Jan 14 07:35PM -0800

On 1/4/2023 3:23 PM, Alf P. Steinbach wrote:
 
>> https://skfb.ly/oCqn7
 
> You should put this to music by Nirvana, In Bloom.
 
> - Alf
 
https://youtu.be/usADINi17cI
 
;^) ?
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: