-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwarehousingUtil.go
110 lines (101 loc) · 4.03 KB
/
warehousingUtil.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
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
package api
func (ws *WareSourcing) Append(ws2 WareSourcing) {
for packtype, locations := range ws2.ByPackType {
ws.AppendByPackType(packtype, locations...)
}
for modName, v := range ws2.ByModule {
for packtype, locations := range v {
ws.AppendByModule(modName, packtype, locations...)
}
}
for wareID, locations := range ws2.ByWare {
ws.AppendByWare(wareID, locations...)
}
}
func (ws *WareSourcing) AppendByPackType(packtype PackType, locations ...WarehouseLocation) {
if ws.ByPackType == nil {
ws.ByPackType = make(map[PackType][]WarehouseLocation)
}
if ws.ByPackType[packtype] == nil {
ws.ByPackType[packtype] = locations
return
}
ws.ByPackType[packtype] = append(ws.ByPackType[packtype], locations...)
}
func (ws *WareSourcing) AppendByModule(modName ModuleName, packtype PackType, locations ...WarehouseLocation) {
if ws.ByModule == nil {
ws.ByModule = make(map[ModuleName]map[PackType][]WarehouseLocation)
}
if ws.ByModule[modName] == nil {
ws.ByModule[modName] = make(map[PackType][]WarehouseLocation)
}
if ws.ByModule[modName][packtype] == nil {
ws.ByModule[modName][packtype] = locations
return
}
ws.ByModule[modName][packtype] = append(ws.ByModule[modName][packtype], locations...)
}
func (ws *WareSourcing) AppendByWare(wareID WareID, locations ...WarehouseLocation) {
if ws.ByWare == nil {
ws.ByWare = make(map[WareID][]WarehouseLocation)
}
if ws.ByWare[wareID] == nil {
ws.ByWare[wareID] = locations
return
}
ws.ByWare[wareID] = append(ws.ByWare[wareID], locations...)
}
// PivotToWareIDs returns a new and reduced WareSourcing where all data is
// indexed ByWareID for each wareID in the argument set.
// All the ByPackType for a type "tar" will be appended to the ByWareID
// index for all wareIDs of type "tar", and so forth.
// ByModule data is ignored (you should flip that to ByWareID-indexed
// immediately when you load it).
func (ws WareSourcing) PivotToWareIDs(wareIDs map[WareID]struct{}) WareSourcing {
ws2 := WareSourcing{ByWare: make(map[WareID][]WarehouseLocation, len(wareIDs))}
for wareID := range wareIDs {
// Copy over anything already explicitly wareID-indexed.
ws2.ByWare[wareID] = ws.ByWare[wareID]
// Append packtype-general info.
ws2.ByWare[wareID] = append(ws2.ByWare[wareID], ws.ByPackType[wareID.Type]...)
}
return ws2
}
// PivotToWareID is like PivotToWareIDs but for a single WareID; and shortcuts
// immediately to returning a flat list of WarehouseLocation.
func (ws WareSourcing) PivotToWareID(wareID WareID) (v []WarehouseLocation) {
// Copy over anything already explicitly wareID-indexed.
v = append(v, ws.ByWare[wareID]...)
// Append packtype-general info.
v = append(v, ws.ByPackType[wareID.Type]...)
return
}
// PivotToInputs is a shortcut for calling PivotToWareIDs with the set of
// inputs to a bound Op.
func (ws WareSourcing) PivotToInputs(frm Formula) WareSourcing {
wareIDs := make(map[WareID]struct{}, len(frm.Inputs))
for _, wareID := range frm.Inputs {
wareIDs[wareID] = struct{}{}
}
return ws.PivotToWareIDs(wareIDs)
}
// PivotToModuleWare returns WareSourcing where all data is indexed ByWareID
// (like PivotToInputs and PivotToWareIDs), also applying any ByModule-index
// info for the named module. (This is typically used immediately after
// loading the mirrors info in a module's release catalog, in order to avoid
// needed to carry around the module-oriented info any longer.)
func (ws WareSourcing) PivotToModuleWare(wareID WareID, assumingModName ModuleName) WareSourcing {
ws2 := WareSourcing{ByWare: make(map[WareID][]WarehouseLocation, 1)}
// Copy over anything already explicitly wareID-indexed.
ws2.ByWare[wareID] = ws.ByWare[wareID]
// Append packtype-general info.
ws2.ByWare[wareID] = append(ws2.ByWare[wareID], ws.ByPackType[wareID.Type]...)
// Append module info.
forMod := ws.ByModule[assumingModName]
if forMod == nil {
return ws2
}
ws2.ByWare[wareID] = append(ws2.ByWare[wareID], forMod[wareID.Type]...)
return ws2
}
// TODO: a bunch of these methods should probably check for repeated warehouseLocation entries and drop subsequent ones?