Skip to content

Commit

Permalink
add utilities for applying options and copying protobufs
Browse files Browse the repository at this point in the history
  • Loading branch information
paulwe committed Sep 18, 2024
1 parent 9675e0c commit 3a2d712
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 29 deletions.
37 changes: 8 additions & 29 deletions utils/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,16 @@

package utils

import (
"time"
import "math"

"golang.org/x/exp/constraints"
)

type Numeric interface {
constraints.Signed | constraints.Unsigned | time.Duration
}

func Max[T Numeric](vs ...T) T {
return Least(func(a, b T) bool { return a > b }, vs...)
}

func Min[T Numeric](vs ...T) T {
return Least(func(a, b T) bool { return a < b }, vs...)
}

func Most[T Numeric](less func(a, b T) bool, vs ...T) T {
return Least(func(a, b T) bool { return !less(a, b) }, vs...)
func LogisticFunc(x0, L, k float64) func(x float64) float64 {
return func(x float64) float64 {
return L / (1 + math.Pow(math.E, -k*(x-x0)))
}
}

func Least[T Numeric](less func(a, b T) bool, vs ...T) T {
if len(vs) == 0 {
return 0
}
v := vs[0]
for i := 1; i < len(vs); i++ {
if less(vs[i], v) {
v = vs[i]
}
func FastLogisticFunc(x0, L, k float64) func(x float64) float64 {
return func(x float64) float64 {
return L / 2 * (1 + k*(x-x0)/(1+math.Abs(k*(x-x0))))
}
return v
}
22 changes: 22 additions & 0 deletions utils/options/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2023 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package options

func Apply[T any, F ~func(T)](o T, opts []F) T {
for _, opt := range opts {
opt(o)
}
return o
}
21 changes: 21 additions & 0 deletions utils/proto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2023 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package utils

import "google.golang.org/protobuf/proto"

func CloneProto[T proto.Message](m T) T {
return proto.Clone(m).(T)
}

0 comments on commit 3a2d712

Please sign in to comment.