istio

Форк
0
/
maps.go 
77 строк · 2.5 Кб
1
// Copyright Istio Authors
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
package maps
16

17
import "maps" // nolint: depguard
18

19
// Equal reports whether two maps contain the same key/value pairs.
20
// Values are compared using ==.
21
func Equal[M1, M2 ~map[K]V, K, V comparable](m1 M1, m2 M2) bool {
22
	return maps.Equal(m1, m2)
23
}
24

25
// Clone returns a copy of the map.
26
// The elements are copied using assignment, so this is a shallow clone.
27
func Clone[M ~map[K]V, K comparable, V any](m M) M {
28
	return maps.Clone(m)
29
}
30

31
// Values returns the values of the map m.
32
// The values will be in an indeterminate order.
33
func Values[M ~map[K]V, K comparable, V any](m M) []V {
34
	r := make([]V, 0, len(m))
35
	for _, v := range m {
36
		r = append(r, v)
37
	}
38
	return r
39
}
40

41
// Keys returns the keys of the map m.
42
// The keys will be in an indeterminate order.
43
func Keys[M ~map[K]V, K comparable, V any](m M) []K {
44
	r := make([]K, 0, len(m))
45
	for k := range m {
46
		r = append(r, k)
47
	}
48
	return r
49
}
50

51
// MergeCopy creates a new map by merging all key/value pairs from base and override.
52
// When a key in override is already present in base,
53
// the value in base will be overwritten by the value associated
54
// with the key in override.
55
func MergeCopy[M1 ~map[K]V, M2 ~map[K]V, K comparable, V any](base M1, override M2) M1 {
56
	dst := make(M1, len(base)+len(override))
57
	maps.Copy(dst, base)
58
	maps.Copy(dst, override)
59
	return dst
60
}
61

62
// Contains checks if all key-value pairs in 'subset' are present in 'superset'.
63
// It returns true only if every key in 'subset' exists in 'superset' and their corresponding values are equal.
64
func Contains[M1, M2 ~map[K]V, K comparable, V comparable](superset M1, subset M2) bool {
65
	for key, value := range subset {
66
		if supersetValue, ok := superset[key]; !ok || supersetValue != value {
67
			return false
68
		}
69
	}
70
	return true
71
}
72

73
// EqualFunc is like Equal, but compares values using eq.
74
// Keys are still compared with ==.
75
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 {
76
	return maps.EqualFunc(m1, m2, eq)
77
}
78

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.