Wednesday, December 7, 2016

Digest for comp.lang.c++@googlegroups.com - 25 updates in 8 topics

ram@zedat.fu-berlin.de (Stefan Ram): Dec 06 11:40PM

Today, I read a bit in an Ocaml manual which said
 
insert elt lst =
match lst with
[] -> [elt]
| head :: tail -> if elt <= head then elt :: lst else head :: insert elt tail
 
as a part of an insertation-sort program.
 
I was trying to translate that into C++, and got this:
 
main.cpp
 
#include <algorithm>
#include <forward_list>
#include <iostream>
#include <iterator> // std::ostream_iterator
#include <ostream>
#include <string>
 
using namespace ::std::literals;
 
template< class L, class I, class T >
void insert( L & lst, I bbegin, I begin, I end, T elt )
{ if( begin == end )lst.insert_after( bbegin, elt ); else
{ if( elt <= lst.front() )lst.insert_after( bbegin, elt ); else
insert( lst, ++bbegin, ++begin, end, elt ); }}
 
int main()
{ ::std::forward_list< int >list;
::insert( list, list.before_begin(), list.begin(), list.end(), 50 );
::insert( list, list.before_begin(), list.begin(), list.end(), 30 );
::insert( list, list.before_begin(), list.begin(), list.end(), 70 );
copy( list.begin(), list.end(), ::std::ostream_iterator< int >( ::std::cout, " " ));
::std::cout << '\n'; }
 
transcript
 
30 50 70
 
Can my C++ ::insert function be simplified?
ram@zedat.fu-berlin.de (Stefan Ram): Dec 07 12:13AM

