packing: Refactoring

This commit is contained in:
Hajime Hoshi 2018-03-06 02:52:36 +09:00
parent 7e70d5ca69
commit 951568e403

View File

@ -79,7 +79,7 @@ func square(width, height int) float64 {
return float64(height) / float64(width) return float64(height) / float64(width)
} }
func (n *Node) alloc(width, height int) *Node { func (p *Page) alloc(n *Node, width, height int) *Node {
if n.width < width || n.height < height { if n.width < width || n.height < height {
return nil return nil
} }
@ -124,15 +124,15 @@ func (n *Node) alloc(width, height int) *Node {
parent: n, parent: n,
} }
} }
return n.child0.alloc(width, height) return p.alloc(n.child0, width, height)
} }
if n.child0 == nil || n.child1 == nil { if n.child0 == nil || n.child1 == nil {
panic("not reached") panic("not reached")
} }
if node := n.child0.alloc(width, height); node != nil { if node := p.alloc(n.child0, width, height); node != nil {
return node return node
} }
if node := n.child1.alloc(width, height); node != nil { if node := p.alloc(n.child1, width, height); node != nil {
return node return node
} }
return nil return nil
@ -155,7 +155,7 @@ func (p *Page) Alloc(width, height int) *Node {
if height < minSize { if height < minSize {
height = minSize height = minSize
} }
n := p.root.alloc(width, height) n := p.alloc(p.root, width, height)
p.m.Unlock() p.m.Unlock()
return n return n
} }