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

Any fast method to swap two nodes in a STL list?

To swap to nodes in list, changing pointers is enough. But the stl swap
method seems to copy their values, doesn't it?

Nov 22 '05 #1
10 9996

"Atlas" <Ma*****@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
To swap to nodes in list, changing pointers is enough. But the stl swap
method seems to copy their values, doesn't it?


All standard library containers have copy semantics.
Why is this a problem?

-Mike
Nov 22 '05 #2
Atlas wrote:
To swap to nodes in list, changing pointers is enough. But the stl swap
method seems to copy their values, doesn't it?


std::list have several methods called 'splice' which can move nodes in a
list without copying.

john
Nov 22 '05 #3
John Harrison wrote:
Atlas wrote:
To swap to nodes in list, changing pointers is enough. But the stl swap
method seems to copy their values, doesn't it?


std::list have several methods called 'splice' which can move nodes in a
list without copying.


That's not allowed for what he wants to do. A list may not splice()
with itself.

Kristo

Nov 22 '05 #4
Atlas wrote:
To swap to nodes in list, changing pointers is enough. But the stl swap
method seems to copy their values, doesn't it?


In general, yes. But std::swap is specialized for all of the standard
container classes, so calling swap on two list elements probably just
exchanges pointers.

Kristo

Nov 22 '05 #5
"Kristo" <kr*******@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
That's not allowed for what he wants to do. A list may not splice()
with itself.


Use splice to move the elements in question into another list, then use
splice again to move them back.
Nov 22 '05 #6

Kristo wrote:
John Harrison wrote:
Atlas wrote:
To swap to nodes in list, changing pointers is enough. But the stl swap
method seems to copy their values, doesn't it?


std::list have several methods called 'splice' which can move nodes in a
list without copying.


That's not allowed for what he wants to do. A list may not splice()
with itself.


Isn't there an overloaded splice that does allow the list to be the
same? I got the following from the SGI STL documentation, but I'll be
suprised if it wasn't in the standard:

void splice(iterator position,
list<T, Alloc>& x,
iterator i);
"position must be a valid iterator in *this, and i must be a
dereferenceable iterator in x. Splice moves the element pointed to by i
from x to *this, inserting it before position. All iterators remain
valid, including iterators that point to elements of x. If position ==
i or position == ++i, this function is a null operation. This function
is constant time."
Hope this helps,
-shez-

Nov 22 '05 #7
Kristo wrote:
John Harrison wrote:
Atlas wrote:
To swap to nodes in list, changing pointers is enough. But the stl swap
method seems to copy their values, doesn't it?


std::list have several methods called 'splice' which can move nodes in a
list without copying.

That's not allowed for what he wants to do. A list may not splice()
with itself.

Kristo


There are three different versions of splice. Two of them allow a list
to splice with itself. It all depends on precisely what the OP wants to do.

john
Nov 22 '05 #8

John Harrison wrote:
Kristo wrote:
John Harrison wrote:
Atlas wrote:

To swap to nodes in list, changing pointers is enough. But the stl swap
method seems to copy their values, doesn't it?
std::list have several methods called 'splice' which can move nodes in a
list without copying.

That's not allowed for what he wants to do. A list may not splice()
with itself.

Kristo


There are three different versions of splice. Two of them allow a list
to splice with itself. It all depends on precisely what the OP wants to do.

john


Unfortunately I didn't find any one like "Two of them allow a list to
splice with itself".

Nov 22 '05 #9
"Kristo" <kr*******@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
: Atlas wrote:
: > To swap to nodes in list, changing pointers is enough. But the stl
swap
: > method seems to copy their values, doesn't it?
:
: In general, yes. But std::swap is specialized for all of the standard
: container classes, so calling swap on two list elements probably just
: exchanges pointers.
It doesn't.
If you call swap on two iterators, it is the iterators that will be
swapped.
If you swap the dereferenced iterators, std::swap will have no way to know
that both elements are part of a list. And the side effect of swapping
the values, anyway, are different from those of exchanging the position
of two list items.
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form

Nov 22 '05 #10
"Atlas" <Ma*****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
: John Harrison wrote:
....
: > >>Atlas wrote:
: > >>
: > >>>To swap to nodes in list, changing pointers is enough. But the stl
swap
: > >>>method seems to copy their values, doesn't it?
....
: > There are three different versions of splice. Two of them allow a list
: > to splice with itself. It all depends on precisely what the OP wants
to do.
: >
: > john
:
: Unfortunately I didn't find any one like "Two of them allow a list to
: splice with itself".

I'm surprised this thread does not seem to get anywhere yet: this problem
ought to have a reasonably simple solution.
So let me concretely give it a try.

The easiest seems to use the second form of list.splice().
From the standard:
<<<
void splice(iterator position, list<T,Allocator>& x, iterator i);

Effects: Inserts an element pointed to by i from list x before position
and removes the element from x.
The result is unchanged if position == i or position == ++i. Invalidates
only the iterators and references to the spliced element.
Throws: Nothing
Requires: i is a valid dereferenceable iterator of x.
Complexity: Constant time.

Since you can have position==i, it seems obvious that x can be the same
list as 'this'.

So wouldn't something like the following work?
template<class T, class Allocator>
void swap_list_items
( std::list<T,Allocator>& l
, std::list<T,Allocator>::iterator a
, std::list<T,Allocator>::iterator b
)
{
std::list<T,Allocator>::iterator aPlus = a;
++aPlus; // after position of a, will not be invalidated
l.splice( b, l, a ); // move a before b, invalidates a
l.splice( aPlus, l, b ); // move b before aPlus (where A was)
}

This untested snippet is just a proposed solution, for others to review,
and might be buggy. But I'm sure we can work something out ;)

Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Nov 22 '05 #11

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

Similar topics

3
by: Christopher Jeris | last post by:
Please help me understand the differences, in semantics, browser support and moral preferredness, between the following three methods of swapping content in and out of a page via JavaScript. I...
1
by: Sasha | last post by:
How can I swap two xml nodes that belong to the same parent? Examples would be great! Thank you, Sasha
10
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
12
by: Eugen J. Sobchenko | last post by:
Hi! I'm writing function which swaps two arbitrary elements of double-linked list. References to the next element of list must be unique or NULL (even during swap procedure), the same condition...
3
by: Sam | last post by:
Hi, How can I swap positions and values of two nodes in a treeview ? Thx
1
by: David Hirschfield | last post by:
I've written a tree-like data structure that stores arbitrary python objects. The objective was for the tree structure to allow any number of children per node, and any number of root nodes...and...
3
by: jesper | last post by:
I would like some feedback on this. A while back I was trying my hand at some pathfinding for a small game I was making. I did not know anything about it so I read some stuff and came up with the...
19
by: Juha Nieminen | last post by:
If I'm not completely mistaken, the only reason why std::list::size() may be (and usually is) a linear-time operation is because they want std::list::splice() to be a constant-time operation, and...
2
by: IdlePhaedrus | last post by:
Hi, I have a FFT routine that I converted from C++ to VB in a module as follows: Const M_PI = 3.1415926535897931 ' Fast Fourier Transform Public Sub FFT(ByRef rex() As Single, ByRef imx() As...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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.