>{ if( begin == end )lst.insert_after( bbegin, elt ); else
> { if( elt <= lst.front() )lst.insert_after( bbegin, elt ); else
 
PS: I think the comparison should be »elt <= *begin«.
ram@zedat.fu-berlin.de (Stefan Ram): Dec 07 10:37AM

>I don't really see the point in translating this using
>std::list/forward_list. More interesting would be a direct translation
>using some sort of cons-list (e.g., as in
 
Yes, I missed those too. But I stopped short of implementing
them myself, because I was afraid of not understanding the
possible memory-management issues involved. After all, I only
have used cons-lists in languages with a garbage collector
so far.
 
Maybe the shared pointer from the standard can help me here.
ram@zedat.fu-berlin.de (Stefan Ram): Dec 07 12:49PM

Do I get this right? I can use »{ 2 }« as an argument when
I want a more strict type handling? For example,
 
#include <cmath> // ::std::floor
#include <iostream> // ::std::cout
#include <ostream> // <<
#include <string> // ""s
 
using namespace ::std::literals;
 
int main(){ ::std::cout << ::std::floor( { 2.0 } )<< "\n"s; }
 
Is this deemed to be "good style"? If so, why does a clang
tool give me a warning?
 
main.cpp:8:42: warning: braces around scalar initializer [clang-diagnostic-braced-scalar-init]
int main(){ ::std::cout << ::std::floor( { 2.0 } )<< "\n"s; }
^
And below, do I say that I do not want implicit conversion
to int?
 
#include <cmath> // ::std::floor
#include <iostream> // ::std::cout
#include <ostream> // <<
#include <string> // ""s
 
using namespace ::std::literals;
 
int main(){ ::std::cout << ::std::floor( { 2 } )<< "\n"s; }
 
The messages of my compiler say
 
main.cpp: In function 'int main()':
main.cpp:8:48: error: call of overloaded 'floor(<brace-enclosed initializer list>)' is ambiguous
int main(){ ::std::cout << ::std::floor( { 2 } )<< "\n"s; }
^
I was expecting a message more like, »error: expected one
of float, double, or long double, but got int.«.
 
So, maybe, I still do not understand the meaning of such an
initializer clause as an argument?
bleachbot <bleachbot@httrack.com>: Dec 07 09:21PM +0100

ram@zedat.fu-berlin.de (Stefan Ram): Dec 07 09:38PM

>The messages of my compiler say
>main.cpp: In function 'int main()':
>main.cpp:8:48: error: call of overloaded 'floor(<brace-enclosed initializer list>)' is ambiguous
 
Another installation of the same compiler gave me exactly
the error message that I was expecting, i.e., that there is
narrowing. So, I now am somewhat confident that I got
the meaning right and the above error message might be
misleading.
bleachbot <bleachbot@httrack.com>: Dec 07 11:04PM +0100

Ramine <ramine@1.1>: Dec 07 05:06PM -0500

Hello.....
 
 
What's Wrong with the Actor Model
 
Read more here:
 
https://jaksa.wordpress.com/2015/10/13/whats-wrong-with-the-actor-model/
 
 
Thank you,
Amine Moulay Ramdane.
Christian Steins <cs01@quantentunnel.de>: Dec 07 07:56PM +0100

Hi,
the program from this tutorial video:
 
https://youtu.be/iCL6GYoi1RU
 
works a bit different on my system (GCC-TDM, Windows7):
 
C:\apps\CodeBlocks\Projects>.\a.exe
632059 632059
 
C:\apps\CodeBlocks\Projects>.\a.exe
632059 632059
 
So, on every run the same numbers are displayed.
 
How to get different random numbers on each run?
 
Version of my compiler: g++ (tdm64-1) 4.9.2
 
Christian
 
------------------------------------------------
// c++14 future test
 
#include <iostream>
#include <set>
#include <future>
#include <algorithm>
#include <random>
 
using namespace std;
 
auto make_sorted_random(const size_t num_elems)
{
std::set<int> retval;
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, num_elems-1);
 
std::generate_n( std::inserter(retval, retval.end()),
num_elems,
[&](){ return dis(gen); } );
 
return retval;
}
 
int main()
{
auto f1 = async(launch::async, make_sorted_random, 1000000);
auto f2 = async(launch::async, make_sorted_random, 1000000);
 
cout << f1.get().size() << ' ' << f2.get().size() << endl;
 
}
Ben Bacarisse <ben.usenet@bsb.me.uk>: Dec 07 09:14PM

Christian Steins <cs01@quantentunnel.de> writes:
<snip>
> How to get different random numbers on each run?
<snip>
> {
> std::set<int> retval;
> std::random_device rd;
 
std::random_device is not guaranteed to use a genuinely random source
and may yield the same sequence every time.
 
The simplest solution is to delete that line and seed the mt19937
object with the time. You should only seed a generator once, so you
need to make the mt19937 object static:
 
> std::mt19937 gen(rd());
 
static std::mt19937 gen(std::time(0));
 
(#include <ctime> at the top). You can use C++'s own std::crono support
but for simple testing, I just use the old time function.
 
> [&](){ return dis(gen); } );
 
> return retval;
> }
 
<snip>
--
Ben.
legalize+jeeves@mail.xmission.com (Richard): Dec 07 09:21PM

[Please do not mail me a copy of your followup]
 
Ben Bacarisse <ben.usenet@bsb.me.uk> spake the secret code
 
>The simplest solution is to delete that line and seed the mt19937
>object with the time. You should only seed a generator once, so you
>need to make the mt19937 object static:
 
Nit: static objects like this make unit testing more difficult.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Terminals Wiki <http://terminals-wiki.org>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
Christian Steins <cs01@quantentunnel.de>: Dec 07 11:00PM +0100

Am 07.12.2016 um 22:14 schrieb Ben Bacarisse:
 
>> std::random_device rd;
 
> std::random_device is not guaranteed to use a genuinely random source
> and may yield the same sequence every time.
 
Ok, and by checking rd.entropy() I can find out if it's a real random
device (entropy > 0) or not.
 
In my case it returns 0.
 
Christian
Daniel <danielaparker@gmail.com>: Dec 07 01:52PM -0800

Code below compiled with vs2015:
 
#include <vector>
 
struct SomeTraits
{
template <class T>
using vec = std::vector<T, std::allocator<T>>;
};
 
template <class Traits>
struct A
{
 
typedef typename Traits::vec<int> v1_t; // (1)
 
using v2_t = typename Traits::vec<int>; // (2)
};
 
int main()
{
typedef SomeTraits::vec<int> v_t1;
using v_t2 = SomeTraits::vec<int>;
 
v_t1 v1; // OK
v_t2 v2; // OK
 
A<SomeTraits> a; // Not OK
}
 
Instantiating A<SomeTraits> results in "syntax error: '<'" for both (1) and (2).
Is that expected?
 
Thanks,
Daniel
Ramine <ramine@1.1>: Dec 07 03:23PM -0500

Hello.......
 
I was thinking more..
 
I think the USL methodology can be used this way on multicores:
 
If you test with my USL tools and it gives you a small coefficient
Alpha and Beta of USL law, so the probability to hit the contention at
3X more is smaller, so you can predict scalability up to 3X.
 
If you test with my USL tools and it gives a much bigger coefficient
Alpha and Beta, so you are hitting contention more, and it will not scale.
 
Also i have easy the job for you by compiling my Universal Scalability
Law for Delphi and FreePascal programs to windows 32bit and 64bit
executables, you will find them inside the zip file, they are really
fantastic and powerful, they are GUI and command line, please read about
them and download them from here, i have just formatted correctly the
webpage:
 
https://sites.google.com/site/aminer68/universal-scalability-law-for-delphi-and-freepascal
 
 
Thank you,
Amine Moulay Ramdane.
ruben safir <ruben@mrbrklyn.com>: Dec 06 09:14PM -0500

On 12/06/2016 06:25 PM, Paavo Helde wrote:
> What you mean by "correct"? That the big-endian convention follows the
> convention for hand-written numbers, at least in the Western cultural
> tradition?
 
No it is correct because that is how 13 would be written
 
00 00 00 0d
 
> This was probably the initial motivation behind the
> big-endian format (?)
 
> By the way, this is all explained on the Wikipedia page with graphs!
 
it sucks, and so do its graphs. In fact, I'm at a point with wikipedea
that it might be /dev/nulled the ip addresses to it and the dns entries.
ruben safir <ruben@mrbrklyn.com>: Dec 06 09:15PM -0500

On 12/06/2016 09:14 PM, ruben safir wrote:
 
>> By the way, this is all explained on the Wikipedia page with graphs!
 
> it sucks, and so do its graphs. In fact, I'm at a point with wikipedea
> that it might be /dev/nulled the ip addresses to it and the dns entries.
 
https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Data/endian.html>
scott@slp53.sl.home (Scott Lurndal): Dec 07 01:35PM

> return n;
> }
 
>(You also have to watch out for buffer overflows.)
 
I'd read it as a 32-bit int then byteswap it:
 
static inline uint32
swap32(uint32 value)
{
__asm__ __volatile__ ("bswap %0": "=a"(value): "0"(value));
return value;
}
 
or
 
static inline uint32
swap32(uint32 value)
{
return __builtin_bswap32(value);
}
Gareth Owen <gwowen@gmail.com>: Dec 07 06:02PM

>> tradition?
 
> No it is correct because that is how 13 would be written
 
> 00 00 00 0d
 
Sheesh.
legalize+jeeves@mail.xmission.com (Richard): Dec 07 06:46PM

[Please do not mail me a copy of your followup]
 
David Brown <david.brown@hesbynett.no> spake the secret code
 
>[...] (Incidentally, use hex
>for this sort of thing - octal had no place in computing outside of
>"chmod" since the 1970's.)
 
As with chmod, it only has a place when groups of 3 bits are
significant.
 
You're right that it's awkward to express bytes as octal because
bytes are 8 bits, not 6 or 9.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Terminals Wiki <http://terminals-wiki.org>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
scott@slp53.sl.home (Scott Lurndal): Dec 07 07:48PM

>>"chmod" since the 1970's.)
 
>As with chmod, it only has a place when groups of 3 bits are
>significant.
 
Not to mention that chmod has supported human readable arguments
for the last two decades plus:
 
chmod u+w,g-w,o=x file
kushal bhattacharya <bhattacharya.kushal4@gmail.com>: Dec 07 03:52AM -0800

hi,
I am trying to run thread in background.My scenario is first i accept value as command line argument to create number of connect client in n number of threads(where n is the value from command line).Then i take user input for publishing message to that connected clients.(this function is also run in n separate threads the connect client threads have stopped by now).the function has infinite while loop where sleep() is implemented and the message is published to console at a certain timeout.sleep is done after the message is published so the execution jumps accross n threads publishing message.The thing i now want is at the same time i want to provide user input to the console .With this architecture i can't really have anything in mind to take user input simultaneously now.How can i do that.Should i use daemon threads? and if so how do i implement that.
Thanks,
Kushal
Paavo Helde <myfirstname@osa.pri.ee>: Dec 07 03:35PM +0200

On 7.12.2016 13:52, kushal bhattacharya wrote:
> I am trying to run thread in background.My scenario is first i accept value as command line argument to create number of connect client in n number of threads(where n is the value from command line).Then i take user input for publishing message to that connected clients.(this function is also run in n separate threads the connect client threads have stopped by now).the function has infinite while loop where sleep() is implemented and the message is published to console at a certain timeout.sleep is done after the message is published so the execution jumps accross n threads publishing message.The thing i now want is at the same time i want to provide user input to the console .With this architecture i can't really have anything in mind to take user input simultaneously now.How can i do that.Should i use daemon threads? and if so how do i implement that.
> Thanks,
> Kushal
 
The console is shared by the whole process and this creates a lot of
problems when multiple threads try to read it. In my experience the
cleanest solution is to have an extra dedicated thread for console input
(and also output, if you want it to be displayed nicely). This thread
should sit in an infinite loop and wait for messages/commands from other
threads. When any other thread wants to get some input from the user
then it sends a message/command to the console thread, which outputs the
question/prompt, then reads in the user input and sends it back to the
original thread. Then it goes back to waiting in the infinite loop.
 
If the user wants to give some command while no thread is asking
anything, he should press Ctrl-C. This should be handled in yet another
thread (see SetConsoleCtrlHandler() and/or sigwait()) which communicates
with the console thread via the exact same mechanism.
 
To make this post on-topic: in C++ one can conveniently use std::deque
(of course, properly mutex-protected) for such inter-thread message queues.
 
hth
Paavo
Melzzzzz <mel@zzzzz.com>: Dec 07 01:08AM +0100

On 6 Dec 2016 23:40:45 GMT
> elt tail
 
> as a part of an insertation-sort program.
 
> Can my C++ ::insert function be simplified?
Functional languages have great expressive power when working with
lists.
 
 
Haskell:
 
isort :: Ord a => [a] -> [a]
isort xs = foldr insert [] xs
where
insert x [] = [x]
insert x (y:ys) = if x<y then x:y:ys
else y: insert x ys
 
msort :: Ord a =>[a] -> [a]
msort xs
| n < 2 = xs
| otherwise = merge (msort x1s) (msort x2s)
where
n = length xs
(x1s,x2s) = splitAt (n`quot`2) xs
merge xs ys = case (xs,ys) of
([], ys') -> ys'
(xs', []) -> xs'
(x:xs',y:ys') | x < y -> x : merge xs' ys
| otherwise -> y : merge xs ys'
 
--
press any key to continue or any other to quit
Marcel Mueller <news.5.maazl@spamgourmet.org>: Dec 07 01:23AM +0100

On 07.12.16 00.40, Stefan Ram wrote:
> { if( begin == end )lst.insert_after( bbegin, elt ); else
> { if( elt <= lst.front() )lst.insert_after( bbegin, elt ); else
> insert( lst, ++bbegin, ++begin, end, elt ); }}
 
This is buggy as lst.front() will not take care of the incremented begin
iterator on recursive calls. It just happens to work for your test case.
 
> Can my C++ ::insert function be simplified?
 
Well, in C++ probably nobody would write this as a recursion.
But, of course, this could be done.
 
At least you could eliminate the parameters begin and end and the
duplicate insert_after.
 
void insert(L& lst, I bbegin, T elt)
{ I old_begin = bbegin++;
if (bbegin == lst.end() || elt <= *bbegin)
lst.insert_after(old_bbegin, elt);
else
insert(lst, bbegin, elt);
}
 
But as already mentioned, a simple while loop would do the job even
better and eliminates the bbegin parameter too.
 
OK, if the compiler can handle TCO the generated code might be almost
the same and not recursive in both cases. But it cannot eliminate the
additional bbegin parameter at the initial call from main unless
everything is inlined.
 
 
Marcel
Ben Bacarisse <ben.usenet@bsb.me.uk>: Dec 07 03:26AM


> transcript
 
> 30 50 70
 
> Can my C++ ::insert function be simplified?
 
template<class L, class T>
void insert(L &lst, T elt)
{
lst.merge({elt});
}
 
?
 
--
Ben.
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: