| 1 | // |
| 2 | // Copyright (c) 2009-2010 Mikko Mononen [email protected] |
| 3 | // |
| 4 | // This software is provided 'as-is', without any express or implied |
| 5 | // warranty. In no event will the authors be held liable for any damages |
| 6 | // arising from the use of this software. |
| 7 | // Permission is granted to anyone to use this software for any purpose, |
| 8 | // including commercial applications, and to alter it and redistribute it |
| 9 | // freely, subject to the following restrictions: |
| 10 | // 1. The origin of this software must not be misrepresented; you must not |
| 11 | // claim that you wrote the original software. If you use this software |
| 12 | // in a product, an acknowledgment in the product documentation would be |
| 13 | // appreciated but is not required. |
| 14 | // 2. Altered source versions must be plainly marked as such, and must not be |
| 15 | // misrepresented as being the original software. |
| 16 | // 3. This notice may not be removed or altered from any source distribution. |
| 17 | // |
| 18 | |
| 19 | #include "DetourNode.h" |
| 20 | #include "DetourAlloc.h" |
| 21 | #include "DetourAssert.h" |
| 22 | #include "DetourCommon.h" |
| 23 | #include <string.h> |
| 24 | |
| 25 | #ifdef DT_POLYREF64 |
| 26 | // From Thomas Wang, https://gist.github.com/badboy/6267743 |
| 27 | inline unsigned int dtHashRef(dtPolyRef a) |
| 28 | { |
| 29 | a = (~a) + (a << 18); // a = (a << 18) - a - 1; |
| 30 | a = a ^ (a >> 31); |
| 31 | a = a * 21; // a = (a + (a << 2)) + (a << 4); |
| 32 | a = a ^ (a >> 11); |
| 33 | a = a + (a << 6); |
| 34 | a = a ^ (a >> 22); |
| 35 | return (unsigned int)a; |
| 36 | } |
| 37 | #else |
| 38 | inline unsigned int dtHashRef(dtPolyRef a) |
| 39 | { |
| 40 | a += ~(a<<15); |
| 41 | a ^= (a>>10); |
| 42 | a += (a<<3); |
| 43 | a ^= (a>>6); |
| 44 | a += ~(a<<11); |
| 45 | a ^= (a>>16); |
| 46 | return (unsigned int)a; |
| 47 | } |
| 48 | #endif |
| 49 | |
| 50 | ////////////////////////////////////////////////////////////////////////////////////////// |
| 51 | dtNodePool::dtNodePool(int maxNodes, int hashSize) : |
| 52 | m_nodes(0), |
| 53 | m_first(0), |
| 54 | m_next(0), |
| 55 | m_maxNodes(maxNodes), |
| 56 | m_hashSize(hashSize), |
| 57 | m_nodeCount(0) |
| 58 | { |
| 59 | dtAssert(dtNextPow2(m_hashSize) == (unsigned int)m_hashSize); |
| 60 | // pidx is special as 0 means "none" and 1 is the first node. For that reason |
| 61 | // we have 1 fewer nodes available than the number of values it can contain. |
| 62 | dtAssert(m_maxNodes > 0 && m_maxNodes <= DT_NULL_IDX && m_maxNodes <= (1 << DT_NODE_PARENT_BITS) - 1); |
| 63 | |
| 64 | m_nodes = (dtNode*)dtAlloc(sizeof(dtNode)*m_maxNodes, DT_ALLOC_PERM); |
| 65 | m_next = (dtNodeIndex*)dtAlloc(sizeof(dtNodeIndex)*m_maxNodes, DT_ALLOC_PERM); |
| 66 | m_first = (dtNodeIndex*)dtAlloc(sizeof(dtNodeIndex)*hashSize, DT_ALLOC_PERM); |
| 67 | |
| 68 | dtAssert(m_nodes); |
| 69 | dtAssert(m_next); |
| 70 | dtAssert(m_first); |
| 71 | |
| 72 | memset(m_first, 0xff, sizeof(dtNodeIndex)*m_hashSize); |
| 73 | memset(m_next, 0xff, sizeof(dtNodeIndex)*m_maxNodes); |
| 74 | } |
| 75 | |
| 76 | dtNodePool::~dtNodePool() |
| 77 | { |
| 78 | dtFree(m_nodes); |
| 79 | dtFree(m_next); |
| 80 | dtFree(m_first); |
| 81 | } |
| 82 | |
| 83 | void dtNodePool::clear() |
| 84 | { |
| 85 | memset(m_first, 0xff, sizeof(dtNodeIndex)*m_hashSize); |
| 86 | m_nodeCount = 0; |
| 87 | } |
| 88 | |
| 89 | unsigned int dtNodePool::findNodes(dtPolyRef id, dtNode** nodes, const int maxNodes) |
| 90 | { |
| 91 | int n = 0; |
| 92 | unsigned int bucket = dtHashRef(id) & (m_hashSize-1); |
| 93 | dtNodeIndex i = m_first[bucket]; |
| 94 | while (i != DT_NULL_IDX) |
| 95 | { |
| 96 | if (m_nodes[i].id == id) |
| 97 | { |
| 98 | if (n >= maxNodes) |
| 99 | return n; |
| 100 | nodes[n++] = &m_nodes[i]; |
| 101 | } |
| 102 | i = m_next[i]; |
| 103 | } |
| 104 | |
| 105 | return n; |
| 106 | } |
| 107 | |
| 108 | dtNode* dtNodePool::findNode(dtPolyRef id, unsigned char state) |
| 109 | { |
| 110 | unsigned int bucket = dtHashRef(id) & (m_hashSize-1); |
| 111 | dtNodeIndex i = m_first[bucket]; |
| 112 | while (i != DT_NULL_IDX) |
| 113 | { |
| 114 | if (m_nodes[i].id == id && m_nodes[i].state == state) |
| 115 | return &m_nodes[i]; |
| 116 | i = m_next[i]; |
| 117 | } |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | dtNode* dtNodePool::getNode(dtPolyRef id, unsigned char state) |
| 122 | { |
| 123 | unsigned int bucket = dtHashRef(id) & (m_hashSize-1); |
| 124 | dtNodeIndex i = m_first[bucket]; |
| 125 | dtNode* node = 0; |
| 126 | while (i != DT_NULL_IDX) |
| 127 | { |
| 128 | if (m_nodes[i].id == id && m_nodes[i].state == state) |
| 129 | return &m_nodes[i]; |
| 130 | i = m_next[i]; |
| 131 | } |
| 132 | |
| 133 | if (m_nodeCount >= m_maxNodes) |
| 134 | return 0; |
| 135 | |
| 136 | i = (dtNodeIndex)m_nodeCount; |
| 137 | m_nodeCount++; |
| 138 | |
| 139 | // Init node |
| 140 | node = &m_nodes[i]; |
| 141 | node->pidx = 0; |
| 142 | node->cost = 0; |
| 143 | node->total = 0; |
| 144 | node->id = id; |
| 145 | node->state = state; |
| 146 | node->flags = 0; |
| 147 | |
| 148 | m_next[i] = m_first[bucket]; |
| 149 | m_first[bucket] = i; |
| 150 | |
| 151 | return node; |
| 152 | } |
| 153 | |
| 154 | |
| 155 | ////////////////////////////////////////////////////////////////////////////////////////// |
| 156 | dtNodeQueue::dtNodeQueue(int n) : |
| 157 | m_heap(0), |
| 158 | m_capacity(n), |
| 159 | m_size(0) |
| 160 | { |
| 161 | dtAssert(m_capacity > 0); |
| 162 | |
| 163 | m_heap = (dtNode**)dtAlloc(sizeof(dtNode*)*(m_capacity+1), DT_ALLOC_PERM); |
| 164 | dtAssert(m_heap); |
| 165 | } |
| 166 | |
| 167 | dtNodeQueue::~dtNodeQueue() |
| 168 | { |
| 169 | dtFree(m_heap); |
| 170 | } |
| 171 | |
| 172 | void dtNodeQueue::bubbleUp(int i, dtNode* node) |
| 173 | { |
| 174 | int parent = (i-1)/2; |
| 175 | // note: (index > 0) means there is a parent |
| 176 | while ((i > 0) && (m_heap[parent]->total > node->total)) |
| 177 | { |
| 178 | m_heap[i] = m_heap[parent]; |
| 179 | i = parent; |
| 180 | parent = (i-1)/2; |
| 181 | } |
| 182 | m_heap[i] = node; |
| 183 | } |
| 184 | |
| 185 | void dtNodeQueue::trickleDown(int i, dtNode* node) |
| 186 | { |
| 187 | int child = (i*2)+1; |
| 188 | while (child < m_size) |
| 189 | { |
| 190 | if (((child+1) < m_size) && |
| 191 | (m_heap[child]->total > m_heap[child+1]->total)) |
| 192 | { |
| 193 | child++; |
| 194 | } |
| 195 | m_heap[i] = m_heap[child]; |
| 196 | i = child; |
| 197 | child = (i*2)+1; |
| 198 | } |
| 199 | bubbleUp(i, node); |
| 200 | } |