gitee.com/sy_183/go-common@v1.0.5-0.20231205030221-958cfe129b47/slice/slice.go (about)

     1  package slice
     2  
     3  import (
     4  	sliceUnsafe "gitee.com/sy_183/go-common/slice/unsafe"
     5  	"unsafe"
     6  )
     7  
     8  func JoinNew[E any](ss ...[]E) []E {
     9  	switch len(ss) {
    10  	case 0:
    11  		return nil
    12  	case 1:
    13  		return append([]E(nil), ss[0]...)
    14  	case 2:
    15  		return append(ss[0], ss[1]...)
    16  	default:
    17  		var n int
    18  		for _, s := range ss {
    19  			n += len(s)
    20  		}
    21  		s := make([]E, n)
    22  		sp := copy(s, ss[0])
    23  		for _, es := range ss[1:] {
    24  			sp += copy(s[sp:], es)
    25  		}
    26  		return s
    27  	}
    28  }
    29  
    30  func Assign[E any](s []E, l, c int) []E {
    31  	if cap(s) >= c {
    32  		return s[:l:c]
    33  	}
    34  	n := make([]E, l, c)
    35  	copy(n, s)
    36  	return n
    37  }
    38  
    39  //go:linkname growslice reflect.growslice
    40  func growslice(t unsafe.Pointer, old sliceUnsafe.Slice, num int) sliceUnsafe.Slice
    41  
    42  type emptyInterface struct {
    43  	typ  unsafe.Pointer
    44  	word unsafe.Pointer
    45  }
    46  
    47  func Grow[E any](s []E, n int) []E {
    48  	p := (*sliceUnsafe.Slice)(unsafe.Pointer(&s))
    49  	if p.Len+n > p.Cap {
    50  		a := any(s)
    51  		*p = growslice((*emptyInterface)(unsafe.Pointer(&a)).typ, *p, n)
    52  		return *(*[]E)(unsafe.Pointer(p))
    53  	}
    54  	return s
    55  }
    56  
    57  func AssignLen[E any](s []E, l int) []E {
    58  	if cap(s) >= l {
    59  		return s[:l]
    60  	}
    61  	s = Grow(s, l-len(s))
    62  	return s[:l]
    63  }
    64  
    65  func Delete[E any](s []E, i int) []E {
    66  	return s[:copy(s[i:], s[i+1:])+i]
    67  }
    68  
    69  func Deletes[E any](s []E, delete func(i int) bool) []E {
    70  	var ec int
    71  	i1, i2, i3 := -1, -1, -1
    72  	for i := 0; i < len(s); i++ {
    73  		if delete(i) {
    74  			if i1 < 0 {
    75  				i1, i2 = i, i+1
    76  			} else if i2 == i {
    77  				i2++
    78  			} else {
    79  				copy(s[i1-ec:], s[i2:i3])
    80  				ec += i2 - i1
    81  				i1, i2, i3 = i, i+1, -1
    82  			}
    83  		} else if i2 > 0 {
    84  			if i3 < 0 {
    85  				i3 = i + 1
    86  			} else if i3 == i {
    87  				i3++
    88  			}
    89  		}
    90  	}
    91  	if i2 > 0 {
    92  		if i3 > 0 {
    93  			copy(s[i1-ec:], s[i2:i3])
    94  		}
    95  		ec += i2 - i1
    96  	}
    97  	s = s[:len(s)-ec]
    98  	return s
    99  }
   100  
   101  func Reverse[E any](s []E) {
   102  	c := len(s) >> 1
   103  	for i := 0; i < c; i++ {
   104  		s[i], s[len(s)-i-1] = s[len(s)-i-1], s[i]
   105  	}
   106  }
   107  
   108  type Entry[E any] struct {
   109  	Index int
   110  	Elem  E
   111  }
   112  
   113  func NewEntry[E any](i int, elem E) Entry[E] {
   114  	return Entry[E]{Index: i, Elem: elem}
   115  }
   116  
   117  func ForEach[E any](s []E, action func(E)) {
   118  	for i := range s {
   119  		action(s[i])
   120  	}
   121  }
   122  
   123  func ForEachP[E any](s []E, action func(*E)) {
   124  	for i := range s {
   125  		action(&s[i])
   126  	}
   127  }
   128  
   129  func Map[I, O any](s []I, mapper func(I) O) (mapped []O) {
   130  	mapped = make([]O, len(s))
   131  	for i := range s {
   132  		mapped[i] = mapper(s[i])
   133  	}
   134  	return
   135  }
   136  
   137  func MapP[I, O any](s []I, mapper func(*I) O) (mapped []O) {
   138  	mapped = make([]O, len(s))
   139  	for i := range s {
   140  		mapped[i] = mapper(&s[i])
   141  	}
   142  	return
   143  }
   144  
   145  func Filter[E any](s []E, filter func(E) bool) (filtered []E) {
   146  	for i := range s {
   147  		if filter(s[i]) {
   148  			filtered = append(filtered, s[i])
   149  		}
   150  	}
   151  	return
   152  }
   153  
   154  func FilterP[E any](s []E, filter func(*E) bool) (filtered []E) {
   155  	for i := range s {
   156  		if filter(&s[i]) {
   157  			filtered = append(filtered, s[i])
   158  		}
   159  	}
   160  	return
   161  }
   162  
   163  func FilterPP[E any](s []E, filter func(*E) bool) (filtered []*E) {
   164  	for i := range s {
   165  		if filter(&s[i]) {
   166  			filtered = append(filtered, &s[i])
   167  		}
   168  	}
   169  	return
   170  }
   171  
   172  func Reduce[E, R any](s []E, reduce func(cur R, elem E) R, init R) R {
   173  	for i := 0; i < len(s); i++ {
   174  		init = reduce(init, s[i])
   175  	}
   176  	return init
   177  }
   178  
   179  func ReduceP[E, R any](s []E, reduce func(cur R, elem *E) R, init R) R {
   180  	for i := 0; i < len(s); i++ {
   181  		init = reduce(init, &s[i])
   182  	}
   183  	return init
   184  }