AkHeap.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*******************************************************************************
  2. The content of this file includes portions of the AUDIOKINETIC Wwise Technology
  3. released in source code form as part of the SDK installer package.
  4. Commercial License Usage
  5. Licensees holding valid commercial licenses to the AUDIOKINETIC Wwise Technology
  6. may use this file in accordance with the end user license agreement provided
  7. with the software or, alternatively, in accordance with the terms contained in a
  8. written agreement between you and Audiokinetic Inc.
  9. Apache License Usage
  10. Alternatively, this file may be used under the Apache License, Version 2.0 (the
  11. "Apache License"); you may not use this file except in compliance with the
  12. Apache License. You may obtain a copy of the Apache License at
  13. http://www.apache.org/licenses/LICENSE-2.0.
  14. Unless required by applicable law or agreed to in writing, software distributed
  15. under the Apache License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
  16. OR CONDITIONS OF ANY KIND, either express or implied. See the Apache License for
  17. the specific language governing permissions and limitations under the License.
  18. Copyright (c) 2023 Audiokinetic Inc.
  19. *******************************************************************************/
  20. #pragma once
  21. #include <AK/Tools/Common/AkKeyArray.h>
  22. template <class T_KEY, class T_ITEM, class U_POOL, class U_KEY = AkGetArrayKey< T_KEY, T_ITEM >, class TGrowBy = AkGrowByPolicy_DEFAULT, class TMovePolicy = AkAssignmentMovePolicy<T_ITEM>, class TComparePolicy = AkDefaultSortedKeyCompare<T_KEY> >
  23. class CAkHeap : public AkArray< T_ITEM, const T_ITEM &, U_POOL, TGrowBy, TMovePolicy >
  24. {
  25. typedef AkArray< T_ITEM, const T_ITEM &, U_POOL, TGrowBy, TMovePolicy > Base;
  26. public:
  27. T_ITEM * Insert(T_KEY in_Key)
  28. {
  29. if (Base::AddLast())
  30. {
  31. int insertIdx = Base::m_uLength - 1;
  32. while (insertIdx != 0)
  33. {
  34. int parentIdx = Parent(insertIdx);
  35. if (Lesser(in_Key, U_KEY::Get(Base::m_pItems[parentIdx])))
  36. {
  37. TMovePolicy::Move(Base::m_pItems[insertIdx], Base::m_pItems[parentIdx]);
  38. insertIdx = parentIdx;
  39. }
  40. else
  41. {
  42. break;
  43. }
  44. }
  45. T_ITEM* pItem = &Base::m_pItems[insertIdx];
  46. // Set key
  47. U_KEY::Get(*pItem) = in_Key;
  48. return pItem;
  49. }
  50. return NULL;
  51. }
  52. void RemoveRoot()
  53. {
  54. if (Base::m_uLength <= 1)
  55. {
  56. Base::m_uLength = 0;
  57. return;
  58. }
  59. Base::m_pItems[0] = Base::m_pItems[Base::m_uLength - 1];
  60. Base::m_uLength--;
  61. Heapify();
  62. }
  63. void Heapify()
  64. {
  65. AkUInt32 idx = 0;
  66. while(true)
  67. {
  68. AkUInt32 left = LeftChild(idx);
  69. AkUInt32 right = RightChild(idx);
  70. AkUInt32 smallest = idx;
  71. if (left < Base::m_uLength && Lesser(Base::m_pItems[left].key, Base::m_pItems[idx].key) )
  72. smallest = left;
  73. if (right < Base::m_uLength && Lesser(Base::m_pItems[right].key, Base::m_pItems[smallest].key) )
  74. smallest = right;
  75. if (smallest == idx)
  76. break;
  77. Swap(idx, smallest);
  78. idx = smallest;
  79. }
  80. }
  81. private:
  82. AkForceInline bool Lesser(T_KEY &a, T_KEY &b) const
  83. {
  84. return TComparePolicy::Lesser((void*)this, a, b);
  85. }
  86. AkForceInline void Swap(AkUInt32 in_i0, AkUInt32 in_i1)
  87. {
  88. T_ITEM temp;
  89. TMovePolicy::Move(temp, Base::m_pItems[in_i0]);
  90. TMovePolicy::Move(Base::m_pItems[in_i0], Base::m_pItems[in_i1]);
  91. TMovePolicy::Move(Base::m_pItems[in_i1], temp);
  92. }
  93. AkForceInline AkUInt32 Parent(AkUInt32 i)
  94. {
  95. return (i - 1U) / 2U;
  96. }
  97. AkForceInline AkUInt32 LeftChild(AkUInt32 i)
  98. {
  99. return (2U * i + 1U);
  100. }
  101. AkForceInline AkUInt32 RightChild(AkUInt32 i)
  102. {
  103. return (2U * i + 2U);
  104. }
  105. };