Is it safe to use an indexed unpacked vector?

I just posted this code:

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 what unsafeThawcould be used here because I am only thawing the indexedversion xs. However, then it seemed to me that the non-located vectors of tuples - like these pairs of index values ​​here - are really stored as two non-located vectors, one for indices and one for values. Therefore, it seems plausible that it indexeddoes not generate a new vector of values ​​at all, but simply associates it with the index vector. In this case, the STmodification xsimay spoil xs.

When tested, this does not seem to happen. Can I rely on this, or is it better to use thaw?

+4
source share

Source: https://habr.com/ru/post/1660636/


All Articles