This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Specialising when swap / iter_swap exists
- From: chris <caj at cs dot york dot ac dot uk>
- To: libstdc++ <libstdc++ at gcc dot gnu dot org>
- Date: Thu, 07 Oct 2004 11:11:38 +0100
- Subject: Specialising when swap / iter_swap exists
Some algorithms in libstdc++, such as reverse, use only iter_swap when
operating on elements.
However, some other algorithms, including rotate, sort, etc. don't use
swap but instead use assignment to move variables around.
Changing all these to swap would be more efficent in for those cases
where an efficent swap or iter_swap exists, but using iter_swap instead
of assignment where there is no efficent iter_swap (which includes the
cases where the iterator's value_type is something like char or int)
then (as two tests) sort and rotate appear to take about twice as long.
I have 4 broad ideas on what to do about this...
1) Use iter_swap except on a fixed set of types (int,char,etc.)
2) Only use iter_swap on a fixed set of types (vector<T>, list<T>, etc.)
3) Consider introducing a general method for marking if a type has an
efficent type
4) Introduce a new templated function:
template<T>
void move(T& a,T& b); with a condition something like "assigns b the
value of a, leaves a with a valid but undefined value".
Where the normal definition of move would be simply "b=a", but for types
where swapping is more efficent than assignment use "swap(a,b)". This
would fit in more naturally with the existing definitions of the
algorithm, but would be very slightly less efficent than just using swap
in the algorithms.
I suspect that someone has already thought about this kind of thing, but
have been unable to find any references to it. I am interested on any
thoughts on this, because whatever decision (if any) is made, it will
require some quite substansal edits to the algorithms..
Thank you,
Chris