-
Notifications
You must be signed in to change notification settings - Fork 125
improve Array::makei #1753
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
base: main
Are you sure you want to change the base?
improve Array::makei #1753
Conversation
Uses the same implementation as Array::make, which is panic when len < 0.
Pull Request Test Coverage Report for Build 5598Details
💛 - Coveralls |
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.
Please do not move the APIs.
If you want to use the intrinsics, just copy them to the array
package.
Maybe you can just make |
if length <= 0 { | ||
[] | ||
} else { | ||
let array = Array::make(length, value(0)) |
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 want to avoid redundent fill values Array::make(length, value(0))
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 don't see why it's redundent: when you have Array::make_uninit
, it also has this fill operation, except it's filled with some other value.
The only thing that may change is the performance difference between set
and unsafe_set
.
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.
Array::make
has two fill operations.
Array::makei
call Array::make
has three fill operations,
pub fn Array::make[T](len : Int, elem : T) -> Array[T] {
let arr = Array::make_uninit(len) // (1)
for i in 0..<len {
arr.unsafe_set(i, elem) // (2)
}
arr
}
pub fn Array::makei[T](length : Int, value : (Int) -> T) -> Array[T] {
if length <= 0 {
[]
} else {
let array = Array::make(length, value(0)) // (2)
for i in 1..<length {
array[i] = value(i) // (3)
}
array
}
}
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.
@Yu-zh Do we have intrinsics for these cases?
I also want to use |
So how to copy them to the |
Copy them as well. |
Array related implementation will be moved to |
Uses the same implementation as Array::make, which is panic when len < 0.