func (tree *B2DynamicTree) ~b2DynamicTree() { // This frees the entire tree in one shot. b2Free(tree.M_nodes); } Allocate a node from the pool. Grow the pool if necessary.
()
| 216 | |
| 217 | // Allocate a node from the pool. Grow the pool if necessary. |
| 218 | func (tree *B2DynamicTree) AllocateNode() int { |
| 219 | |
| 220 | // Expand the node pool as needed. |
| 221 | if tree.M_freeList == B2_nullNode { |
| 222 | B2Assert(tree.M_nodeCount == tree.M_nodeCapacity) |
| 223 | |
| 224 | // The free list is empty. Rebuild a bigger pool. |
| 225 | tree.M_nodes = append(tree.M_nodes, make([]B2TreeNode, tree.M_nodeCapacity)...) |
| 226 | tree.M_nodeCapacity *= 2 |
| 227 | |
| 228 | // Build a linked list for the free list. The parent |
| 229 | // pointer becomes the "next" pointer. |
| 230 | for i := tree.M_nodeCount; i < tree.M_nodeCapacity-1; i++ { |
| 231 | tree.M_nodes[i].Next = i + 1 |
| 232 | tree.M_nodes[i].Height = -1 |
| 233 | } |
| 234 | |
| 235 | tree.M_nodes[tree.M_nodeCapacity-1].Next = B2_nullNode |
| 236 | tree.M_nodes[tree.M_nodeCapacity-1].Height = -1 |
| 237 | tree.M_freeList = tree.M_nodeCount |
| 238 | } |
| 239 | |
| 240 | // Peel a node off the free list. |
| 241 | nodeId := tree.M_freeList |
| 242 | tree.M_freeList = tree.M_nodes[nodeId].Next |
| 243 | tree.M_nodes[nodeId].Parent = B2_nullNode |
| 244 | tree.M_nodes[nodeId].Child1 = B2_nullNode |
| 245 | tree.M_nodes[nodeId].Child2 = B2_nullNode |
| 246 | tree.M_nodes[nodeId].Height = 0 |
| 247 | tree.M_nodes[nodeId].UserData = nil |
| 248 | tree.M_nodeCount++ |
| 249 | |
| 250 | return nodeId |
| 251 | } |
| 252 | |
| 253 | // Return a node to the pool. |
| 254 | func (tree *B2DynamicTree) FreeNode(nodeId int) { |
no test coverage detected