57 template <
typename Val,
typename Priority,
typename Cmp >
67 template <
typename Val,
typename Priority,
typename Cmp >
69 std::initializer_list< std::pair< Val, Priority > > list) :
72 _heap_.reserve(list.size());
73 for (
const auto& elt: list) {
74 insert(elt.first, elt.second);
82 template <
typename Val,
typename Priority,
typename Cmp >
91 for (
const auto& val_and_index:
_indices_) {
92 const Val* val = &(val_and_index.first);
93 const std::vector< Size >& vect = val_and_index.second;
94 for (
auto index: vect) {
95 _heap_[index].second = val;
101 template <
typename Val,
typename Priority,
typename Cmp >
111 template <
typename Val,
typename Priority,
typename Cmp >
118 template <
typename Val,
typename Priority,
typename Cmp >
134 for (
const auto& val_and_index:
_indices_) {
135 const Val* val = &(val_and_index.first);
136 const std::vector< Size >& vect = val_and_index.second;
137 for (
auto index: vect) {
138 _heap_[index].second = val;
153 template <
typename Val,
typename Priority,
typename Cmp >
161 _cmp_ = std::move(from._cmp_);
163 _heap_ = std::move(from._heap_);
171 template <
typename Val,
typename Priority,
typename Cmp >
175 return *(
_heap_[0].second);
179 template <
typename Val,
typename Priority,
typename Cmp >
187 template <
typename Val,
typename Priority,
typename Cmp >
193 template <
typename Val,
typename Priority,
typename Cmp >
199 template <
typename Val,
typename Priority,
typename Cmp >
208 template <
typename Val,
typename Priority,
typename Cmp >
216 template <
typename Val,
typename Priority,
typename Cmp >
221 const Val& del_val = *(
_heap_[index].second);
222 std::vector< Size >& vect_index =
_indices_[del_val];
223 if (vect_index.size() == 1)
_indices_.erase(del_val);
225 for (
auto& v_index: vect_index) {
226 if (v_index == index) {
227 v_index = vect_index.back();
228 vect_index.pop_back();
235 std::pair< Priority, const Val* > last = std::move(
_heap_.back());
254 for (
auto& v_index: vect_index) {
263 _heap_[i] = std::move(last);
264 std::vector< Size >& last_indices = _indices_[*(_heap_[i].second)];
265 for (
auto& v_index: last_indices) {
274 template <
typename Val,
typename Priority,
typename Cmp >
280 template <
typename Val,
typename Priority,
typename Cmp >
286 template <
typename Val,
typename Priority,
typename Cmp >
290 Val v = *(
_heap_[0].second);
297 template <
typename Val,
typename Priority,
typename Cmp >
304 template <
typename Val,
typename Priority,
typename Cmp >
308 std::vector< Size >* new_vect;
309 if (
auto existing =
_indices_.tryGet(val); !existing) {
310 auto& new_elt =
_indices_.insert(val, std::vector< Size >());
311 new_val = &(new_elt.first);
312 new_vect = &(new_elt.second);
315 new_vect = &(*existing);
319 new_vect->push_back(0);
321 if (new_vect->empty()) { _indices_.erase(val); }
326 _heap_.push_back(std::pair< Priority, const Val* >(
priority, new_val));
328 if (new_vect->size() == 1) { _indices_.erase(val); }
332 std::pair< Priority, const Val* > new_heap_val = std::move(_heap_[_nb_elements_]);
336 Size i = _nb_elements_ - 1;
338 for (
Size j = (i - 1) >> 1; i && _cmp_(new_heap_val.first, _heap_[j].first);
339 i = j, j = (j - 1) >> 1) {
340 _heap_[i] = std::move(_heap_[j]);
341 std::vector< Size >& vect_index = _indices_[*(_heap_[i].second)];
342 for (
auto& index: vect_index) {
351 _heap_[i].first = std::move(new_heap_val.first);
352 _heap_[i].second = new_val;
353 new_vect->back() = i;
359 template <
typename Val,
typename Priority,
typename Cmp >
363 std::vector< Size >* new_vect;
364 if (
auto existing =
_indices_.tryGet(val); !existing) {
365 auto& new_elt =
_indices_.insert(std::move(val), std::vector< Size >());
366 new_val = &(new_elt.first);
367 new_vect = &(new_elt.second);
370 new_vect = &(*existing);
374 new_vect->push_back(0);
376 if (new_vect->empty()) { _indices_.erase(*new_val); }
381 _heap_.push_back(std::pair< Priority, const Val* >(std::move(priority), new_val));
383 if (new_vect->size() == 1) { _indices_.erase(*new_val); }
387 std::pair< Priority, const Val* > new_heap_val = std::move(_heap_[_nb_elements_]);
393 for (
Size j = (i - 1) >> 1; i &&
_cmp_(new_heap_val.first,
_heap_[j].first);
394 i = j, j = (j - 1) >> 1) {
397 for (
auto& index: vect_index) {
406 _heap_[i].first = std::move(new_heap_val.first);
407 _heap_[i].second = new_val;
408 new_vect->back() = i;
414 template <
typename Val,
typename Priority,
typename Cmp >
415 template <
typename... Args >
417 std::pair< Val, Priority > new_elt
418 = std::make_pair< Val, Priority >(std::forward< Args >(args)...);
419 return insert(std::move(new_elt.first), std::move(new_elt.second));
423 template <
typename Val,
typename Priority,
typename Cmp >
429 template <
typename Val,
typename Priority,
typename Cmp >
435 template <
typename Val,
typename Priority,
typename Cmp >
445 template <
typename Val,
typename Priority,
typename Cmp >
448 std::stringstream stream;
452 if (deja) stream <<
" , ";
454 stream <<
"(" <<
_heap_[i].first <<
" , " << *(
_heap_[i].second) <<
")";
463 template <
typename Val,
typename Priority,
typename Cmp >
465 const Priority& new_priority) {
472 const Val* val =
_heap_[index].second;
479 i = j, j = (j - 1) >> 1) {
482 for (
auto& idx: vect_index) {
501 for (
auto& idx: vect_index) {
510 _heap_[i].first = new_priority;
513 for (
auto& idx: vect_index) {
524 template <
typename Val,
typename Priority,
typename Cmp >
526 Priority&& new_priority) {
533 const Val* val =
_heap_[index].second;
540 i = j, j = (j - 1) >> 1) {
543 for (
auto& idx: vect_index) {
562 for (
auto& idx: vect_index) {
571 _heap_[i].first = std::move(new_priority);
574 for (
auto& idx: vect_index) {
585 template <
typename Val,
typename Priority,
typename Cmp >
587 const Priority& new_priority) {
588 std::vector< Size >& vect_index =
_indices_[elt];
590 for (
auto index: vect_index) {
596 template <
typename Val,
typename Priority,
typename Cmp >
602 template <
typename Val,
typename Priority,
typename Cmp >
The class for generic Hash Tables.
A MultiPriorityQueue is a heap in which each element has a mutable priority and duplicates are allowe...
Cmp _cmp_
Comparison function.
Size insert(const Val &val, const Priority &priority)
Inserts a new (a copy) element in the priority queue.
Size _nb_elements_
The number of elements in the heap.
const Val & top() const
Returns the element at the top of the priority queue.
void eraseByPos(Size index)
Removes the element at position "index" from the priority queue.
std::vector< std::pair< Priority, const Val * > > _heap_
An array storing all the elements of the heap as well as their score.
Val pop()
Removes the top element from the priority queue and return it.
void setPriority(const Val &elt, const Priority &new_priority)
Modifies the priority of each instance of a given element.
void erase(const Val &val)
Removes a given element from the priority queue (but does not return it).
bool empty() const noexcept
Indicates whether the priority queue is empty.
void eraseTop()
Removes the top of the priority queue (but does not return it).
MultiPriorityQueue< Val, Priority, Cmp > & operator=(const MultiPriorityQueue< Val, Priority, Cmp > &from)
Copy operator.
bool contains(const Val &val) const
Indicates whether the priority queue contains a given value.
const Val & operator[](Size index_elt) const
Returns the element at index "index_elt" from the priority queue.
Size setPriorityByPos(Size index, const Priority &new_priority)
Modifies the priority of the element at position "index" of the queue.
HashTable< Val, std::vector< Size > > _indices_
A hashtable for quickly finding the elements by their value.
const HashTable< Val, std::vector< Size > > & allValues() const
Returns a gum::HashTable the keys of which are the values stored in the queue.
Size capacity() const noexcept
Return the size of the internal structure storing the priority queue.
~MultiPriorityQueue()
Class destructor.
std::string toString() const
Displays the content of the queue.
void clear()
Removes all the elements from the queue.
const Priority & topPriority() const
Returns the priority of the top element.
const Priority & priority(const Val &elt) const
Returns the priority of an instance of the value passed in argument.
void resize(Size new_size)
Changes the size of the internal structure storing the priority queue.
Size emplace(Args &&... args)
Emplace a new element into the priority queue.
Size size() const noexcept
Returns the number of elements in the priority queue.
MultiPriorityQueue(Cmp compare=Cmp(), Size capacity=GUM_MULTIPLE_PRIORITY_QUEUE_DEFAULT_CAPACITY)
Basic constructor.
Exception : the element we looked for cannot be found.
#define GUM_ERROR(type, msg)
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Priority queues in which the same element can appear several times.
gum is the global namespace for all aGrUM entities
std::ostream & operator<<(std::ostream &stream, const AVLTree< Val, Cmp > &tree)
display the content of a tree