forked from marcetin/parallelcoin
-
Notifications
You must be signed in to change notification settings - Fork 5
/
poolgen.go
68 lines (60 loc) · 1.76 KB
/
poolgen.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
package main
import (
"os"
)
type poolType struct {
name, sliceName, constructor string
}
var types = []poolType{
{"Bool", "bools", "Bool(false)"},
{"List", "lists", "List()"},
{"Checkable", "checkables", "Checkable()"},
{"Clickable", "clickables", "Clickable()"},
{"Editor", "editors", "Editor()"},
{"IncDec", "incDecs", "IncDec()"},
}
func main() {
var out string
out += `// generated by go run github.com/p9c/pod/pkg/gui/poolgen/poolgen.go; DO NOT EDIT
//
//go:generate go run poolgen/poolgen.go poolgen/log.go
package gui
`
for i := range types {
out += `
func (p *Pool) Get` + types[i].name + `() (out *` + types[i].name + `) {
if len(p.` + types[i].sliceName + `) >= p.` + types[i].sliceName + `InUse {
for i := 0; i < 10; i++ {
p.` + types[i].sliceName + ` = append(p.` + types[i].sliceName + `, p.` + types[i].constructor + `)
}
}
out = p.` + types[i].sliceName + `[p.` + types[i].sliceName + `InUse]
p.` + types[i].sliceName + `InUse++
return
}
func (p *Pool) Free` + types[i].name + `(b *` + types[i].name + `) {
for i := 0; i < p.` + types[i].sliceName + `InUse; i++ {
if p.` + types[i].sliceName + `[i] == b {
if i != p.` + types[i].sliceName + `InUse-1 {
// move the item to the end. the next allocation will be then at index p.` + types[i].sliceName + `InUse
tmp := p.` + types[i].sliceName + `[i]
p.` + types[i].sliceName + ` = append(p.` + types[i].sliceName + `[:i], p.` + types[i].sliceName + `[i+1:]...)
p.` + types[i].sliceName + ` = append(p.` + types[i].sliceName + `, tmp)
p.` + types[i].sliceName + `InUse--
break
}
}
}
}
`
}
if fd, e := os.Create("pooltypes.go"); E.Chk(e) {
} else {
defer func() {
if e := fd.Close(); E.Chk(e) {
}
}()
if _, e = fd.Write([]byte(out)); E.Chk(e) {
}
}
}