Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <utility>
  3. #include <vector>
  4. #include <stack>
  5. #include <algorithm>
  6.  
  7. template <typename key_t, typename val_t>
  8. class Custom_QuickHeap {
  9. /*
  10. * @brief An implementation of QuickHeap data structure
  11. * see Gonzalo Navarro and Rodrigo Paredes - "Quickheaps: Simple, Efficient, and Cache-Oblivious"
  12. */
  13. public:
  14. Custom_QuickHeap(const size_t max_capacity);
  15. Custom_QuickHeap(const std::vector<std::pair<key_t, val_t>>& array, const size_t max_capacity);
  16. ~Custom_QuickHeap();
  17.  
  18. void insert(const key_t& key, const val_t& value);
  19. std::pair<key_t, val_t> peakMin();
  20. std::pair<key_t, val_t> extractMin();
  21.  
  22. void size();
  23. void dump();
  24.  
  25. private:
  26. std::vector<std::pair<key_t, val_t>>* heap;
  27. std::stack<size_t>* pivots;
  28.  
  29. size_t heap_begin;
  30. size_t capacity;
  31. size_t current_size;
  32. };
  33.  
  34. template <typename key_t, typename val_t>
  35. Custom_QuickHeap<key_t, val_t>::Custom_QuickHeap(const size_t max_capacity) {
  36. /*
  37. * @brief constructor of an empty QuickHeap
  38. * @param max_capacity - maximal amount of elements in heap
  39. */
  40. capacity = max_capacity + 1; // + 1 is for artificial "+inf" pivot
  41. current_size = 0;
  42.  
  43. heap = new std::vector<std::pair<key_t, val_t>>(capacity);
  44. pivots = new std::stack<size_t>;
  45.  
  46. pivots->push(0);
  47. heap_begin = 0;
  48. }
  49.  
  50. template <typename key_t, typename val_t>
  51. Custom_QuickHeap<key_t, val_t>::Custom_QuickHeap(const std::vector<std::pair<key_t, val_t>>& array,
  52. const size_t max_capacity) {
  53. /*
  54. * @brief constructor of a QuickHeap from an array;
  55. * @param array - array from which heap will be built
  56. * @param max_capacity - maximal amount of elements in heap
  57. */
  58. capacity = std::max(max_capacity, array.size()) + 1; // + 1 is for artificial "+inf" pivot
  59. current_size = array.size();
  60.  
  61. heap = new std::vector<std::pair<key_t, val_t>>(capacity);
  62. pivots = new std::stack<size_t>;
  63.  
  64. pivots->push(array.size());
  65. heap_begin = 0;
  66.  
  67. for (size_t i = 0; i < array.size(); ++i) {
  68. heap->at(i) = array[i];
  69. }
  70. }
  71.  
  72. int main() {
  73. std::cout << "Hello, World!" << std::endl;
  74. return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement