packing: Add comments

This commit is contained in:
Hajime Hoshi 2018-03-08 00:18:28 +09:00
parent 1dd32066d2
commit 86be32dfb5
2 changed files with 20 additions and 13 deletions

View File

@ -231,19 +231,28 @@ func (p *Page) Extend() bool {
newSize := p.size * 2 newSize := p.size * 2
edgeNodes := []*Node{} edgeNodes := []*Node{}
abort := errors.New("abort") abort := errors.New("abort")
aborted := false canExtendNodes := true
_ = walk(p.root, func(n *Node) error { _ = walk(p.root, func(n *Node) error {
if n.x+n.width < p.size && n.y+n.height < p.size { if n.x+n.width < p.size && n.y+n.height < p.size {
return nil return nil
} }
if n.used { if n.used {
aborted = true canExtendNodes = false
return abort return abort
} }
edgeNodes = append(edgeNodes, n) edgeNodes = append(edgeNodes, n)
return nil return nil
}) })
if aborted { if canExtendNodes {
for _, n := range edgeNodes {
if n.x+n.width == p.size {
n.width += newSize - p.size
}
if n.y+n.height == p.size {
n.height += newSize - p.size
}
}
} else {
leftUpper := p.root leftUpper := p.root
leftLower := &Node{ leftLower := &Node{
x: 0, x: 0,
@ -278,15 +287,6 @@ func (p *Page) Extend() bool {
} }
left.parent = p.root left.parent = p.root
right.parent = p.root right.parent = p.root
} else {
for _, n := range edgeNodes {
if n.x+n.width == p.size {
n.width += newSize - p.size
}
if n.y+n.height == p.size {
n.height += newSize - p.size
}
}
} }
p.size = newSize p.size = newSize

View File

@ -211,6 +211,7 @@ func TestExtend(t *testing.T) {
p := &Page{} p := &Page{}
s := p.Size() s := p.Size()
p.Alloc(s/2, s/2) p.Alloc(s/2, s/2)
// Now Extend extends the page by exntending the existing edge nodes.
p.Extend() p.Extend()
if p.Alloc(s*3/2, s*2) == nil { if p.Alloc(s*3/2, s*2) == nil {
t.Fail() t.Fail()
@ -223,7 +224,7 @@ func TestExtend(t *testing.T) {
} }
} }
func TestExtend2(t *testing.T) { func TestExtendAddingNewNodes(t *testing.T) {
p := &Page{} p := &Page{}
s := p.Size() s := p.Size()
p.Alloc(s/2, s/2) p.Alloc(s/2, s/2)
@ -232,7 +233,13 @@ func TestExtend2(t *testing.T) {
p.Alloc(s/2, s/2) p.Alloc(s/2, s/2)
p.Free(n1) p.Free(n1)
p.Free(n2) p.Free(n2)
// There is an allocation at lower left.
// In this case, Extend doesn't extend the existing edge nodes, but
// instead adds new nodes.
p.Extend() p.Extend()
if p.Alloc(s*3/2, s*2) != nil {
t.Fail()
}
if p.Alloc(s, s*2) == nil { if p.Alloc(s, s*2) == nil {
t.Fail() t.Fail()
} }