mirror of
https://github.com/vlang/v.git
synced 2025-08-04 02:07:28 -04:00
math.big: speed up ~10x integer_from_radix() (#24674)
This commit is contained in:
parent
cecbc7294a
commit
e8fe334396
@ -1,5 +1,6 @@
|
|||||||
module big
|
module big
|
||||||
|
|
||||||
|
import math
|
||||||
import math.bits
|
import math.bits
|
||||||
import strings
|
import strings
|
||||||
import strconv
|
import strconv
|
||||||
@ -272,13 +273,17 @@ fn integer_from_regular_string(characters string, radix u32) Integer {
|
|||||||
|
|
||||||
mut result := zero_int
|
mut result := zero_int
|
||||||
radix_int := integer_from_u32(radix)
|
radix_int := integer_from_u32(radix)
|
||||||
|
pow := radix_options[int(radix)]
|
||||||
for index := start_index; index < characters.len; index++ {
|
radix_pow := radix_int.pow(u32(pow))
|
||||||
digit := characters[index]
|
for i := start_index; i < characters.len; i += pow {
|
||||||
value := digit_array.index(digit)
|
end := math.min(i + pow, characters.len)
|
||||||
|
num_str := characters[i..end]
|
||||||
result *= radix_int
|
if num_str.len == pow {
|
||||||
result += integer_from_int(value)
|
result *= radix_pow
|
||||||
|
} else {
|
||||||
|
result *= radix_int.pow(u32(num_str.len))
|
||||||
|
}
|
||||||
|
result += integer_from_u32(regular_string_to_radix(num_str, radix))
|
||||||
}
|
}
|
||||||
|
|
||||||
return Integer{
|
return Integer{
|
||||||
@ -287,6 +292,15 @@ fn integer_from_regular_string(characters string, radix u32) Integer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn regular_string_to_radix(characters string, radix u32) u32 {
|
||||||
|
mut result := u32(0)
|
||||||
|
|
||||||
|
for c in characters {
|
||||||
|
result = result * radix + u32(digit_array.index(c))
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
// abs returns the absolute value of the integer `a`.
|
// abs returns the absolute value of the integer `a`.
|
||||||
pub fn (a Integer) abs() Integer {
|
pub fn (a Integer) abs() Integer {
|
||||||
return if a.signum == 0 {
|
return if a.signum == 0 {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user