mirror of
https://github.com/gocsaf/csaf.git
synced 2025-12-22 11:55:40 +01:00
Factored out set checks
This commit is contained in:
parent
f74c5123c2
commit
172c1cd85c
2 changed files with 54 additions and 37 deletions
43
util/set.go
Normal file
43
util/set.go
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
// 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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue