From bef2f0bbf27caf3d2b4dcc6311aac59cd60d6614 Mon Sep 17 00:00:00 2001 From: hanemile Date: Mon, 28 Jan 2019 20:30:38 +0100 Subject: added a mutex around the insertion possibly fixing collisions --- quadtree.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/quadtree.go b/quadtree.go index b88c314..95d4780 100644 --- a/quadtree.go +++ b/quadtree.go @@ -7,6 +7,7 @@ import ( "math" "os" "os/exec" + "sync" ) type Node struct { @@ -61,10 +62,11 @@ func (n *Node) Subdivide() { // Insert inserts the given star into the Node or the tree it is called on func (n *Node) Insert(star Star2D) error { + var mutex = &sync.Mutex{} + mutex.Lock() // if the subtree does not contain a node, insert the star if n.Star == (Star2D{}) { - // if a subtree is present, insert the star into that subtree if n.Subtrees != [4]*Node{} { QuadrantBlocking := star.getRelativePositionInt(n.Boundry) @@ -82,7 +84,6 @@ func (n *Node) Insert(star Star2D) error { // Move the star blocking the slot into it's subtree using a recursive call on this function // and add the star to the slot } else { - // if the node does not all ready have child nodes, subdivide it if n.Subtrees == ([4]*Node{}) { n.Subdivide() @@ -105,6 +106,8 @@ func (n *Node) Insert(star Star2D) error { star = Star2D{} } + mutex.Unlock() + // fmt.Println("Done inserting %v, the tree looks like this: %v", star, n) return nil } -- cgit 1.4.1