473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

std::partition

Is there any more hands on way to make a copy of a vector where pred is
TRUE.(in the code snip)
And I need a copy becourse OnFX might or might not alter List.

typedef std::vector<CS *> LIST;
LIST List;
LIST::iterator et=std::partition(List.begin(),List.end(),pred);
LIST l;
for(LIST::iterator i=List.begin();i !=et;i++)
l.push_back(*i);
std::for_each(l.begin(),l.end(),std::mem_fun(&CS:: OnFX));

//thx
//jota
Jul 22 '05 #1
6 2937
jota wrote:
Is there any more hands on way to make a copy of a vector where pred
is TRUE.(in the code snip)
And I need a copy becourse OnFX might or might not alter List.

typedef std::vector<CS *> LIST;
LIST List;
LIST::iterator et=std::partition(List.begin(),List.end(),pred);
LIST l;
for(LIST::iterator i=List.begin();i !=et;i++)
l.push_back(*i);
std::for_each(l.begin(),l.end(),std::mem_fun(&CS:: OnFX));


It seems that a copy_if function was forgotten in the standard library,
but you can easily write your own:

(from TC++PL)

template<class In, class Out, class Pred>
Out copy_if(In first, In last, Out res, Pred p)
{
while (first != last) {
if (p(*first)) *res++ = *first;
++first;
}
return res;
}

Then you can do:

copy_if(List.begin(), List.end(), std::back_inserter(l), pred);

Jul 22 '05 #2
Rolf Magnus <ra******@t-online.de> wrote:
It seems that a copy_if function was forgotten in the standard library,
It is spelled 'std::remove_copy_if()' and you need to negate the predicate.
but you can easily write your own:


Of course, this "easy" implementation is inferior to a really good
implementation. Even for something simple as this it is possible to
implement optimizations, e.g. loop unrolling (what the compiler does
for you is just a small step) and the segmented sequence optimization
(which of course applies only if segmented sequence come into the play).
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
Jul 22 '05 #3
Dietmar Kuehl wrote:
Rolf Magnus <ra******@t-online.de> wrote:
It seems that a copy_if function was forgotten in the standard
library,


It is spelled 'std::remove_copy_if()' and you need to negate the
predicate.


Stroustrup says something else. He writes in his book that copy_if _was_
forgotten, and gives the code that I wrote down as alternative.

Jul 22 '05 #4
> Stroustrup says something else. He writes in his book that copy_if _was_
forgotten, and gives the code that I wrote down as alternative.

And I used it, and it worked just fine!
'std::remove_copy_if()' seems to be backwords of what te real world needs!
//jota

Jul 22 '05 #5

"Dietmar Kuehl" <di***********@yahoo.com> wrote in message
news:5b**************************@posting.google.c om...
Rolf Magnus <ra******@t-online.de> wrote:
It seems that a copy_if function was forgotten in the standard
library,
It is spelled 'std::remove_copy_if()' and you need to negate the predicate.
but you can easily write your own:
Of course, this "easy" implementation is inferior to a really good
implementation. Even for something simple as this it is possible to
implement optimizations, e.g. loop unrolling (what the compiler does
for you is just a small step) and the segmented sequence

optimization (which of course applies only if segmented sequence come into the

play).

Hi Dietmar,

Would you explain what the segmented sequence optimization is? My
googling only turns up two other references (both by you) which assume
the reader already knows what it is.
It sounds interesting. ...

Jonathan


Jul 22 '05 #6
Jonathan Turkanis wrote:
Would you explain what the segmented sequence optimization is? My
googling only turns up two other references (both by you) which assume
the reader already knows what it is.


I thought I explained it in some Usenet articles already. However, here
we go:

There are a bunch of segmented sequences in the standard library and the
user might provide own containers which might also be segmented. With a
segmented sequence I mean something which is split into some lower level
segments. 'std::deque' is the obvious example: it is a sequence which is
actually represented by an array of subsequences, i.e. an array of segments.
Other examples include 'std::[io]streambuf_iterator's (the segments here are
the internal character buffers), certain hashes (the segments are the
buffers), etc. The problems with these sequences is that iterators typically
need to do additional checks and compilers cannot really remove them because
they don't know about the structure.

The idea of the optimization is rather simple: instead of processing the
sequence uniformly, the segments are individually processed. That is, the
algorithm actually just iterates over the segments and delegates processing
of the segments to an algorithm using lower level iterators. For example,
rather than using 'std::deque<T>::iterator', the segments can be processed
using 'T*' which is much faster on most systems.

The major trick is to define an interface which tells the algorithm that it
is supposed to handle a segmented sequence and how to access the individual
segments. I don't have a complete implementation of this stuff available
but when I used it, I used something like 'segment_traits' which provided
two kinds of iterators:
- an iterator for the segments
- an iterator for the contents of a segment

The segment iterator would be obtained from the passed 'begin()' and 'end()'
iterators and provide access to the begin and end of the current segment. I
just looked at my 'for_each()' which provides this optimization but it is
pretty confusing. You can have a look at it at
<http://cvs.sourceforge.net/viewcvs.py/cxxrt/cxxrt-source/include/cxxrt/for_each.h?view=markup>.
The actual logic is hidden in the '_Cxxrt_iterhelper' (which is also
available from the CVS). Since then I have decided for myself that
optimizations like this cannot really be factored out into class but that
algorithms should aggressively reuse other algorithms and I would implement
the optimization now right in one algorithms which is internally used by
other algorithms.

Matt Austern coined the term "segmented sequences" and gave a presentation
on the idea at a workshop on Generic Programming at Schloß Dagstuhl. He
come up with the idea in the context of hashes he was implementing.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jason Heyes | last post by:
What is a good way of removing elements from std::vector so that the elements removed satisfy a predicate and end up stored in another std::vector. It seems as though the algorithm std::remove_if...
2
by: Matthias Kaeppler | last post by:
Hello, I am having trouble getting std::equal_range to perform as I wish, and I can't find my error. Here's the situation: I have a vector of pointers to filesystem path objects (a file list,...
18
by: Matthias Kaeppler | last post by:
Hi, in my program, I have to sort containers of objects which can be 2000 items big in some cases. Since STL containers are based around copying and since I need to sort these containers quite...
1
by: Daniel J Watkins | last post by:
Hi, When I attempt to resize a column in a 2d vector I receive the error listed below. I'm using the libraries supplied with VxWorks. 0xf3fe450 (tExcTask): memPartFree: invalid block...
5
by: Yannick Tremblay | last post by:
Hi, I have a std::map<id, timestampcollection. The id is the normal reference so the key-value ordering is correct. However, once in a blue moon, I want to prune this collection. So that...
82
by: Peter Olcott | last post by:
I need std::vector like capability for several custom classes. I already discussed this extensively in the thread named ArrayList without Boxing and Unboxing. The solution was to simply create...
2
by: William | last post by:
for example, there is an array: int a={2, 3, 1,3, 2,1}; i want to group it as: {2,2,3,3,1,1} , not care the order. i can use std::sort( &a, &a); but this will also sort the array which will get...
15
by: Lambda | last post by:
I'm writing a function to partition a vector according to a predicate. For example: vector<string>::size_type partition(vector<Student_info&v) { vector<string>::size_type i, j; i = -1; j =...
10
by: ikarus | last post by:
Hello C++ Gurus! I'm comparing sorting algorithm for study goals. I've compared STL std::sort and hand-coded introsort on millions (tens of millions) of integers array sorting. It was tested...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.