diff options
author | hanemile <hanemile@protonmail.com> | 2019-01-28 20:30:38 +0100 |
---|---|---|
committer | hanemile <hanemile@protonmail.com> | 2019-01-28 20:30:38 +0100 |
commit | bef2f0bbf27caf3d2b4dcc6311aac59cd60d6614 (patch) | |
tree | b5b476c035164b6830458580924ed8dd3725689f | |
parent | 6102ad0b000d4b213cff5928239807f993771a40 (diff) |
added a mutex around the insertion possibly fixing collisions
-rw-r--r-- | quadtree.go | 7 |
1 files 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 } |