-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement mutable vectors using arrays #146
Conversation
allocBeside :: Int -> a -> Array b #-> (Array b, Array a) | ||
allocBeside size val orig | ||
| size >= 0 = (orig, Array (Unsafe.newMutArr size val)) | ||
| otherwise = orig `lseq` error ("Trying to allocate an array of size " ++ show size) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find this function interesting. It is required since when implementing HashMap's, insertions can cause a new, bigger array to be created. However, we can not use alloc
function there since it requires a continuation; and making HashMap insertions take a continuation would be unwieldy.
I wanted to replace it with something more generic, but couldn't figure out how to do it nicely. I think this connects to @aspiwack 's suggestion on #130.
where | ||
unsafeLength :: Vector a -> (Vector a, Int) | ||
unsafeLength v@(Vec (len, _) _) = (v, len) | ||
length :: Vector a #-> (Vector a, Unrestricted Int) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I replaced this function to return an Unrestricted Int
instead of Int
, to be more consistent with other functions like read
.
|
||
-- | Read from a vector, with an in-range index and error for an index that is | ||
-- out of range (with the usual range @0..length-1@). | ||
read :: HasCallStack => Vector a #-> Int -> (Vector a, a) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new version of this function returns a Unrestricted a
instead. Since the vector does not contain its values linearly, I think it was overly restrictive.
import qualified Prelude | ||
|
||
-- # Data types | ||
------------------------------------------------------------------------------- | ||
|
||
data Array a where | ||
Array :: Int -> (MutableArray# RealWorld a) -> Array a | ||
Array :: MutableArray# RealWorld a -> Array a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now the Array
does not have an extra size parameter, since MutableArray#
already comes with a sizeof
method.
Whatever I think of this change. It didn't belong to this PR. Additionally, this is not the sort of change that you should make without talking about it first. We've all got better things to do than revert this in the course of this PR, though I do hope this sort of conflation won't happen again. But please open an issue to discuss the order of arguments in these functions. (under the bikeshedding label, I assume) |
This should have been another PR. |
I'm happy to spend some time sending separate PR's for each. Sorry about this. |
This PR refactors
Data.Vector.Mutable.Linear
to useData.Array.Mutable.Linear
instead ofUnsafe.MutableArray
. I also did a few refactors around the Array code.In summary:
unsafeRead
andunsafeWrite
functions from vectors and array to reduce unnecessary bounds-checking in vector code.Array
had a few resize functions with counter-intutive semantics. I replaced them with three functions:allocBeside
,growBy
andshrinkTo
. Let me know if you prefer something else.MutableArray
,Array
andVector
used to require at least one element inside. I removed this constraint. It seems likeMutableArray
can have 0 length, and therefore there is no reasonArray
andVector
to have that restriction.So I'm sorry if this PR is a bit all around the place. I suggest you review the modules in this order:
Unsafe.MutableArray
Data.Array.Mutable.Linear
Data.Vector.Mutable.Linear.
Data.HashMap.Linear
(only contains changes because of the parameter order change)