import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Algorithms.Intro as VAlgo
argSort :: (Ord a, VU.Unbox a) => VU.Vector a -> VU.Vector Int
argSort xs = VU.map fst $ VU.create $ do
xsi <- VU.unsafeThaw $ VU.indexed xs
VAlgo.sortBy (comparing snd) xsi
return xsi
My reasoning was that unsafeThaw is safe to use here because I only thaw the indexed version of xs. However, then it occured to me that unboxed vectors of tuples – like these index-value pairs here – are really stored as two unboxed vectors, one for the indices and one for the values. Hence it seems plausible that indexed would not actually generate a new value vector at all, but just couple it with an index vector. In this case, ST-modifying xsi could possibly mess up xs.
Upon testing, this does not seem to happen though. Can I rely on this, or should I better use thaw?