Google OR-Tools: ortools/base/adjustable_priority_queue.h Source File
27 bool operator()(T* a, T* b) const { return (*compare_)(*a, *b); }
83 T* Top() { return elems_[0]; }
85 const T* Top() const { return elems_[0]; }
87 void AllTop(std::vector<T*>* topvec) {
89 if (Size() == 0) return;
90 std::list<int> need_to_check_children;
91 need_to_check_children.push_back(0);
94 while (!need_to_check_children.empty()) {
95 int ind = need_to_check_children.front();
96 need_to_check_children.pop_front();
97 topvec->push_back(elems_[ind]);
98 int leftchild = 1 + 2 * ind;
99 if (leftchild < Size()) {
101 need_to_check_children.push_back(leftchild);
103 int rightchild = leftchild + 1;
104 if (rightchild < Size() &&
115 int Size() const { return elems_.size(); }
118 int Capacity() const { return elems_.capacity(); }
123 bool IsEmpty() const { return elems_.empty(); }
125 void Clear() { elems_.clear(); }
130 for (int i = 0; i < elems_.size(); ++i) {
131 int left_child = 1 + 2 * i;
132 if (left_child < elems_.size()) {
136 int right_child = left_child + 1;
147 const std::vector<T*>* Raw() const { return &elems_; }
150 void AdjustUpwards(int i) {
153 const int parent = (i - 1) / 2;
154 if (!c_(*elems_[parent], *t)) {
157 elems_[i] = elems_[parent];
158 elems_[i]->SetHeapIndex(i);
159 i = parent;
161 elems_[i] = t;
165 void AdjustDownwards(int i) {
168 const int left_child = 1 + 2 * i;
169 if (left_child >= elems_.size()) {
172 const int right_child = left_child + 1;
173 const int next_i = (right_child < elems_.size() &&
174 c_(*elems_[left_child], *elems_[right_child]))
177 if (!c_(*t, *elems_[next_i])) {
180 elems_[i] = elems_[next_i];
181 elems_[i]->SetHeapIndex(i);
182 i = next_i;
184 elems_[i] = t;