-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpool.tmpl
101 lines (86 loc) · 1.94 KB
/
pool.tmpl
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
{{/* -*- mode: Go -*- */}}
// Copyright 2016 Platina Systems, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
{{if ne .TAGS ""}}
// +build {{.TAGS}}
{{end}}
{{define "elib"}}{{if ne . "elib"}}elib.{{end}}{{end}}
package {{.Package}}
{{if ne .Package "elib"}}
import (
"github.com/platinasystems/elib"
)
{{end}}
type {{.PoolType}} struct {
{{template "elib" .Package}}Pool
{{.Data}} []{{.Type}}
}
func (p * {{.PoolType}}) GetIndex() (i uint) {
l := uint(len(p.{{.Data}}))
i = p.Pool.GetIndex(l)
if i >= l {
p.Validate(i)
}
return i
}
func (p * {{.PoolType}}) PutIndex(i uint) (ok bool) {
return p.Pool.PutIndex(i)
}
func (p * {{.PoolType}}) IsFree(i uint) (v bool) {
v = i >= uint(len(p.{{.Data}}))
if !v {
v = p.Pool.IsFree(i)
}
return
}
func (p * {{.PoolType}}) Resize(n uint) {
c := uint(cap(p.{{.Data}}))
l := uint(len(p.{{.Data}}) + int(n))
if l > c {
c = {{template "elib" .Package}}NextResizeCap(l)
q := make([]{{.Type}}, l, c)
copy(q, p.{{.Data}})
p.{{.Data}} = q
}
p.{{.Data}} = p.{{.Data}}[:l]
}
func (p * {{.PoolType}}) Validate(i uint) {
c := uint(cap(p.{{.Data}}))
l := uint(i) + 1
if l > c {
c = {{template "elib" .Package}}NextResizeCap(l)
q := make([]{{.Type}}, l, c)
copy(q, p.{{.Data}})
p.{{.Data}} = q
}
if l > uint(len(p.{{.Data}})) {
p.{{.Data}} = p.{{.Data}}[:l]
}
}
func (p * {{.PoolType}}) Elts() uint {
return uint(len(p.{{.Data}})) - p.FreeLen()
}
func (p * {{.PoolType}}) Len() uint {
return uint(len(p.{{.Data}}))
}
func (p * {{.PoolType}}) Foreach(f func(x {{.Type}})) {
for i := range p.{{.Data}} {
if !p.Pool.IsFree(uint(i)) {
f(p.{{.Data}}[i])
}
}
}
func (p * {{.PoolType}}) ForeachIndex(f func(i uint)) {
for i := range p.{{.Data}} {
if !p.Pool.IsFree(uint(i)) {
f(uint(i))
}
}
}
func (p * {{.PoolType}}) Reset() {
p.Pool.Reset()
if len(p.{{.Data}}) > 0 {
p.{{.Data}} = p.{{.Data}}[:0]
}
}