refactor errors

This commit is contained in:
2024-10-14 13:29:05 -05:00
parent 1e33a81a2c
commit 39d4a1e7f0
20 changed files with 398 additions and 444 deletions

View File

@@ -6,8 +6,8 @@ import (
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"go-nkode/hashset"
"log"
"math/big"
r "math/rand"
"time"
@@ -17,12 +17,25 @@ type ShuffleTypes interface {
[]int | int | []uint64 | uint64
}
// fisherYatesShuffle shuffles a slice of bytes in place using the Fisher-Yates algorithm.
var (
ErrFisherYatesShuffle = errors.New("unable to shuffle array")
ErrRandomBytes = errors.New("random bytes error")
ErrRandNonRepeatingUint64 = errors.New("list length must be less than 2^32")
ErrParseHexString = errors.New("parse hex string error")
ErrMatrixTranspose = errors.New("matrix cannot be nil or empty")
ErrListToMatrixNotDivisible = errors.New("list to matrix not possible")
ErrElementNotInArray = errors.New("element not in array")
ErrDecodeBase64Str = errors.New("decode base64 err")
ErrRandNonRepeatingInt = errors.New("list length must be less than 2^31")
ErrXorLengthMismatch = errors.New("xor length mismatch")
)
func fisherYatesShuffle[T ShuffleTypes](b *[]T) error {
for i := len(*b) - 1; i > 0; i-- {
bigJ, err := rand.Int(rand.Reader, big.NewInt(int64(i+1)))
if err != nil {
return err
log.Print("fisher yates shuffle error: ", err)
return ErrFisherYatesShuffle
}
j := bigJ.Int64()
(*b)[i], (*b)[j] = (*b)[j], (*b)[i]
@@ -38,7 +51,8 @@ func RandomBytes(n int) ([]byte, error) {
b := make([]byte, n)
_, err := rand.Read(b)
if err != nil {
return nil, err
log.Print("error in random bytes: ", err)
return nil, ErrRandomBytes
}
return b, nil
}
@@ -72,7 +86,7 @@ func GenerateRandomInt() (int, error) {
func GenerateRandomNonRepeatingUint64(listLen int) ([]uint64, error) {
if listLen > int(1)<<32 {
return nil, errors.New("list length must be less than 2^32")
return nil, ErrRandNonRepeatingUint64
}
listSet := make(hashset.Set[uint64])
for {
@@ -92,7 +106,7 @@ func GenerateRandomNonRepeatingUint64(listLen int) ([]uint64, error) {
func GenerateRandomNonRepeatingInt(listLen int) ([]int, error) {
if listLen > int(1)<<31 {
return nil, errors.New("list length must be less than 2^31")
return nil, ErrRandNonRepeatingInt
}
listSet := make(hashset.Set[int])
for {
@@ -112,7 +126,8 @@ func GenerateRandomNonRepeatingInt(listLen int) ([]int, error) {
func XorLists(l0 []uint64, l1 []uint64) ([]uint64, error) {
if len(l0) != len(l1) {
return nil, errors.New(fmt.Sprintf("list len mismatch %d, %d", len(l0), len(l1)))
log.Printf("list len mismatch %d, %d", len(l0), len(l1))
return nil, ErrXorLengthMismatch
}
xorList := make([]uint64, len(l0))
@@ -131,7 +146,8 @@ func EncodeBase64Str(data []uint64) string {
func DecodeBase64Str(encoded string) ([]uint64, error) {
dataBytes, err := base64.StdEncoding.DecodeString(encoded)
if err != nil {
return nil, err
log.Print("error decoding base64 str: ", err)
return nil, ErrDecodeBase64Str
}
data := ByteArrToUint64Arr(dataBytes)
return data, nil
@@ -179,13 +195,13 @@ func ByteArrToIntArr(byteArr []byte) []int {
return intArr
}
func IndexOf[T uint64 | int](arr []T, el T) int {
func IndexOf[T uint64 | int](arr []T, el T) (int, error) {
for idx, val := range arr {
if val == el {
return idx
return idx, nil
}
}
return -1
return -1, ErrElementNotInArray
}
func IdentityArray(arrLen int) []int {
@@ -199,7 +215,8 @@ func IdentityArray(arrLen int) []int {
func ListToMatrix(listArr []int, numbCols int) ([][]int, error) {
if len(listArr)%numbCols != 0 {
panic(fmt.Sprintf("Array is not evenly divisible by number of columns: %d mod %d = %d", len(listArr), numbCols, len(listArr)%numbCols))
log.Printf("Array is not evenly divisible by number of columns: %d mod %d = %d", len(listArr), numbCols, len(listArr)%numbCols)
return nil, ErrListToMatrixNotDivisible
}
numbRows := len(listArr) / numbCols
matrix := make([][]int, numbRows)
@@ -213,7 +230,8 @@ func ListToMatrix(listArr []int, numbCols int) ([][]int, error) {
func MatrixTranspose(matrix [][]int) ([][]int, error) {
if matrix == nil || len(matrix) == 0 {
return nil, fmt.Errorf("matrix cannot be nil or empty")
log.Print("can't transpose nil or zero len matrix")
return nil, ErrMatrixTranspose
}
rows := len(matrix)
@@ -222,7 +240,8 @@ func MatrixTranspose(matrix [][]int) ([][]int, error) {
// Check if the matrix is not rectangular
for _, row := range matrix {
if len(row) != cols {
return nil, fmt.Errorf("all rows must have the same number of columns")
log.Print("all rows must have the same number of columns")
return nil, ErrMatrixTranspose
}
}
@@ -267,7 +286,8 @@ func ParseHexString(hexStr string) ([]byte, error) {
// Decode the hex string into bytes
bytes, err := hex.DecodeString(hexStr)
if err != nil {
return nil, err
log.Print("parse hex string err: ", err)
return nil, ErrParseHexString
}
return bytes, nil
}