DHART
Loading...
Searching...
No Matches
unique_queue.cpp
Go to the documentation of this file.
1
7
8#include <unique_queue.h>
9
10namespace HF::GraphGenerator{
12 {
13 // Only insert if it's not set to 1
14 if (hashmap[p])
15 return false;
16
17 // Push it to the end of the queue, then
18 // mark it in the hashmap.
19 node_queue.push(p);
20 hashmap[p] = 1;
21
22 return true;
23 }
24
26 {
27 auto r = node_queue.front();
28 node_queue.pop();
29 return r;
30 }
31
32 int UniqueQueue::size() const {return node_queue.size();}
33
35 auto r = node_queue.front();
36 node_queue.pop();
37
38 // Erase r from the hashmap to "forget" about it
39 hashmap.erase(r);
40 return r;
41 }
42
44 return hashmap.count(p) > 0;
45 }
46
48 // Forcibly set this to 1. Will have no effect
49 // if we've already seen this node.
50 hashmap[p] = 1;
51
52 node_queue.push(p);
53 return true;
54 }
55 bool UniqueQueue::empty() const {return size() == 0;}
56
58 {
59 // Queue has no clear function. We can however potentially use a swap idiom
60 // to speed this up.
61 while (!node_queue.empty()) node_queue.pop();
62 }
63
64 std::vector<SpatialStructures::Node> UniqueQueue::popMany(int max)
65 {
66 // Create a new vector of nodes
67 std::vector<SpatialStructures::Node> out_nodes;
68 out_nodes.reserve(size());
69
70 // Pop a node until the queue is empty, we hit max,
71 // or max is never set.
72 int num_popped = 0;
73 while (!node_queue.empty() && num_popped < max)
74 {
75 out_nodes.push_back(pop());
76 ++num_popped;
77 }
78 return out_nodes;
79 }
80}
Contains definitions for the UniqueQueue class.
Generate a graph of accessible space from a given start point.
std::unordered_map< int, HIT_FLAG > hashmap
Hashmap used for assigning and storing hitflags for IDs.
std::queue< HF::SpatialStructures::Node > node_queue
The underlying queue.
Definition: unique_queue.h:31
HF::SpatialStructures::Node pop()
Remove the topmost node from the queue and return it.
bool forcePush(const HF::SpatialStructures::Node &p)
Forcibly push a node onto the queue without checking it. Saves a hash function, but risks breaking th...
int size() const
Determine how many nodes are currently in the queue.
void clearQueue()
Clear every node from the queue without forgetting them.
bool empty() const
Tell if the queue is empty.
HF::SpatialStructures::Node popFromDict()
Retrieve the topmost node from the queue, but "forget" about it to allow it to enter again in the fut...
bool push(const HF::SpatialStructures::Node &p)
Add a node to the queue if it has never previously been in the queue.
std::vector< SpatialStructures::Node > popMany(int max=-1)
Pop a set amount of nodes from the queue, and return them as a vector.
bool hasNode(const HF::SpatialStructures::Node &p) const
Check to see if the node has ever been in the queue.
A point in space with an ID.
Definition: node.h:38