473,396 Members | 1,942 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,396 software developers and data experts.

is there any transform_if algorithm?

hi, i need to implement several functions which are similar with
transform, but just oprerate on the elements that match some condition,
i have wirtten one by myself:

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

but it is something subtle:

1). if the prediction return false? does output iterator increase?
2). how about exception safe?
3). efficiency problem, if we can predict the input will match the
codition, then we have gotten the information that functor will do.

my question is there any better way?

Dec 15 '05 #1
8 6315
"baibaichen" <ba********@gmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
hi, i need to implement several functions which are similar with
transform, but just oprerate on the elements that match some condition,
i have wirtten one by myself:

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

but it is something subtle:

1). if the prediction return false? does output iterator increase?
2). how about exception safe?
3). efficiency problem, if we can predict the input will match the
codition, then we have gotten the information that functor will do.

my question is there any better way?


I'm not sure what your function could do that can't be done with
std::transform. Consider:

class func
{
public:
int operator () (int xj, int yj) const
{
if (xj == 2)
return yj; // don't change the output value
else
return 2 * xj; // change the output value
}
};

std::transform(x, x+3, y, y, func());

does the same thing as:

class pred
{
public:
bool operator () (int xj) const {return xj != 2;}
};

class op
{
public:
int operator () (int xj) const {return 2 * xj;}
};

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

transform_if(x, x+3, y, pred(), op());

The trick is to send the output stream to the functor along with the input
stream.

--
Cy
http://home.rochester.rr.com/cyhome/
Dec 15 '05 #2
no, it is TOTALly different when the Out template parameter is just
output iterator,i.e. the insert_iterator and ostream_iterator, their
oprerator++ do nothing!!

here is "it is something subtle" in my previous post,i.e. if the
prediction return false, does i need increase outpout iterator? it is
diffcult to answser, consider:
1. if the output equal input, i.e. i write back to input iterator, i
need increase
2. if output is a real iterator, such as a vector.begin() and that
vector has enough space, i can not increase.
3. if the ouput is something like insert_iterator or ostream_iterator,
i do not care..

the better way what i can image is the input template parameter is
something like filter iterator, so we do not need tramsform_if, but i
can not find any useful inforamtion about it.

Dec 15 '05 #3

baibaichen wrote:
hi, i need to implement several functions which are similar with
transform, but just oprerate on the elements that match some condition,
i have wirtten one by myself:

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

but it is something subtle:

1). if the prediction return false? does output iterator increase?
2). how about exception safe?
3). efficiency problem, if we can predict the input will match the
codition, then we have gotten the information that functor will do.

my question is there any better way?


Dunno if this will help you, but you can implement the transform_if in
terms of remove_copy_if and for_each (or transform).
Maybe this is the reason why there is no transform_if.

--
KM

Dec 15 '05 #4
really? could u write more details?

thanks

Dec 15 '05 #5

baibaichen wrote:
hi, i need to implement several functions which are similar with
transform, but just oprerate on the elements that match some condition,


Check the VTL. It's a view library, and one of the components offers
a filtered view over a collection. If you use that view as an input to
transform,
you're done. Another solution is to use an iterator wrapper around your
output iterator that applies the transformation. Do both, and you can
even
use std::copy()

HTH,
Michiel Salters

Dec 15 '05 #6
On 2005-12-15, baibaichen <ba********@gmail.com> wrote:
no, it is TOTALly different when the Out template parameter is
just output iterator,i.e. the insert_iterator and
ostream_iterator, their oprerator++ do nothing!!


That need not concern you. Algorithm transform will do the right
thing. As long as the algorithm you're using accepts the type of
iterator you pass in, all will be well.

--
Neil Cerutti
Dec 15 '05 #7

baibaichen wrote:
really? could u write more details?

thanks


Supposing "p" is the predicate you want the elements to fulfill and
"op" the operation you want to apply, you can write something like:
remove_copy_if(iter1.begin(), iter1.end(), iter2.begin(), not1(op));
transform(iter2.begin(), iter2.end(), iter2.begin(), p);
The remove_copy_if will remove all elements from iter1 that do not
match your predicate and copy them to iter2 (i.e., it will copy all
elements that DO match your predicate).
After that just execute transform on iter2 copying it to itself (hence
why i said it maybe might be better to use for_each).
--
KM

Dec 15 '05 #8
i have tested VTL before, but unfortunately , i can not compile it. :(

Dec 16 '05 #9

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

Similar topics

6
by: Jack Smith | last post by:
Hello, any help appreciated with following problem. I figured out the algorithm (I think), just having trouble proving it is optimal. Suppose we are given n tasks each of which takes 1 unit...
16
by: cody | last post by:
I have to write an algorithm with must ensure that objects are put in buckets (which are always 4 in size). The objects have two properties: A and B. It is not allowed that in a bucket are objects...
10
by: bpontius | last post by:
The GES Algorithm A Surprisingly Simple Algorithm for Parallel Pattern Matching "Partially because the best algorithms presented in the literature are difficult to understand and to implement,...
32
by: Cmorriskuerten | last post by:
HI, is this is this solution to test if a number is a prime number or not: /* * Is n a prime number? * Return TRUE (1): n is a prime number * Return FALSE (0): n is a *not* a prime number...
113
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same...
4
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
2
by: Julio C. Hernandez Castro | last post by:
Dear all, We have just developped a new block cipher called Raiden, following a Feistel Network structure by means of genetic programming. Our intention now consists on getting as much feedback...
0
by: aruna | last post by:
hey guys i earlier had very valuable responses from you all for base64 encoding algorithm.so thank for that. so now i need your assistance to do a float encoding algorithm. you may wonder why i'm...
1
by: almurph | last post by:
Hi everyone, Concerning the Needleman-Wunsch algorithm (cf. http://en.wikipedia.org/wiki/Needleman-Wunsch_algorithm) I have noticed a possible loop. Inside the algorithm there is an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.