Skip to content

Commit ba78760

Browse files
author
Rongbo Zhang
committed
[LazyPopUniquePriorityQueue] Moved the class definiation from vpr/src/pack/greedy_candidate_selector.h to vpr/src/util/lazy_pop_unique_priority_queue.h
1 parent 01b84d3 commit ba78760

File tree

3 files changed

+175
-186
lines changed

3 files changed

+175
-186
lines changed

vpr/src/pack/greedy_candidate_selector.cpp

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,67 +1045,3 @@ void GreedyCandidateSelector::update_candidate_selector_finalize_cluster(
10451045
}
10461046
}
10471047

1048-
template <typename T_key, typename T_sort>
1049-
void LazyPopUniquePriorityQueue<T_key, T_sort>::push(T_key key, T_sort value){
1050-
// Insert the key and sort value pair into the queue if it is not already present
1051-
if (content_set.find(key) != content_set.end()) {
1052-
// If the key is already in the queue, do nothing
1053-
return;
1054-
}
1055-
heap.emplace_back(key, value);
1056-
std::push_heap(heap.begin(),
1057-
heap.end(),
1058-
LazyPopUniquePriorityQueueCompare());
1059-
content_set.insert(key);
1060-
}
1061-
1062-
template <typename T_key, typename T_sort>
1063-
std::pair<T_key,T_sort> LazyPopUniquePriorityQueue<T_key, T_sort>::pop(){
1064-
std::pair<T_key, T_sort> top_pair;
1065-
while (heap.size() > 0) {
1066-
top_pair = heap.front();
1067-
// remove the key from the heap and the tracking set
1068-
std::pop_heap(heap.begin(),
1069-
heap.end(),
1070-
LazyPopUniquePriorityQueueCompare());
1071-
heap.pop_back();
1072-
content_set.erase(top_pair.first);
1073-
1074-
// checking if the highest value's key is in the delete pending set
1075-
// if it is, remove it from the delete pending set and find the next best gain's key
1076-
if (delete_pending_set.find(top_pair.first) != delete_pending_set.end()) {
1077-
delete_pending_set.erase(top_pair.first);
1078-
top_pair = std::pair<T_key, T_sort>();
1079-
} else {
1080-
break;
1081-
}
1082-
}
1083-
return top_pair;
1084-
}
1085-
1086-
template <typename T_key, typename T_sort>
1087-
void LazyPopUniquePriorityQueue<T_key, T_sort>::remove(T_key key) {
1088-
// If the key is in the priority queue, remove it from the heap and reheapify
1089-
// Otherwise, do nothing.
1090-
if (content_set.find(key) != content_set.end()) {
1091-
content_set.erase(key);
1092-
delete_pending_set.erase(key);
1093-
for (int i = 0; i < heap.size(); i++) {
1094-
if (heap[i].first == key) {
1095-
heap.erase(heap.begin() + i);
1096-
break;
1097-
}
1098-
}
1099-
std::make_heap(heap.begin(), heap.end(), LazyPopUniquePriorityQueueCompare());
1100-
}
1101-
}
1102-
1103-
template <typename T_key, typename T_sort>
1104-
void LazyPopUniquePriorityQueue<T_key, T_sort>::remove_at_pop_time(T_key key) {
1105-
//if the key is in the list, start tracking it in the delete pending list.
1106-
// Otherwise, do nothing.
1107-
if (content_set.find(key) != content_set.end()) {
1108-
delete_pending_set.insert(key);
1109-
}
1110-
}
1111-

vpr/src/pack/greedy_candidate_selector.h

Lines changed: 1 addition & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "cluster_legalizer.h"
1717
#include "physical_types.h"
1818
#include "vtr_vector.h"
19+
#include "lazy_pop_unique_priority_queue.h"
1920

2021
// Forward declarations
2122
class AtomNetlist;
@@ -29,128 +30,6 @@ struct t_molecule_stats;
2930
struct t_packer_opts;
3031

3132

