/src/zeek/src/PriorityQueue.cc
Line | Count | Source |
1 | | // See the file "COPYING" in the main distribution directory for copyright. |
2 | | |
3 | | #include "zeek/PriorityQueue.h" |
4 | | |
5 | | #include <cstdlib> |
6 | | |
7 | | #include "zeek/Reporter.h" |
8 | | |
9 | | namespace zeek::detail { |
10 | | |
11 | 66 | PriorityQueue::PriorityQueue(int initial_size) : max_heap_size(initial_size) { heap = new PQ_Element*[max_heap_size]; } |
12 | | |
13 | 0 | PriorityQueue::~PriorityQueue() { |
14 | 0 | for ( int i = 0; i < heap_size; ++i ) |
15 | 0 | delete heap[i]; |
16 | |
|
17 | 0 | delete[] heap; |
18 | 0 | } |
19 | | |
20 | 2.71M | PQ_Element* PriorityQueue::Remove() { |
21 | 2.71M | if ( heap_size == 0 ) |
22 | 242k | return nullptr; |
23 | | |
24 | 2.47M | PQ_Element* top = heap[0]; |
25 | | |
26 | 2.47M | --heap_size; |
27 | 2.47M | SetElement(0, heap[heap_size]); |
28 | 2.47M | BubbleDown(0); |
29 | | |
30 | 2.47M | top->SetOffset(-1); // = not in heap |
31 | 2.47M | return top; |
32 | 2.71M | } |
33 | | |
34 | 677k | PQ_Element* PriorityQueue::Remove(PQ_Element* e) { |
35 | 677k | if ( e->Offset() < 0 || e->Offset() >= heap_size || heap[e->Offset()] != e ) |
36 | 0 | return nullptr; // not in heap |
37 | | |
38 | 677k | e->MinimizeTime(); |
39 | 677k | BubbleUp(e->Offset()); |
40 | | |
41 | 677k | PQ_Element* e2 = Remove(); |
42 | | |
43 | 677k | if ( e != e2 ) |
44 | 0 | reporter->InternalError("inconsistency in PriorityQueue::Remove"); |
45 | | |
46 | 677k | return e2; |
47 | 677k | } |
48 | | |
49 | 2.47M | bool PriorityQueue::Add(PQ_Element* e) { |
50 | 2.47M | SetElement(heap_size, e); |
51 | | |
52 | 2.47M | BubbleUp(heap_size); |
53 | | |
54 | 2.47M | ++cumulative_num; |
55 | | |
56 | 2.47M | if ( ++heap_size > peak_heap_size ) |
57 | 100k | peak_heap_size = heap_size; |
58 | | |
59 | 2.47M | if ( heap_size >= max_heap_size ) |
60 | 220 | return Resize(max_heap_size * 2); |
61 | 2.47M | else |
62 | 2.47M | return true; |
63 | 2.47M | } |
64 | | |
65 | 220 | bool PriorityQueue::Resize(int new_size) { |
66 | 220 | PQ_Element** tmp = new PQ_Element*[new_size]; |
67 | 141k | for ( int i = 0; i < max_heap_size; ++i ) |
68 | 141k | tmp[i] = heap[i]; |
69 | | |
70 | 220 | delete[] heap; |
71 | 220 | heap = tmp; |
72 | | |
73 | 220 | max_heap_size = new_size; |
74 | | |
75 | 220 | return heap != nullptr; |
76 | 220 | } |
77 | | |
78 | 5.70M | void PriorityQueue::BubbleUp(int bin) { |
79 | 5.70M | if ( bin == 0 ) |
80 | 1.17M | return; |
81 | | |
82 | 4.53M | int p = Parent(bin); |
83 | 4.53M | if ( heap[p]->Time() > heap[bin]->Time() ) { |
84 | 2.55M | Swap(p, bin); |
85 | 2.55M | BubbleUp(p); |
86 | 2.55M | } |
87 | 4.53M | } |
88 | | |
89 | 4.22M | void PriorityQueue::BubbleDown(int bin) { |
90 | 4.22M | double v = heap[bin]->Time(); |
91 | | |
92 | 4.22M | int l = LeftChild(bin); |
93 | 4.22M | int r = RightChild(bin); |
94 | | |
95 | 4.22M | if ( l >= heap_size ) |
96 | 603k | return; // No children. |
97 | | |
98 | 3.62M | if ( r >= heap_size ) { // Just a left child. |
99 | 296k | if ( heap[l]->Time() < v ) |
100 | 23.7k | Swap(l, bin); |
101 | 296k | } |
102 | | |
103 | 3.32M | else { |
104 | 3.32M | double lv = heap[l]->Time(); |
105 | 3.32M | double rv = heap[r]->Time(); |
106 | | |
107 | 3.32M | if ( lv < rv ) { |
108 | 567k | if ( lv < v ) { |
109 | 528k | Swap(l, bin); |
110 | 528k | BubbleDown(l); |
111 | 528k | } |
112 | 567k | } |
113 | | |
114 | 2.75M | else if ( rv < v ) { |
115 | 1.22M | Swap(r, bin); |
116 | 1.22M | BubbleDown(r); |
117 | 1.22M | } |
118 | 3.32M | } |
119 | 3.62M | } |
120 | | |
121 | | } // namespace zeek::detail |