mirror of
https://github.com/gocsaf/csaf.git
synced 2025-12-22 11:55:40 +01:00
43 lines
977 B
Go
43 lines
977 B
Go
// This file is Free Software under the MIT License
|
|
// without warranty, see README.md and LICENSES/MIT.txt for details.
|
|
//
|
|
// SPDX-License-Identifier: MIT
|
|
//
|
|
// SPDX-FileCopyrightText: 2023 German Federal Office for Information Security (BSI) <https://www.bsi.bund.de>
|
|
// Software-Engineering: 2023 Intevation GmbH <https://intevation.de>
|
|
|
|
package util
|
|
|
|
// Set is a simple set type.
|
|
type Set[K comparable] map[K]struct{}
|
|
|
|
// Contains returns if the set contains a given key or not.
|
|
func (s Set[K]) Contains(k K) bool {
|
|
_, found := s[k]
|
|
return found
|
|
}
|
|
|
|
// Add adds a key to the set.
|
|
func (s Set[K]) Add(k K) {
|
|
s[k] = struct{}{}
|
|
}
|
|
|
|
// Keys returns the keys of the set.
|
|
func (s Set[K]) Keys() []K {
|
|
keys := make([]K, 0, len(s))
|
|
for k := range s {
|
|
keys = append(keys, k)
|
|
}
|
|
return keys
|
|
}
|
|
|
|
// Difference returns the differnce of two sets.
|
|
func (s Set[K]) Difference(t Set[K]) Set[K] {
|
|
d := Set[K]{}
|
|
for k := range s {
|
|
if !t.Contains(k) {
|
|
d.Add(k)
|
|
}
|
|
}
|
|
return d
|
|
}
|