clash-prelude icon indicating copy to clipboard operation
clash-prelude copied to clipboard

Function to replace slice of vector

Open adamwalker opened this issue 8 years ago • 5 comments

Would it be possible to add a function, replaceVec, similar to "replace" from CLaSH.Sized.Vector which replaces a slice instead of a single element? Perhaps something like:

replaceVec :: (KnownNat m) => SNat o -> Vec m a -> Vec (o + p + m) a -> Vec (o + p + m) a
replaceVec offset replacement vec = vecTruncated ++ replacement ++ vecTail
    where
    vecTruncated =  take offset vec
    vecTail      =  drop (offset `addSNat` lengthS replacement) vec

adamwalker avatar Sep 05 '16 04:09 adamwalker

@adamwalker Do you want a "static" version, like the example you posted (which is sorta similar to setSlice), or do you want a "dynamic" version, similar to the replace function you mentioned? Or do you want both?

christiaanb avatar Sep 05 '16 08:09 christiaanb

Good point. I actually want both, if thats doable.

adamwalker avatar Sep 05 '16 08:09 adamwalker

So what should be the out-of-bounds behaviour for the dynamic version?

  • Should it be similar to replace, and give a run-time error when the offset + length replacement > length vec?
  • Should it truncate the LSBs of replacement?

christiaanb avatar Sep 05 '16 08:09 christiaanb

Well, I think it should behave however you decide is most sensible :) But, in my opinion it should behave similar to replace and give a runtime error. What does replace do in actual hardware when the index is out of bounds?

adamwalker avatar Sep 05 '16 08:09 adamwalker

The worst thing possible: the index will wrap around.

christiaanb avatar Sep 05 '16 08:09 christiaanb