File: values.go

package info (click to toggle)
golang-github-mesos-mesos-go 0.0.6%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 11,724 kB
  • sloc: makefile: 163
file content (142 lines) | stat: -rw-r--r-- 3,076 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package mesos

func (left *Value_Scalar) Compare(right *Value_Scalar) int {
	var (
		a = convertToFixed64(left.GetValue())
		b = convertToFixed64(right.GetValue())
	)
	if a < b {
		return -1
	}
	if a > b {
		return 1
	}
	return 0
}

func (left *Value_Ranges) Compare(right *Value_Ranges) int {
	return Ranges(left.GetRange()).Compare(right.GetRange())
}

func (left *Value_Set) Compare(right *Value_Set) int {
	i, j := left.GetItem(), right.GetItem()
	if len(i) <= len(j) {
		b := make(map[string]struct{}, len(j))
		for _, x := range j {
			b[x] = struct{}{}
		}
		// make sure that each item on the left exists on the right,
		// otherwise left is not a subset of right.
		a := make(map[string]struct{}, len(i))
		for _, x := range i {
			if _, ok := b[x]; !ok {
				return 1
			}
			a[x] = struct{}{}
		}
		// if every item on the right also exists on the left, then
		// the sets are equal, otherwise left < right
		for x := range b {
			if _, ok := a[x]; !ok {
				return -1
			}
		}
		return 0
	}
	return 1
}

func (left *Value_Set) Add(right *Value_Set) *Value_Set {
	lefty := left.GetItem()
	righty := right.GetItem()
	c := len(lefty) + len(righty)
	if c == 0 {
		return nil
	}
	m := make(map[string]struct{}, c)
	for _, v := range lefty {
		m[v] = struct{}{}
	}
	for _, v := range righty {
		m[v] = struct{}{}
	}
	x := make([]string, 0, len(m))
	for v := range m {
		x = append(x, v)
	}
	return &Value_Set{Item: x}
}

func (left *Value_Set) Subtract(right *Value_Set) *Value_Set {
	// for each item in right, remove it from left
	lefty := left.GetItem()
	righty := right.GetItem()
	if c := len(lefty); c == 0 {
		return nil
	} else if len(righty) == 0 {
		x := make([]string, c)
		copy(x, lefty)
		return &Value_Set{Item: x}
	}

	a := make(map[string]struct{}, len(lefty))
	for _, x := range lefty {
		a[x] = struct{}{}
	}
	for _, x := range righty {
		delete(a, x)
	}
	if len(a) == 0 {
		return nil
	}
	i := 0
	for k := range a {
		lefty[i] = k
		i++
	}
	return &Value_Set{Item: lefty[:len(a)]}
}

func (left *Value_Ranges) Add(right *Value_Ranges) *Value_Ranges {
	a, b := Ranges(left.GetRange()), Ranges(right.GetRange())
	c := len(a) + len(b)
	if c == 0 {
		return nil
	}
	x := make(Ranges, c)
	if len(a) > 0 {
		copy(x, a)
	}
	if len(b) > 0 {
		copy(x[len(a):], b)
	}
	return &Value_Ranges{
		Range: x.Sort().Squash(),
	}
}

func (left *Value_Ranges) Subtract(right *Value_Ranges) *Value_Ranges {
	a, b := Ranges(left.GetRange()), Ranges(right.GetRange())
	if len(a) > 1 {
		x := make(Ranges, len(a))
		copy(x, a)
		a = x.Sort().Squash()
	}
	for _, r := range b {
		a = a.Remove(r)
	}
	if len(a) == 0 {
		return nil
	}
	return &Value_Ranges{Range: a}
}

func (left *Value_Scalar) Add(right *Value_Scalar) *Value_Scalar {
	sum := convertToFixed64(left.GetValue()) + convertToFixed64(right.GetValue())
	return &Value_Scalar{Value: convertToFloat64(sum)}
}

func (left *Value_Scalar) Subtract(right *Value_Scalar) *Value_Scalar {
	diff := convertToFixed64(left.GetValue()) - convertToFixed64(right.GetValue())
	return &Value_Scalar{Value: convertToFloat64(diff)}
}