DetourNode.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // Copyright (c) 2009 Mikko Mononen memon@inside.org
  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. #include "DetourNode.h"
  19. #include <string.h>
  20. //////////////////////////////////////////////////////////////////////////////////////////
  21. dtNodePool::dtNodePool(int maxNodes, int hashSize) :
  22. m_nodes(0),
  23. m_first(0),
  24. m_next(0),
  25. m_maxNodes(maxNodes),
  26. m_hashSize(hashSize),
  27. m_nodeCount(0)
  28. {
  29. m_nodes = new dtNode[m_maxNodes];
  30. m_next = new unsigned short[m_maxNodes];
  31. m_first = new unsigned short[hashSize];
  32. memset(m_first, 0xff, sizeof(unsigned short)*m_hashSize);
  33. memset(m_next, 0xff, sizeof(unsigned short)*m_maxNodes);
  34. }
  35. dtNodePool::~dtNodePool()
  36. {
  37. delete [] m_nodes;
  38. delete [] m_next;
  39. delete [] m_first;
  40. }
  41. void dtNodePool::clear()
  42. {
  43. memset(m_first, 0xff, sizeof(unsigned short)*m_hashSize);
  44. m_nodeCount = 0;
  45. }
  46. const dtNode* dtNodePool::findNode(unsigned int id) const
  47. {
  48. unsigned int bucket = hashint(id) & (m_hashSize-1);
  49. unsigned short i = m_first[bucket];
  50. while (i != 0xffff)
  51. {
  52. if (m_nodes[i].id == id)
  53. return &m_nodes[i];
  54. i = m_next[i];
  55. }
  56. return 0;
  57. }
  58. dtNode* dtNodePool::getNode(unsigned int id)
  59. {
  60. unsigned int bucket = hashint(id) & (m_hashSize-1);
  61. unsigned short i = m_first[bucket];
  62. dtNode* node = 0;
  63. while (i != 0xffff)
  64. {
  65. if (m_nodes[i].id == id)
  66. return &m_nodes[i];
  67. i = m_next[i];
  68. }
  69. if (m_nodeCount >= m_maxNodes)
  70. return 0;
  71. i = (unsigned short)m_nodeCount;
  72. m_nodeCount++;
  73. // Init node
  74. node = &m_nodes[i];
  75. node->pidx = 0;
  76. node->cost = 0;
  77. node->total = 0;
  78. node->id = id;
  79. node->flags = 0;
  80. m_next[i] = m_first[bucket];
  81. m_first[bucket] = i;
  82. return node;
  83. }
  84. //////////////////////////////////////////////////////////////////////////////////////////
  85. dtNodeQueue::dtNodeQueue(int n) :
  86. m_heap(0),
  87. m_capacity(n),
  88. m_size(0)
  89. {
  90. m_heap = new dtNode*[m_capacity+1];
  91. }
  92. dtNodeQueue::~dtNodeQueue()
  93. {
  94. delete [] m_heap;
  95. }
  96. void dtNodeQueue::bubbleUp(int i, dtNode* node)
  97. {
  98. int parent = (i-1)/2;
  99. // note: (index > 0) means there is a parent
  100. while ((i > 0) && (m_heap[parent]->total > node->total))
  101. {
  102. m_heap[i] = m_heap[parent];
  103. i = parent;
  104. parent = (i-1)/2;
  105. }
  106. m_heap[i] = node;
  107. }
  108. void dtNodeQueue::trickleDown(int i, dtNode* node)
  109. {
  110. int child = (i*2)+1;
  111. while (child < m_size)
  112. {
  113. if (((child+1) < m_size) &&
  114. (m_heap[child]->total > m_heap[child+1]->total))
  115. {
  116. child++;
  117. }
  118. m_heap[i] = m_heap[child];
  119. i = child;
  120. child = (i*2)+1;
  121. }
  122. bubbleUp(i, node);
  123. }