[Haskell-cafe] vector to uvector and back again

Dan Doel dan.doel at gmail.com
Fri Feb 12 08:44:06 EST 2010


On Friday 12 February 2010 8:12:51 am Roman Leshchinskiy wrote:
> That's actually a conscious decision. Since vectors support O(1) slicing,
> you can simply copy a slice of the source vector into a slice of the
> target vector.

Ah! I hadn't thought of that. That makes sense.

> At the moment, it is (although it ought to be wrapped in a nicer
> interface). Something like memcpy doesn't work for Data.Vector.Unboxed
> because the ByteArrays aren't pinned. I don't really want to provide
> thawing until someone convinces me that it is actually useful.

Well, my use case is (of course) that I have lots of algorithms on mutable 
arrays, but they work just as well on immutable arrays by creating an 
intermediary. So I provided a combinator 'apply' that did something like:

  apply algo iv = new (safeThaw iv >>= \mv -> algo mv >> return mv)

In uvector, the safeThaw part was copying iv into mv with a provided function. 
For the port, I used unstream . stream, which works fine assuming stream 
produces a correct size hint, I guess. That's the extent of what I have use 
for at the moment, though.

> BTW, vector also supports array recycling so you could implement true
> in-place sorting for fused pipelines. Something like
> 
>   map (+1) . sort . update xs
> 
> wouldn't allocate any temporary arrays in that case.

I'll look into it.

-- Dan


More information about the Haskell-Cafe mailing list