42 lines
705 B
Go
42 lines
705 B
Go
package lfu
|
|
|
|
import (
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type Measurable interface {
|
|
Size() (int, error)
|
|
}
|
|
|
|
func DefaultGetValueSize[V any](value V) (int, error) {
|
|
switch v := any(value).(type) {
|
|
case int:
|
|
return v, nil
|
|
case int8:
|
|
return int(v), nil
|
|
case int32:
|
|
return int(v), nil
|
|
case int64:
|
|
return int(v), nil
|
|
case float32:
|
|
return int(v), nil
|
|
case float64:
|
|
return int(v), nil
|
|
case []byte:
|
|
return len(v), nil
|
|
case string:
|
|
return len(v), nil
|
|
}
|
|
|
|
if measurable, ok := any(value).(Measurable); ok {
|
|
size, err := measurable.Size()
|
|
if err != nil {
|
|
return 0, errors.WithStack(err)
|
|
}
|
|
|
|
return size, nil
|
|
}
|
|
|
|
return 0, errors.Errorf("could not retrieve size of type '%T'", value)
|
|
}
|