File: set.go

package info (click to toggle)
golang-github-containers-image 5.36.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 5,152 kB
  • sloc: sh: 267; makefile: 100
file content (55 lines) | stat: -rw-r--r-- 813 bytes parent folder | download
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
package set

import (
	"iter"
	"maps"
)

// FIXME:
// - Docstrings
// - This should be in a public library somewhere

type Set[E comparable] struct {
	m map[E]struct{}
}

func New[E comparable]() *Set[E] {
	return &Set[E]{
		m: map[E]struct{}{},
	}
}

func NewWithValues[E comparable](values ...E) *Set[E] {
	s := New[E]()
	for _, v := range values {
		s.Add(v)
	}
	return s
}

func (s *Set[E]) Add(v E) {
	s.m[v] = struct{}{} // Possibly writing the same struct{}{} presence marker again.
}

func (s *Set[E]) AddSeq(seq iter.Seq[E]) {
	for v := range seq {
		s.Add(v)
	}
}

func (s *Set[E]) Delete(v E) {
	delete(s.m, v)
}

func (s *Set[E]) Contains(v E) bool {
	_, ok := s.m[v]
	return ok
}

func (s *Set[E]) Empty() bool {
	return len(s.m) == 0
}

func (s *Set[E]) All() iter.Seq[E] {
	return maps.Keys(s.m)
}