File: uuidset.go

package info (click to toggle)
golang-github-ovn-org-libovsdb 0.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 1,440 kB
  • sloc: makefile: 52; sh: 14
file content (101 lines) | stat: -rw-r--r-- 1,468 bytes parent folder | download | duplicates (2)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package cache

type void struct{}
type uuidset map[string]void

func newUUIDSet(uuids ...string) uuidset {
	s := uuidset{}
	for _, uuid := range uuids {
		s[uuid] = void{}
	}
	return s
}

func (s uuidset) add(uuid string) {
	s[uuid] = void{}
}

func (s uuidset) remove(uuid string) {
	delete(s, uuid)
}

func (s uuidset) has(uuid string) bool {
	_, ok := s[uuid]
	return ok
}

func (s uuidset) equals(o uuidset) bool {
	if len(s) != len(o) {
		return false
	}
	for uuid := range s {
		if !o.has(uuid) {
			return false
		}
	}
	return true
}

func (s uuidset) getAny() string {
	for k := range s {
		return k
	}
	return ""
}

func (s uuidset) list() []string {
	uuids := make([]string, 0, len(s))
	for uuid := range s {
		uuids = append(uuids, uuid)
	}
	return uuids
}

func (s uuidset) empty() bool {
	return len(s) == 0
}

func addUUIDSet(s1, s2 uuidset) uuidset {
	if len(s2) == 0 {
		return s1
	}
	if s1 == nil {
		s1 = uuidset{}
	}
	for uuid := range s2 {
		s1.add(uuid)
	}
	return s1
}

func substractUUIDSet(s1, s2 uuidset) uuidset {
	if len(s1) == 0 || len(s2) == 0 {
		return s1
	}
	for uuid := range s2 {
		s1.remove(uuid)
	}
	return s1
}

func intersectUUIDSets(s1, s2 uuidset) uuidset {
	if len(s1) == 0 || len(s2) == 0 {
		return nil
	}
	var big uuidset
	var small uuidset
	if len(s1) > len(s2) {
		big = s1
		small = s2
	} else {
		big = s2
		small = s1
	}
	f := uuidset{}
	for uuid := range small {
		if big.has(uuid) {
			f.add(uuid)
		}
	}
	return f
}