Dragonfly2

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

17
//go:generate mockgen -destination mocks/set_mock.go -source set.go -package mocks
18

19
package set
20

21
type Set[T comparable] interface {
22
	Values() []T
23
	Add(T) bool
24
	Delete(T)
25
	Contains(...T) bool
26
	Len() uint
27
	Clear()
28
}
29

30
type set[T comparable] map[T]struct{}
31

32
func New[T comparable]() Set[T] {
33
	return &set[T]{}
34
}
35

36
func (s *set[T]) Values() []T {
37
	var result []T
38
	for k := range *s {
39
		result = append(result, k)
40
	}
41

42
	return result
43
}
44

45
func (s *set[T]) Add(v T) bool {
46
	_, found := (*s)[v]
47
	if found {
48
		return false
49
	}
50

51
	(*s)[v] = struct{}{}
52
	return true
53
}
54

55
func (s *set[T]) Delete(v T) {
56
	delete(*s, v)
57
}
58

59
func (s *set[T]) Contains(vals ...T) bool {
60
	for _, v := range vals {
61
		if _, ok := (*s)[v]; !ok {
62
			return false
63
		}
64
	}
65

66
	return true
67
}
68

69
func (s *set[T]) Len() uint {
70
	return uint(len(*s))
71
}
72

73
func (s *set[T]) Clear() {
74
	*s = set[T]{}
75
}
76

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

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

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

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