Checkpoint
This commit is contained in:
138 files changed
+1898
-57607
No files matched your search
@@ -0,0 +1,78 @@
|
||||
package util
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrInterleaveInvalidElementCount = errors.New("each individual slice must have the same element count")
|
||||
ErrDeinterleaveInvalidElementCount = errors.New("number of elements in slice is not evenly divisible by slice count")
|
||||
)
|
||||
|
||||
// InterleaveSlices takes one more slices as input and returns a new
|
||||
// "interleaved" result. For example, given:
|
||||
//
|
||||
// s0: [ 0, 3, 6, 9 ]
|
||||
// s1: [ 1, 4, 7, 10 ]
|
||||
// s2: [ 2, 5, 8, 11 ]
|
||||
//
|
||||
// InterleaveSlices(s1, s2, s3) will return:
|
||||
//
|
||||
// [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ]
|
||||
//
|
||||
// InterleaveSlices will fail if any individual slice has a different length.
|
||||
func InterleaveSlices[T any](s1 []T, slices ...[]T) ([]T, error) {
|
||||
|
||||
elementsPerSlice := len(s1)
|
||||
totalSlices := 1 + len(slices)
|
||||
totalElements := elementsPerSlice * totalSlices
|
||||
|
||||
res := make([]T, totalElements)
|
||||
for i := 0; i < elementsPerSlice; i++ {
|
||||
res[totalSlices*i] = s1[i]
|
||||
}
|
||||
for s := range slices {
|
||||
slice := slices[s]
|
||||
if len(slice) != elementsPerSlice {
|
||||
return nil, ErrInterleaveInvalidElementCount
|
||||
}
|
||||
for i := 0; i < elementsPerSlice; i++ {
|
||||
res[(totalSlices*i)+s+1] = slice[i]
|
||||
}
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// DeinterleaveSlices is the inverse of InterleaveSlices. It divides a single
|
||||
// slice into 'sliceCount' output slices. For example, given:
|
||||
//
|
||||
// s: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ]
|
||||
//
|
||||
// DeinterleaveSlices(s, 3) will return:
|
||||
//
|
||||
// s0: [ 0, 3, 6, 9 ]
|
||||
// s1: [ 1, 4, 7, 10 ]
|
||||
// s2: [ 2, 5, 8, 11 ]
|
||||
//
|
||||
// DeinterleaveSlices will fail if 's' isn't evenly divisible by 'sliceCount'.
|
||||
func DeinterleaveSlices[T any](s []T, sliceCount int) ([][]T, error) {
|
||||
|
||||
remainder := len(s) % sliceCount
|
||||
if remainder != 0 {
|
||||
return nil, ErrDeinterleaveInvalidElementCount
|
||||
}
|
||||
elementsPerSlice := len(s) / sliceCount
|
||||
|
||||
result := make([][]T, sliceCount)
|
||||
for i := 0; i < sliceCount; i++ {
|
||||
|
||||
// Fill slice 'i' with strided data from 's'
|
||||
tmp := make([]T, elementsPerSlice)
|
||||
for j := 0; j < len(tmp); j++ {
|
||||
tmp[j] = s[sliceCount*j+i]
|
||||
}
|
||||
|
||||
result[i] = tmp
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// JoinHostPort is like net.JoinHostPort but accepts a default host and port.
|
||||
func JoinHostPort(host, port, defaultHost, defaultPort string) string {
|
||||
if host == "" {
|
||||
host = defaultHost
|
||||
}
|
||||
if port == "" {
|
||||
port = defaultPort
|
||||
}
|
||||
return net.JoinHostPort(host, port)
|
||||
}
|
||||
|
||||
// MakeAddr accepts an empty string, just a hostname or just a port number, or just a port name prefixed by a colon and complements the missing parts from the default address.
|
||||
//
|
||||
// MakeAddr("", "localhost:80") -> "localhost:80"
|
||||
// MakeAddr("1.1.1.1", "localhost:80") -> "1.1.1.1:80"
|
||||
// MakeAddr("81", "localhost:80") -> "localhost:81"
|
||||
// MakeAddr("foo", "localhost:80") -> "foo:80"
|
||||
// MakeAddr(":https", "localhost:80") -> "localhost:https"
|
||||
func MakeAddr(addr, defaultAddr string) string {
|
||||
if addr == "" {
|
||||
// Fast case
|
||||
return defaultAddr
|
||||
}
|
||||
|
||||
defaultHost, defaultPort, err := net.SplitHostPort(defaultAddr)
|
||||
if err != nil {
|
||||
panic(err) // default was malformed, error in code!
|
||||
}
|
||||
|
||||
i := strings.LastIndexByte(addr, ':')
|
||||
if i < 0 {
|
||||
if _, err = strconv.ParseInt(addr, 10, 16); err == nil {
|
||||
// Just a port number
|
||||
return net.JoinHostPort(defaultHost, addr)
|
||||
}
|
||||
// Just a host
|
||||
return net.JoinHostPort(addr, defaultPort)
|
||||
} else if i == 0 {
|
||||
// Just a port name
|
||||
return net.JoinHostPort(defaultHost, addr[1:])
|
||||
} else if i == len(addr)-1 {
|
||||
// Just a host with a colon
|
||||
return net.JoinHostPort(addr[:i], defaultPort)
|
||||
}
|
||||
|
||||
// Assume already full qualified
|
||||
return addr
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package util_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"go.maze.io/harmon/internal/util"
|
||||
)
|
||||
|
||||
func TestMakeAddr(t *testing.T) {
|
||||
var (
|
||||
testDefault = "localhost:80"
|
||||
testCases = []struct {
|
||||
Test string
|
||||
Want string
|
||||
}{
|
||||
{"", testDefault},
|
||||
{"81", "localhost:81"},
|
||||
{"1.1.1.1", "1.1.1.1:80"},
|
||||
{"foo", "foo:80"},
|
||||
{":https", "localhost:https"},
|
||||
{"1.2.3.4:", "1.2.3.4:80"},
|
||||
}
|
||||
)
|
||||
for _, test := range testCases {
|
||||
t.Run(test.Test, func(it *testing.T) {
|
||||
v := util.MakeAddr(test.Test, testDefault)
|
||||
if v != test.Want {
|
||||
it.Fatalf("expected %q to return %q, got %q", test.Test, test.Want, v)
|
||||
} else {
|
||||
it.Logf("%q resolved to %q", test.Test, v)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package util
|
||||
|
||||
import "periph.io/x/conn/v3/physic"
|
||||
|
||||
func ParseFrequency(s string) (physic.Frequency, error) {
|
||||
var f physic.Frequency
|
||||
if err := f.Set(s); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
Reference in new issue
Block a user