Skip to content

Commit

Permalink
Changed memory management to (somewhat) speed up loading of large dat…
Browse files Browse the repository at this point in the history
…asets; mostly in boundingBox().
  • Loading branch information
ctessum committed Dec 18, 2013
1 parent c18328d commit 1dfb773
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 28 deletions.
48 changes: 25 additions & 23 deletions geom.go
Expand Up @@ -164,18 +164,22 @@ func (r *Rect) String() string {
// NewRect constructs and returns a pointer to a Rect given a corner point and
// the lengths of each dimension. The point p should be the most-negative point
// on the rectangle (in every dimension) and every length should be positive.
func NewRect(p Point, lengths []float64) (*Rect, error) {
func NewRect(p Point, lengths []float64) (r *Rect, err error) {
r = new(Rect)
r.p = p
if len(p) != len(lengths) {
return nil, &DimError{len(p), len(lengths)}
err = &DimError{len(p), len(lengths)}
return
}
q := make([]float64, len(p))
r.q = make([]float64, len(p))
for i := range p {
if lengths[i] <= 0 {
return nil, DistError(lengths[i])
err = DistError(lengths[i])
return
}
q[i] = p[i] + lengths[i]
r.q[i] = p[i] + lengths[i]
}
return &Rect{p, q}, nil
return
}

// size computes the measure of a rectangle (the product of its side lengths).
Expand Down Expand Up @@ -304,40 +308,38 @@ func (p Point) ToRect(tol float64) *Rect {
}

// boundingBox constructs the smallest rectangle containing both r1 and r2.
func boundingBox(r1, r2 *Rect) *Rect {
func boundingBox(r1, r2 *Rect) (bb *Rect) {
bb = new(Rect)
dim := len(r1.p)
bb.p = make([]float64, dim)
bb.q = make([]float64, dim)
if len(r2.p) != dim {
panic(DimError{dim, len(r2.p)})
}

p := make([]float64, dim)
lengths := make([]float64, dim)
for i := range p {
for i := 0; i < dim; i++ {
if r1.p[i] <= r2.p[i] {
p[i] = r1.p[i]
bb.p[i] = r1.p[i]
} else {
p[i] = r2.p[i]
bb.p[i] = r2.p[i]
}

if r1.q[i] <= r2.q[i] {
lengths[i] = r2.q[i] - p[i]
bb.q[i] = r2.q[i]
} else {
lengths[i] = r1.q[i] - p[i]
bb.q[i] = r1.q[i]
}
}

r, _ := NewRect(p, lengths)
return r
return
}

// boundingBoxN constructs the smallest rectangle containing all of r...
func boundingBoxN(rects ...*Rect) *Rect {
func boundingBoxN(rects ...*Rect) (bb *Rect) {
if len(rects) == 1 {
return rects[0]
bb = rects[0]
return
}
bb := boundingBox(rects[0], rects[1])
bb = boundingBox(rects[0], rects[1])
for _, rect := range rects[2:] {
bb = boundingBox(bb, rect)
}
return bb
return
}
11 changes: 6 additions & 5 deletions rtree.go
Expand Up @@ -189,12 +189,13 @@ func (n *node) getEntry() *entry {
}

// computeBoundingBox finds the MBR of the children of n.
func (n *node) computeBoundingBox() *Rect {
childBoxes := []*Rect{}
for _, e := range n.entries {
childBoxes = append(childBoxes, e.bb)
func (n *node) computeBoundingBox() (bb *Rect) {
childBoxes := make([]*Rect, len(n.entries))
for i, e := range n.entries {
childBoxes[i] = e.bb
}
return boundingBoxN(childBoxes...)
bb = boundingBoxN(childBoxes...)
return
}

// split splits a node into two groups while attempting to minimize the
Expand Down

0 comments on commit 1dfb773

Please sign in to comment.