github.com/database64128/shadowsocks-go@v1.7.0/maps/maps.go (about)

     1  package maps
     2  
     3  // Keys returns the keys of the map m.
     4  // The keys will be in an indeterminate order.
     5  func Keys[M ~map[K]V, K comparable, V any](m M) []K {
     6  	r := make([]K, 0, len(m))
     7  	for k := range m {
     8  		r = append(r, k)
     9  	}
    10  	return r
    11  }
    12  
    13  // Values returns the values of the map m.
    14  // The values will be in an indeterminate order.
    15  func Values[M ~map[K]V, K comparable, V any](m M) []V {
    16  	r := make([]V, 0, len(m))
    17  	for _, v := range m {
    18  		r = append(r, v)
    19  	}
    20  	return r
    21  }
    22  
    23  // Clear removes all entries from m, leaving it empty.
    24  func Clear[M ~map[K]V, K comparable, V any](m M) {
    25  	for k := range m {
    26  		delete(m, k)
    27  	}
    28  }
    29  
    30  // Clone returns a copy of m.  This is a shallow clone:
    31  // the new keys and values are set using ordinary assignment.
    32  func Clone[M ~map[K]V, K comparable, V any](m M) M {
    33  	// Preserve nil in case it matters.
    34  	if m == nil {
    35  		return nil
    36  	}
    37  	r := make(M, len(m))
    38  	for k, v := range m {
    39  		r[k] = v
    40  	}
    41  	return r
    42  }