473,479 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Changing the direction of bi-directional iterator

Hello,

could you please help me with following problem:

I have reversible container which holds (unsorted) instances of objects
with common base class. From time to time I need to search for last
occurance of instance meeting some criteria. When I find it I need to
move all folowing instances ("tail") into different cointainer (of same
type) while preserving their order.

I'm using find_if with container's reverse_iterators for the search, but
don't know how to preform the movement. If I use container constructor
with reverse_iterators the order of items in new container would be
reversed. This can be solved by reversing the container, but it might
take time if the tail has many items.

So my question is: "Is there any way how to do this (except own
implementation of course)?" Or more generally: "Is there any way how to
create normal (forward) iterator from reverse_iterator?"

Thanks in advance, Ales

----- ilustrative code -----

class base_t;

class container_t: list<base_t>

bool test(base_t& item);

container_t tail(container_t& c)
{
c::reverse_iterator found = find_if(c.rbegin(), c.rend(), test);
if (found == c.rend())
return container_t();
container_t result(c.rbegin(), found); // this is reversed tail
reverse(result.begin(), result.end()); // this takes time
return result;
}

Jul 22 '05 #1
5 1809
On Sun, 02 May 2004 02:06:53 +0200, AlesD <al****@seznam.cz> wrote:
Hello,

could you please help me with following problem:

I have reversible container which holds (unsorted) instances of objects
with common base class. From time to time I need to search for last
occurance of instance meeting some criteria. When I find it I need to
move all folowing instances ("tail") into different cointainer (of same
type) while preserving their order.

I'm using find_if with container's reverse_iterators for the search, but
don't know how to preform the movement. If I use container constructor
with reverse_iterators the order of items in new container would be
reversed. This can be solved by reversing the container, but it might
take time if the tail has many items.

So my question is: "Is there any way how to do this (except own
implementation of course)?" Or more generally: "Is there any way how to
create normal (forward) iterator from reverse_iterator?"
Yes, apply the base() member function to the reverse iterator. I've shown a
way you can apply it in your code below.

Thanks in advance, Ales

----- ilustrative code -----

class base_t;

class container_t: list<base_t>

bool test(base_t& item);

container_t tail(container_t& c)
{
c::reverse_iterator found = find_if(c.rbegin(), c.rend(), test);
if (found == c.rend())
return container_t();
Replace this:
container_t result(c.rbegin(), found); // this is reversed tail
reverse(result.begin(), result.end()); // this takes time
with:
container_t result;
copy(found.base(), c.end(), back_inserter(result));

This is assuming all the other issues in the code you've shown are
resolved; I'm going under the assumption you weren't interested in comments
on other issues and just wanted to see how to deal with the issue at hand.
HTH,
-leor
return result;
}


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2
Leor Zolman wrote:
On Sun, 02 May 2004 02:06:53 +0200, AlesD <al****@seznam.cz> wrote:

Hello,
(snip)
So my question is: "Is there any way how to do this (except own
implementation of course)?" Or more generally: "Is there any way how to
create normal (forward) iterator from reverse_iterator?"

Yes, apply the base() member function to the reverse iterator. I've shown a
way you can apply it in your code below.

Replace this:
container_t result(c.rbegin(), found); // this is reversed tail
reverse(result.begin(), result.end()); // this takes time


with:
container_t result;
copy(found.base(), c.end(), back_inserter(result));

This is assuming all the other issues in the code you've shown are
resolved; I'm going under the assumption you weren't interested in comments
on other issues and just wanted to see how to deal with the issue at hand.
HTH,
-leor


Thank you very much for your advice. I would just like to add what I
discovered later in case someone follows this thread:

----------
reverse_bidirectional_iterator must meat following requierements:

reverse_bidirectional_iterator(i).base() == i
&*ri == &*(--ri.base())
----------

Note the decrement in second reqirement! Although it seems strange that
ri and ri.base() does not point to same thing it has it's reason which
is reversing of ranges. If [rbegin(), rend()) is valid range then
[rend().base(), rbegin().base()) is also valid range.
Thanks again for your help

Jul 22 '05 #3
On Sun, 02 May 2004 22:45:49 +0200, AlesD <al****@seznam.cz> wrote:
Leor Zolman wrote:
Replace this:
container_t result(c.rbegin(), found); // this is reversed tail
reverse(result.begin(), result.end()); // this takes time


with:
container_t result;
copy(found.base(), c.end(), back_inserter(result));

Thank you very much for your advice. I would just like to add what I
discovered later in case someone follows this thread:

----------
reverse_bidirectional_iterator must meat following requierements:

reverse_bidirectional_iterator(i).base() == i
&*ri == &*(--ri.base())
----------

Note the decrement in second reqirement! Although it seems strange that
ri and ri.base() does not point to same thing it has it's reason which
is reversing of ranges. If [rbegin(), rend()) is valid range then
[rend().base(), rbegin().base()) is also valid range.
Yes, it is a bit mind-bending when you start thinking about the
relationship of iterators to reverse_iterators. I didn't broach the subject
in my post because the only real pitfall, attempting to invoke base() upon
an "rend()" iterator, wasn't going to happen (you'd already special-cased
that scenario).

But as long as we're following up, I might also mention that my replacement
code above can combined with the return statement and further reduced to
one simple line:

return container_t(found.base(), c.end());

However, this does require a constructor (in this case, I used a template)
in the container_t class of the form:

template<typename iter>
container_t(iter beg, iter end) : list<base_t>(beg, end) {}

as well as support for member function templates (which you should have
with any compiler in modern use other than MSVC 6).
-leor


Thanks again for your help


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #4
> the only real pitfall, attempting to invoke base() upon an "rend()" iterator

Could you please explain why that is a pitfall?

Given the following implementation from vector<T> on my system...

reverse_iterator rend()
{
return (reverse_iterator(begin()));
}

....it seems that retrieving the base iterator from the reverse_iterator would
not in and of itself be a pitfall.

Thanks
Jul 22 '05 #5
On 02 May 2004 23:34:47 GMT, da*********@aol.com (DaKoadMunky) wrote:
the only real pitfall, attempting to invoke base() upon an "rend()" iterator
Could you please explain why that is a pitfall?

Given the following implementation from vector<T> on my system...

reverse_iterator rend()
{
return (reverse_iterator(begin()));
}

...it seems that retrieving the base iterator from the reverse_iterator would
not in and of itself be a pitfall.


I'm sorry, you're right. I was confusing the application of base() to a
reverse_iterator with the process of obtaining an iterator that points to
the same element as its corresponding reverse_iterator (by decrementing the
iterator). There's no iterator that corresponds to rend().
-leor

Thanks


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #6

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

Similar topics

0
3333
by: DD | last post by:
Hi Guys! Just would like to share with you my experiense in this matter. I was trying to evaluate how suitable Oracle OLAP for our applications. As probably you did, I have downloaded from OTN...
0
1539
by: V?ronique | last post by:
hi everyone, if you are a BI tool user, speak spanish, then you might be interested in decideo, which is a community of BI tool users. the newsletter is on: ...
31
4068
by: Greg Scharlemann | last post by:
Given some recent success on a simple form validation (mainly due to the kind folks in this forum), I've tried to tackle something a bit more difficult. I'm pulling data down from a database and...
0
1070
by: al | last post by:
Greeings All, Your kine help is really appreciated. I have this simple UserControl(ChangeDirection.aspx) with a button on it to trigger change of Browser direction,right to left(rtl)(there is...
4
2294
by: mohdowais | last post by:
Hi I am fairly new to ASP.NET and I am trying to migrate an existing ASP 3.0 application to asp.Net, and I seem to have hit a roadblock (maybe I just need more coffee). The current application is...
0
1843
by: YellowFin Announcements | last post by:
Announcing Yellowfin Version 2.4 release. (www.yellowfin.com.au) Existing SQLserver users can take advantage of this upgrade as soon as convenient. V2.4 Exciting changes in the latest...
0
1355
by: YellowFin | last post by:
Yellowfin International today announced that Yellowfin has joined IBM's Partner Program, and that the Yellowfin BI Suite are now validated on IBM's popular DB2 9 database. This partnership opens...
0
1341
by: WI Projects | last post by:
Hi, A CMM Level 5 company is looking for Microsoft Business Intelligence (MS BI) professionals for its Hyderabad location. There is a an element of onsite travel & work to this position. ...
0
1772
by: YellowFin Announcements | last post by:
Yellowfin, Powered by J2EE, Jasper, BIRT, Spring, jfreeChart Advanta releases ATLAS BI using Yellowfin Business Intelligence 3rd of August, 2007 - Advanta Software, specialist software...
0
1098
by: zyro | last post by:
Hi, This is Michael Bowen. I've put together cubegeek.com as a new and hopefully exciting site for the BI professional to network, share get information. This is the industry's first portal...
0
7019
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
7067
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
6847
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...
1
4757
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4463
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2980
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2970
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1288
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
555
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.