32-
/**
33-
* @brief Lazy Pop Unique Priority Queue
34-
*
35-
* This is a priority queue that is used to sort items which are identified by the key
36-
* and sorted by the sort value.
37-
*
38-
* It uses a vector to store the key and sort value pair.
39-
* It uses a set to store the keys that are in the vector for uniqueness checking
40-
* and a set to store the delete pending keys which will be removed at pop time.
41-
*/
42-
template <typename T_key, typename T_sort>
43-
class LazyPopUniquePriorityQueue {
44-
public:
45-
/** @brief The custom comparsion struct for sorting the items in the priority queue.
46-
* A less than comparison will put the item with the highest sort value to the front of the queue.
47-
* A greater than comparison will put the item with the lowest sort value to the front of the queue.
48-
*/
49-
struct LazyPopUniquePriorityQueueCompare {
50-
bool operator()(const std::pair<T_key, T_sort>& a,
51-
const std::pair<T_key, T_sort>& b) const {
52-
return a.second < b.second;
53-
}
54-
};
55-
56-
/// @brief The vector maintained as heap to store the key and sort value pair.
57-
std::vector<std::pair<T_key, T_sort>> heap;
58-
59-
/// @brief The set to store the keys that are in the queue. This is used to ensure uniqueness
60-
std::unordered_set<T_key> content_set;
61-
62-
/// @brief The set to store the delete pending item from the queue refered by the key.
63-
std::unordered_set<T_key> delete_pending_set;
64-
65-
/**
66-
* @brief Constructor of the Lazy Pop Unique Priority Queue class.
67-
*/
68-
LazyPopUniquePriorityQueue();
69-
70-
/**
71-
* @brief Destructor of the Lazy Pop Unique Priority Queue class.
72-
*/
73-
~LazyPopUniquePriorityQueue();
74-
75-
/**
76-
* @brief Push the key and the sort value as a pair into the priority queue.
77-
*
78-
* @param key
79-
* The unique key for the item that will be pushed onto the queue.
80-
* @param value
81-
* The sort value used for sorting the item.
82-
*/
83-
void push(T_key key, T_sort value);
84-
85-
/**
86-
* @brief Pop the top item from the priority queue.
87-
*
88-
* @return The key and sort value pair.
89-
*/
90-
std::pair<T_key,T_sort> pop();
91-
92-
/**
93-
* @brief Remove the item with matching key value from the priority queue
94-
* This fill immediately remove the item and re-heapify the queue.
95-
*
96-
* @param key
97-
* The key of the item to be delected from the queue
98-
*/
99-
void remove(T_key key);
100-
101-
/**
102-
* @brief Remove the item with matching key value from the priority queue at pop time.
103-
* Add the key to the delete pending set for tracking,
104-
* and it will be deleted when it is popped.
105-
*
106-
* This function will not immediately delete the key from the
107-
* priority queue. It will be deleted when it is popped. Thus do not
108-
* expect a size reduction in the priority queue immediately.
109-
* @param key
110-
* The key of the item to be delected from the queue at pop time.
111-
*/
112-
void remove_at_pop_time(T_key key);
113-
114-
/**
115-
* @brief Check if the priority queue is empty.
116-
*
117-
* @return True if the priority queue is empty, false otherwise.
118-
*/
119-
bool empty(){
120-
return heap.empty();
121-
}
122-
123-
/**
124-
* @brief Clears the priority queue and the tracking sets.
125-
*
126-
* @return None
127-
*/
128-
void clear(){
129-
heap.clear();
130-
content_set.clear();
131-
delete_pending_set.clear();
132-
}
133-
134-
/**
135-
* @brief Get the size of the priority queue.
136-
*
137-
* @return The size of the priority queue.
138-
*/
139-
size_t size(){
140-
return heap.size();
141-
}
142-
143-
/**
144-
* @brief Check if the item refered by the key is in the priority queue.
145-
*
146-
* @param key
147-
* The key of the item.
148-
* @return True if the key is in the priority queue, false otherwise.
149-
*/
150-
bool contains(T_key key){
151-
return content_set.find(key) != content_set.end();
152-
}
153-
};
15433

