diff --git a/pkg/custom_slice.go b/pkg/custom_slice.go index c0b1530..4704b95 100644 --- a/pkg/custom_slice.go +++ b/pkg/custom_slice.go @@ -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 } diff --git a/pkg/types.go b/pkg/types.go index d9e51a8..e23c34b 100644 --- a/pkg/types.go +++ b/pkg/types.go @@ -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 } diff --git a/pkg/utils.go b/pkg/utils.go index afb0b4e..94db843 100644 --- a/pkg/utils.go +++ b/pkg/utils.go @@ -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) {