14#ifndef OR_TOOLS_BASE_ADJUSTABLE_PRIORITY_QUEUE_H_
15#define OR_TOOLS_BASE_ADJUSTABLE_PRIORITY_QUEUE_H_
23template <
typename T,
typename Comparator>
27 bool operator()(T* a, T* b)
const {
return (*compare_)(*a, *b); }
33template <
typename T,
typename Comp = std::less<T> >
48 elems_.push_back(val);
49 AdjustUpwards(elems_.size() - 1);
53 int end = elems_.size() - 1;
54 int i = val->GetHeapIndex();
59 elems_[i] = elems_[end];
60 elems_[i]->SetHeapIndex(i);
66 int i = val->GetHeapIndex();
67 return (i >= 0 && i < elems_.size() && elems_[i] == val);
72 int i = val->GetHeapIndex();
73 int parent = (i - 1) / 2;
74 if (lower_priority(elems_[parent], val)) {
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() &&
106 need_to_check_children.push_back(rightchild);
115 int Size()
const {
return elems_.size(); }
123 bool IsEmpty()
const {
return elems_.empty(); }
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;
137 if (right_child < elems_.size()) {
147 const std::vector<T*>*
Raw()
const {
return &elems_; }
150 void AdjustUpwards(
int i) {
151 T*
const t = elems_[i];
153 const int parent = (i - 1) / 2;
154 if (!c_(*elems_[parent], *t)) {
157 elems_[
i] = elems_[parent];
158 elems_[
i]->SetHeapIndex(i);
165 void AdjustDownwards(
int i) {
166 T*
const t = elems_[
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);
189 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