rename emptyBlock vars to emptyHeap

This commit is contained in:
onyx-and-iris 2024-12-21 19:48:26 +00:00
parent 3ccdd9fdf6
commit 5cc9a43723

View File

@ -10,18 +10,18 @@ import (
log "github.com/sirupsen/logrus"
)
const numEmptyBlocks int = 9
const numEmptyHeaps int = 9
type disk struct {
data []int
fileblocks []*block
emptyblocks [numEmptyBlocks]minHeap
data []int
fileblocks []*block
emptyHeaps [numEmptyHeaps]minHeap
}
func newDisk(raw []int) *disk {
fileblocks := make([]*block, 0)
emptyblockHeaps := make([]minHeap, numEmptyBlocks)
for i := range numEmptyBlocks {
emptyblockHeaps := make([]minHeap, numEmptyHeaps)
for i := range numEmptyHeaps {
heap.Init(&emptyblockHeaps[i])
}
@ -48,7 +48,7 @@ func newDisk(raw []int) *disk {
log.Debugf("\n%v\n%v", fileblocks, emptyblockHeaps)
return &disk{data: raw, fileblocks: fileblocks, emptyblocks: [numEmptyBlocks]minHeap(emptyblockHeaps)}
return &disk{data: raw, fileblocks: fileblocks, emptyHeaps: [numEmptyHeaps]minHeap(emptyblockHeaps)}
}
func (d *disk) defragment() {
@ -71,12 +71,12 @@ func (d *disk) defragment() {
emptyblock.length -= d.fileblocks[i].length
if emptyblock.length > 0 {
log.Debugf("emptyblock resized %d", emptyblock.length)
heap.Push(&d.emptyblocks[emptyblock.length-1], emptyblock)
heap.Push(&d.emptyHeaps[emptyblock.length-1], emptyblock)
}
// now create a new empty block and push it to the appropriate heap
heap.Push(
&d.emptyblocks[oldLength-1],
&d.emptyHeaps[oldLength-1],
newBlock(kindOfEmpty, math.MaxInt, oldStart, oldLength, empty),
)
}
@ -85,12 +85,12 @@ func (d *disk) defragment() {
func (d *disk) getNextEmptyBlock(currentFile *block) (*block, error) {
// collect all minblocks the same size as the current file or greater
minBlocks := []*block{}
for j := currentFile.length; j <= numEmptyBlocks; j++ {
if d.emptyblocks[j-1].Len() == 0 {
for j := currentFile.length; j <= numEmptyHeaps; j++ {
if d.emptyHeaps[j-1].Len() == 0 {
continue
}
currentblock := heap.Pop(&d.emptyblocks[j-1]).(*block)
currentblock := heap.Pop(&d.emptyHeaps[j-1]).(*block)
minBlocks = append(minBlocks, currentblock)
}
@ -104,12 +104,12 @@ func (d *disk) getNextEmptyBlock(currentFile *block) (*block, error) {
})
// push back the ones we won't be using
for _, block := range minBlocks[1:] {
heap.Push(&d.emptyblocks[block.length-1], block)
heap.Push(&d.emptyHeaps[block.length-1], block)
}
// if the lowest id minblock is positioned after the current file, push it back and return an error
if minBlocks[0].start >= currentFile.start {
heap.Push(&d.emptyblocks[minBlocks[0].length-1], minBlocks[0])
heap.Push(&d.emptyHeaps[minBlocks[0].length-1], minBlocks[0])
return nil, errors.New("no empty blocks found")
}
@ -123,9 +123,9 @@ func (d *disk) write() {
allBlocks = append(allBlocks, d.fileblocks...)
for i := range numEmptyBlocks {
for !d.emptyblocks[i].IsEmpty() {
allBlocks = append(allBlocks, heap.Pop(&d.emptyblocks[i]).(*block))
for i := range numEmptyHeaps {
for !d.emptyHeaps[i].IsEmpty() {
allBlocks = append(allBlocks, heap.Pop(&d.emptyHeaps[i]).(*block))
}
}