compare_min(unsigned int a,unsigned int b)1 bool compare_min(unsigned int a, unsigned int b) {
2     return a > b;
3 }
4 
compare_max(unsigned int a,unsigned int b)5 bool compare_max(unsigned int a, unsigned int b) {
6     return a < b;
7 }
8 
9 template <class order_by_template_type>
fast_priority_queue(unsigned int queue_size)10 fast_priority_queue<order_by_template_type>::fast_priority_queue(unsigned int queue_size) {
11     this->queue_size = queue_size;
12     internal_list.reserve(queue_size);
13 }
14 
15 template <class order_by_template_type>
insert(order_by_template_type main_value,int data)16 void fast_priority_queue<order_by_template_type>::insert(order_by_template_type main_value, int data) {
17     // Because it's ehap we can remove
18 
19     // Append new element to the end of list
20     internal_list.push_back(main_value);
21 
22     // Convert list to the complete heap
23     // Up to logarithmic in the distance between first and last: Compares elements and potentially
24     // swaps (or moves) them until rearranged as a longer heap.
25     std::push_heap(internal_list.begin(), internal_list.end(), compare_min);
26 
27     if (this->internal_list.size() >= queue_size) {
28         // And now we should remove minimal element from the internal_list
29         // Prepare heap to remove min element
30         std::pop_heap(internal_list.begin(), internal_list.end(), compare_min);
31         // Remove element from the head
32         internal_list.pop_back();
33     }
34 }
35 
36 template <class order_by_template_type>
get_min_element()37 order_by_template_type fast_priority_queue<order_by_template_type>::get_min_element() {
38     // We will return head of list because it's consists minimum element
39     return internal_list.front();
40 }
41 
42 template <class order_by_template_type>
print_internal_list()43 void fast_priority_queue<order_by_template_type>::print_internal_list() {
44     for (unsigned int i = 0; i < internal_list.size(); i++) {
45         std::cout << internal_list[i] << std::endl;
46     }
47 }
48 
print()49 template <class order_by_template_type> void fast_priority_queue<order_by_template_type>::print() {
50     // Create new list for sort because we can't do it in place
51     std::vector<order_by_template_type> sorted_list;
52 
53     // Allocate enough space
54     sorted_list.reserve(internal_list.size());
55 
56     // Copy to new vector with copy constructor
57     sorted_list = internal_list;
58 
59     // Execute heap sort because array paritally sorted already
60     std::sort_heap(sorted_list.begin(), sorted_list.end(), compare_min);
61 
62     for (unsigned int i = 0; i < sorted_list.size(); i++) {
63         std::cout << sorted_list[i] << std::endl;
64     }
65 }
66