From 3a2d712d31c6259fb4ef4ecb6dbcccc152e829c7 Mon Sep 17 00:00:00 2001 From: Paul Wells Date: Tue, 17 Sep 2024 23:03:12 -0700 Subject: [PATCH] add utilities for applying options and copying protobufs --- utils/math.go | 37 ++++++++----------------------------- utils/options/options.go | 22 ++++++++++++++++++++++ utils/proto.go | 21 +++++++++++++++++++++ 3 files changed, 51 insertions(+), 29 deletions(-) create mode 100644 utils/options/options.go create mode 100644 utils/proto.go diff --git a/utils/math.go b/utils/math.go index 6f663221..02c1d439 100644 --- a/utils/math.go +++ b/utils/math.go @@ -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 } diff --git a/utils/options/options.go b/utils/options/options.go new file mode 100644 index 00000000..b2ef975b --- /dev/null +++ b/utils/options/options.go @@ -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 +} diff --git a/utils/proto.go b/utils/proto.go new file mode 100644 index 00000000..61389d00 --- /dev/null +++ b/utils/proto.go @@ -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) +}