435 _heap_[i].first = new_priority;
436 _heap_[i].second = val;
443 template <
typename Val,
typename Priority,
typename Cmp,
bool Gen >
446 Priority&& new_priority) {
453 const Val* val =
_heap_[index].second;
460 i = j, j = (j - 1) >> 1) {
479 _heap_[i].first = std::move(new_priority);
487 template <
typename Val,
typename Priority,
typename Cmp,
bool Gen >
490 const Priority& new_priority) {
495 template <
typename Val,
typename Priority,
typename Cmp,
bool Gen >
498 Priority&& new_priority) {
503 template <
typename Val,
typename Priority,
typename Cmp,
bool Gen >
504 INLINE
const Priority&
510 template <
typename Val,
typename Priority,
typename Cmp,
bool Gen >
511 INLINE
const Priority&
516 return _heap_[index].first;
524 template <
typename Val,
typename Priority,
typename Cmp >
527 Size capacity) : _indices_(capacity >> 1, true, true), _cmp_(compare) {
535 template <
typename Val,
typename Priority,
typename Cmp >
537 std::initializer_list< std::pair< Val, Priority > > list) :
538 _indices_(Size(list.size()) / 2, true, true) {
540 _heap_.reserve(list.size());
541 for (
const auto& elt: list) {
542 insert(elt.first, elt.second);
550 template <
typename Val,
typename Priority,
typename Cmp >
553 _heap_(from._heap_), _indices_(from._indices_), _nb_elements_(from._nb_elements_),
560 template <
typename Val,
typename Priority,
typename Cmp >
563 _heap_(std::move(from._heap_)), _indices_(std::move(from._indices_)),
564 _nb_elements_(std::move(from._nb_elements_)), _cmp_(std::move(from._cmp_)) {
570 template <
typename Val,
typename Priority,
typename Cmp >
577 template <
typename Val,
typename Priority,
typename Cmp >
591 _indices_ = from._indices_;
592 _heap_ = from._heap_;
593 _nb_elements_ = from._nb_elements_;
607 template <
typename Val,
typename Priority,
typename Cmp >
616 _indices_ = std::move(from._indices_);
617 _heap_ = std::move(from._heap_);
618 _cmp_ = std::move(from._cmp_);
619 _nb_elements_ = std::move(from._nb_elements_);
626 template <
typename Val,
typename Priority,
typename Cmp >
628 if (!_nb_elements_) {
GUM_ERROR(NotFound,
"empty priority queue") }
630 return _heap_[0].second;
634 template <
typename Val,
typename Priority,
typename Cmp >
635 INLINE
const Priority&
637 if (!_nb_elements_) {
GUM_ERROR(NotFound,
"empty priority queue") }
639 return _heap_[0].first;
643 template <
typename Val,
typename Priority,
typename Cmp >
645 return _nb_elements_;
649 template <
typename Val,
typename Priority,
typename Cmp >
651 return Size(_heap_.capacity());
655 template <
typename Val,
typename Priority,
typename Cmp >
657 if (new_size < _nb_elements_)
return;
659 _heap_.reserve(new_size);
660 _indices_.resize(new_size / 2);
664 template <
typename Val,
typename Priority,
typename Cmp >
672 template <
typename Val,
typename Priority,
typename Cmp >
674 if (index >= _nb_elements_)
return;
677 _indices_.erase(_heap_[index].second);
680 std::pair< Priority, Val > last = std::move(_heap_[_nb_elements_ - 1]);
684 if (!_nb_elements_ || (index == _nb_elements_))
return;
689 for (
Size j = (index << 1) + 1; j < _nb_elements_; i = j, j = (j << 1) + 1) {
691 if ((j + 1 < _nb_elements_) && _cmp_(_heap_[j + 1].first, _heap_[j].first)) ++j;
694 if (_cmp_(last.first, _heap_[j].first))
break;
697 _heap_[i] = std::move(_heap_[j]);
698 _indices_[_heap_[i].second] = i;
702 _heap_[i] = std::move(last);
703 _indices_[_heap_[i].second] = i;
707 template <
typename Val,
typename Priority,
typename Cmp >
710 eraseByPos(_indices_[val]);
711 }
catch (NotFound
const&) {}
715 template <
typename Val,
typename Priority,
typename Cmp >
721 template <
typename Val,
typename Priority,
typename Cmp >
723 if (!_nb_elements_) {
GUM_ERROR(NotFound,
"empty priority queue") }
725 Val v = _heap_[0].second;
732 template <
typename Val,
typename Priority,
typename Cmp >
739 template <
typename Val,
typename Priority,
typename Cmp >
742 const Priority& priority) {
748 _heap_.push_back(std::pair< Priority, Val >(priority, val));
750 _indices_.erase(val);
754 std::pair< Priority, Val > new_heap_val = std::move(_heap_[_nb_elements_]);
758 Size i = _nb_elements_ - 1;
760 for (
Size j = (i - 1) >> 1; i && _cmp_(new_heap_val.first, _heap_[j].first);
761 i = j, j = (j - 1) >> 1) {
762 _heap_[i] = std::move(_heap_[j]);
763 _indices_[_heap_[i].second] = i;
767 _heap_[i].first = std::move(new_heap_val.first);
768 _heap_[i].second = val;
775 template <
typename Val,
typename Priority,
typename Cmp >
777 Priority&& priority) {
783 _heap_.push_back(std::pair< Priority, Val >(std::move(priority), val));
785 _indices_.erase(val);
789 std::pair< Priority, Val > new_heap_val = std::move(_heap_[_nb_elements_]);
793 Size i = _nb_elements_ - 1;
795 for (
Size j = (i - 1) >> 1; i && _cmp_(new_heap_val.first, _heap_[j].first);
796 i = j, j = (j - 1) >> 1) {
797 _heap_[i] = std::move(_heap_[j]);
798 _indices_[_heap_[i].second] = i;
802 _heap_[i].first = std::move(new_heap_val.first);
803 _heap_[i].second = val;
810 template <
typename Val,
typename Priority,
typename Cmp >
811 template <
typename... Args >
813 std::pair< Val, Priority > new_elt
814 = std::make_pair< Val, Priority >(std::forward< Args >(args)...);
815 return insert(new_elt.first, std::move(new_elt.second));
819 template <
typename Val,
typename Priority,
typename Cmp >
821 return (_nb_elements_ == 0);
825 template <
typename Val,
typename Priority,
typename Cmp >
827 return _indices_.exists(val);
831 template <
typename Val,
typename Priority,
typename Cmp >
834 if (index >= _nb_elements_) {
835 GUM_ERROR(NotFound,
"not enough elements in the PriorityQueueImplementation")
838 return _heap_[index].second;
842 template <
typename Val,
typename Priority,
typename Cmp >
845 std::stringstream stream;
848 for (
Size i = 0; i != _nb_elements_; ++i, deja =
true) {
849 if (deja) stream <<
" , ";
851 stream <<
"(" << _heap_[i].first <<
" , " << _heap_[i].second <<
")";
860 template <
typename Val,
typename Priority,
typename Cmp >
863 const Priority& new_priority) {
865 if (index >= _nb_elements_) {
866 GUM_ERROR(NotFound,
"not enough elements in the PriorityQueueImplementation")
870 Val val = _heap_[index].second;
876 for (
Size j = (i - 1) >> 1; i && _cmp_(new_priority, _heap_[j].first);
877 i = j, j = (j - 1) >> 1) {
878 _heap_[i] = std::move(_heap_[j]);
879 _indices_[_heap_[i].second] = i;
883 for (
Size j = (i << 1) + 1; j < _nb_elements_; i = j, j = (j << 1) + 1) {
885 if ((j + 1 < _nb_elements_) && _cmp_(_heap_[j + 1].first, _heap_[j].first)) ++j;
888 if (_cmp_(new_priority, _heap_[j].first))
break;
891 _heap_[i] = std::move(_heap_[j]);
892 _indices_[_heap_[i].second] = i;
896 _heap_[i].first = new_priority;
897 _heap_[i].second = val;
904 template <
typename Val,
typename Priority,
typename Cmp >
907 Priority&& new_priority) {
909 if (index >= _nb_elements_) {
910 GUM_ERROR(NotFound,
"not enough elements in the PriorityQueueImplementation")
914 Val val =
_heap_[index].second;
921 i = j, j = (j - 1) >> 1) {
927 for (
Size j = (i << 1) + 1; j < _nb_elements_; i = j, j = (j << 1) + 1) {
929 if ((j + 1 < _nb_elements_) && _cmp_(_heap_[j + 1].first, _heap_[j].first)) ++j;
932 if (_cmp_(new_priority, _heap_[j].first))
break;
935 _heap_[i] = std::move(_heap_[j]);
936 _indices_[_heap_[i].second] = i;
940 _heap_[i].first = std::move(new_priority);
948 template <
typename Val,
typename Priority,
typename Cmp >
951 const Priority& new_priority) {
952 setPriorityByPos(_indices_[elt], new_priority);
956 template <
typename Val,
typename Priority,
typename Cmp >
959 Priority&& new_priority) {
964 template <
typename Val,
typename Priority,
typename Cmp >
965 INLINE
const Priority&
967 return _heap_[_indices_[elt]].first;
971 template <
typename Val,
typename Priority,
typename Cmp >
972 INLINE
const Priority&
974 if (index > _nb_elements_) {
977 return _heap_[index].first;
985 template <
typename Val,
typename Priority,
typename Cmp >
993 template <
typename Val,
typename Priority,
typename Cmp >
995 std::initializer_list< std::pair< Val, Priority > > list) :
1002 template <
typename Val,
typename Priority,
typename Cmp >
1011 template <
typename Val,
typename Priority,
typename Cmp >
1020 template <
typename Val,
typename Priority,
typename Cmp >
1027 template <
typename Val,
typename Priority,
typename Cmp >
1035 template <
typename Val,
typename Priority,
typename Cmp >
1043 template <
typename Val,
typename Priority,
typename Cmp >