mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-08 13:50:48 +00:00
rename emptyBlock vars to emptyHeap
This commit is contained in:
parent
3ccdd9fdf6
commit
5cc9a43723
@ -10,18 +10,18 @@ import (
|
|||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
const numEmptyBlocks int = 9
|
const numEmptyHeaps int = 9
|
||||||
|
|
||||||
type disk struct {
|
type disk struct {
|
||||||
data []int
|
data []int
|
||||||
fileblocks []*block
|
fileblocks []*block
|
||||||
emptyblocks [numEmptyBlocks]minHeap
|
emptyHeaps [numEmptyHeaps]minHeap
|
||||||
}
|
}
|
||||||
|
|
||||||
func newDisk(raw []int) *disk {
|
func newDisk(raw []int) *disk {
|
||||||
fileblocks := make([]*block, 0)
|
fileblocks := make([]*block, 0)
|
||||||
emptyblockHeaps := make([]minHeap, numEmptyBlocks)
|
emptyblockHeaps := make([]minHeap, numEmptyHeaps)
|
||||||
for i := range numEmptyBlocks {
|
for i := range numEmptyHeaps {
|
||||||
heap.Init(&emptyblockHeaps[i])
|
heap.Init(&emptyblockHeaps[i])
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,7 +48,7 @@ func newDisk(raw []int) *disk {
|
|||||||
|
|
||||||
log.Debugf("\n%v\n%v", fileblocks, emptyblockHeaps)
|
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() {
|
func (d *disk) defragment() {
|
||||||
@ -71,12 +71,12 @@ func (d *disk) defragment() {
|
|||||||
emptyblock.length -= d.fileblocks[i].length
|
emptyblock.length -= d.fileblocks[i].length
|
||||||
if emptyblock.length > 0 {
|
if emptyblock.length > 0 {
|
||||||
log.Debugf("emptyblock resized %d", emptyblock.length)
|
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
|
// now create a new empty block and push it to the appropriate heap
|
||||||
heap.Push(
|
heap.Push(
|
||||||
&d.emptyblocks[oldLength-1],
|
&d.emptyHeaps[oldLength-1],
|
||||||
newBlock(kindOfEmpty, math.MaxInt, oldStart, oldLength, empty),
|
newBlock(kindOfEmpty, math.MaxInt, oldStart, oldLength, empty),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -85,12 +85,12 @@ func (d *disk) defragment() {
|
|||||||
func (d *disk) getNextEmptyBlock(currentFile *block) (*block, error) {
|
func (d *disk) getNextEmptyBlock(currentFile *block) (*block, error) {
|
||||||
// collect all minblocks the same size as the current file or greater
|
// collect all minblocks the same size as the current file or greater
|
||||||
minBlocks := []*block{}
|
minBlocks := []*block{}
|
||||||
for j := currentFile.length; j <= numEmptyBlocks; j++ {
|
for j := currentFile.length; j <= numEmptyHeaps; j++ {
|
||||||
if d.emptyblocks[j-1].Len() == 0 {
|
if d.emptyHeaps[j-1].Len() == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
currentblock := heap.Pop(&d.emptyblocks[j-1]).(*block)
|
currentblock := heap.Pop(&d.emptyHeaps[j-1]).(*block)
|
||||||
minBlocks = append(minBlocks, currentblock)
|
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
|
// push back the ones we won't be using
|
||||||
for _, block := range minBlocks[1:] {
|
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 the lowest id minblock is positioned after the current file, push it back and return an error
|
||||||
if minBlocks[0].start >= currentFile.start {
|
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")
|
return nil, errors.New("no empty blocks found")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,9 +123,9 @@ func (d *disk) write() {
|
|||||||
|
|
||||||
allBlocks = append(allBlocks, d.fileblocks...)
|
allBlocks = append(allBlocks, d.fileblocks...)
|
||||||
|
|
||||||
for i := range numEmptyBlocks {
|
for i := range numEmptyHeaps {
|
||||||
for !d.emptyblocks[i].IsEmpty() {
|
for !d.emptyHeaps[i].IsEmpty() {
|
||||||
allBlocks = append(allBlocks, heap.Pop(&d.emptyblocks[i]).(*block))
|
allBlocks = append(allBlocks, heap.Pop(&d.emptyHeaps[i]).(*block))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user