472,954 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Peer Review Request - BinaryTransform Iterator

I need an interative form of the std::transform algorithm. In other words,
instead of letting transform do its work and accessing the results
afterwards, I need to access each transformation as it occurs. The reason is
that if I rely solely on std::transform, I would have to make several calls
to it storing the results in buffers. While this would look nice, it implies
a lot of looping in the background. So I've written a
"BinaryTransformIterator." The idea is to run several of these iterators in
one loop, manipulating and combining the results as I go along. The hope is
that this will make my code more concise. However, at this point this
exercise is more of an experiment; I'm just playing around at present. I'm
not sure yet if it's useful.

At any rate, I'm hoping for comments and about the correctness of the code
and any suggestions for improving it.

template<typename InputIterator1,
typename InputIterator2,
typename BinaryFunction>
class BinaryTransformIterator :
public std::iterator<std::input_iterator_tag,
typename BinaryFunction::result_type>
{
private:
typedef BinaryTransformIterator<InputIterator1,
InputIterator2,
BinaryFunctionself;

InputIterator1 myFirst1;
InputIterator1 myLast1;
InputIterator2 myFirst2;
BinaryFunction myFunction;

value_type result;

public:
BinaryTransformIterator()
{
myFirst1 = myLast1;
}

BinaryTransformIterator(InputIterator1 first1,
InputIterator1 last1,
InputIterator2 first2,
BinaryFunction function = BinaryFunction()) :
myFirst1(first1),
myLast1(last1),
myFirst2(first2),
myFunction(function)
{
if(!IsDone())
{
result = myFunction(*myFirst1, *myFirst2);
}
}

bool operator==(const self &other)
{
return this->IsDone() == other.IsDone() ? true :
(this->myFirst1 == other.myFirst1) &&
(this->myLast1 == other.myLast1) &&
(this->myFirst2 == other.myFirst2);
}

bool operator!=(const self &other)
{
return !(*this == other);
}

value_type operator*()
{
assert(!IsDone());

return result;
}

self &operator++()
{
assert(!IsDone());

++myFirst1;
++myFirst2;

if(!IsDone())
{
result = myFunction(*myFirst1, *myFirst2);
}

return *this;
}

self operator++(int)
{
assert(!IsDone());

self temp = *this;

++myFirst1;
++myFirst2;

if(!IsDone())
{
result = myFunction(*myFirst1, *myFirst2);
}

return temp;
}

private:
bool IsDone() const
{
return myFirst1 == myLast1;
}
};

// TEST

typedef BinaryTransformIterator<float *, float *, std::multiplies<float
iterator;

int main(void)
{
float buffer1[] = { 1, 2, 3, 4, 5 };
float buffer2[] = { 2, 2, 2, 2, 2 };

iterator first(&buffer1[0], &buffer1[5], buffer2);
iterator last;

for(int i = 0; first != last; i++)
{
assert(*first == buffer1[i] * buffer2[i]);

++first;
}

return 0;
}
Jul 28 '08 #1
0 977

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

Similar topics

0
by: Molly | last post by:
Peace, Pythonphiles! Python's std lib BaseHTTPServer: I'm stuck with a nasty spoiler -- I get erratic "Network error: disconnected by peer" for _POST_ requests only. =Background= I'm...
13
by: cruiserweight | last post by:
I am a web designer working in a virtual vacuum. Any and all feedback for my latest project would be immensely appreciated. I am assuming this kind of request is okay. If not please advise. The...
6
by: Jan Roland Eriksson | last post by:
For those interested, read and rip it up as you wish :-) ===== Archive-name: www/stylesheets/newsgroup-faq Posting-Frequency: twice a week (mondays and thursdays) Last-modified: July 20, 2004...
3
by: ma740988 | last post by:
Faced with the need to instrument/log data I've decided to create an instrumentation/log class. My initial thoughts (baby steps) on this instrumentation/log class is as follows: 1. Time Stamping...
0
by: Rick | last post by:
Hello, I'm six months into asp.net/c# and enjoying it. A couple weeks ago I needed to write a "simple" high performance counter, the short story is it turned into a mini project. Many lesson...
1
by: Rick | last post by:
Hello, I'm six months into asp.net/c# and enjoying it. I needed to code a "simple" high performance counter... a couple weeks later and a lot of learning I think I did it. Many lesson lessons...
0
by: Larry Serflaten | last post by:
I am planning to share a Cards.DLL out on Planet Source Code and GotDotNet. But before I send it out to the public I would like to get a peer review to be sure it works as intended, and to avoid...
21
by: Johan Tibell | last post by:
I would be grateful if someone had a minute or two to review my hash table implementation. It's not yet commented but hopefully it's short and idiomatic enough to be readable. Some of the code...
0
by: Avinash Vora | last post by:
On Aug 4, 2008, at 10:44 AM, Trevor Slocum wrote: I've had this problem too. Well, I have about the same level of experience that you do, but I'll take a look at the code and see if I can...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
1
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.