473,511 Members | 9,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

map : iterator increment/decrement code


I was wondering why the STL RB Tree implementation is
used when the iterators of map are incremented/decremented
to yield successors and predecessors? This takes O(log n)
time (and a lot of memory thrashing depending on the size
of the tree) whereas just a simple pointer chase would make it O(1).
Is there an easy way to code this in C++ (Threaded red
black tree? ) OR Modify map so that map<>::iterator, ++it/--it
becomes O(1)?

Thanks in advance for your comments,
--j
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 23 '05 #1
4 8389
John wrote:
I was wondering why the STL RB Tree implementation is
used when the iterators of map are incremented/decremented
to yield successors and predecessors?
I am wondering why are you sure that it is what's happening.
This takes O(log n)
time (and a lot of memory thrashing depending on the size
of the tree) whereas just a simple pointer chase would make it O(1).
And why are you sure that it's not so?
Is there an easy way to code this in C++ (Threaded red
black tree? ) OR Modify map so that map<>::iterator, ++it/--it
becomes O(1)?


What makes you think that all this would be necessary? Do you have
any definite results from running some tests that the performance of
++/-- is not acceptable? And even if you do, what makes you think
that it's not just your particular implementation of the Standard
library? AFAIK, there is no specific requirement in the Standard
that 'std::map' or its iterators have any particular implementation.

V
Jul 23 '05 #2
"John" <we**********@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
I was wondering why the STL RB Tree implementation is
used when the iterators of map are incremented/decremented
to yield successors and predecessors?
Because the RB tree is also used to do inserts, erases, and
finds?
This takes O(log n)
time (and a lot of memory thrashing depending on the size
of the tree) whereas just a simple pointer chase would make it O(1).
And in practice, *most* increments and decrements (in a large
tree) are indeed O(1). It's just that every once in a while
you come to the end of a subtree and have to do O(log N)
operations to find the successor. So amortized time complexity
is O(log N).
Is there an easy way to code this in C++ (Threaded red
black tree? ) OR Modify map so that map<>::iterator, ++it/--it
becomes O(1)?


You probably could, at the expense of making inserts and erases
even more expensive. They'd have to update two more links
(predecessor and successor) in addition to the existing three
(parent, left subtree, and right subtree). I suspect the
time complexity would still be O(log N), but with a notably
bigger multiplier.

And if you care about memory thrashing, you're bound to get
more of it with five pointers per node instead of three.
The member functions insert and erase also get bigger, so
you increase the chance of code thrashing.

And if you care about implementation bugs (as I do), you're
bound to get more of them when the code has to maintain two
only loosely related data structures instead of one to preserve
tree integrity.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 23 '05 #3
John wrote:

I was wondering why the STL RB Tree implementation is
used when the iterators of map are incremented/decremented
to yield successors and predecessors? This takes O(log n)
time (and a lot of memory thrashing depending on the size
of the tree) whereas just a simple pointer chase would make it O(1).
Actually it is implementation dependent, but in any case, the
implementation you are hinting at is amortized constant time. That is not
that bad a design for an iterator: in most cases you will use them to
iterate through a rather longish piece of the map.
Is there an easy way to code this in C++ (Threaded red
black tree? )
That depends: what do you consider easy? You would have to run your own
RB-tree implementation.
OR Modify map so that map<>::iterator, ++it/--it becomes O(1)?


That would be possible with containers that are templated on the underlying
tree structure. I do not know if there is an implementation available that
supports this.
Best

Kai-Uwe Bux

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 23 '05 #4
P.J. Plauger wrote:
"John" <we**********@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
I was wondering why the STL RB Tree implementation is
used when the iterators of map are incremented/decremented
to yield successors and predecessors?
Because the RB tree is also used to do inserts, erases, and
finds?
This takes O(log n)
time (and a lot of memory thrashing depending on the size
of the tree) whereas just a simple pointer chase would make it

O(1).
And in practice, *most* increments and decrements (in a large
tree) are indeed O(1). It's just that every once in a while
you come to the end of a subtree and have to do O(log N)
operations to find the successor. So amortized time complexity
is O(log N).


If you iterate from begin() to end() you go along each edge
exactly twice (once down, once up). And since there are exactly
size()-1 edges in a tree (plus 1 additional from the non-node
"root") the increment time complexity is amortized constant
unless you mean amortized _with respect to_ something else than
incrementing every incrementable iterator once.

Vladimir Marko
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 23 '05 #5

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

Similar topics

9
3175
by: Mark Turney | last post by:
I was reading "Practical C++ Programming" yesterday, and it mentioned that the order of execution for post-increment and post-decrement operators was ambiguous. I had previously learned that a...
0
1937
by: nick | last post by:
Hi, I need to manage a "layered" collection of objects, where each layer grows independently, e.g, o--+--+--+--+--+ 1st layer | o 2nd layer (empty) | o--+--+--+ 3rd...
8
6238
by: lovecreatesbeauty | last post by:
Hello experts, Why can this difference between prefix increment/decrement and postfix increment/decrement reside in built-in operators for built-in data types? Thanks. // test.cpp // //...
7
1907
by: PengYu.UT | last post by:
I'm wondering is the standard defined behavior of past bound iterator. In the following example it seems that afer first "--it", it point to -1 index. I'm wondering if it is true independent of...
5
13270
by: Stuart | last post by:
Hi all, Iv'e got a page that has a mass amount of input fields, all of which require a decimal figure. To make it easier when it comes to inputting data, I'm trying to setup + and - links that...
21
5681
by: T.A. | last post by:
I understand why it is not safe to inherit from STL containers, but I have found (in SGI STL documentation) that for example bidirectional_iterator class can be used to create your own iterator...
3
1554
by: Johs | last post by:
I have implemented a red-black tree based on the description in Introduction to Algorithms Cormen section 13. But I would like to make all iterator operations to take O(1) time in worst case. Is...
16
6045
by: Juha Nieminen | last post by:
I'm actually not sure about this one: Does the standard guarantee that if there's at least one element in the data container, then "--container.end()" will work and give an iterator to the last...
4
14876
by: qlin88 | last post by:
Hi, In STL multi-map, the lower_bound, upper_bound,equal_range all return an iterator. Now ifone wants to iterate from an upper bound towards a lower bound, what would be the best way to do it?...
0
7430
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...
1
7089
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
7517
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
5673
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,...
1
5072
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
4743
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
3230
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
1581
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 ...
0
451
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.