-
Notifications
You must be signed in to change notification settings - Fork 2
/
elemheap.go
57 lines (49 loc) · 1.18 KB
/
elemheap.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
package rst
/*
Elements are not allocated one at a time, but in rows of
elemHeap.RowSize elements. After N elements have been
requested, a row is exhausted, and the next one will
be allocated. Previously allocated rows are tracked in
elemHeap.rows.
The Reset() method allows to reset the current position (row, and
position within the row), which allows reusing elements. Whether
elements can be reused, depends on the value of the hasGlobals
field.
*/
type elemHeap struct {
rows [][]element
heapPos
rowSize int
base heapPos
hasGlobals bool
}
type heapPos struct {
iRow int
row []element
}
func (h *elemHeap) nextRow() []element {
h.iRow++
if h.iRow == len(h.rows) {
h.rows = append(h.rows, make([]element, h.rowSize))
}
h.row = h.rows[h.iRow]
return h.row
}
func (h *elemHeap) init(size int) {
h.rowSize = size
h.rows = [][]element{make([]element, size)}
h.row = h.rows[h.iRow]
h.base = h.heapPos
}
func (h *elemHeap) Reset() {
if !h.hasGlobals {
h.heapPos = h.base
} else {
/* Don't restore saved position in case elements added
* after the previous Reset call are needed in
* global context, like notes.
*/
h.hasGlobals = false
h.base = h.heapPos
}
}