36 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc >
38 Cmp compare,
Size capacity) :
39 __indices(capacity >> 1, true, false),
41 __heap.reserve(capacity);
48 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc >
50 std::initializer_list< std::pair< Val, Priority > > list) :
51 __indices(
Size(list.size()) / 2, true, false) {
53 __heap.reserve(list.size());
54 for (
const auto& elt : list) {
55 insert(elt.first, elt.second);
63 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc >
67 __indices(from.__indices), __nb_elements(from.__nb_elements),
73 for (
const auto& val_and_index : __indices) {
74 const Val* val = &(val_and_index.first);
75 const std::vector< Size >& vect = val_and_index.second;
76 for (
auto index : vect) {
77 __heap[index].second = val;
83 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc >
84 template <
typename OtherAlloc >
87 __indices(from.__indices),
88 __nb_elements(from.__nb_elements), __cmp(from.__cmp) {
94 __heap.reserve(from.
__heap.size());
95 for (
const auto& elt : from.
__heap) {
96 __heap.push_back(elt);
98 for (
const auto& val_and_index : __indices) {
99 const Val* val = &(val_and_index.first);
100 const std::vector< Size >& vect = val_and_index.second;
101 for (
auto index : vect) {
102 __heap[index].second = val;
109 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc >
112 __heap(
std::move(from.__heap)),
113 __indices(
std::move(from.__indices)),
114 __nb_elements(
std::move(from.__nb_elements)), __cmp(
std::move(from.__cmp)) {
120 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc >
127 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc >
144 for (
const auto& val_and_index : __indices) {
145 const Val* val = &(val_and_index.first);
146 const std::vector< Size >& vect = val_and_index.second;
147 for (
auto index : vect) {
148 __heap[index].second = val;
163 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc >
164 template <
typename OtherAlloc >
181 __heap.reserve(from.
__heap.size());
182 for (
const auto& elt : from.
__heap) {
183 __heap.push_back(elt);
185 for (
const auto& val_and_index : __indices) {
186 const Val* val = &(val_and_index.first);
187 const std::vector< Size >& vect = val_and_index.second;
188 for (
auto index : vect) {
189 __heap[index].second = val;
204 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc >
213 __cmp = std::move(from.__cmp);
214 __indices = std::move(from.__indices);
215 __heap = std::move(from.__heap);
216 __nb_elements = std::move(from.__nb_elements);
223 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc >
227 return *(__heap[0].second);
231 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc >
232 INLINE
const Priority&
236 return __heap[0].first;
240 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc >
243 return __nb_elements;
247 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc >
250 return Size(__heap.capacity());
254 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc >
257 if (new_size < __nb_elements)
return;
259 __heap.reserve(new_size);
260 __indices.
resize(new_size / 2);
264 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc >
272 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc >
274 if (index >= __nb_elements)
return;
277 const Val& del_val = *(__heap[index].second);
278 std::vector< Size >& vect_index = __indices[del_val];
279 if (vect_index.size() == 1)
280 __indices.
erase(del_val);
282 for (
auto& v_index : vect_index) {
283 if (v_index == index) {
284 v_index = vect_index.back();
285 vect_index.pop_back();
292 std::pair< Priority, const Val* > last = std::move(__heap.back());
296 if (!__nb_elements || (index == __nb_elements))
return;
301 for (
Size j = (index << 1) + 1; j < __nb_elements; i = j, j = (j << 1) + 1) {
303 if ((j + 1 < __nb_elements) && __cmp(__heap[j + 1].first, __heap[j].first))
307 if (__cmp(last.first, __heap[j].first))
break;
310 __heap[i] = std::move(__heap[j]);
311 std::vector< Size >& vect_index = __indices[*(__heap[i].second)];
312 for (
auto& v_index : vect_index) {
321 __heap[i] = std::move(last);
322 std::vector< Size >& last_indices = __indices[*(__heap[i].second)];
323 for (
auto& v_index : last_indices) {
324 if (v_index == __nb_elements) {
332 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc >
336 eraseByPos(__indices[val][0]);
341 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc >
347 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc >
351 Val v = *(__heap[0].second);
358 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc >
366 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc >
368 const Val& val,
const Priority& priority) {
371 std::vector< Size >* new_vect;
372 if (!__indices.exists(val)) {
373 auto& new_elt = __indices.
insert(val, std::vector< Size >());
374 new_val = &(new_elt.first);
375 new_vect = &(new_elt.second);
377 new_val = &(__indices.key(val));
378 new_vect = &(__indices[val]);
382 new_vect->push_back(0);
384 if (new_vect->empty()) { __indices.erase(val); }
389 __heap.push_back(std::pair< Priority, const Val* >(priority, new_val));
391 if (new_vect->size() == 1) { __indices.erase(val); }
395 std::pair< Priority, const Val* > new_heap_val =
396 std::move(__heap[__nb_elements]);
400 Size i = __nb_elements - 1;
402 for (
Size j = (i - 1) >> 1; i && __cmp(new_heap_val.first, __heap[j].first);
403 i = j, j = (j - 1) >> 1) {
404 __heap[i] = std::move(__heap[j]);
405 std::vector< Size >& vect_index = __indices[*(__heap[i].second)];
406 for (
auto& index : vect_index) {
415 __heap[i].first = std::move(new_heap_val.first);
416 __heap[i].second = new_val;
417 new_vect->back() = i;
423 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc >
426 Priority&& priority) {
429 std::vector< Size >* new_vect;
430 if (!__indices.exists(val)) {
431 auto& new_elt = __indices.
insert(std::move(val), std::vector< Size >());
432 new_val = &(new_elt.first);
433 new_vect = &(new_elt.second);
435 new_val = &(__indices.key(val));
436 new_vect = &(__indices[val]);
440 new_vect->push_back(0);
442 if (new_vect->empty()) { __indices.erase(*new_val); }
448 std::pair< Priority, const Val* >(std::move(priority), new_val));
450 if (new_vect->size() == 1) { __indices.erase(*new_val); }
454 std::pair< Priority, const Val* > new_heap_val =
455 std::move(__heap[__nb_elements]);
459 Size i = __nb_elements - 1;
461 for (
Size j = (i - 1) >> 1; i && __cmp(new_heap_val.first, __heap[j].first);
462 i = j, j = (j - 1) >> 1) {
463 __heap[i] = std::move(__heap[j]);
464 std::vector< Size >& vect_index = __indices[*(__heap[i].second)];
465 for (
auto& index : vect_index) {
474 __heap[i].first = std::move(new_heap_val.first);
475 __heap[i].second = new_val;
476 new_vect->back() = i;
482 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc >
483 template <
typename... Args >
486 std::pair< Val, Priority > new_elt =
487 std::make_pair< Val, Priority >(std::forward< Args >(args)...);
488 return insert(std::move(new_elt.first), std::move(new_elt.second));
492 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc >
495 return (__nb_elements == 0);
499 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc >
501 const Val& val)
const {
502 return __indices.exists(val);
506 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc >
509 if (index >= __nb_elements) {
513 return *(__heap[index].second);
517 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc >
520 std::stringstream stream;
523 for (
Size i = 0; i != __nb_elements; ++i, deja =
true) {
524 if (deja) stream <<
" , ";
526 stream <<
"(" << __heap[i].first <<
" , " << *(__heap[i].second) <<
")";
535 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc >
537 Size index,
const Priority& new_priority) {
539 if (index >= __nb_elements) {
544 const Val* val = __heap[index].second;
550 for (
Size j = (i - 1) >> 1; i && __cmp(new_priority, __heap[j].first);
551 i = j, j = (j - 1) >> 1) {
552 __heap[i] = std::move(__heap[j]);
553 std::vector< Size >& vect_index = __indices[*(__heap[i].second)];
554 for (
auto& idx : vect_index) {
563 for (
Size j = (i << 1) + 1; j < __nb_elements; i = j, j = (j << 1) + 1) {
565 if ((j + 1 < __nb_elements) && __cmp(__heap[j + 1].first, __heap[j].first))
569 if (__cmp(new_priority, __heap[j].first))
break;
572 __heap[i] = std::move(__heap[j]);
573 std::vector< Size >& vect_index = __indices[*(__heap[i].second)];
574 for (
auto& idx : vect_index) {
583 __heap[i].first = new_priority;
584 __heap[i].second = val;
585 std::vector< Size >& vect_index = __indices[*(__heap[i].second)];
586 for (
auto& idx : vect_index) {
597 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc >
599 Size index, Priority&& new_priority) {
601 if (index >= __nb_elements) {
606 const Val* val = __heap[index].second;
612 for (
Size j = (i - 1) >> 1; i && __cmp(new_priority, __heap[j].first);
613 i = j, j = (j - 1) >> 1) {
614 __heap[i] = std::move(__heap[j]);
615 std::vector< Size >& vect_index = __indices[*(__heap[i].second)];
616 for (
auto& idx : vect_index) {
625 for (
Size j = (i << 1) + 1; j < __nb_elements; i = j, j = (j << 1) + 1) {
627 if ((j + 1 < __nb_elements) && __cmp(__heap[j + 1].first, __heap[j].first))
631 if (__cmp(new_priority, __heap[j].first))
break;
634 __heap[i] = std::move(__heap[j]);
635 std::vector< Size >& vect_index = __indices[*(__heap[i].second)];
636 for (
auto& idx : vect_index) {
645 __heap[i].first = std::move(new_priority);
646 __heap[i].second = val;
647 std::vector< Size >& vect_index = __indices[*(__heap[i].second)];
648 for (
auto& idx : vect_index) {
659 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc >
661 const Val& elt,
const Priority& new_priority) {
662 std::vector< Size >& vect_index = __indices[elt];
664 for (
auto index : vect_index) {
665 setPriorityByPos(index, new_priority);
670 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc >
672 const Val& elt)
const {
673 return __heap[__indices[elt][0]].first;
677 template <
typename Val,
typename Priority,
typename Cmp,
typename Alloc >
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
INLINE std::ostream & operator<<(std::ostream &stream, const MultiPriorityQueue< Val, Priority, Cmp, Alloc > &queue)
std::vector< std::pair< Priority, const Val *>, HeapAlloc > __heap
An array storing all the elements of the heap as well as their score.
friend class MultiPriorityQueue
Making all MultiPriorityQueue friend with themselves.
Size __nb_elements
The number of elements in the heap.
std::string toString() const
Displays the content of the queue.
Cmp __cmp
Comparison function.
void clear()
Removes all the elements from the queue.
void erase(const Val &val)
Removes a given element from the priority queue (but does not return it).
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
The class for generic Hash Tables.
Size insert(const Val &val, const Priority &priority)
Inserts a new (a copy) element in the priority queue.
void resize(Size new_size)
Changes the size of the internal structure storing the priority queue.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
A MultiPriorityQueue is a heap in which each element has a mutable priority and duplicates are allowe...
HashTable< Val, std::vector< Size >, IndexAlloc > __indices
A hashtable for quickly finding the elements by their value.
#define GUM_ERROR(type, msg)