github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/base/bslice/calculable.go (about)

     1  package bslice
     2  
     3  import (
     4  	"github.com/songzhibin97/go-baseutils/base/bmath"
     5  	"github.com/songzhibin97/go-baseutils/base/bternaryexpr"
     6  	"github.com/songzhibin97/go-baseutils/base/btype"
     7  )
     8  
     9  // =====================================================================================================================
    10  // unsafe
    11  
    12  func NewUnsafeCalculableBSlice[E btype.Integer | btype.Float]() *UnsafeCalculableBSlice[E] {
    13  	return &UnsafeCalculableBSlice[E]{
    14  		UnsafeOrderedBSlice: NewUnsafeOrderedBSlice[E](),
    15  	}
    16  }
    17  
    18  func NewUnsafeCalculableBSliceBySlice[E btype.Integer | btype.Float](s []E) *UnsafeCalculableBSlice[E] {
    19  	return &UnsafeCalculableBSlice[E]{
    20  		UnsafeOrderedBSlice: NewUnsafeOrderedBSliceBySlice[E](s),
    21  	}
    22  }
    23  
    24  type UnsafeCalculableBSlice[E btype.Integer | btype.Float] struct {
    25  	*UnsafeOrderedBSlice[E]
    26  }
    27  
    28  func (x *UnsafeCalculableBSlice[E]) Sum() E {
    29  	var r E
    30  	for _, e := range x.ToMetaSlice() {
    31  		r += e
    32  	}
    33  	return r
    34  }
    35  
    36  func (x *UnsafeCalculableBSlice[E]) Avg() E {
    37  	var r E
    38  	list := x.ToMetaSlice()
    39  	ln := len(list)
    40  	for _, e := range list {
    41  		r += e
    42  	}
    43  	return x.Sum() / bternaryexpr.TernaryExpr(ln == 0, 1, E(ln))
    44  }
    45  
    46  func (x *UnsafeCalculableBSlice[E]) Max() E {
    47  	var r E
    48  	list := x.ToMetaSlice()
    49  	ln := len(list)
    50  	if ln != 0 {
    51  		r = list[0]
    52  	}
    53  	for i := 1; i < ln; i++ {
    54  		r = bmath.Max(r, list[i])
    55  	}
    56  	return r
    57  }
    58  
    59  func (x *UnsafeCalculableBSlice[E]) Min() E {
    60  	var r E
    61  	list := x.ToMetaSlice()
    62  	ln := len(list)
    63  	if ln != 0 {
    64  		r = list[0]
    65  	}
    66  	for i := 1; i < ln; i++ {
    67  		r = bmath.Min(r, list[i])
    68  	}
    69  	return r
    70  }
    71  
    72  // =====================================================================================================================
    73  // safe
    74  
    75  func NewSafeCalculableBSlice[E btype.Integer | btype.Float]() *SafeCalculableBSlice[E] {
    76  	return &SafeCalculableBSlice[E]{
    77  		SafeOrderedBSlice: NewSafeOrderedBSlice[E](),
    78  	}
    79  }
    80  
    81  func NewSafeCalculableBSliceBySlice[E btype.Integer | btype.Float](s []E) *SafeCalculableBSlice[E] {
    82  	return &SafeCalculableBSlice[E]{
    83  		SafeOrderedBSlice: NewSafeOrderedBSliceBySlice[E](s),
    84  	}
    85  }
    86  
    87  type SafeCalculableBSlice[E btype.Integer | btype.Float] struct {
    88  	*SafeOrderedBSlice[E]
    89  }
    90  
    91  func (x *SafeCalculableBSlice[E]) Sum() E {
    92  	list := x.ToMetaSlice()
    93  	var r E
    94  	for _, e := range list {
    95  		r += e
    96  	}
    97  	return r
    98  }
    99  
   100  func (x *SafeCalculableBSlice[E]) Avg() E {
   101  	var r E
   102  	list := x.ToMetaSlice()
   103  	ln := len(list)
   104  	for _, e := range list {
   105  		r += e
   106  	}
   107  	return x.Sum() / bternaryexpr.TernaryExpr(ln == 0, 1, E(ln))
   108  }
   109  
   110  func (x *SafeCalculableBSlice[E]) Max() E {
   111  	var r E
   112  	list := x.ToMetaSlice()
   113  	ln := len(list)
   114  	if ln != 0 {
   115  		r = list[0]
   116  	}
   117  	for i := 1; i < ln; i++ {
   118  		r = bmath.Max(r, list[i])
   119  	}
   120  	return r
   121  }
   122  
   123  func (x *SafeCalculableBSlice[E]) Min() E {
   124  	var r E
   125  	list := x.ToMetaSlice()
   126  	ln := len(list)
   127  	if ln != 0 {
   128  		r = list[0]
   129  	}
   130  	for i := 1; i < ln; i++ {
   131  		r = bmath.Min(r, list[i])
   132  	}
   133  	return r
   134  }