Friday, August 14, 2015

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

Paul <pepstein5@gmail.com>: Aug 14 02:57AM -0700

Please could someone tell me the simplest way to count the number of unique values in a container when the values are not necessarily consecutive?
 
For example,
 
std::unordered_map<int, int> example;
example[5] = 3;
example[1] = 7;
example[0] = 3;
 
If we apply the desired function to example, we would get 2 because there are two distinct values -- 3 and 7.
 
The way I know how to do this simply is to insert or copy into a std::set and apply size() to the set but that seems inefficient and inelegant.
 
Thank you,
 
Paul
c.milanesi.bg@gmail.com: Aug 14 06:18AM -0700

Paul wrote:
 
> If we apply the desired function to example, we would get 2 because there are two distinct values -- 3 and 7.
 
> The way I know how to do this simply is to insert or copy into a std::set
> and apply size() to the set but that seems inefficient and inelegant.
 
I think it is very elegant, and rather efficient.
 
If you use an unordered_set, it is probably more efficient.
 
Which solution is more elegant than this one?
 
std::unordered_set<int> s;
for (auto& pair: example)
s.insert(pair.second);
 
--
Carlo Milanesi
Barry Schwarz <schwarzb@dqel.com>: Aug 14 11:06AM -0700

On Fri, 14 Aug 2015 02:57:31 -0700 (PDT), Paul <pepstein5@gmail.com>
wrote:
 
>Please could someone tell me the simplest way to count the number of unique values in a container when the values are not necessarily consecutive?
 
>For example,
 
>std::unordered_map<int, int> example;
 
Would the issue in question be any different if example had type
std::map<int,int>
instead of std::unordered_map?
 
 
--
Remove del for email
Kate Sendall <kate.alicia.sendall@googlemail.com>: Aug 14 06:25AM -0700

Hello all,
 
I failed a project for my on-line course about 2 months ago, and just received a letter with a new brief (simpler than the first, which was kind of the Board). It's still in my amateur opinion a pretty tricky brief. I only just started learning C++ and I still struggle with basic concepts of programming, unfortunately.
 
Basically, I'm asking for your help - I DON'T WANT CODE FROM YOU. But I need some serious pointers (ha, pointers. Get it??). My tutors are on leave until the 16th, and my deadline is fast approaching. I need to get something DOWN that WORKS. Otherwise I will have to postpone my second year with them so that I can retake the programming class for a year... which might not be too bad, in all honesty. It would give me time to learn the language and get super good at it. But there's a price tag to postponing. :(
 
 
The brief is pretty straightforward: I am required to write a simple 'Turtle' graphics program that can do three major things. First, is allow the user to change its parameters so that the spirolateral colour, size, length of segments and/or iterations can change. Second, is to have several preset spirolaterals that the user can choose. Finally, is the ability to save the resulting spirolateral as an image file.
 

I'm using codeblocks, and have imported all my libraries and stuff for SDL2. I'm using the Lazyfoo website to guide me on how to do the basics, such as opening a window, splitting it into viewports etc so I can have my user input sliders and whatnot on the right hand side. I know the rules behind spirolaterals, but I'm not sure how to implement it into a c++ program with SDL2, haha. I also have NO idea how to produce lines from dots or points. The SDL documentation is hard to follow, and the descriptions on the website are pretty vague, and not written for beginners like myself.
 
As much as I hate to admit it, programming isn't coming naturally to me - I am really finding it hard to understand and follow basic ideas. I passed my mathematics exam with flying colours - almost a 'first' - I'm not stupid. But programming is catching me out. I always have so many questions, and I can't find any answers.
 
Thanks for taking the time to read and hopefully write back to me! :)
 
Kate L.
JiiPee <no@notvalid.com>: Aug 14 01:22AM +0100

On 13/08/2015 22:43, Paul wrote:
> }
 
 
 
> return {min, max};
> }
 
I think you can just use
 
