-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathheap.tmpl
78 lines (64 loc) · 1.75 KB
/
heap.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
// 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.
{{/* -*- mode: Go -*- */}}
{{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 {{.HeapType}} struct {
{{template "elib" .Package}}Heap
{{.Data}} []{{.Type}}
ids []{{template "elib" .Package}}Index
}
func (p * {{.HeapType}}) GetAligned(size, log2Alignment uint) (offset uint) {
l := uint(len(p.{{.Data}}))
id, offset := p.Heap.GetAligned(size, log2Alignment)
if offset+size >= l {
p.Validate(offset + size - 1)
}
for i := uint(0); i < size; i++ {
p.ids[offset + i] = id
}
return
}
func (p * {{.HeapType}}) Get(size uint) uint { return p.GetAligned(size, 0) }
func (p * {{.HeapType}}) Put(offset uint) {
p.Heap.Put(p.Id(offset))
}
func (p * {{.HeapType}}) IsFree(offset uint) bool {
return p.Heap.IsFree(p.Id(offset))
}
func (p * {{.HeapType}}) 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)
r := make([]{{template "elib" .Package}}Index, l, c)
copy(q, p.{{.Data}})
copy(r, p.ids)
p.{{.Data}} = q
p.ids = r
}
if l > uint(len(p.{{.Data}})) {
p.{{.Data}} = p.{{.Data}}[:l]
p.ids = p.ids[:l]
}
}
func (p *{{.HeapType}}) Id(offset uint) {{template "elib" .Package}}Index {
return p.ids[offset]
}
func (p *{{.HeapType}}) Slice(offset uint) []{{.Type}} {
l := p.Len(p.Id(offset))
if (offset + l) > uint(len(p.elts)) {
p.Validate(offset + l - 1)
}
return p.{{.Data}}[offset:offset+l]
}