Topic: swap" and the HP STL implementation


Author: jbuck@Synopsys.COM (Joe Buck)
Date: 1996/03/19
Raw View
Many STL containers and standard library objects (e.g. string) provide a
swap function, since it is often much cheaper to exchange two complex
objects directly than to use a temporary.

STL also includes iter_swap, for swapping two objects given iterators
to point to them, and in the STL implementation the sort algorithm uses
iter_swap heavily.  But iter_swap doesn't use swap: this means that
things like sorts of vectors of strings are slow.  ObjectSpace copied
HP's implementation, so theirs has the same deficiency.

Is there any reason why iter_swap can't be implemented as

template <class ForwardIterator1, class ForwardIterator2>
inline void iter_swap(ForwardIterator1 a, ForwardIterator2 b) {
    swap(*a, *b);
}

which would gain efficiency in cases where a fast swap exists or could
be provided?

Also (to be sure that this is standard-related) would it make sense to
either require, or strongly suggest as a quality-of-implementation
mechanism, that higher-level STL algorithms use lower-level algorithms in
a prescribed way so that if a user implements specializations for some
algorithms, he/she can be assured that the others run faster?





--
-- Joe Buck  <jbuck@synopsys.com> (not speaking for Synopsys, Inc)

Work for something because it is good,
not just because it stands a chance to succeed.    -- Vaclav Havel
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]