cleanup code

This commit is contained in:
2025-05-01 18:04:42 +00:00
parent a446bbd923
commit 2351faf2d7
3 changed files with 13 additions and 27 deletions

View File

@@ -6,38 +6,31 @@ type PositiveSlice[T any] struct {
defaultValue T
}
func (a *PositiveSlice[T]) TranslateIndex(idx int) int {
return idx
}
func (a *PositiveSlice[T]) Valid(idx int) bool {
actualIdx := a.TranslateIndex(idx)
return 0 <= actualIdx && actualIdx < len(a.valid) && a.valid[actualIdx]
return 0 <= idx && idx < len(a.valid) && a.valid[idx]
}
func (a *PositiveSlice[T]) Get(idx int) T {
actualIdx := a.TranslateIndex(idx)
if 0 <= actualIdx && actualIdx < len(a.valid) && a.valid[actualIdx] { // idx is in the slice
return a.data[actualIdx]
if 0 <= idx && idx < len(a.valid) && a.valid[idx] { // idx is in the slice
return a.data[idx]
} else { // idx is out of the slice
return a.defaultValue
}
}
func (a *PositiveSlice[T]) Set(idx int, value T) {
actualIdx := a.TranslateIndex(idx)
if actualIdx < 0 || actualIdx >= len(a.valid) { // idx is outside the slice
// expand data array to actualIdx
newData := make([]T, 2*actualIdx+1)
if idx < 0 || idx >= len(a.valid) { // idx is outside the slice
// expand data array to 2*idx
newData := make([]T, 2*idx+1)
copy(newData, a.data)
a.data = newData
// expand valid array to actualIdx
newValid := make([]bool, 2*actualIdx+1)
// expand valid array to 2*idx
newValid := make([]bool, 2*idx+1)
copy(newValid, a.valid)
a.valid = newValid
}
a.data[actualIdx] = value
a.valid[actualIdx] = true
a.data[idx] = value
a.valid[idx] = true
}

View File

@@ -71,12 +71,9 @@ type Wavefront struct { // since wavefronts store diag distance, they should nev
// NewWavefront: returns a new wavefront with size accomodating lo and hi (inclusive)
func NewWavefront(lo int, hi int) *Wavefront {
a := &Wavefront{}
a.lohi = PackWavefrontLoHi(lo, hi)
size := hi - lo
newData := make([]WavefrontValue, size+1)
a.data = newData
a.data = make([]WavefrontValue, size+1)
return a
}

View File

@@ -63,18 +63,14 @@ func SafeArgMax[T constraints.Integer](valids []bool, values []T) (bool, int) {
hasValid := false
maxIndex := 0
maxValue := math.MinInt
for i := 0; i < len(valids); i++ {
for i := range valids {
if valids[i] && int(values[i]) > maxValue {
hasValid = true
maxIndex = i
maxValue = int(values[i])
}
}
if hasValid {
return true, maxIndex
} else {
return false, 0
}
return hasValid, maxIndex
}
func SafeArgMin[T constraints.Integer](valids []bool, values []T) (bool, int) {