-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.go
176 lines (154 loc) · 4.5 KB
/
helper.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package starbox
import (
"fmt"
"os"
"sort"
"go.starlark.net/starlark"
"github.com/1set/starlet"
"github.com/1set/starlet/dataconv"
"github.com/h2so5/here"
)
const (
memoryTypeName = "collective_memory"
)
// NewMemory creates a new shared dictionary for la mémoire collective.
func NewMemory() *dataconv.SharedDict {
return dataconv.NewNamedSharedDict(memoryTypeName)
}
// AttachMemory adds a shared dictionary to the global environment before execution.
func (s *Starbox) AttachMemory(name string, memory *dataconv.SharedDict) {
s.mu.Lock()
defer s.mu.Unlock()
if s.hasExec {
log.DPanic("cannot add memory after execution")
}
if s.globals == nil {
s.globals = make(starlet.StringAnyMap)
}
s.globals[name] = memory
}
// CreateMemory creates a new shared dictionary for la mémoire collective with the given name, and adds it to the global environment before execution.
func (s *Starbox) CreateMemory(name string) *dataconv.SharedDict {
s.mu.Lock()
defer s.mu.Unlock()
if s.hasExec {
log.DPanic("cannot add memory after execution")
}
if s.globals == nil {
s.globals = make(starlet.StringAnyMap)
}
memory := dataconv.NewNamedSharedDict(memoryTypeName)
s.globals[name] = memory
return memory
}
var (
// HereDoc returns unindented string as here-document.
HereDoc = here.Doc
// HereDocf returns formatted unindented string as here-document.
HereDocf = here.Docf
)
// HERE GOES THE INTERNALS
// eprintln likes fmt.Println but use stderr as the output.
func eprintln(a ...interface{}) (n int, err error) {
return fmt.Fprintln(os.Stderr, a...)
}
// stringsMapSet returns a new map[string]struct{} with the given strings as keys working as a set.
func stringsMapSet(ss ...[]string) map[string]struct{} {
m := make(map[string]struct{})
for _, s := range ss {
for _, v := range s {
m[v] = struct{}{}
}
}
return m
}
// mapSetStrings returns a new slice of sorted strings with keys from the given map[string]struct{}.
func mapSetStrings(m map[string]struct{}) []string {
ss := make([]string, 0, len(m))
for s := range m {
ss = append(ss, s)
}
sort.Strings(ss)
return ss
}
// intersectStrings returns a new slice of strings with common elements from the given slices.
func intersectStrings(a, b []string) []string {
// create a map to track strings in the first slice
seen := stringsMapSet(a)
// create a map to track strings in both slices
intersect := make(map[string]struct{})
for _, s := range b {
if _, ok := seen[s]; ok {
intersect[s] = struct{}{}
}
}
// convert the map to a slice
return mapSetStrings(intersect)
}
// uniqueStrings returns a new slice of strings with duplicates removed and sorted.
func uniqueStrings(ss []string) []string {
if len(ss) < 2 {
return ss
}
m := make(map[string]struct{}, len(ss))
for _, s := range ss {
m[s] = struct{}{}
}
unique := make([]string, 0, len(m))
for s := range m {
unique = append(unique, s)
}
sort.Strings(unique)
return unique
}
// removeUniques removes the given strings from the slice of strings.
// It returns a new slice of strings without the removed strings or duplicates.
func removeUniques(ss []string, removes ...string) []string {
// create a map to track strings to remove
remove := make(map[string]bool)
for _, r := range removes {
remove[r] = true
}
// create a map to track strings already added to the output slice
seen := make(map[string]bool)
output := make([]string, 0, len(ss))
for _, s := range ss {
if !remove[s] && !seen[s] {
seen[s] = true
output = append(output, s)
}
}
return output
}
// appendUniques appends the given strings to the slice of strings, if not already exists.
// It returns a new slice of strings without duplicates.
func appendUniques(ss []string, appends ...string) []string {
seen := make(map[string]bool) // tracks seen strings
output := make([]string, 0, len(ss)+len(appends)) // preallocate memory for the output slice
// add unique strings from the original slice to the output slice
for _, s := range ss {
if !seen[s] {
output = append(output, s)
seen[s] = true
}
}
// append new unique strings to the output slice
for _, s := range appends {
if !seen[s] {
output = append(output, s)
seen[s] = true
}
}
return output
}
// starlarkStringList converts a slice of strings to a list of starlark.Values.
func starlarkStringList(ss []string) *starlark.List {
if len(ss) == 0 {
return starlark.NewList(nil)
}
values := make([]starlark.Value, len(ss))
for i, s := range ss {
values[i] = starlark.String(s)
}
return starlark.NewList(values)
}