From 86be32dfb5081c520a01e9072d6f8562b0086a2f Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Thu, 8 Mar 2018 00:18:28 +0900 Subject: [PATCH] packing: Add comments --- internal/packing/packing.go | 24 ++++++++++++------------ internal/packing/packing_test.go | 9 ++++++++- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/internal/packing/packing.go b/internal/packing/packing.go index 0578de3e0..2f9fb6090 100644 --- a/internal/packing/packing.go +++ b/internal/packing/packing.go @@ -231,19 +231,28 @@ func (p *Page) Extend() bool { newSize := p.size * 2 edgeNodes := []*Node{} abort := errors.New("abort") - aborted := false + canExtendNodes := true _ = walk(p.root, func(n *Node) error { if n.x+n.width < p.size && n.y+n.height < p.size { return nil } if n.used { - aborted = true + canExtendNodes = false return abort } edgeNodes = append(edgeNodes, n) 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 leftLower := &Node{ x: 0, @@ -278,15 +287,6 @@ func (p *Page) Extend() bool { } left.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 diff --git a/internal/packing/packing_test.go b/internal/packing/packing_test.go index cf2037f81..a33cf5c76 100644 --- a/internal/packing/packing_test.go +++ b/internal/packing/packing_test.go @@ -211,6 +211,7 @@ func TestExtend(t *testing.T) { p := &Page{} s := p.Size() p.Alloc(s/2, s/2) + // Now Extend extends the page by exntending the existing edge nodes. p.Extend() if p.Alloc(s*3/2, s*2) == nil { t.Fail() @@ -223,7 +224,7 @@ func TestExtend(t *testing.T) { } } -func TestExtend2(t *testing.T) { +func TestExtendAddingNewNodes(t *testing.T) { p := &Page{} s := p.Size() p.Alloc(s/2, s/2) @@ -232,7 +233,13 @@ func TestExtend2(t *testing.T) { p.Alloc(s/2, s/2) p.Free(n1) 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() + if p.Alloc(s*3/2, s*2) != nil { + t.Fail() + } if p.Alloc(s, s*2) == nil { t.Fail() }