-
Notifications
You must be signed in to change notification settings - Fork 87
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
add CCVector.resize_with and CCVector.resize_with_init, tests and doc #389
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
04ab484
add CCVector.resize_with and CCVector.resize_with_init, tests and doc
RadioPotin 69a6d42
raise Inv_arg, change semantics of vec.resize_with/with_init
RadioPotin 4114f1a
call invalid_arg, rearrange branching
RadioPotin 2a6259e
applye reviews, fix bug, add test
RadioPotin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -201,6 +201,46 @@ let push v x = | |
let v = of_list [1;2;3] in push v 4; to_list v = [1;2;3;4] | ||
*) | ||
|
||
let resize_with v f size = | ||
if size > Sys.max_array_length then | ||
invalid_arg "vec.resize_with: size too big" | ||
else if size > Array.length v.vec then | ||
let new_vec = Array.make size (f 0) in | ||
Array.blit v.vec 0 new_vec 0 (v.size - 1); | ||
for i = v.size to size - 1 do | ||
Array.unsafe_set new_vec i (f i) | ||
done; | ||
v.vec <- new_vec; | ||
v.size <- size | ||
|
||
(*$T | ||
let v = make 1 0 in to_list (resize_with v (fun i -> i) 5) = [0;1;2;3;4] | ||
let v = make 1 0 in length (resize_with v (fun i -> i) 5) = 5 | ||
|
||
let v = make 5 0 in to_list (resize_with v (fun i -> i) 5) = [0;0;0;0;0] | ||
let v = make 5 0 in to_list (resize_with v (fun i -> i) 6) = [0;0;0;0;0;6] | ||
let v = make 5 0 in to_list (resize_with v (fun i -> i) -1) = [0;0;0;0;0] | ||
let v = make 5 0 in length (resize_with v (fun i -> i) 5) = 5 | ||
*) | ||
|
||
let resize_with_init v ~init size = | ||
if size > Sys.max_array_length then | ||
invalid_arg "vec.resize_with_init: size too big" | ||
else if size > Array.length v.vec then | ||
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. Same, this doesn't do anything in case |
||
let new_vec = Array.make size init in | ||
Array.blit v.vec 0 new_vec 0 (v.size - 1); | ||
v.vec <- new_vec; | ||
v.size <- size | ||
|
||
(*$T | ||
let v = make 1 0 in to_list (resize_with_init v ~init:1 5) = [1;1;1;1;1] | ||
let v = make 1 0 in length (resize_with_init v ~init:1 5) = 5 | ||
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. need some tests to cover the case mentioned above :) |
||
|
||
let v = make 5 0 in to_list (resize_with_init v ~init:1 5) = [1;1;1;1;1] | ||
let v = make 5 0 in to_list (resize_with_init v ~init:1 -1) = [1;1;1;1;1] | ||
let v = make 5 0 in length (resize_with_init v ~init:1 5) = 5 | ||
*) | ||
|
||
(** Add all elements of b to a *) | ||
let append a b = | ||
if array_is_empty_ a then ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Still not correct, this will not work if, say,
v.size = 5
butArray.length v.vec = 10
when we want to resize to 8.I think it's simpler to call the function to ensure capacity is >= size, and then add missing elements.