golang.org/x/tools/gopls@v0.15.3/internal/util/maps/maps.go (about)

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package maps
     6  
     7  // Group returns a new non-nil map containing the elements of s grouped by the
     8  // keys returned from the key func.
     9  func Group[K comparable, V any](s []V, key func(V) K) map[K][]V {
    10  	m := make(map[K][]V)
    11  	for _, v := range s {
    12  		k := key(v)
    13  		m[k] = append(m[k], v)
    14  	}
    15  	return m
    16  }
    17  
    18  // Keys returns the keys of the map M.
    19  func Keys[M ~map[K]V, K comparable, V any](m M) []K {
    20  	r := make([]K, 0, len(m))
    21  	for k := range m {
    22  		r = append(r, k)
    23  	}
    24  	return r
    25  }
    26  
    27  // SameKeys reports whether x and y have equal sets of keys.
    28  func SameKeys[K comparable, V1, V2 any](x map[K]V1, y map[K]V2) bool {
    29  	if len(x) != len(y) {
    30  		return false
    31  	}
    32  	for k := range x {
    33  		if _, ok := y[k]; !ok {
    34  			return false
    35  		}
    36  	}
    37  	return true
    38  }