56 template <
typename Val,
typename Priority,
typename Cmp >
66 template <
typename Val,
typename Priority,
typename Cmp >
68 std::initializer_list< std::pair< Val, Priority > > list) :
71 _heap_.reserve(list.size());
72 for (
const auto& elt: list) {
73 insert(elt.first, elt.second);
81 template <
typename Val,
typename Priority,
typename Cmp >
90 for (
const auto& val_and_index:
_indices_) {
91 const Val* val = &(val_and_index.first);
92 const std::vector< Size >& vect = val_and_index.second;
93 for (
auto index: vect) {
94 _heap_[index].second = val;
100 template <
typename Val,
typename Priority,
typename Cmp >
110 template <
typename Val,
typename Priority,
typename Cmp >
117 template <
typename Val,
typename Priority,
typename Cmp >
133 for (
const auto& val_and_index:
_indices_) {
134 const Val* val = &(val_and_index.first);
135 const std::vector< Size >& vect = val_and_index.second;
136 for (
auto index: vect) {
137 _heap_[index].second = val;
152 template <
typename Val,
typename Priority,
typename Cmp >
160 _cmp_ = std::move(from._cmp_);
162 _heap_ = std::move(from._heap_);
170 template <
typename Val,
typename Priority,
typename Cmp >
174 return *(
_heap_[0].second);
178 template <
typename Val,
typename Priority,
typename Cmp >
186 template <
typename Val,
typename Priority,
typename Cmp >
192 template <
typename Val,
typename Priority,
typename Cmp >
198 template <
typename Val,
typename Priority,
typename Cmp >
207 template <
typename Val,
typename Priority,
typename Cmp >
215 template <
typename Val,
typename Priority,
typename Cmp >
220 const Val& del_val = *(
_heap_[index].second);
221 std::vector< Size >& vect_index =
_indices_[del_val];
222 if (vect_index.size() == 1)
_indices_.erase(del_val);
224 for (
auto& v_index: vect_index) {
225 if (v_index == index) {
226 v_index = vect_index.back();
227 vect_index.pop_back();
234 std::pair< Priority, const Val* > last = std::move(
_heap_.back());
253 for (
auto& v_index: vect_index) {
262 _heap_[i] = std::move(last);
263 std::vector< Size >& last_indices = _indices_[*(_heap_[i].second)];
264 for (
auto& v_index: last_indices) {
273 template <
typename Val,
typename Priority,
typename Cmp >
281 template <
typename Val,
typename Priority,
typename Cmp >
287 template <
typename Val,
typename Priority,
typename Cmp >
298 template <
typename Val,
typename Priority,
typename Cmp >
305 template <
typename Val,
typename Priority,
typename Cmp >
309 std::vector< Size >* new_vect;
311 auto& new_elt =
_indices_.insert(val, std::vector< Size >());
312 new_val = &(new_elt.first);
313 new_vect = &(new_elt.second);
320 new_vect->push_back(0);
322 if (new_vect->empty()) {
_indices_.erase(val); }
327 _heap_.push_back(std::pair< Priority, const Val* >(
priority, new_val));
329 if (new_vect->size() == 1) { _indices_.erase(val); }
333 std::pair< Priority, const Val* > new_heap_val = std::move(_heap_[_nb_elements_]);
337 Size i = _nb_elements_ - 1;
339 for (
Size j = (i - 1) >> 1; i && _cmp_(new_heap_val.first, _heap_[j].first);
340 i = j, j = (j - 1) >> 1) {
341 _heap_[i] = std::move(_heap_[j]);
342 std::vector< Size >& vect_index = _indices_[*(_heap_[i].second)];
343 for (
auto& index: vect_index) {
352 _heap_[i].first = std::move(new_heap_val.first);
353 _heap_[i].second = new_val;
354 new_vect->back() = i;
360 template <
typename Val,
typename Priority,
typename Cmp >
364 std::vector< Size >* new_vect;
366 auto& new_elt =
_indices_.insert(std::move(val), std::vector< Size >());
367 new_val = &(new_elt.first);
368 new_vect = &(new_elt.second);
375 new_vect->push_back(0);
377 if (new_vect->empty()) { _indices_.erase(*new_val); }
382 _heap_.push_back(std::pair< Priority, const Val* >(std::move(priority), new_val));
384 if (new_vect->size() == 1) { _indices_.erase(*new_val); }
388 std::pair< Priority, const Val* > new_heap_val = std::move(_heap_[_nb_elements_]);
394 for (
Size j = (i - 1) >> 1; i &&
_cmp_(new_heap_val.first,
_heap_[j].first);
395 i = j, j = (j - 1) >> 1) {
398 for (
auto& index: vect_index) {
407 _heap_[i].first = std::move(new_heap_val.first);
409 new_vect->back() = i;
415 template <
typename Val,
typename Priority,
typename Cmp >
416 template <
typename... Args >
418 std::pair< Val, Priority > new_elt
419 = std::make_pair< Val, Priority >(std::forward< Args >(args)...);
420 return insert(std::move(new_elt.first), std::move(new_elt.second));
424 template <
typename Val,
typename Priority,
typename Cmp >
430 template <
typename Val,
typename Priority,
typename Cmp >
436 template <
typename Val,
typename Priority,
typename Cmp >
442 return *(
_heap_[index].second);
446 template <
typename Val,
typename Priority,
typename Cmp >
449 std::stringstream stream;
453 if (deja) stream <<
" , ";
455 stream <<
"(" <<
_heap_[i].first <<
" , " << *(
_heap_[i].second) <<
")";
464 template <
typename Val,
typename Priority,
typename Cmp >
466 const Priority& new_priority) {
473 const Val* val =
_heap_[index].second;
480 i = j, j = (j - 1) >> 1) {
483 for (
auto& idx: vect_index) {
502 for (
auto& idx: vect_index) {
511 _heap_[i].first = new_priority;
514 for (
auto& idx: vect_index) {
525 template <
typename Val,
typename Priority,
typename Cmp >
527 Priority&& new_priority) {
534 const Val* val =
_heap_[index].second;
541 i = j, j = (j - 1) >> 1) {
544 for (
auto& idx: vect_index) {
563 for (
auto& idx: vect_index) {
572 _heap_[i].first = std::move(new_priority);
575 for (
auto& idx: vect_index) {
586 template <
typename Val,
typename Priority,
typename Cmp >
588 const Priority& new_priority) {
589 std::vector< Size >& vect_index =
_indices_[elt];
591 for (
auto index: vect_index) {
597 template <
typename Val,
typename Priority,
typename Cmp >
603 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.
void resize(Size new_size)
Changes the size of the internal structure storing the priority queue.
const HashTable< Val, std::vector< Size > > & allValues() const
Returns a gum::HashTable the keys of which are the values stored in the queue.
const Priority & topPriority() const
Returns the priority of the top element.
Size size() const noexcept
Returns the number of elements in the priority queue.
Size capacity() const noexcept
Return the size of the internal structure storing the priority queue.
bool empty() const noexcept
Indicates whether the priority queue is empty.
void eraseByPos(Size index)
Removes the element at position "index" from the priority queue.
const Val & operator[](Size index_elt) const
Returns the element at index "index_elt" 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.
void setPriority(const Val &elt, const Priority &new_priority)
Modifies the priority of each instance of a given element.
const Val & top() const
Returns the element at the top of the priority queue.
Val pop()
Removes the top element from the priority queue and return it.
MultiPriorityQueue< Val, Priority, Cmp > & operator=(const MultiPriorityQueue< Val, Priority, Cmp > &from)
Copy operator.
const Priority & priority(const Val &elt) const
Returns the priority of an instance of the value passed in argument.
Size setPriorityByPos(Size index, const Priority &new_priority)
Modifies the priority of the element at position "index" of the queue.
bool contains(const Val &val) const
Indicates whether the priority queue contains a given value.
HashTable< Val, std::vector< Size > > _indices_
A hashtable for quickly finding the elements by their value.
~MultiPriorityQueue()
Class destructor.
void eraseTop()
Removes the top of the priority queue (but does not return it).
void erase(const Val &val)
Removes a given element from the priority queue (but does not return it).
std::string toString() const
Displays the content of the queue.
void clear()
Removes all the elements from the queue.
Size emplace(Args &&... args)
Emplace a new element into 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