From c94a7902c064d30aecf2a57dc4b77b0b389e6b31 Mon Sep 17 00:00:00 2001 From: = <=> Date: Tue, 30 Sep 2025 17:41:14 +0000 Subject: [PATCH] remove imports, switch to opt=s, add debug index.html --- Makefile | 2 +- index.html | 31 +++++++++++++++++++++++++++++++ pkg/types.go | 4 ++++ pkg/utils.go | 32 +++++++++++++------------------- pkg/wfa.go | 12 ++++-------- 5 files changed, 53 insertions(+), 28 deletions(-) create mode 100644 index.html diff --git a/Makefile b/Makefile index f7a93e3..4fc9165 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ build: clean @echo "======================== Building Binary =======================" minify wfa.js > dist/wfa.js - GOOS=js GOARCH=wasm CGO_ENABLED=0 tinygo build -panic=trap -no-debug -opt=2 -target=wasm -o dist/wfa.wasm . + GOOS=js GOARCH=wasm CGO_ENABLED=0 tinygo build -panic=trap -no-debug -opt=s -target=wasm -o dist/wfa.wasm . clean: @echo "======================== Cleaning Project ======================" diff --git a/index.html b/index.html new file mode 100644 index 0000000..0c2d337 --- /dev/null +++ b/index.html @@ -0,0 +1,31 @@ + + + + + + + + + + +

Result:

+ + \ No newline at end of file diff --git a/pkg/types.go b/pkg/types.go index 1211736..957513b 100644 --- a/pkg/types.go +++ b/pkg/types.go @@ -1,5 +1,9 @@ package wfa +type Integer interface { + ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr +} + type Result struct { Score int CIGAR string diff --git a/pkg/utils.go b/pkg/utils.go index fa5840b..7e75b19 100644 --- a/pkg/utils.go +++ b/pkg/utils.go @@ -1,24 +1,18 @@ package wfa -import ( - "math" - "strings" - - "golang.org/x/exp/constraints" -) +const MaxInt = int(^uint(0) >> 1) +const MinInt = -MaxInt - 1 // convert an unsigned into to string func UIntToString(num uint) string { // num assumed to be positive - var builder strings.Builder + str := []rune{} for num > 0 { digit := num % 10 - builder.WriteRune(rune('0' + digit)) + str = append(str, rune('0'+digit)) num /= 10 } - // Reverse the string as we built it in reverse order - str := []rune(builder.String()) for i, j := 0, len(str)-1; i < j; i, j = i+1, j-1 { str[i], str[j] = str[j], str[i] } @@ -28,7 +22,7 @@ func UIntToString(num uint) string { // num assumed to be positive // decode runlength encoded string such as CIGARs func RunLengthDecode(encoded string) string { - decoded := strings.Builder{} + decoded := []rune{} length := len(encoded) i := 0 @@ -44,30 +38,30 @@ func RunLengthDecode(encoded string) string { if i < length { char := encoded[i] for j := 0; j < runLength; j++ { - decoded.WriteByte(char) + decoded = append(decoded, rune(char)) } i++ // Move past the character } } - return decoded.String() + return string(decoded) } // given the min index, return the item in values at that index -func SafeMin[T constraints.Integer](values []T, idx int) T { +func SafeMin[T Integer](values []T, idx int) T { return values[idx] } // given the max index, return the item in values at that index -func SafeMax[T constraints.Integer](values []T, idx int) T { +func SafeMax[T Integer](values []T, idx int) T { return values[idx] } // given array of values and corresponding array of valid flags, find the min of value which is valid or return false if there does not exist any -func SafeArgMin[T constraints.Integer](valids []bool, values []T) (bool, int) { +func SafeArgMin[T Integer](valids []bool, values []T) (bool, int) { hasValid := false minIndex := 0 - minValue := math.MaxInt + minValue := MaxInt for i := 0; i < len(valids); i++ { if valids[i] && int(values[i]) < minValue { hasValid = true @@ -83,10 +77,10 @@ func SafeArgMin[T constraints.Integer](valids []bool, values []T) (bool, int) { } // given array of values and corresponding array of valid flags, find the max of value which is valid or return false if there does not exist any -func SafeArgMax[T constraints.Integer](valids []bool, values []T) (bool, int) { +func SafeArgMax[T Integer](valids []bool, values []T) (bool, int) { hasValid := false maxIndex := 0 - maxValue := math.MinInt + maxValue := MinInt for i := range valids { if valids[i] && int(values[i]) > maxValue { hasValid = true diff --git a/pkg/wfa.go b/pkg/wfa.go index a6ca9f2..cdee111 100644 --- a/pkg/wfa.go +++ b/pkg/wfa.go @@ -1,9 +1,5 @@ package wfa -import ( - "strings" -) - // WFAlign takes strings s1, s2, penalties, and returns the score and CIGAR if doCIGAR is true func WFAlign(s1 string, s2 string, penalties Penalty, doCIGAR bool) Result { n := len(s1) @@ -189,11 +185,11 @@ func WFBacktrace(M *WavefrontComponent, I *WavefrontComponent, D *WavefrontCompo } } - CIGAR := strings.Builder{} + CIGAR := "" for i := len(Ops) - 1; i > 0; i-- { - CIGAR.WriteString(UIntToString(Counts[i])) - CIGAR.WriteRune(Ops[i]) + CIGAR += UIntToString(Counts[i]) + CIGAR += string(Ops[i]) } - return CIGAR.String() + return CIGAR }