Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > map : iterator increment/decrement code

Reply
Thread Tools

map : iterator increment/decrement code

 
 
John
Guest
Posts: n/a
 
      05-15-2005

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! ]

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      05-15-2005
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


 
Reply With Quote
 
 
 
 
P.J. Plauger
Guest
Posts: n/a
 
      05-16-2005
"John" <> wrote in message
news: 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! ]

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      05-16-2005
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! ]

 
Reply With Quote
 
Vladimir Marko
Guest
Posts: n/a
 
      05-17-2005
P.J. Plauger wrote:
> "John" <> wrote in message
> news: 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! ]

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What makes an iterator an iterator? Steven D'Aprano Python 28 04-20-2007 03:34 AM
will an iterator to a map becomes invalid when an element is inserted into the map wolverine C++ 3 07-31-2006 12:24 PM
Difference between Java iterator and iterator in Gang of Four Hendrik Maryns Java 18 12-22-2005 05:14 AM
How to convert from std::list<T*>::iterator to std::list<const T*>::iterator? PengYu.UT@gmail.com C++ 6 10-30-2005 03:31 AM
Iterator doubts, Decision on Iterator usage greg C++ 6 07-17-2003 01:26 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57