File: set_subtract.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 (56 lines) | stat: -rw-r--r-- 1,297 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
package versions

import "fmt"

type setSubtract struct {
	from setI
	sub  setI
}

func (s setSubtract) Has(v Version) bool {
	return s.from.Has(v) && !s.sub.Has(v)
}

func (s setSubtract) AllRequested() Set {
	// Our set requests anything that is requested by "from", unless it'd
	// be excluded by "sub". Notice that the whole of "sub" is used, rather
	// than just the requested parts, because requesting is a positive
	// action only.
	return Set{setI: s.from}.AllRequested().Subtract(Set{setI: s.sub})
}

func (s setSubtract) GoString() string {
	return fmt.Sprintf("(%#v).Subtract(%#v)", s.from, s.sub)
}

// Subtract returns a new set that has all of the versions from the receiver
// except for any versions in the other given set.
//
// If the receiver is finite then the returned set is also finite.
func (s Set) Subtract(other Set) Set {
	if other == None || s == None {
		return s
	}
	if other == All {
		return None
	}
	return Set{
		setI: setSubtract{
			from: s.setI,
			sub:  other.setI,
		},
	}
}

var _ setFinite = setSubtract{}

func (s setSubtract) isFinite() bool {
	// subtract is finite if its "from" is finite
	return isFinite(s.from)
}

func (s setSubtract) listVersions() List {
	ret := s.from.(setFinite).listVersions()
	ret = ret.Filter(Set{setI: s.sub})
	return ret
}