File: set_union.go

package info (click to toggle)
golang-github-apparentlymart-go-versions 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 292 kB
  • sloc: makefile: 2
file content (121 lines) | stat: -rw-r--r-- 2,313 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package versions

import (
	"bytes"
	"fmt"
)

type setUnion []setI

func (s setUnion) Has(v Version) bool {
	for _, ss := range s {
		if ss.Has(v) {
			return true
		}
	}
	return false
}

func (s setUnion) AllRequested() Set {
	// Since a union includes everything from its members, it includes all
	// of the requested versions from its members too.
	if len(s) == 0 {
		return None
	}
	si := make(setUnion, 0, len(s))
	for _, ss := range s {
		ar := ss.AllRequested()
		if ar == None {
			continue
		}
		si = append(si, ar.setI)
	}
	if len(si) == 1 {
		return Set{setI: si[0]}
	}
	return Set{setI: si}
}

func (s setUnion) GoString() string {
	var buf bytes.Buffer
	fmt.Fprint(&buf, "versions.Union(")
	for i, ss := range s {
		if i == 0 {
			fmt.Fprint(&buf, ss.GoString())
		} else {
			fmt.Fprintf(&buf, ", %#v", ss)
		}
	}
	fmt.Fprint(&buf, ")")
	return buf.String()
}

// Union creates a new set that contains all of the given versions.
//
// The result is finite only if the receiver and all of the other given sets
// are finite.
func Union(sets ...Set) Set {
	if len(sets) == 0 {
		return None
	}

	r := make(setUnion, 0, len(sets))
	for _, set := range sets {
		if set == None {
			continue
		}
		if su, ok := set.setI.(setUnion); ok {
			r = append(r, su...)
		} else {
			r = append(r, set.setI)
		}
	}
	if len(r) == 1 {
		return Set{setI: r[0]}
	}
	return Set{setI: r}
}

// Union returns a new set that contains all of the versions from the
// receiver and all of the versions from each of the other given sets.
//
// The result is finite only if the receiver and all of the other given sets
// are finite.
func (s Set) Union(others ...Set) Set {
	r := make(setUnion, 1, len(others)+1)
	r[0] = s.setI
	for _, ss := range others {
		if ss == None {
			continue
		}
		if su, ok := ss.setI.(setUnion); ok {
			r = append(r, su...)
		} else {
			r = append(r, ss.setI)
		}
	}
	if len(r) == 1 {
		return Set{setI: r[0]}
	}
	return Set{setI: r}
}

var _ setFinite = setUnion{}

func (s setUnion) isFinite() bool {
	// union is finite only if all of its members are finite
	for _, ss := range s {
		if !isFinite(ss) {
			return false
		}
	}
	return true
}

func (s setUnion) listVersions() List {
	var ret List
	for _, ss := range s {
		ret = append(ret, ss.(setFinite).listVersions()...)
	}
	return ret
}