LocHeap.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* Copyright (c) 2015, 2020 The Linux Foundation. All rights reserved.
  2. *
  3. * Redistribution and use in source and binary forms, with or without
  4. * modification, are permitted provided that the following conditions are
  5. * met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above
  9. * copyright notice, this list of conditions and the following
  10. * disclaimer in the documentation and/or other materials provided
  11. * with the distribution.
  12. * * Neither the name of The Linux Foundation, nor the names of its
  13. * contributors may be used to endorse or promote products derived
  14. * from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  20. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  23. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  24. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  25. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  26. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. */
  29. #ifndef __LOC_HEAP__
  30. #define __LOC_HEAP__
  31. #include <stddef.h>
  32. #include <string.h>
  33. namespace loc_util {
  34. // abstract class to be implemented by client to provide a rankable class
  35. class LocRankable {
  36. public:
  37. virtual inline ~LocRankable() {}
  38. // method to rank objects of such type for sorting purposes.
  39. // The pointer of the input node would be stored in the heap.
  40. // >0 if ranks higher than the input;
  41. // ==0 if equally ranks with the input;
  42. // <0 if ranks lower than the input
  43. virtual int ranks(LocRankable& rankable) = 0;
  44. // convenient method to rank objects of such type for sorting purposes.
  45. inline bool outRanks(LocRankable& rankable) { return ranks(rankable) > 0; }
  46. };
  47. // opaque class to provide service implementation.
  48. class LocHeapNode;
  49. // a heap whose left and right children are not sorted. It is sorted only vertically,
  50. // i.e. parent always ranks higher than children, if they exist. Ranking algorithm is
  51. // implemented in Rankable. The reason that there is no sort between children is to
  52. // help beter balance the tree with lower cost. When a node is pushed to the tree,
  53. // it is guaranteed that the subtree that is smaller gets to have the new node.
  54. class LocHeap {
  55. protected:
  56. LocHeapNode* mTree;
  57. public:
  58. inline LocHeap() : mTree(NULL) {}
  59. ~LocHeap();
  60. // push keeps the tree sorted by rank, it also tries to balance the
  61. // tree by adding the new node to the smaller of the subtrees.
  62. // node is reference to an obj that is managed by client, that client
  63. // creates and destroyes. The destroy should happen after the
  64. // node is popped out from the heap.
  65. void push(LocRankable& node);
  66. // Peeks the node data on tree top, which has currently the highest ranking
  67. // There is no change the tree structure with this operation
  68. // Returns NULL if the tree is empty, otherwise pointer to the node data of
  69. // the tree top.
  70. LocRankable* peek();
  71. // pop keeps the tree sorted by rank, but it does not try to balance
  72. // the tree.
  73. // Return - pointer to the node popped out, or NULL if heap is already empty
  74. LocRankable* pop();
  75. // navigating through the tree and find the node that ranks the same
  76. // as the input data, then remove it from the tree. Rank is implemented
  77. // by rankable obj.
  78. // returns the pointer to the node removed; or NULL (if failed).
  79. LocRankable* remove(LocRankable& rankable);
  80. #ifdef __LOC_UNIT_TEST__
  81. bool checkTree();
  82. uint32_t getTreeSize();
  83. #endif
  84. };
  85. } // namespace loc_util
  86. #endif //__LOC_HEAP__