std::minmax_element : http://en.cppreference.com/w/cpp/algorithm/minmax_element
Melzzzzz <mel@zzzzz.com>: Aug 14 02:36AM +0200

On Fri, 14 Aug 2015 01:22:09 +0100
 
> I think you can just use
 
> std::minmax_element :
> http://en.cppreference.com/w/cpp/algorithm/minmax_element
 
He uses int's. That means in his version compiler has opportunity to
use (v)pminsd/(v)pmaxsd instructions to find minimum and maximum
because he is not interested in index rather just values.
I wouldn't be surprised that on SSE4.1/AVX2 machine his version would
perform at least twice as fast as iterator version.
Melzzzzz <mel@zzzzz.com>: Aug 14 02:38AM +0200

On Fri, 14 Aug 2015 02:36:39 +0200
> because he is not interested in index rather just values.
> I wouldn't be surprised that on SSE4.1/AVX2 machine his version would
> perform at least twice as fast as iterator version.
 
But he has to change index type not to be unsigned rather same type as
vec.size() returns. I discovered that that prevents this type of
optimization(s).
JiiPee <no@notvalid.com>: Aug 14 02:18AM +0100

On 14/08/2015 01:38, Melzzzzz wrote:
 
>> He uses int's. That means in his version compiler has opportunity to
>> use (v)pminsd/(v)pmaxsd instructions to find minimum and maximum
>> because he is not interested in index rather just values.
 
but I guess it depends on how massively he uses that function. If there
is only minor benefit then might as well use
 
minmax_element. They say optimization should be done last and only if clear benefit, right? so depends on how heavily he uses it...
 
 
 
> But he has to change index type not to be unsigned rather same type as
> vec.size() returns. I discovered that that prevents this type of
> optimization(s).
 
better maybe:
for(const auto a : vec)
{
 
if(a < min)
min = a;
else if (a > max)
max = a;
 
}
I guess the compiler here would chose the best type, right? auto knows
what is the best type....
Melzzzzz <mel@zzzzz.com>: Aug 14 05:10AM +0200

On Fri, 14 Aug 2015 02:18:37 +0100
> there is only minor benefit then might as well use
 
> minmax_element. They say optimization should be done last and only if
> clear benefit, right? so depends on how heavily he uses it...
 
According to my testing (i7 4790) with gcc it is faster 1.5 times then
minmax_element and with clang 1.9 times.
 
 
> }
> I guess the compiler here would chose the best type, right? auto
> knows what is the best type....
 
Yes, this is version I tested.
Melzzzzz <mel@zzzzz.com>: Aug 14 05:21AM +0200

On Fri, 14 Aug 2015 05:10:42 +0200
> > >>> http://en.cppreference.com/w/cpp/algorithm/minmax_element
 
> According to my testing (i7 4790) with gcc it is faster 1.5 times then
> minmax_element and with clang 1.9 times.
That is with larger than cache, vector.
But if in L1 cache speed is about same . I think that compilers produce
same optimization in both cases but somehow minmax_element gets worse
access times on large vector.
Victor Bazarov <v.bazarov@comcast.invalid>: Aug 13 11:39PM -0400

On 8/13/2015 11:21 PM, Melzzzzz wrote:
> But if in L1 cache speed is about same . I think that compilers produce
> same optimization in both cases but somehow minmax_element gets worse
> access times on large vector.
 
But that's not what the OP is after, methinks. Paul needs the largest
difference between two adjacent elements of the same vector if sorted.
Doesn't minmax give the opposite ends of the sorted vector?
 
V
--
I do not respond to top-posted replies, please don't ask
Melzzzzz <mel@zzzzz.com>: Aug 14 05:52AM +0200

On Thu, 13 Aug 2015 23:39:59 -0400
 
> But that's not what the OP is after, methinks. Paul needs the
> largest difference between two adjacent elements of the same vector
> if sorted. Doesn't minmax give the opposite ends of the sorted vector?
 
Yes, but this is just first step in algorithm. That is, finding min/max
values.
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: