-
Notifications
You must be signed in to change notification settings - Fork 25
/
envs.go
49 lines (40 loc) · 1.23 KB
/
envs.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
// Copyright (c) 2019, The Emergent Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package env
import (
"fmt"
"github.com/emer/emergent/v2/etime"
)
// Envs is a map of environments organized according
// to the evaluation mode string (recommended key value)
type Envs map[string]Env
// Init initializes the map if not yet
func (es *Envs) Init() {
if *es == nil {
*es = make(map[string]Env)
}
}
// Add adds Env(s), using its Label as the key
func (es *Envs) Add(evs ...Env) {
es.Init()
for _, ev := range evs {
(*es)[ev.Label()] = ev
}
}
// ByMode returns env by etime.Modes evaluation mode as the map key.
// returns nil if not found
func (es *Envs) ByMode(mode etime.Modes) Env {
return (*es)[mode.String()]
}
// ModeDi returns the string of the given mode appended with
// _di data index with leading zero.
func ModeDi(mode etime.Modes, di int) string {
return fmt.Sprintf("%s_%02d", mode.String(), di)
}
// ByModeDi returns env by etime.Modes evaluation mode and
// data parallel index as the map key, using ModeDi function.
// returns nil if not found
func (es *Envs) ByModeDi(mode etime.Modes, di int) Env {
return (*es)[ModeDi(mode, di)]
}