-
Notifications
You must be signed in to change notification settings - Fork 40
Add ability to shrink/slice vectors and arrays #181
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
Conversation
| Array newSize (Unsafe.resizeMutArr mut seed newSize) | ||
|
|
||
| -- XXX: Replace with toVec | ||
| -- | Copy a slice of the array, starting from given offset and copying given |
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.
Document which of the returned arrays is the slice.
src/Data/HashMap/Linear.hs
Outdated
|
|
||
| size :: HashMap k v #-> (HashMap k v, Int) | ||
| size (HashMap sz ct@(Count c) arr) = (HashMap sz ct arr, c) | ||
| size :: HashMap k v #-> (HashMap k v, 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.
Technically should be in a different PR but don't bother right now. I think it's cumbersome so I'll approve this one but going forward that's a good idea and cleaner.
src/Data/Set/Mutable/Linear.hs
Outdated
| where | ||
| fromHashMapSize :: (Linear.HashMap a (), Int) #-> (Set a, Int) | ||
| fromHashMapSize (hmap, size) = (Set hmap, size) | ||
| size :: Keyed a => Set a #-> (Set 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.
Same as comment on hashmaps.
|
|
||
| -- | Write to an element already written to before. Note: this will not write | ||
| -- to elements beyond the current size of the array and will error instead. | ||
| -- | Number of elements inside the vector. |
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.
Mention that this is different from the capacity.
src/Data/Vector/Mutable/Linear.hs
Outdated
| let -- Calculate the closest power of growth factor | ||
| -- larger than required size. | ||
| newSize = | ||
| constGrowthFactor |
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.
Add a comment mentioning this is a constant defined in this file above.
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 feels a bit odd to add a comment like -- this is defined at the top of this file. I would say that it is the IDE's responsibility (or grep's). Anyway, a comment won't harm anyone, so I'm happy to put the it there (unless you suggest a better one :)).
src/Data/Vector/Mutable/Linear.hs
Outdated
|
|
||
| -- | Same as 'read', but does not do bounds-checking. The behaviour is undefined | ||
| -- when passed an invalid index. | ||
| unsafeRead :: HasCallStack => Vector a #-> Int -> (Vector a, Unrestricted 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.
For consistency, could you change read and write to use the same elegant design as the arrays that call unsafeRead and unsafeWrite with assertIndexInRange ix arr that panics if the index is not in range?
|
🎉 All dependencies have been resolved ! |
Previously, vectors could only grow. This commit adds functions around resizing and slicing vectors and arrays. * Adds a `pop` function, and renames `snoc` to `push`, just because they sound (to me) more natural than `unsnoc`. * Adds `slice` functions to arrays and vectors that can return a sub-range of elements. * Adds a `resizeToFit` function to vectors to reduce the capacity of a vector.
67032f2 to
636324a
Compare
Depends on #180, As part of #165.
Previously, vectors could only grow. This PR adds functions around resizing and slicing vectors and arrays.
popfunction, and renamessnoctopush, just because it sounds more natural to me thanunsnoc. Happy to revert this change if you prefer the former.slicefunctions to arrays and vectors that can return a sub-range of elements.shrinkToFitfunction to vectors to reduce the capacity of a vector.Alongside with tests for each, of course.