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

     1  package bmap
     2  
     3  // copies github.com/golang/exp/tree/master/maps
     4  
     5  // Keys returns the keys of the map m.
     6  // The keys will be in an indeterminate order.
     7  func Keys[M ~map[K]V, K comparable, V any](m M) []K {
     8  	r := make([]K, 0, len(m))
     9  	for k := range m {
    10  		r = append(r, k)
    11  	}
    12  	return r
    13  }
    14  
    15  // Values returns the values of the map m.
    16  // The values will be in an indeterminate order.
    17  func Values[M ~map[K]V, K comparable, V any](m M) []V {
    18  	r := make([]V, 0, len(m))
    19  	for _, v := range m {
    20  		r = append(r, v)
    21  	}
    22  	return r
    23  }
    24  
    25  // Equal reports whether two maps contain the same key/value pairs.
    26  // Values are compared using ==.
    27  func Equal[M1, M2 ~map[K]V, K, V comparable](m1 M1, m2 M2) bool {
    28  	if len(m1) != len(m2) {
    29  		return false
    30  	}
    31  	for k, v1 := range m1 {
    32  		if v2, ok := m2[k]; !ok || v1 != v2 {
    33  			return false
    34  		}
    35  	}
    36  	return true
    37  }
    38  
    39  // EqualFunc is like Equal, but compares values using eq.
    40  // Keys are still compared with ==.
    41  func EqualFunc[M1 ~map[K]V1, M2 ~map[K]V2, K comparable, V1, V2 any](m1 M1, m2 M2, eq func(V1, V2) bool) bool {
    42  	if len(m1) != len(m2) {
    43  		return false
    44  	}
    45  	for k, v1 := range m1 {
    46  		if v2, ok := m2[k]; !ok || !eq(v1, v2) {
    47  			return false
    48  		}
    49  	}
    50  	return true
    51  }
    52  
    53  // Clear removes all entries from m, leaving it empty.
    54  func Clear[M ~map[K]V, K comparable, V any](m M) {
    55  	for k := range m {
    56  		delete(m, k)
    57  	}
    58  }
    59  
    60  // Clone returns a copy of m.  This is a shallow clone:
    61  // the new keys and values are set using ordinary assignment.
    62  func Clone[M ~map[K]V, K comparable, V any](m M) M {
    63  	// Preserve nil in case it matters.
    64  	if m == nil {
    65  		return nil
    66  	}
    67  	r := make(M, len(m))
    68  	for k, v := range m {
    69  		r[k] = v
    70  	}
    71  	return r
    72  }
    73  
    74  // Copy copies all key/value pairs in src adding them to dst.
    75  // When a key in src is already present in dst,
    76  // the value in dst will be overwritten by the value associated
    77  // with the key in src.
    78  func Copy[M1 ~map[K]V, M2 ~map[K]V, K comparable, V any](dst M1, src M2) {
    79  	for k, v := range src {
    80  		dst[k] = v
    81  	}
    82  }
    83  
    84  // DeleteFunc deletes any key/value pairs from m for which del returns true.
    85  func DeleteFunc[M ~map[K]V, K comparable, V any](m M, del func(K, V) bool) {
    86  	for k, v := range m {
    87  		if del(k, v) {
    88  			delete(m, k)
    89  		}
    90  	}
    91  }