15534
/**
15635
* @brief Stats on the gain of a cluster.
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
#ifndef VPR_LAZY_POP_UNIQUE_PRIORITY_QUEUE_H
2+
#define VPR_LAZY_POP_UNIQUE_PRIORITY_QUEUE_H
3+
4+
#include <unordered_set>
5+
#include <vector>
6+
#include <algorithm>
7+
8+
/**
9+
* @brief Lazy Pop Unique Priority Queue
10+
*
11+
* This is a priority queue that is used to sort items which are identified by the key
12+
* and sorted by the sort value.
13+
*
14+
* It uses a vector to store the key and sort value pair.
15+
* It uses a set to store the keys that are in the vector for uniqueness checking
16+
* and a set to store the delete pending keys which will be removed at pop time.
17+
*/
18+
template <typename T_key, typename T_sort>
19+
class LazyPopUniquePriorityQueue {
20+
public:
21+
/** @brief The custom comparsion struct for sorting the items in the priority queue.
22+
* A less than comparison will put the item with the highest sort value to the front of the queue.
23+
* A greater than comparison will put the item with the lowest sort value to the front of the queue.
24+
*/
25+
struct LazyPopUniquePriorityQueueCompare {
26+
bool operator()(const std::pair<T_key, T_sort>& a,
27+
const std::pair<T_key, T_sort>& b) const {
28+
return a.second < b.second;
29+
}
30+
};
31+
32+
/// @brief The vector maintained as heap to store the key and sort value pair.
33+
std::vector<std::pair<T_key, T_sort>> heap;
34+
35+
/// @brief The set to store the keys that are in the queue. This is used to ensure uniqueness
36+
std::unordered_set<T_key> content_set;
37+
38+
/// @brief The set to store the delete pending item from the queue refered by the key.
39+
std::unordered_set<T_key> delete_pending_set;
40+
41+
/**
42+
* @brief Push the key and the sort value as a pair into the priority queue.
43+
*
44+
* @param key
45+
* The unique key for the item that will be pushed onto the queue.
46+
* @param value
47+
* The sort value used for sorting the item.
48+
*/
49+
void push(T_key key, T_sort value){
50+
// Insert the key and sort value pair into the queue if it is not already present
51+
if (content_set.find(key) != content_set.end()) {
52+
// If the key is already in the queue, do nothing
53+
return;
54+
}
55+
heap.emplace_back(key, value);
56+
std::push_heap(heap.begin(),
57+
heap.end(),
58+
LazyPopUniquePriorityQueueCompare());
59+
content_set.insert(key);
60+
}
61+
62+
/**
63+
* @brief Pop the top item from the priority queue.
64+
*
65+
* @return The key and sort value pair.
66+
*/
67+
std::pair<T_key,T_sort> pop(){
68+
std::pair<T_key, T_sort> top_pair;
69+
while (heap.size() > 0) {
70+
top_pair = heap.front();
71+
// remove the key from the heap and the tracking set
72+
std::pop_heap(heap.begin(),
73+
heap.end(),
74+
LazyPopUniquePriorityQueueCompare());
75+
heap.pop_back();
76+
content_set.erase(top_pair.first);
77+
78+
// checking if the highest value's key is in the delete pending set
79+
// if it is, remove it from the delete pending set and find the next best gain's key
80+
if (delete_pending_set.find(top_pair.first) != delete_pending_set.end()) {
81+
delete_pending_set.erase(top_pair.first);
82+
top_pair = std::pair<T_key, T_sort>();
83+
} else {
84+
break;
85+
}
86+
}
87+
return top_pair;
88+
}
89+
90+
/**
91+
* @brief Remove the item with matching key value from the priority queue
92+
* This fill immediately remove the item and re-heapify the queue.
93+
*
94+
* @param key
95+
* The key of the item to be delected from the queue
96+
*/
97+
void remove(T_key key){
98+
// If the key is in the priority queue, remove it from the heap and reheapify
99+
// Otherwise, do nothing.
100+
if (content_set.find(key) != content_set.end()) {
101+
content_set.erase(key);
102+
delete_pending_set.erase(key);
103+
for (int i = 0; i < heap.size(); i++) {
104+
if (heap[i].first == key) {
105+
heap.erase(heap.begin() + i);
106+
break;
107+
}
108+
}
109+
std::make_heap(heap.begin(), heap.end(), LazyPopUniquePriorityQueueCompare());
110+
}
111+
}
112+
113+
114+
/**
115+
* @brief Remove the item with matching key value from the priority queue at pop time.
116+
* Add the key to the delete pending set for tracking,
117+
* and it will be deleted when it is popped.
118+
*
119+
* This function will not immediately delete the key from the
120+
* priority queue. It will be deleted when it is popped. Thus do not
121+
* expect a size reduction in the priority queue immediately.
122+
* @param key
123+
* The key of the item to be delected from the queue at pop time.
124+
*/
125+
void remove_at_pop_time(T_key key){
126+
//if the key is in the list, start tracking it in the delete pending list.
127+
// Otherwise, do nothing.
128+
if (content_set.find(key) != content_set.end()) {
129+
delete_pending_set.insert(key);
130+
}
131+
}
132+
133+
/**
134+
* @brief Check if the priority queue is empty.
135+
*
136+
* @return True if the priority queue is empty, false otherwise.
137+
*/
138+
bool empty(){
139+
return heap.empty();
140+
}
141+
142+
/**
143+
* @brief Clears the priority queue and the tracking sets.
144+
*
145+
* @return None
146+
*/
147+
void clear(){
148+
heap.clear();
149+
content_set.clear();
150+
delete_pending_set.clear();
151+
}
152+
153+
/**
154+
* @brief Get the size of the priority queue.
155+
*
156+
* @return The size of the priority queue.
157+
*/
158+
size_t size(){
159+
return heap.size();
160+
}
161+
162+
/**
163+
* @brief Check if the item refered by the key is in the priority queue.
164+
*
165+
* @param key
166+
* The key of the item.
167+
* @return True if the key is in the priority queue, false otherwise.
168+
*/
169+
bool contains(T_key key){
170+
return content_set.find(key) != content_set.end();
171+
}
172+
};
173+
174+
#endif

0 commit comments

Comments
 (0)