-
-
Notifications
You must be signed in to change notification settings - Fork 46
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
fix(iter): make combinations and permutations generic #223
base: main
Are you sure you want to change the base?
Changes from 2 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 |
---|---|---|
|
@@ -3,31 +3,31 @@ module iter | |
import vsl.util | ||
import math | ||
|
||
pub struct PermutationsIter { | ||
pub struct PermutationsIter[T] { | ||
mut: | ||
pos u64 | ||
idxs []int | ||
cycles []int | ||
pub: | ||
repeat int | ||
size u64 | ||
data []f64 | ||
data []T | ||
} | ||
|
||
// PermutationsIter.new will return an iterator that allows | ||
// lazy computation for all length `r` permutations of `data` | ||
pub fn PermutationsIter.new(data []f64, r int) PermutationsIter { | ||
pub fn PermutationsIter.new[T](data []T, r int) PermutationsIter[T] { | ||
n := data.len | ||
if r > n { | ||
return PermutationsIter{ | ||
return PermutationsIter[T]{ | ||
data: data | ||
repeat: r | ||
} | ||
} | ||
size := u64(math.factorial(n) / math.factorial(n - r)) | ||
idxs := util.arange(n) | ||
cycles := util.range(n, n - r, step: -1) | ||
return PermutationsIter{ | ||
return PermutationsIter[T]{ | ||
Comment on lines
+19
to
+30
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. 💡 Codebase verification Potential integer overflow risk requires bounds checking Based on the code inspection, the
Recommendations:
🔗 Analysis chainPotential factorial overflow for large n. 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Checking all usage of PermutationsIter.new to verify the range of r and data length
rg --multiline "PermutationsIter\.new\s*\(\s*\w+,\s*\w+\)" -A 5
Length of output: 65 Script: #!/bin/bash
# Let's try a different approach to find usages of PermutationsIter
rg "PermutationsIter" -A 5
# Also check the implementation of math.factorial
ast-grep --pattern 'fn factorial($_) {
$$$
}'
Length of output: 1314 Script: #!/bin/bash
# Let's check the math module for factorial implementation
rg "fn factorial" -A 10
# Also check for any tests that might show usage patterns
fd "test.*\.v" --exec rg "PermutationsIter" -A 5 {}
Length of output: 79 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. @wfee2000 could you check this please? 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.
|
||
data: data | ||
repeat: r | ||
size: size | ||
|
@@ -37,7 +37,7 @@ pub fn PermutationsIter.new(data []f64, r int) PermutationsIter { | |
} | ||
|
||
// next will return next permutation if possible | ||
pub fn (mut o PermutationsIter) next() ?[]f64 { | ||
pub fn (mut o PermutationsIter[T]) next[T]() ?[]T { | ||
// base case for every iterator | ||
if o.pos == o.size { | ||
return none | ||
|
@@ -69,9 +69,9 @@ pub fn (mut o PermutationsIter) next() ?[]f64 { | |
} | ||
|
||
// permutations returns successive `r` length permutations of elements in `data` | ||
pub fn permutations(data []f64, r int) [][]f64 { | ||
mut perms := PermutationsIter.new(data, r) | ||
mut result := [][]f64{cap: int(perms.size)} | ||
pub fn permutations[T](data []T, r int) [][]T { | ||
mut perms := PermutationsIter.new[T](data, r) | ||
mut result := [][]T{cap: int(perms.size)} | ||
for perm in perms { | ||
result << perm | ||
} | ||
|
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.
💡 Codebase verification
Based on the code found, I can now generate the final response as we have enough information about the implementation of
choose
and its potential overflow behavior.Add overflow checks for combinatorial calculations
The current implementation of
choose(n, r)
usesuint_binomial
which has overflow protection, but the size checkr > n
should happen before any calculations to prevent unnecessary work and potential integer overflow. Consider:Reasoning:
uint_binomial
has overflow checks and will panic on overflowr > n
after callingfun.choose(n, r)
, which could lead to unnecessary calculationschoose
function internally uses factorial calculations which can overflow for large numbers🔗 Analysis chain
Ensure safe usage of combinatorial expansions.
Similar to permutations, calling fun.choose(n, r) could be large. Check for potential overflow or extremely large memory allocations.
Let me gather information about the implementation of
fun.choose
and related functions to verify the safety measures.Let me try to find the implementation of
fun.choose
specifically.Let me try a different approach to find the implementation using ripgrep.
🏁 Scripts executed
The following scripts were executed for the analysis:
Error: No shell scripts found
Script:
Length of output: 70495
Script:
Length of output: 108
Script:
Length of output: 2363
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.
@wfee2000 could you check this please?
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.