/
resources.go
46 lines (41 loc) · 991 Bytes
/
resources.go
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
package simmarket
// Resource represents a single kind of resource.
type Resource any
// Resources is a map of resource type to amount or price.
type Resources map[Resource]float64
// MergeIn merges the given resouce set with the current one.
func (r Resources) MergeIn(o Resources) {
for resource, units := range o {
r[resource] = r[resource] + units
}
}
// Clone makes a copy of the resource set.
func (r Resources) Clone() Resources {
returnValue := make(Resources)
for resource, units := range r {
returnValue[resource] = units
}
return returnValue
}
// Eq returns true if the two sets are equal.
func (r Resources) Eq(o Resources) bool {
for resource, units := range r {
if otherUnits, ok := o[resource]; ok {
if otherUnits != units {
return false
}
} else {
return false
}
}
for resource, otherUnits := range o {
if units, ok := r[resource]; ok {
if units != otherUnits {
return false
}
} else {
return false
}
}
return true
}