2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-10-29 22:27:22 +02:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2020-05-11 08:44:42 +02:00
|
|
|
package metric
|
2019-10-29 22:27:22 +02:00
|
|
|
|
|
|
|
//go:generate stringer -type=NumberKind
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-11-15 23:01:20 +02:00
|
|
|
"math"
|
2019-10-29 22:27:22 +02:00
|
|
|
"sync/atomic"
|
2020-05-11 08:44:42 +02:00
|
|
|
|
2020-08-18 05:25:03 +02:00
|
|
|
"go.opentelemetry.io/otel/internal"
|
2019-10-29 22:27:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// NumberKind describes the data type of the Number.
|
|
|
|
type NumberKind int8
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Int64NumberKind means that the Number stores int64.
|
|
|
|
Int64NumberKind NumberKind = iota
|
|
|
|
// Float64NumberKind means that the Number stores float64.
|
|
|
|
Float64NumberKind
|
|
|
|
)
|
|
|
|
|
2020-03-13 00:43:19 +02:00
|
|
|
// Zero returns a zero value for a given NumberKind
|
|
|
|
func (k NumberKind) Zero() Number {
|
|
|
|
switch k {
|
|
|
|
case Int64NumberKind:
|
|
|
|
return NewInt64Number(0)
|
|
|
|
case Float64NumberKind:
|
|
|
|
return NewFloat64Number(0.)
|
|
|
|
default:
|
|
|
|
return Number(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 19:51:49 +02:00
|
|
|
// Minimum returns the minimum representable value
|
|
|
|
// for a given NumberKind
|
|
|
|
func (k NumberKind) Minimum() Number {
|
|
|
|
switch k {
|
|
|
|
case Int64NumberKind:
|
|
|
|
return NewInt64Number(math.MinInt64)
|
|
|
|
case Float64NumberKind:
|
|
|
|
return NewFloat64Number(-1. * math.MaxFloat64)
|
|
|
|
default:
|
|
|
|
return Number(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 00:07:58 +02:00
|
|
|
// Maximum returns the maximum representable value
|
|
|
|
// for a given NumberKind
|
|
|
|
func (k NumberKind) Maximum() Number {
|
|
|
|
switch k {
|
|
|
|
case Int64NumberKind:
|
|
|
|
return NewInt64Number(math.MaxInt64)
|
|
|
|
case Float64NumberKind:
|
|
|
|
return NewFloat64Number(math.MaxFloat64)
|
|
|
|
default:
|
|
|
|
return Number(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-29 22:27:22 +02:00
|
|
|
// Number represents either an integral or a floating point value. It
|
|
|
|
// needs to be accompanied with a source of NumberKind that describes
|
|
|
|
// the actual type of the value stored within Number.
|
|
|
|
type Number uint64
|
|
|
|
|
|
|
|
// - constructors
|
|
|
|
|
|
|
|
// NewNumberFromRaw creates a new Number from a raw value.
|
|
|
|
func NewNumberFromRaw(r uint64) Number {
|
|
|
|
return Number(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewInt64Number creates an integral Number.
|
|
|
|
func NewInt64Number(i int64) Number {
|
2020-05-11 08:44:42 +02:00
|
|
|
return NewNumberFromRaw(internal.Int64ToRaw(i))
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewFloat64Number creates a floating point Number.
|
|
|
|
func NewFloat64Number(f float64) Number {
|
2020-05-11 08:44:42 +02:00
|
|
|
return NewNumberFromRaw(internal.Float64ToRaw(f))
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
2020-06-23 07:59:51 +02:00
|
|
|
// NewNumberSignChange returns a number with the same magnitude and
|
|
|
|
// the opposite sign. `kind` must describe the kind of number in `nn`.
|
|
|
|
//
|
|
|
|
// Does not change Uint64NumberKind values.
|
|
|
|
func NewNumberSignChange(kind NumberKind, nn Number) Number {
|
|
|
|
switch kind {
|
|
|
|
case Int64NumberKind:
|
|
|
|
return NewInt64Number(-nn.AsInt64())
|
|
|
|
case Float64NumberKind:
|
|
|
|
return NewFloat64Number(-nn.AsFloat64())
|
|
|
|
}
|
|
|
|
return nn
|
|
|
|
}
|
|
|
|
|
2019-10-29 22:27:22 +02:00
|
|
|
// - as x
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// AsNumber gets the Number.
|
2019-12-09 23:03:11 +02:00
|
|
|
func (n *Number) AsNumber() Number {
|
|
|
|
return *n
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// AsRaw gets the uninterpreted raw value. Might be useful for some
|
|
|
|
// atomic operations.
|
2019-12-09 23:03:11 +02:00
|
|
|
func (n *Number) AsRaw() uint64 {
|
|
|
|
return uint64(*n)
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// AsInt64 assumes that the value contains an int64 and returns it as
|
|
|
|
// such.
|
2019-12-09 23:03:11 +02:00
|
|
|
func (n *Number) AsInt64() int64 {
|
2020-05-11 08:44:42 +02:00
|
|
|
return internal.RawToInt64(n.AsRaw())
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// AsFloat64 assumes that the measurement value contains a float64 and
|
|
|
|
// returns it as such.
|
2019-12-09 23:03:11 +02:00
|
|
|
func (n *Number) AsFloat64() float64 {
|
2020-05-11 08:44:42 +02:00
|
|
|
return internal.RawToFloat64(n.AsRaw())
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// - as x atomic
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// AsNumberAtomic gets the Number atomically.
|
2019-10-29 22:27:22 +02:00
|
|
|
func (n *Number) AsNumberAtomic() Number {
|
|
|
|
return NewNumberFromRaw(n.AsRawAtomic())
|
|
|
|
}
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// AsRawAtomic gets the uninterpreted raw value atomically. Might be
|
|
|
|
// useful for some atomic operations.
|
2019-10-29 22:27:22 +02:00
|
|
|
func (n *Number) AsRawAtomic() uint64 {
|
|
|
|
return atomic.LoadUint64(n.AsRawPtr())
|
|
|
|
}
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// AsInt64Atomic assumes that the number contains an int64 and returns
|
|
|
|
// it as such atomically.
|
2019-10-29 22:27:22 +02:00
|
|
|
func (n *Number) AsInt64Atomic() int64 {
|
|
|
|
return atomic.LoadInt64(n.AsInt64Ptr())
|
|
|
|
}
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// AsFloat64Atomic assumes that the measurement value contains a
|
|
|
|
// float64 and returns it as such atomically.
|
2019-10-29 22:27:22 +02:00
|
|
|
func (n *Number) AsFloat64Atomic() float64 {
|
2020-05-11 08:44:42 +02:00
|
|
|
return internal.RawToFloat64(n.AsRawAtomic())
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// - as x ptr
|
|
|
|
|
|
|
|
// AsRawPtr gets the pointer to the raw, uninterpreted raw
|
|
|
|
// value. Might be useful for some atomic operations.
|
|
|
|
func (n *Number) AsRawPtr() *uint64 {
|
|
|
|
return (*uint64)(n)
|
|
|
|
}
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// AsInt64Ptr assumes that the number contains an int64 and returns a
|
|
|
|
// pointer to it.
|
2019-10-29 22:27:22 +02:00
|
|
|
func (n *Number) AsInt64Ptr() *int64 {
|
2020-05-11 08:44:42 +02:00
|
|
|
return internal.RawPtrToInt64Ptr(n.AsRawPtr())
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// AsFloat64Ptr assumes that the number contains a float64 and returns a
|
|
|
|
// pointer to it.
|
2019-10-29 22:27:22 +02:00
|
|
|
func (n *Number) AsFloat64Ptr() *float64 {
|
2020-05-11 08:44:42 +02:00
|
|
|
return internal.RawPtrToFloat64Ptr(n.AsRawPtr())
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// - coerce
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// CoerceToInt64 casts the number to int64. May result in
|
|
|
|
// data/precision loss.
|
2019-12-09 23:03:11 +02:00
|
|
|
func (n *Number) CoerceToInt64(kind NumberKind) int64 {
|
2019-10-29 22:27:22 +02:00
|
|
|
switch kind {
|
|
|
|
case Int64NumberKind:
|
|
|
|
return n.AsInt64()
|
|
|
|
case Float64NumberKind:
|
|
|
|
return int64(n.AsFloat64())
|
|
|
|
default:
|
|
|
|
// you get what you deserve
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// CoerceToFloat64 casts the number to float64. May result in
|
|
|
|
// data/precision loss.
|
2019-12-09 23:03:11 +02:00
|
|
|
func (n *Number) CoerceToFloat64(kind NumberKind) float64 {
|
2019-10-29 22:27:22 +02:00
|
|
|
switch kind {
|
|
|
|
case Int64NumberKind:
|
|
|
|
return float64(n.AsInt64())
|
|
|
|
case Float64NumberKind:
|
|
|
|
return n.AsFloat64()
|
|
|
|
default:
|
|
|
|
// you get what you deserve
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// - set
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// SetNumber sets the number to the passed number. Both should be of
|
|
|
|
// the same kind.
|
2019-10-29 22:27:22 +02:00
|
|
|
func (n *Number) SetNumber(nn Number) {
|
|
|
|
*n.AsRawPtr() = nn.AsRaw()
|
|
|
|
}
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// SetRaw sets the number to the passed raw value. Both number and the
|
|
|
|
// raw number should represent the same kind.
|
2019-10-29 22:27:22 +02:00
|
|
|
func (n *Number) SetRaw(r uint64) {
|
|
|
|
*n.AsRawPtr() = r
|
|
|
|
}
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// SetInt64 assumes that the number contains an int64 and sets it to
|
|
|
|
// the passed value.
|
2019-10-29 22:27:22 +02:00
|
|
|
func (n *Number) SetInt64(i int64) {
|
|
|
|
*n.AsInt64Ptr() = i
|
|
|
|
}
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// SetFloat64 assumes that the number contains a float64 and sets it
|
|
|
|
// to the passed value.
|
2019-10-29 22:27:22 +02:00
|
|
|
func (n *Number) SetFloat64(f float64) {
|
|
|
|
*n.AsFloat64Ptr() = f
|
|
|
|
}
|
|
|
|
|
|
|
|
// - set atomic
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// SetNumberAtomic sets the number to the passed number
|
|
|
|
// atomically. Both should be of the same kind.
|
2019-10-29 22:27:22 +02:00
|
|
|
func (n *Number) SetNumberAtomic(nn Number) {
|
|
|
|
atomic.StoreUint64(n.AsRawPtr(), nn.AsRaw())
|
|
|
|
}
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// SetRawAtomic sets the number to the passed raw value
|
|
|
|
// atomically. Both number and the raw number should represent the
|
|
|
|
// same kind.
|
2019-10-29 22:27:22 +02:00
|
|
|
func (n *Number) SetRawAtomic(r uint64) {
|
|
|
|
atomic.StoreUint64(n.AsRawPtr(), r)
|
|
|
|
}
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// SetInt64Atomic assumes that the number contains an int64 and sets
|
|
|
|
// it to the passed value atomically.
|
2019-10-29 22:27:22 +02:00
|
|
|
func (n *Number) SetInt64Atomic(i int64) {
|
|
|
|
atomic.StoreInt64(n.AsInt64Ptr(), i)
|
|
|
|
}
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// SetFloat64Atomic assumes that the number contains a float64 and
|
|
|
|
// sets it to the passed value atomically.
|
2019-10-29 22:27:22 +02:00
|
|
|
func (n *Number) SetFloat64Atomic(f float64) {
|
2020-05-11 08:44:42 +02:00
|
|
|
atomic.StoreUint64(n.AsRawPtr(), internal.Float64ToRaw(f))
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// - swap
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// SwapNumber sets the number to the passed number and returns the old
|
|
|
|
// number. Both this number and the passed number should be of the
|
|
|
|
// same kind.
|
2019-10-29 22:27:22 +02:00
|
|
|
func (n *Number) SwapNumber(nn Number) Number {
|
|
|
|
old := *n
|
|
|
|
n.SetNumber(nn)
|
|
|
|
return old
|
|
|
|
}
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// SwapRaw sets the number to the passed raw value and returns the old
|
|
|
|
// raw value. Both number and the raw number should represent the same
|
|
|
|
// kind.
|
2019-10-29 22:27:22 +02:00
|
|
|
func (n *Number) SwapRaw(r uint64) uint64 {
|
|
|
|
old := n.AsRaw()
|
|
|
|
n.SetRaw(r)
|
|
|
|
return old
|
|
|
|
}
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// SwapInt64 assumes that the number contains an int64, sets it to the
|
|
|
|
// passed value and returns the old int64 value.
|
2019-10-29 22:27:22 +02:00
|
|
|
func (n *Number) SwapInt64(i int64) int64 {
|
|
|
|
old := n.AsInt64()
|
|
|
|
n.SetInt64(i)
|
|
|
|
return old
|
|
|
|
}
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// SwapFloat64 assumes that the number contains an float64, sets it to
|
|
|
|
// the passed value and returns the old float64 value.
|
2019-10-29 22:27:22 +02:00
|
|
|
func (n *Number) SwapFloat64(f float64) float64 {
|
|
|
|
old := n.AsFloat64()
|
|
|
|
n.SetFloat64(f)
|
|
|
|
return old
|
|
|
|
}
|
|
|
|
|
|
|
|
// - swap atomic
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// SwapNumberAtomic sets the number to the passed number and returns
|
|
|
|
// the old number atomically. Both this number and the passed number
|
|
|
|
// should be of the same kind.
|
2019-10-29 22:27:22 +02:00
|
|
|
func (n *Number) SwapNumberAtomic(nn Number) Number {
|
|
|
|
return NewNumberFromRaw(atomic.SwapUint64(n.AsRawPtr(), nn.AsRaw()))
|
|
|
|
}
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// SwapRawAtomic sets the number to the passed raw value and returns
|
|
|
|
// the old raw value atomically. Both number and the raw number should
|
|
|
|
// represent the same kind.
|
2019-10-29 22:27:22 +02:00
|
|
|
func (n *Number) SwapRawAtomic(r uint64) uint64 {
|
|
|
|
return atomic.SwapUint64(n.AsRawPtr(), r)
|
|
|
|
}
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// SwapInt64Atomic assumes that the number contains an int64, sets it
|
|
|
|
// to the passed value and returns the old int64 value atomically.
|
2019-10-29 22:27:22 +02:00
|
|
|
func (n *Number) SwapInt64Atomic(i int64) int64 {
|
|
|
|
return atomic.SwapInt64(n.AsInt64Ptr(), i)
|
|
|
|
}
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// SwapFloat64Atomic assumes that the number contains an float64, sets
|
|
|
|
// it to the passed value and returns the old float64 value
|
|
|
|
// atomically.
|
2019-10-29 22:27:22 +02:00
|
|
|
func (n *Number) SwapFloat64Atomic(f float64) float64 {
|
2020-05-11 08:44:42 +02:00
|
|
|
return internal.RawToFloat64(atomic.SwapUint64(n.AsRawPtr(), internal.Float64ToRaw(f)))
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// - add
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// AddNumber assumes that this and the passed number are of the passed
|
|
|
|
// kind and adds the passed number to this number.
|
2019-10-29 22:27:22 +02:00
|
|
|
func (n *Number) AddNumber(kind NumberKind, nn Number) {
|
|
|
|
switch kind {
|
|
|
|
case Int64NumberKind:
|
|
|
|
n.AddInt64(nn.AsInt64())
|
|
|
|
case Float64NumberKind:
|
|
|
|
n.AddFloat64(nn.AsFloat64())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// AddRaw assumes that this number and the passed raw value are of the
|
|
|
|
// passed kind and adds the passed raw value to this number.
|
2019-10-29 22:27:22 +02:00
|
|
|
func (n *Number) AddRaw(kind NumberKind, r uint64) {
|
|
|
|
n.AddNumber(kind, NewNumberFromRaw(r))
|
|
|
|
}
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// AddInt64 assumes that the number contains an int64 and adds the
|
|
|
|
// passed int64 to it.
|
2019-10-29 22:27:22 +02:00
|
|
|
func (n *Number) AddInt64(i int64) {
|
|
|
|
*n.AsInt64Ptr() += i
|
|
|
|
}
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// AddFloat64 assumes that the number contains a float64 and adds the
|
|
|
|
// passed float64 to it.
|
2019-10-29 22:27:22 +02:00
|
|
|
func (n *Number) AddFloat64(f float64) {
|
|
|
|
*n.AsFloat64Ptr() += f
|
|
|
|
}
|
|
|
|
|
|
|
|
// - add atomic
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// AddNumberAtomic assumes that this and the passed number are of the
|
|
|
|
// passed kind and adds the passed number to this number atomically.
|
2019-10-29 22:27:22 +02:00
|
|
|
func (n *Number) AddNumberAtomic(kind NumberKind, nn Number) {
|
|
|
|
switch kind {
|
|
|
|
case Int64NumberKind:
|
|
|
|
n.AddInt64Atomic(nn.AsInt64())
|
|
|
|
case Float64NumberKind:
|
|
|
|
n.AddFloat64Atomic(nn.AsFloat64())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// AddRawAtomic assumes that this number and the passed raw value are
|
|
|
|
// of the passed kind and adds the passed raw value to this number
|
|
|
|
// atomically.
|
2019-10-29 22:27:22 +02:00
|
|
|
func (n *Number) AddRawAtomic(kind NumberKind, r uint64) {
|
|
|
|
n.AddNumberAtomic(kind, NewNumberFromRaw(r))
|
|
|
|
}
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// AddInt64Atomic assumes that the number contains an int64 and adds
|
|
|
|
// the passed int64 to it atomically.
|
2019-10-29 22:27:22 +02:00
|
|
|
func (n *Number) AddInt64Atomic(i int64) {
|
|
|
|
atomic.AddInt64(n.AsInt64Ptr(), i)
|
|
|
|
}
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// AddFloat64Atomic assumes that the number contains a float64 and
|
|
|
|
// adds the passed float64 to it atomically.
|
2019-10-29 22:27:22 +02:00
|
|
|
func (n *Number) AddFloat64Atomic(f float64) {
|
|
|
|
for {
|
|
|
|
o := n.AsFloat64Atomic()
|
|
|
|
if n.CompareAndSwapFloat64(o, o+f) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// - compare and swap (atomic only)
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// CompareAndSwapNumber does the atomic CAS operation on this
|
|
|
|
// number. This number and passed old and new numbers should be of the
|
|
|
|
// same kind.
|
2019-10-29 22:27:22 +02:00
|
|
|
func (n *Number) CompareAndSwapNumber(on, nn Number) bool {
|
|
|
|
return atomic.CompareAndSwapUint64(n.AsRawPtr(), on.AsRaw(), nn.AsRaw())
|
|
|
|
}
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// CompareAndSwapRaw does the atomic CAS operation on this
|
|
|
|
// number. This number and passed old and new raw values should be of
|
|
|
|
// the same kind.
|
2019-10-29 22:27:22 +02:00
|
|
|
func (n *Number) CompareAndSwapRaw(or, nr uint64) bool {
|
|
|
|
return atomic.CompareAndSwapUint64(n.AsRawPtr(), or, nr)
|
|
|
|
}
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// CompareAndSwapInt64 assumes that this number contains an int64 and
|
|
|
|
// does the atomic CAS operation on it.
|
2019-10-29 22:27:22 +02:00
|
|
|
func (n *Number) CompareAndSwapInt64(oi, ni int64) bool {
|
|
|
|
return atomic.CompareAndSwapInt64(n.AsInt64Ptr(), oi, ni)
|
|
|
|
}
|
|
|
|
|
2019-10-31 23:55:51 +02:00
|
|
|
// CompareAndSwapFloat64 assumes that this number contains a float64 and
|
|
|
|
// does the atomic CAS operation on it.
|
2019-10-29 22:27:22 +02:00
|
|
|
func (n *Number) CompareAndSwapFloat64(of, nf float64) bool {
|
2020-05-11 08:44:42 +02:00
|
|
|
return atomic.CompareAndSwapUint64(n.AsRawPtr(), internal.Float64ToRaw(of), internal.Float64ToRaw(nf))
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// - compare
|
|
|
|
|
|
|
|
// CompareNumber compares two Numbers given their kind. Both numbers
|
|
|
|
// should have the same kind. This returns:
|
|
|
|
// 0 if the numbers are equal
|
|
|
|
// -1 if the subject `n` is less than the argument `nn`
|
|
|
|
// +1 if the subject `n` is greater than the argument `nn`
|
2019-12-09 23:03:11 +02:00
|
|
|
func (n *Number) CompareNumber(kind NumberKind, nn Number) int {
|
2019-10-29 22:27:22 +02:00
|
|
|
switch kind {
|
|
|
|
case Int64NumberKind:
|
|
|
|
return n.CompareInt64(nn.AsInt64())
|
|
|
|
case Float64NumberKind:
|
|
|
|
return n.CompareFloat64(nn.AsFloat64())
|
|
|
|
default:
|
|
|
|
// you get what you deserve
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CompareRaw compares two numbers, where one is input as a raw
|
|
|
|
// uint64, interpreting both values as a `kind` of number.
|
2019-12-09 23:03:11 +02:00
|
|
|
func (n *Number) CompareRaw(kind NumberKind, r uint64) int {
|
2019-10-29 22:27:22 +02:00
|
|
|
return n.CompareNumber(kind, NewNumberFromRaw(r))
|
|
|
|
}
|
|
|
|
|
|
|
|
// CompareInt64 assumes that the Number contains an int64 and performs
|
|
|
|
// a comparison between the value and the other value. It returns the
|
|
|
|
// typical result of the compare function: -1 if the value is less
|
|
|
|
// than the other, 0 if both are equal, 1 if the value is greater than
|
|
|
|
// the other.
|
2019-12-09 23:03:11 +02:00
|
|
|
func (n *Number) CompareInt64(i int64) int {
|
2019-10-29 22:27:22 +02:00
|
|
|
this := n.AsInt64()
|
|
|
|
if this < i {
|
|
|
|
return -1
|
|
|
|
} else if this > i {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// CompareFloat64 assumes that the Number contains a float64 and
|
|
|
|
// performs a comparison between the value and the other value. It
|
|
|
|
// returns the typical result of the compare function: -1 if the value
|
|
|
|
// is less than the other, 0 if both are equal, 1 if the value is
|
|
|
|
// greater than the other.
|
2019-11-05 00:24:01 +02:00
|
|
|
//
|
|
|
|
// Do not compare NaN values.
|
2019-12-09 23:03:11 +02:00
|
|
|
func (n *Number) CompareFloat64(f float64) int {
|
2019-10-29 22:27:22 +02:00
|
|
|
this := n.AsFloat64()
|
|
|
|
if this < f {
|
|
|
|
return -1
|
|
|
|
} else if this > f {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// - relations to zero
|
|
|
|
|
|
|
|
// IsPositive returns true if the actual value is greater than zero.
|
2019-12-09 23:03:11 +02:00
|
|
|
func (n *Number) IsPositive(kind NumberKind) bool {
|
2019-10-29 22:27:22 +02:00
|
|
|
return n.compareWithZero(kind) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsNegative returns true if the actual value is less than zero.
|
2019-12-09 23:03:11 +02:00
|
|
|
func (n *Number) IsNegative(kind NumberKind) bool {
|
2019-10-29 22:27:22 +02:00
|
|
|
return n.compareWithZero(kind) < 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsZero returns true if the actual value is equal to zero.
|
2019-12-09 23:03:11 +02:00
|
|
|
func (n *Number) IsZero(kind NumberKind) bool {
|
2019-10-29 22:27:22 +02:00
|
|
|
return n.compareWithZero(kind) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// - misc
|
|
|
|
|
|
|
|
// Emit returns a string representation of the raw value of the
|
|
|
|
// Number. A %d is used for integral values, %f for floating point
|
|
|
|
// values.
|
2019-12-09 23:03:11 +02:00
|
|
|
func (n *Number) Emit(kind NumberKind) string {
|
2019-10-29 22:27:22 +02:00
|
|
|
switch kind {
|
|
|
|
case Int64NumberKind:
|
|
|
|
return fmt.Sprintf("%d", n.AsInt64())
|
|
|
|
case Float64NumberKind:
|
|
|
|
return fmt.Sprintf("%f", n.AsFloat64())
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
// AsInterface returns the number as an interface{}, typically used
|
|
|
|
// for NumberKind-correct JSON conversion.
|
2019-12-09 23:03:11 +02:00
|
|
|
func (n *Number) AsInterface(kind NumberKind) interface{} {
|
2019-11-15 23:01:20 +02:00
|
|
|
switch kind {
|
|
|
|
case Int64NumberKind:
|
|
|
|
return n.AsInt64()
|
|
|
|
case Float64NumberKind:
|
|
|
|
return n.AsFloat64()
|
|
|
|
default:
|
|
|
|
return math.NaN()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-29 22:27:22 +02:00
|
|
|
// - private stuff
|
|
|
|
|
2019-12-09 23:03:11 +02:00
|
|
|
func (n *Number) compareWithZero(kind NumberKind) int {
|
2019-10-29 22:27:22 +02:00
|
|
|
switch kind {
|
|
|
|
case Int64NumberKind:
|
|
|
|
return n.CompareInt64(0)
|
|
|
|
case Float64NumberKind:
|
|
|
|
return n.CompareFloat64(0.)
|
|
|
|
default:
|
|
|
|
// you get what you deserve
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|