vectorHeap.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _VECTOREXT_H
  23. #define _VECTOREXT_H
  24. #ifndef _VECTOR_H_
  25. #include "vector.h"
  26. #endif
  27. //-------------------------------------------------------------------------------------
  28. // Based on demo Heap class by Ron Penton
  29. //-------------------------------------------------------------------------------------
  30. template <class T>
  31. class Heap : public Vector<T>
  32. {
  33. using Vector<T>::increment;
  34. using Vector<T>::decrement;
  35. using Vector<T>::back;
  36. using Vector<T>::mElementCount;
  37. using Vector<T>::mArray;
  38. public:
  39. //---------------------------------------------------------
  40. Heap( U32 size, S32 ( *p_compare )( T, T ) )
  41. : Vector< T >( size + 1 )
  42. {
  43. m_compare = p_compare;
  44. }
  45. //---------------------------------------------------------
  46. void enqueue( T element )
  47. {
  48. increment( 1 );
  49. back() = element;
  50. walk_up( mElementCount );
  51. }
  52. //---------------------------------------------------------
  53. void dequeue()
  54. {
  55. if( mElementCount >= 1 )
  56. {
  57. mArray[1] = mArray[mElementCount]; // swap back to front
  58. walk_down( 1 );
  59. decrement( 1 );
  60. }
  61. }
  62. //---------------------------------------------------------
  63. T& item()
  64. {
  65. return mArray[1];
  66. }
  67. //---------------------------------------------------------
  68. void walk_up( U32 index )
  69. {
  70. // set up the parent and child indexes
  71. U32 parent = index / 2;
  72. U32 child = index;
  73. // store the item to walk up in temp buffer
  74. T temp = mArray[child];
  75. while( parent > 0 )
  76. { // if the node to walk up is more than the parent, then swap
  77. // UNUSED: DAVID WYAND -> Node tempParent = mArray[parent];
  78. if( m_compare( temp, mArray[parent] ) > 0 )
  79. {
  80. // swap the parent and child, and go up a level
  81. mArray[child] = mArray[parent];
  82. child = parent;
  83. parent /= 2;
  84. }
  85. else
  86. {
  87. break;
  88. }
  89. }
  90. // put the temp variable (the one that was walked up) into the child index
  91. mArray[child] = temp;
  92. }
  93. //---------------------------------------------------------
  94. void walk_down( U32 index )
  95. {
  96. // calculate the parent and child indexes
  97. U32 parent = index;
  98. U32 child = index * 2;
  99. // store the data to walk down in a temp buffer
  100. T temp = mArray[parent];
  101. // loop through, walking node down the heap until both children are smaller than the node
  102. while( child < mElementCount )
  103. {
  104. // if left child is not the last node in the tree, then
  105. // find out which of the current node's children is largest
  106. if( child < mElementCount - 1 )
  107. {
  108. if( m_compare( mArray[child], mArray[child + 1] ) < 0 )
  109. { // change the pointer to the right child, since it is larger
  110. child++;
  111. }
  112. }
  113. // if the node to walk down is lower than the highest value child.
  114. // move the child up one level
  115. if( m_compare( temp, mArray[child] ) < 0 )
  116. {
  117. mArray[parent] = mArray[child];
  118. parent = child;
  119. child *= 2;
  120. }
  121. else
  122. break;
  123. }
  124. mArray[parent] = temp;
  125. }
  126. //---------------------------------------------------------
  127. S32 ( *m_compare )( T, T );
  128. };
  129. #endif