14#ifndef OR_TOOLS_BASE_ADJUSTABLE_PRIORITY_QUEUE_H_
15#define OR_TOOLS_BASE_ADJUSTABLE_PRIORITY_QUEUE_H_
25template <
typename T,
typename Comparator>
29 bool operator()(T* a, T* b)
const {
return (*compare_)(*a, *b); }
35template <
typename T,
typename Comp = std::less<T> >
50 elems_.push_back(val);
51 AdjustUpwards(elems_.size() - 1);
55 int end = elems_.size() - 1;
56 int i = val->GetHeapIndex();
61 elems_[i] = elems_[end];
62 elems_[i]->SetHeapIndex(i);
68 int i = val->GetHeapIndex();
69 return (i >= 0 && i < elems_.size() && elems_[i] == val);
74 int i = val->GetHeapIndex();
75 int parent = (i - 1) / 2;
76 if (lower_priority(elems_[parent], val)) {
85 T*
Top() {
return elems_[0]; }
87 const T*
Top()
const {
return elems_[0]; }
89 void AllTop(std::vector<T*>* topvec) {
91 if (
Size() == 0)
return;
92 std::list<int> need_to_check_children;
93 need_to_check_children.push_back(0);
96 while (!need_to_check_children.empty()) {
97 int ind = need_to_check_children.front();
98 need_to_check_children.pop_front();
99 topvec->push_back(elems_[ind]);
100 int leftchild = 1 + 2 * ind;
101 if (leftchild <
Size()) {
103 need_to_check_children.push_back(leftchild);
105 int rightchild = leftchild + 1;
106 if (rightchild <
Size() &&
108 need_to_check_children.push_back(rightchild);
117 int Size()
const {
return elems_.size(); }
125 bool IsEmpty()
const {
return elems_.empty(); }
132 for (
int i = 0; i < elems_.size(); ++i) {
133 int left_child = 1 + 2 * i;
134 if (left_child < elems_.size()) {
138 int right_child = left_child + 1;
139 if (right_child < elems_.size()) {
149 const std::vector<T*>*
Raw()
const {
return &elems_; }
152 void AdjustUpwards(
int i) {
153 T*
const t = elems_[i];
155 const int parent = (i - 1) / 2;
156 if (!c_(*elems_[parent], *t)) {
159 elems_[
i] = elems_[parent];
160 elems_[
i]->SetHeapIndex(i);
167 void AdjustDownwards(
int i) {
168 T*
const t = elems_[
i];
170 const int left_child = 1 + 2 *
i;
171 if (left_child >= elems_.size()) {
174 const int right_child = left_child + 1;
175 const int next_i = (right_child < elems_.size() &&
176 c_(*elems_[left_child], *elems_[right_child]))
179 if (!c_(*t, *elems_[next_i])) {
182 elems_[
i] = elems_[next_i];
183 elems_[
i]->SetHeapIndex(i);
191 std::vector<T*> elems_;
AdjustablePriorityQueue(AdjustablePriorityQueue &&)=default
bool Contains(const T *val) const
int Capacity() const
Returns the number of elements for which storage has been allocated.
AdjustablePriorityQueue & operator=(AdjustablePriorityQueue &&)=default
AdjustablePriorityQueue & operator=(const AdjustablePriorityQueue &)=delete
void SetCapacity(size_t c)
Allocates storage for a given number of elements.
void AllTop(std::vector< T * > *topvec)
void Pop()
If there are ties for the top, this returns all of them.
AdjustablePriorityQueue(const Comp &c)
void NoteChangedPriority(T *val)
AdjustablePriorityQueue(const AdjustablePriorityQueue &)=delete
AdjustablePriorityQueue()=default
const std::vector< T * > * Raw() const
bool operator()(T *a, T *b) const
LowerPriorityThan(Comparator *compare)
dual_gradient T(y - `dual_solution`) class DiagonalTrustRegionProblemFromQp