430 _heap_[i].first = new_priority;
431 _heap_[i].second = val;
438 template <
typename Val,
typename Priority,
typename Cmp,
bool Gen >
441 Priority&& new_priority) {
448 const Val* val =
_heap_[index].second;
455 i = j, j = (j - 1) >> 1) {
474 _heap_[i].first = std::move(new_priority);
482 template <
typename Val,
typename Priority,
typename Cmp,
bool Gen >
485 const Priority& new_priority) {
490 template <
typename Val,
typename Priority,
typename Cmp,
bool Gen >
493 Priority&& new_priority) {
498 template <
typename Val,
typename Priority,
typename Cmp,
bool Gen >
505 template <
typename Val,
typename Priority,
typename Cmp,
bool Gen >
511 return _heap_[index].first;
519 template <
typename Val,
typename Priority,
typename Cmp >
522 Size capacity) : _indices_(capacity >> 1, true, true), _cmp_(compare) {
530 template <
typename Val,
typename Priority,
typename Cmp >
532 std::initializer_list< std::pair< Val, Priority > > list) :
533 _indices_(Size(list.size()) / 2, true, true) {
535 _heap_.reserve(list.size());
536 for (
const auto& elt: list) {
537 insert(elt.first, elt.second);
545 template <
typename Val,
typename Priority,
typename Cmp >
548 _heap_(from._heap_), _indices_(from._indices_), _nb_elements_(from._nb_elements_),
555 template <
typename Val,
typename Priority,
typename Cmp >
558 _heap_(std::move(from._heap_)), _indices_(std::move(from._indices_)),
559 _nb_elements_(std::move(from._nb_elements_)), _cmp_(std::move(from._cmp_)) {
565 template <
typename Val,
typename Priority,
typename Cmp >
572 template <
typename Val,
typename Priority,
typename Cmp >
586 _indices_ = from._indices_;
587 _heap_ = from._heap_;
588 _nb_elements_ = from._nb_elements_;
602 template <
typename Val,
typename Priority,
typename Cmp >
611 _indices_ = std::move(from._indices_);
612 _heap_ = std::move(from._heap_);
613 _cmp_ = std::move(from._cmp_);
614 _nb_elements_ = std::move(from._nb_elements_);
621 template <
typename Val,
typename Priority,
typename Cmp >
623 if (!_nb_elements_) {
GUM_ERROR(NotFound,
"empty priority queue") }
625 return _heap_[0].second;
629 template <
typename Val,
typename Priority,
typename Cmp >
631 if (!_nb_elements_) {
GUM_ERROR(NotFound,
"empty priority queue") }
633 return _heap_[0].first;
637 template <
typename Val,
typename Priority,
typename Cmp >
639 return _nb_elements_;
643 template <
typename Val,
typename Priority,
typename Cmp >
645 return Size(_heap_.capacity());
649 template <
typename Val,
typename Priority,
typename Cmp >
651 if (new_size < _nb_elements_)
return;
653 _heap_.reserve(new_size);
654 _indices_.resize(new_size / 2);
658 template <
typename Val,
typename Priority,
typename Cmp >
666 template <
typename Val,
typename Priority,
typename Cmp >
668 if (index >= _nb_elements_)
return;
671 _indices_.erase(_heap_[index].second);
674 std::pair< Priority, Val > last = std::move(_heap_[_nb_elements_ - 1]);
678 if (!_nb_elements_ || (index == _nb_elements_))
return;
683 for (
Size j = (index << 1) + 1; j < _nb_elements_; i = j, j = (j << 1) + 1) {
685 if ((j + 1 < _nb_elements_) && _cmp_(_heap_[j + 1].first, _heap_[j].first)) ++j;
688 if (_cmp_(last.first, _heap_[j].first))
break;
691 _heap_[i] = std::move(_heap_[j]);
692 _indices_[_heap_[i].second] = i;
696 _heap_[i] = std::move(last);
697 _indices_[_heap_[i].second] = i;
701 template <
typename Val,
typename Priority,
typename Cmp >
703 if (
auto ptr = _indices_.tryGet(val)) eraseByPos(*ptr);
707 template <
typename Val,
typename Priority,
typename Cmp >
713 template <
typename Val,
typename Priority,
typename Cmp >
715 if (!_nb_elements_) {
GUM_ERROR(NotFound,
"empty priority queue") }
717 Val v = _heap_[0].second;
724 template <
typename Val,
typename Priority,
typename Cmp >
731 template <
typename Val,
typename Priority,
typename Cmp >
733 const Priority& priority) {
739 _heap_.push_back(std::pair< Priority, Val >(priority, val));
741 _indices_.erase(val);
745 std::pair< Priority, Val > new_heap_val = std::move(_heap_[_nb_elements_]);
749 Size i = _nb_elements_ - 1;
751 for (
Size j = (i - 1) >> 1; i && _cmp_(new_heap_val.first, _heap_[j].first);
752 i = j, j = (j - 1) >> 1) {
753 _heap_[i] = std::move(_heap_[j]);
754 _indices_[_heap_[i].second] = i;
758 _heap_[i].first = std::move(new_heap_val.first);
759 _heap_[i].second = val;
766 template <
typename Val,
typename Priority,
typename Cmp >
768 Priority&& priority) {
774 _heap_.push_back(std::pair< Priority, Val >(std::move(priority), val));
776 _indices_.erase(val);
780 std::pair< Priority, Val > new_heap_val = std::move(_heap_[_nb_elements_]);
784 Size i = _nb_elements_ - 1;
786 for (
Size j = (i - 1) >> 1; i && _cmp_(new_heap_val.first, _heap_[j].first);
787 i = j, j = (j - 1) >> 1) {
788 _heap_[i] = std::move(_heap_[j]);
789 _indices_[_heap_[i].second] = i;
793 _heap_[i].first = std::move(new_heap_val.first);
794 _heap_[i].second = val;
801 template <
typename Val,
typename Priority,
typename Cmp >
802 template <
typename... Args >
804 std::pair< Val, Priority > new_elt
805 = std::make_pair< Val, Priority >(std::forward< Args >(args)...);
806 return insert(new_elt.first, std::move(new_elt.second));
810 template <
typename Val,
typename Priority,
typename Cmp >
812 return (_nb_elements_ == 0);
816 template <
typename Val,
typename Priority,
typename Cmp >
818 return _indices_.exists(val);
822 template <
typename Val,
typename Priority,
typename Cmp >
824 if (index >= _nb_elements_) {
825 GUM_ERROR(NotFound,
"not enough elements in the PriorityQueueImplementation")
828 return _heap_[index].second;
832 template <
typename Val,
typename Priority,
typename Cmp >
835 std::stringstream stream;
838 for (
Size i = 0; i != _nb_elements_; ++i, deja =
true) {
839 if (deja) stream <<
" , ";
841 stream <<
"(" << _heap_[i].first <<
" , " << _heap_[i].second <<
")";
850 template <
typename Val,
typename Priority,
typename Cmp >
853 const Priority& new_priority) {
855 if (index >= _nb_elements_) {
856 GUM_ERROR(NotFound,
"not enough elements in the PriorityQueueImplementation")
860 Val val = _heap_[index].second;
866 for (
Size j = (i - 1) >> 1; i && _cmp_(new_priority, _heap_[j].first);
867 i = j, j = (j - 1) >> 1) {
868 _heap_[i] = std::move(_heap_[j]);
869 _indices_[_heap_[i].second] = i;
873 for (
Size j = (i << 1) + 1; j < _nb_elements_; i = j, j = (j << 1) + 1) {
875 if ((j + 1 < _nb_elements_) && _cmp_(_heap_[j + 1].first, _heap_[j].first)) ++j;
878 if (_cmp_(new_priority, _heap_[j].first))
break;
881 _heap_[i] = std::move(_heap_[j]);
882 _indices_[_heap_[i].second] = i;
886 _heap_[i].first = new_priority;
887 _heap_[i].second = val;
894 template <
typename Val,
typename Priority,
typename Cmp >
897 Priority&& new_priority) {
899 if (index >= _nb_elements_) {
900 GUM_ERROR(NotFound,
"not enough elements in the PriorityQueueImplementation")
904 Val val = _heap_[index].second;
910 for (
Size j = (i - 1) >> 1; i && _cmp_(new_priority, _heap_[j].first);
911 i = j, j = (j - 1) >> 1) {
917 for (
Size j = (i << 1) + 1; j < _nb_elements_; i = j, j = (j << 1) + 1) {
919 if ((j + 1 < _nb_elements_) && _cmp_(_heap_[j + 1].first, _heap_[j].first)) ++j;
922 if (_cmp_(new_priority, _heap_[j].first))
break;
925 _heap_[i] = std::move(_heap_[j]);
926 _indices_[_heap_[i].second] = i;
930 _heap_[i].first = std::move(new_priority);
931 _heap_[i].second = val;
938 template <
typename Val,
typename Priority,
typename Cmp >
941 const Priority& new_priority) {
946 template <
typename Val,
typename Priority,
typename Cmp >
949 Priority&& new_priority) {
950 setPriorityByPos(_indices_[elt], std::move(new_priority));
954 template <
typename Val,
typename Priority,
typename Cmp >
956 return _heap_[_indices_[elt]].first;
960 template <
typename Val,
typename Priority,
typename Cmp >
966 return _heap_[index].first;
974 template <
typename Val,
typename Priority,
typename Cmp >
982 template <
typename Val,
typename Priority,
typename Cmp >
984 std::initializer_list< std::pair< Val, Priority > > list) :
991 template <
typename Val,
typename Priority,
typename Cmp >
1000 template <
typename Val,
typename Priority,
typename Cmp >
1008 template <
typename Val,
typename Priority,
typename Cmp >
1015 template <
typename Val,
typename Priority,
typename Cmp >
1023 template <
typename Val,
typename Priority,
typename Cmp >
1031 template <
typename Val,
typename Priority,
typename Cmp >