HP Integrity NonStop H-Series User Manual
Page 81

The value of the variable divisor indicates which digit is currently being examined. A boolean
flag is used to determine when execution should halt. Each time the while loop is executed a
vector of deques is declared. By placing the declaration of this structure inside the while loop, it
is reinitialized to empty each step. Each time the loop is executed, the values in the list are
copied into the appropriate bucket by executing the function copyIntoBuckets() on each value.
Once distributed into the buckets, the values are gathered back into the list by means of an
accumulation.
void radixSort(list
{
bool flag = true;
int divisor = 1;
while (flag) {
vector< deque
flag = false;
for_each(values.begin(), values.end(),
copyIntoBuckets(...));
accumulate(buckets.begin(), buckets.end(),
values.begin(), listCopy);
divisor *= 10;
}
}
The use of the function accumulate() here is slightly unusual. The "scalar" value being
constructed is the list itself. The initial value for the accumulation is the iterator denoting the
beginning of the list. Each bucket is processed by the following binary function:
list
listCopy(list
deque
{
// copy list back into original list, returning end
return copy(lst.begin(), lst.end(), c);
}
The only difficulty remaining is defining the function copyIntoBuckets(). The problem here is
that the function must take as its argument only the element being inserted, but it must also have
access to the three values buckets, divisor and flag. In languages that permit functions to be
defined within other functions the solution would be to define copyIntoBuckets() as a local
function within the while loop. But C++ has no such facilities. Instead, we must create a class
definition, which can be initialized with references to the appropriate values. The parenthesis
operator for this class is then used as the function for the for_each() invocation in the radix sort
program.
class copyIntoBuckets {
public:
copyIntoBuckets