github.com/andeya/ameda@v1.5.3/float32s.go (about)

     1  package ameda
     2  
     3  // OneFloat32 try to return the first element, otherwise return zero value.
     4  func OneFloat32(f []float32) float32 {
     5  	if len(f) > 0 {
     6  		return f[0]
     7  	}
     8  	return 0
     9  }
    10  
    11  // Float32sCopy creates a copy of the float32 slice.
    12  func Float32sCopy(f []float32) []float32 {
    13  	b := make([]float32, len(f))
    14  	copy(b, f)
    15  	return b
    16  }
    17  
    18  // Float32sToInterfaces converts float32 slice to interface slice.
    19  func Float32sToInterfaces(f []float32) []interface{} {
    20  	r := make([]interface{}, len(f))
    21  	for k, v := range f {
    22  		r[k] = Float32ToInterface(v)
    23  	}
    24  	return r
    25  }
    26  
    27  // Float32sToStrings converts float32 slice to string slice.
    28  func Float32sToStrings(f []float32) []string {
    29  	r := make([]string, len(f))
    30  	for k, v := range f {
    31  		r[k] = Float32ToString(v)
    32  	}
    33  	return r
    34  }
    35  
    36  // Float32sToBools converts float32 slice to bool slice.
    37  // NOTE:
    38  //
    39  //	0 is false, everything else is true
    40  func Float32sToBools(f []float32) []bool {
    41  	r := make([]bool, len(f))
    42  	for k, v := range f {
    43  		r[k] = Float32ToBool(v)
    44  	}
    45  	return r
    46  }
    47  
    48  // Float32sToFloat64s converts float32 slice to float64 slice.
    49  func Float32sToFloat64s(f []float32) []float64 {
    50  	r := make([]float64, len(f))
    51  	for k, v := range f {
    52  		r[k] = Float32ToFloat64(v)
    53  	}
    54  	return r
    55  }
    56  
    57  // Float32sToInts converts float32 slice to int slice.
    58  func Float32sToInts(f []float32) ([]int, error) {
    59  	var err error
    60  	r := make([]int, len(f))
    61  	for k, v := range f {
    62  		r[k], err = Float32ToInt(v)
    63  		if err != nil {
    64  			return r, err
    65  		}
    66  	}
    67  	return r, nil
    68  }
    69  
    70  // Float32sToInt8s converts float32 slice to int8 slice.
    71  func Float32sToInt8s(f []float32) ([]int8, error) {
    72  	var err error
    73  	r := make([]int8, len(f))
    74  	for k, v := range f {
    75  		r[k], err = Float32ToInt8(v)
    76  		if err != nil {
    77  			return r, err
    78  		}
    79  	}
    80  	return r, nil
    81  }
    82  
    83  // Float32sToInt16s converts float32 slice to int16 slice.
    84  func Float32sToInt16s(f []float32) ([]int16, error) {
    85  	var err error
    86  	r := make([]int16, len(f))
    87  	for k, v := range f {
    88  		r[k], err = Float32ToInt16(v)
    89  		if err != nil {
    90  			return r, err
    91  		}
    92  	}
    93  	return r, nil
    94  }
    95  
    96  // Float32sToInt32s converts float32 slice to int32 slice.
    97  func Float32sToInt32s(f []float32) ([]int32, error) {
    98  	var err error
    99  	r := make([]int32, len(f))
   100  	for k, v := range f {
   101  		r[k], err = Float32ToInt32(v)
   102  		if err != nil {
   103  			return r, err
   104  		}
   105  	}
   106  	return r, nil
   107  }
   108  
   109  // Float32sToInt64s converts float32 slice to int64 slice.
   110  func Float32sToInt64s(f []float32) ([]int64, error) {
   111  	var err error
   112  	r := make([]int64, len(f))
   113  	for k, v := range f {
   114  		r[k], err = Float32ToInt64(v)
   115  		if err != nil {
   116  			return r, err
   117  		}
   118  	}
   119  	return r, nil
   120  }
   121  
   122  // Float32sToUints converts float32 slice to uint slice.
   123  func Float32sToUints(f []float32) ([]uint, error) {
   124  	var err error
   125  	r := make([]uint, len(f))
   126  	for k, v := range f {
   127  		r[k], err = Float32ToUint(v)
   128  		if err != nil {
   129  			return r, err
   130  		}
   131  	}
   132  	return r, nil
   133  }
   134  
   135  // Float32sToUint8s converts float32 slice to uint8 slice.
   136  func Float32sToUint8s(f []float32) ([]uint8, error) {
   137  	var err error
   138  	r := make([]uint8, len(f))
   139  	for k, v := range f {
   140  		r[k], err = Float32ToUint8(v)
   141  		if err != nil {
   142  			return r, err
   143  		}
   144  	}
   145  	return r, nil
   146  }
   147  
   148  // Float32sToUint16s converts float32 slice to uint16 slice.
   149  func Float32sToUint16s(f []float32) ([]uint16, error) {
   150  	var err error
   151  	r := make([]uint16, len(f))
   152  	for k, v := range f {
   153  		r[k], err = Float32ToUint16(v)
   154  		if err != nil {
   155  			return r, err
   156  		}
   157  	}
   158  	return r, nil
   159  }
   160  
   161  // Float32sToUint32s converts float32 slice to uint32 slice.
   162  func Float32sToUint32s(f []float32) ([]uint32, error) {
   163  	var err error
   164  	r := make([]uint32, len(f))
   165  	for k, v := range f {
   166  		r[k], err = Float32ToUint32(v)
   167  		if err != nil {
   168  			return r, err
   169  		}
   170  	}
   171  	return r, nil
   172  }
   173  
   174  // Float32sToUint64s converts float32 slice to uint64 slice.
   175  func Float32sToUint64s(f []float32) ([]uint64, error) {
   176  	var err error
   177  	r := make([]uint64, len(f))
   178  	for k, v := range f {
   179  		r[k], err = Float32ToUint64(v)
   180  		if err != nil {
   181  			return r, err
   182  		}
   183  	}
   184  	return r, nil
   185  }
   186  
   187  // Float32sCopyWithin copies part of an slice to another location in the current slice.
   188  // @target
   189  //
   190  //	Zero-based index at which to copy the sequence to. If negative, target will be counted from the end.
   191  //
   192  // @start
   193  //
   194  //	Zero-based index at which to start copying elements from. If negative, start will be counted from the end.
   195  //
   196  // @end
   197  //
   198  //	Zero-based index at which to end copying elements from. CopyWithin copies up to but not including end.
   199  //	If negative, end will be counted from the end.
   200  //	If end is omitted, CopyWithin will copy until the last index (default to len(s)).
   201  func Float32sCopyWithin(f []float32, target, start int, end ...int) {
   202  	target = fixIndex(len(f), target, true)
   203  	if target == len(f) {
   204  		return
   205  	}
   206  	sub := Float32sSlice(f, start, end...)
   207  	for k, v := range sub {
   208  		f[target+k] = v
   209  	}
   210  }
   211  
   212  // Float32sEvery tests whether all elements in the slice pass the test implemented by the provided function.
   213  // NOTE:
   214  //
   215  //	Calling this method on an empty slice will return true for any condition!
   216  func Float32sEvery(f []float32, fn func(f []float32, k int, v float32) bool) bool {
   217  	for k, v := range f {
   218  		if !fn(f, k, v) {
   219  			return false
   220  		}
   221  	}
   222  	return true
   223  }
   224  
   225  // Float32sFill changes all elements in the current slice to a value, from a start index to an end index.
   226  // @value
   227  //
   228  //	Zero-based index at which to copy the sequence to. If negative, target will be counted from the end.
   229  //
   230  // @start
   231  //
   232  //	Zero-based index at which to start copying elements from. If negative, start will be counted from the end.
   233  //
   234  // @end
   235  //
   236  //	Zero-based index at which to end copying elements from. CopyWithin copies up to but not including end.
   237  //	If negative, end will be counted from the end.
   238  //	If end is omitted, CopyWithin will copy until the last index (default to len(s)).
   239  func Float32sFill(f []float32, value float32, start int, end ...int) {
   240  	fixedStart, fixedEnd, ok := fixRange(len(f), start, end...)
   241  	if !ok {
   242  		return
   243  	}
   244  	for k := fixedStart; k < fixedEnd; k++ {
   245  		f[k] = value
   246  	}
   247  }
   248  
   249  // Float32sFilter creates a new slice with all elements that pass the test implemented by the provided function.
   250  func Float32sFilter(f []float32, fn func(f []float32, k int, v float32) bool) []float32 {
   251  	ret := make([]float32, 0)
   252  	for k, v := range f {
   253  		if fn(f, k, v) {
   254  			ret = append(ret, v)
   255  		}
   256  	}
   257  	return ret
   258  }
   259  
   260  // Float32sFind returns the key-value of the first element in the provided slice that satisfies the provided testing function.
   261  // NOTE:
   262  //
   263  //	If not found, k = -1
   264  func Float32sFind(f []float32, fn func(f []float32, k int, v float32) bool) (k int, v float32) {
   265  	for k, v := range f {
   266  		if fn(f, k, v) {
   267  			return k, v
   268  		}
   269  	}
   270  	return -1, 0
   271  }
   272  
   273  // Float32sIncludes determines whether an slice includes a certain value among its entries.
   274  // @fromIndex
   275  //
   276  //	The index to start the search at. Defaults to 0.
   277  func Float32sIncludes(f []float32, valueToFind float32, fromIndex ...int) bool {
   278  	return Float32sIndexOf(f, valueToFind, fromIndex...) > -1
   279  }
   280  
   281  // Float32sIndexOf returns the first index at which a given element can be found in the slice, or -1 if it is not present.
   282  // @fromIndex
   283  //
   284  //	The index to start the search at. Defaults to 0.
   285  func Float32sIndexOf(f []float32, searchElement float32, fromIndex ...int) int {
   286  	idx := getFromIndex(len(f), fromIndex...)
   287  	for k, v := range f[idx:] {
   288  		if searchElement == v {
   289  			return k + idx
   290  		}
   291  	}
   292  	return -1
   293  }
   294  
   295  // Float32sLastIndexOf returns the last index at which a given element can be found in the slice, or -1 if it is not present.
   296  // @fromIndex
   297  //
   298  //	The index to start the search at. Defaults to 0.
   299  func Float32sLastIndexOf(f []float32, searchElement float32, fromIndex ...int) int {
   300  	idx := getFromIndex(len(f), fromIndex...)
   301  	for k := len(f) - 1; k >= idx; k-- {
   302  		if searchElement == f[k] {
   303  			return k
   304  		}
   305  	}
   306  	return -1
   307  }
   308  
   309  // Float32sMap creates a new slice populated with the results of calling a provided function
   310  // on every element in the calling slice.
   311  func Float32sMap(f []float32, fn func(f []float32, k int, v float32) float32) []float32 {
   312  	ret := make([]float32, len(f))
   313  	for k, v := range f {
   314  		ret[k] = fn(f, k, v)
   315  	}
   316  	return ret
   317  }
   318  
   319  // Float32sPop removes the last element from an slice and returns that element.
   320  // This method changes the length of the slice.
   321  func Float32sPop(f *[]float32) (float32, bool) {
   322  	a := *f
   323  	if len(a) == 0 {
   324  		return 0, false
   325  	}
   326  	lastIndex := len(a) - 1
   327  	last := a[lastIndex]
   328  	a = a[:lastIndex]
   329  	*f = a[:len(a):len(a)]
   330  	return last, true
   331  }
   332  
   333  // Float32sPush adds one or more elements to the end of an slice and returns the new length of the slice.
   334  func Float32sPush(f *[]float32, element ...float32) int {
   335  	*f = append(*f, element...)
   336  	return len(*f)
   337  }
   338  
   339  // Float32sPushDistinct adds one or more new elements that do not exist in the current slice at the end.
   340  func Float32sPushDistinct(f []float32, element ...float32) []float32 {
   341  L:
   342  	for _, v := range element {
   343  		for _, vv := range f {
   344  			if vv == v {
   345  				continue L
   346  			}
   347  		}
   348  		f = append(f, v)
   349  	}
   350  	return f
   351  }
   352  
   353  // Float32sReduce executes a reducer function (that you provide) on each element of the slice,
   354  // resulting in a single output value.
   355  // @accumulator
   356  //
   357  //	The accumulator accumulates callback's return values.
   358  //	It is the accumulated value previously returned in the last invocation of the callback—or initialValue,
   359  //	if it was supplied (see below).
   360  //
   361  // @initialValue
   362  //
   363  //	A value to use as the first argument to the first call of the callback.
   364  //	If no initialValue is supplied, the first element in the slice will be used and skipped.
   365  func Float32sReduce(
   366  	f []float32,
   367  	fn func(f []float32, k int, v, accumulator float32) float32, initialValue ...float32,
   368  ) float32 {
   369  	if len(f) == 0 {
   370  		return 0
   371  	}
   372  	start := 0
   373  	acc := f[start]
   374  	if len(initialValue) > 0 {
   375  		acc = initialValue[0]
   376  	} else {
   377  		start += 1
   378  	}
   379  	for k := start; k < len(f); k++ {
   380  		acc = fn(f, k, f[k], acc)
   381  	}
   382  	return acc
   383  }
   384  
   385  // Float32sReduceRight applies a function against an accumulator and each value of the slice (from right-to-left)
   386  // to reduce it to a single value.
   387  // @accumulator
   388  //
   389  //	The accumulator accumulates callback's return values.
   390  //	It is the accumulated value previously returned in the last invocation of the callback—or initialValue,
   391  //	if it was supplied (see below).
   392  //
   393  // @initialValue
   394  //
   395  //	A value to use as the first argument to the first call of the callback.
   396  //	If no initialValue is supplied, the first element in the slice will be used and skipped.
   397  func Float32sReduceRight(
   398  	f []float32,
   399  	fn func(f []float32, k int, v, accumulator float32) float32, initialValue ...float32,
   400  ) float32 {
   401  	if len(f) == 0 {
   402  		return 0
   403  	}
   404  	end := len(f) - 1
   405  	acc := f[end]
   406  	if len(initialValue) > 0 {
   407  		acc = initialValue[0]
   408  	} else {
   409  		end -= 1
   410  	}
   411  	for k := end; k >= 0; k-- {
   412  		acc = fn(f, k, f[k], acc)
   413  	}
   414  	return acc
   415  }
   416  
   417  // Float32sReverse reverses an slice in place.
   418  func Float32sReverse(f []float32) {
   419  	first := 0
   420  	last := len(f) - 1
   421  	for first < last {
   422  		f[first], f[last] = f[last], f[first]
   423  		first++
   424  		last--
   425  	}
   426  }
   427  
   428  // Float32sShift removes the first element from an slice and returns that removed element.
   429  // This method changes the length of the slice.
   430  func Float32sShift(f *[]float32) (float32, bool) {
   431  	a := *f
   432  	if len(a) == 0 {
   433  		return 0, false
   434  	}
   435  	first := a[0]
   436  	a = a[1:]
   437  	*f = a[:len(a):len(a)]
   438  	return first, true
   439  }
   440  
   441  // Float32sSlice returns a copy of a portion of an slice into a new slice object selected
   442  // from begin to end (end not included) where begin and end represent the index of items in that slice.
   443  // The original slice will not be modified.
   444  func Float32sSlice(f []float32, begin int, end ...int) []float32 {
   445  	fixedStart, fixedEnd, ok := fixRange(len(f), begin, end...)
   446  	if !ok {
   447  		return []float32{}
   448  	}
   449  	return Float32sCopy(f[fixedStart:fixedEnd])
   450  }
   451  
   452  // Float32sSome tests whether at least one element in the slice passes the test implemented by the provided function.
   453  // NOTE:
   454  //
   455  //	Calling this method on an empty slice returns false for any condition!
   456  func Float32sSome(f []float32, fn func(f []float32, k int, v float32) bool) bool {
   457  	for k, v := range f {
   458  		if fn(f, k, v) {
   459  			return true
   460  		}
   461  	}
   462  	return false
   463  }
   464  
   465  // Float32sSplice changes the contents of an slice by removing or replacing
   466  // existing elements and/or adding new elements in place.
   467  func Float32sSplice(f *[]float32, start, deleteCount int, items ...float32) {
   468  	a := *f
   469  	if deleteCount < 0 {
   470  		deleteCount = 0
   471  	}
   472  	start, end, _ := fixRange(len(a), start, start+1+deleteCount)
   473  	deleteCount = end - start - 1
   474  	for k := 0; k < len(items); k++ {
   475  		if deleteCount > 0 {
   476  			// replace
   477  			a[start] = items[k]
   478  			deleteCount--
   479  			start++
   480  		} else {
   481  			// insert
   482  			lastSlice := Float32sCopy(a[start:])
   483  			items = items[k:]
   484  			a = append(a[:start], items...)
   485  			a = append(a[:start+len(items)], lastSlice...)
   486  			*f = a[:len(a):len(a)]
   487  			return
   488  		}
   489  	}
   490  	if deleteCount > 0 {
   491  		a = append(a[:start], a[start+1+deleteCount:]...)
   492  	}
   493  	*f = a[:len(a):len(a)]
   494  }
   495  
   496  // Float32sUnshift adds one or more elements to the beginning of an slice and returns the new length of the slice.
   497  func Float32sUnshift(f *[]float32, element ...float32) int {
   498  	*f = append(element, *f...)
   499  	return len(*f)
   500  }
   501  
   502  // Float32sUnshiftDistinct adds one or more new elements that do not exist in the current slice to the beginning
   503  // and returns the new length of the slice.
   504  func Float32sUnshiftDistinct(f *[]float32, element ...float32) int {
   505  	a := *f
   506  	if len(element) == 0 {
   507  		return len(a)
   508  	}
   509  	m := make(map[float32]bool, len(element))
   510  	r := make([]float32, 0, len(a)+len(element))
   511  L:
   512  	for _, v := range element {
   513  		if m[v] {
   514  			continue
   515  		}
   516  		m[v] = true
   517  		for _, vv := range a {
   518  			if vv == v {
   519  				continue L
   520  			}
   521  		}
   522  		r = append(r, v)
   523  	}
   524  	r = append(r, a...)
   525  	*f = r[:len(r):len(r)]
   526  	return len(r)
   527  }
   528  
   529  // Float32sRemoveFirst removes the first matched elements from the slice,
   530  // and returns the new length of the slice.
   531  func Float32sRemoveFirst(p *[]float32, elements ...float32) int {
   532  	a := *p
   533  	m := make(map[interface{}]struct{}, len(elements))
   534  	for _, element := range elements {
   535  		if _, ok := m[element]; ok {
   536  			continue
   537  		}
   538  		m[element] = struct{}{}
   539  		for k, v := range a {
   540  			if v == element {
   541  				a = append(a[:k], a[k+1:]...)
   542  				break
   543  			}
   544  		}
   545  	}
   546  	n := len(a)
   547  	*p = a[:n:n]
   548  	return n
   549  }
   550  
   551  // Float32sRemoveEvery removes all the elements from the slice,
   552  // and returns the new length of the slice.
   553  func Float32sRemoveEvery(p *[]float32, elements ...float32) int {
   554  	a := *p
   555  	m := make(map[interface{}]struct{}, len(elements))
   556  	for _, element := range elements {
   557  		if _, ok := m[element]; ok {
   558  			continue
   559  		}
   560  		m[element] = struct{}{}
   561  		for i := 0; i < len(a); i++ {
   562  			if a[i] == element {
   563  				a = append(a[:i], a[i+1:]...)
   564  				i--
   565  			}
   566  		}
   567  	}
   568  	n := len(a)
   569  	*p = a[:n:n]
   570  	return n
   571  }
   572  
   573  // Float32sIntersect calculates intersection of two or more slices,
   574  // and returns the count of each element.
   575  func Float32sIntersect(f ...[]float32) (intersectCount map[float32]int) {
   576  	if len(f) == 0 {
   577  		return nil
   578  	}
   579  	for _, v := range f {
   580  		if len(v) == 0 {
   581  			return nil
   582  		}
   583  	}
   584  	counts := make([]map[float32]int, len(f))
   585  	for k, v := range f {
   586  		counts[k] = float32sDistinct(v, nil)
   587  	}
   588  	intersectCount = counts[0]
   589  L:
   590  	for k, v := range intersectCount {
   591  		for _, c := range counts[1:] {
   592  			v2 := c[k]
   593  			if v2 == 0 {
   594  				delete(intersectCount, k)
   595  				continue L
   596  			}
   597  			if v > v2 {
   598  				v = v2
   599  			}
   600  		}
   601  		intersectCount[k] = v
   602  	}
   603  	return intersectCount
   604  }
   605  
   606  // Float32sDistinct calculates the count of each different element,
   607  // and only saves these different elements in place if changeSlice is true.
   608  func Float32sDistinct(f *[]float32, changeSlice bool) (distinctCount map[float32]int) {
   609  	if !changeSlice {
   610  		return float32sDistinct(*f, nil)
   611  	}
   612  	a := (*f)[:0]
   613  	distinctCount = float32sDistinct(*f, &a)
   614  	n := len(distinctCount)
   615  	*f = a[:n:n]
   616  	return distinctCount
   617  }
   618  
   619  func float32sDistinct(src []float32, dst *[]float32) map[float32]int {
   620  	m := make(map[float32]int, len(src))
   621  	if dst == nil {
   622  		for _, v := range src {
   623  			n := m[v]
   624  			m[v] = n + 1
   625  		}
   626  	} else {
   627  		a := *dst
   628  		for _, v := range src {
   629  			n := m[v]
   630  			m[v] = n + 1
   631  			if n == 0 {
   632  				a = append(a, v)
   633  			}
   634  		}
   635  		*dst = a
   636  	}
   637  	return m
   638  }
   639  
   640  // Float32SetUnion calculates between multiple collections: set1 ∪ set2 ∪ others...
   641  // This method does not change the existing slices, but instead returns a new slice.
   642  func Float32SetUnion(set1, set2 []float32, others ...[]float32) []float32 {
   643  	m := make(map[float32]struct{}, len(set1)+len(set2))
   644  	r := make([]float32, 0, len(m))
   645  	for _, set := range append([][]float32{set1, set2}, others...) {
   646  		for _, v := range set {
   647  			_, ok := m[v]
   648  			if ok {
   649  				continue
   650  			}
   651  			r = append(r, v)
   652  			m[v] = struct{}{}
   653  		}
   654  	}
   655  	return r
   656  }
   657  
   658  // Float32SetIntersect calculates between multiple collections: set1 ∩ set2 ∩ others...
   659  // This method does not change the existing slices, but instead returns a new slice.
   660  func Float32SetIntersect(set1, set2 []float32, others ...[]float32) []float32 {
   661  	sets := append([][]float32{set2}, others...)
   662  	setsCount := make([]map[float32]int, len(sets))
   663  	for k, v := range sets {
   664  		setsCount[k] = float32sDistinct(v, nil)
   665  	}
   666  	m := make(map[float32]struct{}, len(set1))
   667  	r := make([]float32, 0, len(m))
   668  L:
   669  	for _, v := range set1 {
   670  		if _, ok := m[v]; ok {
   671  			continue
   672  		}
   673  		m[v] = struct{}{}
   674  		for _, m2 := range setsCount {
   675  			if m2[v] == 0 {
   676  				continue L
   677  			}
   678  		}
   679  		r = append(r, v)
   680  	}
   681  	return r
   682  }
   683  
   684  // Float32SetDifference calculates between multiple collections: set1 - set2 - others...
   685  // This method does not change the existing slices, but instead returns a new slice.
   686  func Float32SetDifference(set1, set2 []float32, others ...[]float32) []float32 {
   687  	m := make(map[float32]struct{}, len(set1))
   688  	r := make([]float32, 0, len(set1))
   689  	sets := append([][]float32{set2}, others...)
   690  	for _, v := range sets {
   691  		inter := Float32SetIntersect(set1, v)
   692  		for _, v := range inter {
   693  			m[v] = struct{}{}
   694  		}
   695  	}
   696  	for _, v := range set1 {
   697  		if _, ok := m[v]; !ok {
   698  			r = append(r, v)
   699  			m[v] = struct{}{}
   700  		}
   701  	}
   702  	return r
   703  }