forked from hashicorp/go-set
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.go
65 lines (55 loc) · 1.74 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package set
// Common is a minimal interface that all sets implement.
type Common[T any] interface {
// Slice returns a slice of all elements in the set.
//
// Note: order of elements depends on the underlying implementation.
Slice() []T
// Insert an element into the set.
//
// Returns true if the set is modified as a result.
Insert(T) bool
// InsertSlice inserts all elements from the slice into the set.
//
// Returns true if the set was modified as a result.
InsertSlice([]T) bool
// Size returns the number of elements in the set.
Size() int
// ForEach will call the callback function for each element in the set.
// If the callback returns false, the iteration will stop.
//
// Note: iteration order depends on the underlying implementation.
ForEach(func(T) bool)
}
// InsertSliceFunc inserts all elements from the slice into the set
func InsertSliceFunc[T, E any](s Common[T], items []E, f func(element E) T) {
for _, item := range items {
s.Insert(f(item))
}
}
// InsertSetFunc inserts the elements of a into b, applying the transform function
// to each element before insertion.
//
// Returns true if b was modified as a result.
func InsertSetFunc[T, E any](a Common[T], b Common[E], transform func(T) E) bool {
modified := false
a.ForEach(func(item T) bool {
if b.Insert(transform(item)) {
modified = true
}
return true
})
return modified
}
// SliceFunc produces a slice of the elements in s, applying the transform
// function to each element first.
func SliceFunc[T, E any](s Common[T], transform func(T) E) []E {
slice := make([]E, 0, s.Size())
s.ForEach(func(item T) bool {
slice = append(slice, transform(item))
return true
})
return slice
}