zeek/src/PriorityQueue.h
Max Kellermann 0db61f3094 include cleanup
The Zeek code base has very inconsistent #includes.  Many sources
included a few headers, and those headers included other headers, and
in the end, nearly everything is included everywhere, so missing
#includes were never noticed.  Another side effect was a lot of header
bloat which slows down the build.

First step to fix it: in each source file, its own header should be
included first to verify that each header's includes are correct, and
none is missing.

After adding the missing #includes, I replaced lots of #includes
inside headers with class forward declarations.  In most headers,
object pointers are never referenced, so declaring the function
prototypes with forward-declared classes is just fine.

This patch speeds up the build by 19%, because each compilation unit
gets smaller.  Here are the "time" numbers for a fresh build (with a
warm page cache but without ccache):

Before this patch:

 3144.94user 161.63system 3:02.87elapsed 1808%CPU (0avgtext+0avgdata 2168608maxresident)k
 760inputs+12008400outputs (1511major+57747204minor)pagefaults 0swaps

After this patch:

 2565.17user 141.83system 2:25.46elapsed 1860%CPU (0avgtext+0avgdata 1489076maxresident)k
 72576inputs+9130920outputs (1667major+49400430minor)pagefaults 0swaps
2020-02-04 20:51:02 +01:00

97 lines
1.9 KiB
C++

// See the file "COPYING" in the main distribution directory for copyright.
#pragma once
#include <math.h>
#include <stdint.h>
class PriorityQueue;
class PQ_Element {
public:
explicit PQ_Element(double t) { time = t; offset = -1; }
virtual ~PQ_Element() { }
double Time() const { return time; }
int Offset() const { return offset; }
void SetOffset(int off) { offset = off; }
void MinimizeTime() { time = -HUGE_VAL; }
protected:
PQ_Element() { time = 0; offset = -1; }
double time;
int offset;
};
class PriorityQueue {
public:
explicit PriorityQueue(int initial_size = 16);
~PriorityQueue();
// Returns the top of queue, or nil if the queue is empty.
PQ_Element* Top() const
{
if ( heap_size == 0 )
return 0;
else
return heap[0];
}
// Removes (and returns) top of queue. Returns nil if the queue
// is empty.
PQ_Element* Remove();
// Removes element e. Returns e, or nullptr if e wasn't in the queue.
// Note that e will be modified via MinimizeTime().
PQ_Element* Remove(PQ_Element* e);
// Add a new element to the queue. Returns false on failure (not enough
// memory to add the element), true on success.
bool Add(PQ_Element* e);
int Size() const { return heap_size; }
int PeakSize() const { return peak_heap_size; }
uint64_t CumulativeNum() const { return cumulative_num; }
protected:
bool Resize(int new_size);
void BubbleUp(int bin);
void BubbleDown(int bin);
int Parent(int bin) const
{
return bin >> 1;
}
int LeftChild(int bin) const
{
return bin << 1;
}
int RightChild(int bin) const
{
return LeftChild(bin) + 1;
}
void SetElement(int bin, PQ_Element* e)
{
heap[bin] = e;
e->SetOffset(bin);
}
void Swap(int bin1, int bin2)
{
PQ_Element* t = heap[bin1];
SetElement(bin1, heap[bin2]);
SetElement(bin2, t);
}
PQ_Element** heap;
int heap_size;
int peak_heap_size;
int max_heap_size;
uint64_t cumulative_num;
};