Sorts a vector of T according to cmp, adds
equal elements according to merger, and returns an index map of
the (merged) elements in the proper
order. We allocate the necessary vectors once and use a separate
recursive routine after that. Reading the output properly is done with