swap(x,y)

Richard O'Keefe ok at cs.mu.oz.au
Fri Sep 22 20:05:06 AEST 1989


In article <4310 at buengc.BU.EDU>, bph at buengc.BU.EDU (Blair P. Houghton) writes:
> The issue I'd like to address is that there are some constructs
> that seem entirely obvious, but that are not a part of my favorite
> language.  Some of them aren't there because there's no rational
> way to implement them in a compiler.  A swap-operator, however,
> is not at all that way.

Swapping is a special case of the concurrent assignment operator.
If someone were to invest the time in hacking a compiler, I'd much rather
they worked on doing
	i, j, x := i+1, j-1, f(i, j)
efficiently.  Give me that, and
	x, y := y, x
is easy, and avoids flames from people who spell swap "swop".

> Swapping a couple of variables is the utter basis of (almost) all sorting
> algorithms.

This turns out not to be the case.  For example, an efficient version of
``Quick''sort would not use exchanges, even if the machine had them.
Hoare explained this optimisation back in 1962, and it's in Sedgewick's
Acta Informatica paper.  The significantly more efficient merge sort has
no use at all for exchanges.  Radix sort doesn't use them.  Distributed
partitioning sort can manage just fine without them.

If C libraries have managed with a sorting method (quick sort) which was
KNOWN to be slow when it was first invented, I think it's clear that
the speed of sorting is not a priority for C programmers.

Don't get me wrong, I _like_ exchanges.  I liked having them in Algol 68,
and I liked READLOCK in B6700 Algol (to get an exchange, you did
	DEFINE SWOP(X,Y) = X := READLOCK(X, Y);
and you just didn't worry about locking out other processors for one memref).
But if we were talking extensions, I would much rather have a portable way
of making *sure* that I got the errno appropriate to a failed system call
(such as HP-UX's method that was posted some time ago), and I would really
love a standard set of errno names.



More information about the Comp.lang.c mailing list