github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/tools/slice.go (about)

     1  package tools
     2  
     3  import (
     4  	"bytes"
     5  	"reflect"
     6  )
     7  
     8  /*
     9     Creation Time: 2021 - Jan - 27
    10     Created by:  (ehsan)
    11     Maintainers:
    12        1.  Ehsan N. Moosa (E2)
    13     Auditor: Ehsan N. Moosa (E2)
    14     Copyright Ronak Software Group 2020
    15  */
    16  
    17  // DeleteItemFromSlice deletes item from slice. 'slice' MUST be a slice otherwise panics.
    18  // 'index' MUST be valid otherwise panics.
    19  func DeleteItemFromSlice(slice interface{}, index int) {
    20  	v := reflect.ValueOf(slice)
    21  	if v.Kind() == reflect.Ptr {
    22  		v = reflect.Indirect(v)
    23  	}
    24  	vLength := v.Len()
    25  	if v.Kind() != reflect.Slice {
    26  		panic("slice is not valid")
    27  	}
    28  	if index >= vLength || index < 0 {
    29  		panic("invalid index")
    30  	}
    31  	switch vLength {
    32  	case 1:
    33  		v.SetLen(0)
    34  	default:
    35  		v.Index(index).Set(v.Index(v.Len() - 1))
    36  		v.SetLen(vLength - 1)
    37  	}
    38  }
    39  
    40  // SliceInt64Diff returns a - b and cb will be called on each found difference.
    41  func SliceInt64Diff(a, b []int64, cb func(int64)) {
    42  	for i := 0; i < len(a); i++ {
    43  		found := false
    44  		for j := 0; j < len(b); j++ {
    45  			if a[i] == b[j] {
    46  				found = true
    47  
    48  				break
    49  			}
    50  		}
    51  		if !found && cb != nil {
    52  			cb(a[i])
    53  		}
    54  	}
    55  }
    56  
    57  // SliceUint64Diff returns a - b and cb will be called on each found difference.
    58  func SliceUint64Diff(a, b []uint64, cb func(uint64)) {
    59  	for i := 0; i < len(a); i++ {
    60  		found := false
    61  		for j := 0; j < len(b); j++ {
    62  			if a[i] == b[j] {
    63  				found = true
    64  
    65  				break
    66  			}
    67  		}
    68  		if !found && cb != nil {
    69  			cb(a[i])
    70  		}
    71  	}
    72  }
    73  
    74  // SliceInt32Diff returns a - b and cb will be called on each found difference.
    75  func SliceInt32Diff(a, b []int32, cb func(int32)) {
    76  	for i := 0; i < len(a); i++ {
    77  		found := false
    78  		for j := 0; j < len(b); j++ {
    79  			if a[i] == b[j] {
    80  				found = true
    81  
    82  				break
    83  			}
    84  		}
    85  		if !found && cb != nil {
    86  			cb(a[i])
    87  		}
    88  	}
    89  }
    90  
    91  // SliceUint32Diff returns a - b and cb will be called on each found difference.
    92  func SliceUint32Diff(a, b []uint32, cb func(uint32)) {
    93  	for i := 0; i < len(a); i++ {
    94  		found := false
    95  		for j := 0; j < len(b); j++ {
    96  			if a[i] == b[j] {
    97  				found = true
    98  
    99  				break
   100  			}
   101  		}
   102  		if !found && cb != nil {
   103  			cb(a[i])
   104  		}
   105  	}
   106  }
   107  
   108  // SliceIntDiff returns a - b and cb will be called on each found difference.
   109  func SliceIntDiff(a, b []int, cb func(int)) {
   110  	for i := 0; i < len(a); i++ {
   111  		found := false
   112  		for j := 0; j < len(b); j++ {
   113  			if a[i] == b[j] {
   114  				found = true
   115  
   116  				break
   117  			}
   118  		}
   119  		if !found && cb != nil {
   120  			cb(a[i])
   121  		}
   122  	}
   123  }
   124  
   125  // SliceUintDiff returns a - b and cb will be called on each found difference.
   126  func SliceUintDiff(a, b []uint, cb func(uint)) {
   127  	for i := 0; i < len(a); i++ {
   128  		found := false
   129  		for j := 0; j < len(b); j++ {
   130  			if a[i] == b[j] {
   131  				found = true
   132  
   133  				break
   134  			}
   135  		}
   136  		if !found && cb != nil {
   137  			cb(a[i])
   138  		}
   139  	}
   140  }
   141  
   142  // SliceStringDiff returns a - b and cb will be called on each found difference.
   143  func SliceStringDiff(a, b []string, cb func(string)) {
   144  	for i := 0; i < len(a); i++ {
   145  		found := false
   146  		for j := 0; j < len(b); j++ {
   147  			if a[i] == b[j] {
   148  				found = true
   149  
   150  				break
   151  			}
   152  		}
   153  		if !found && cb != nil {
   154  			cb(a[i])
   155  		}
   156  	}
   157  }
   158  
   159  // SliceBytesDiff returns a - b and cb will be called on each found difference.
   160  func SliceBytesDiff(a, b [][]byte, cb func([]byte)) {
   161  	for i := 0; i < len(a); i++ {
   162  		found := false
   163  		for j := 0; j < len(b); j++ {
   164  			if bytes.Equal(a[i], b[j]) {
   165  				found = true
   166  
   167  				break
   168  			}
   169  		}
   170  		if !found && cb != nil {
   171  			cb(a[i])
   172  		}
   173  	}
   174  }