-
Notifications
You must be signed in to change notification settings - Fork 60
add strict map functions for Array and SmallArray #138
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,6 +57,7 @@ module Data.Primitive.SmallArray | |
| , sizeofSmallMutableArray | ||
| , smallArrayFromList | ||
| , smallArrayFromListN | ||
| , mapSmallArray' | ||
| , unsafeTraverseSmallArray | ||
| ) where | ||
|
|
||
|
|
@@ -436,6 +437,20 @@ unsafeTraverseSmallArray f (SmallArray ar) = SmallArray `liftM` unsafeTraverseAr | |
| #endif | ||
| {-# INLINE unsafeTraverseSmallArray #-} | ||
|
|
||
| -- | Strict map over the elements of the array. | ||
| mapSmallArray' :: (a -> b) -> SmallArray a -> SmallArray b | ||
| #if HAVE_SMALL_ARRAY | ||
| mapSmallArray' f sa = createSmallArray (length sa) (die "mapSmallArray'" "impossible") $ \smb -> | ||
| fix ? 0 $ \go i -> | ||
| when (i < length sa) $ do | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not really sure. I just copied the implementation of
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. History, I imagine. The
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm inclined to leave this as is for now. I'd like to open a separate PR adding benchmarks that check the difference between these two styles. |
||
| x <- indexSmallArrayM sa i | ||
| let !y = f x | ||
| writeSmallArray smb i y *> go (i+1) | ||
| #else | ||
| mapSmallArray' f (SmallArray ar) = SmallArray (mapArray' f ar) | ||
| #endif | ||
| {-# INLINE mapSmallArray' #-} | ||
|
|
||
| #ifndef HAVE_SMALL_ARRAY | ||
| runSmallArray | ||
| :: (forall s. ST s (SmallMutableArray s 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.
It might be worth mentioning in a comment that we use
indexArrayMhere in casefis lazy.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.
Done.