aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
priorityQueue.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
49
50#ifndef GUM_PRIORITY_QUEUE_H
51#define GUM_PRIORITY_QUEUE_H
52
53#include <functional>
54#include <sstream>
55#include <string>
56#include <utility>
57#include <vector>
58
59#include <agrum/agrum.h>
60
62
63#include <initializer_list>
64#include <type_traits>
65
66namespace gum {
67
68#define GUM_PRIORITY_QUEUE_DEFAULT_CAPACITY 10
69
70 // templates provided by this file
71 template < typename Val, typename Priority, typename Cmp >
72 class PriorityQueue;
73 template < typename Val, typename Priority, typename Cmp >
74 std::ostream& operator<<(std::ostream&, const PriorityQueue< Val, Priority, Cmp >&);
75
76 // ===========================================================================
77 // === GENERAL IMPLEMENTATION OF PRIORITY QUEUES ===
78 // ===========================================================================
79
99 template < typename Val, typename Priority, typename Cmp, bool Gen >
102 friend class PriorityQueue< Val, Priority, Cmp >;
103
104 public:
107 using value_type = Val;
108 using reference = Val&;
109 using const_reference = const Val&;
110 using pointer = Val*;
111 using const_pointer = const Val*;
112 using difference_type = std::ptrdiff_t;
114
115 private:
116 // ============================================================================
118 // ============================================================================
120
131
140 explicit PriorityQueueImplementation(std::initializer_list< std::pair< Val, Priority > > list);
141
147
153
158
160
161 public:
162 // ============================================================================
164 // ============================================================================
166
180
189
197 const Val& operator[](Size index_elt) const;
198
200 // ============================================================================
202 // ============================================================================
204
209 Size size() const noexcept;
210
215 bool empty() const noexcept;
216
222 bool contains(const Val& val) const;
223
225
226 const Val& top() const;
227
233 const Priority& topPriority() const;
234
240 Val pop();
241
254 Size insert(const Val& val, const Priority& priority);
255
268 Size insert(Val&& val, Priority&& priority);
269
282 template < typename... Args >
283 Size emplace(Args&&... args);
284
291 void eraseTop();
292
311 void eraseByPos(Size index);
312
325 void erase(const Val& val);
326
336 Size setPriorityByPos(Size index, const Priority& new_priority);
337
347 Size setPriorityByPos(Size index, Priority&& new_priority);
348
355 void setPriority(const Val& elt, const Priority& new_priority);
356
363 void setPriority(const Val& elt, Priority&& new_priority);
364
374 const Priority& priority(const Val& elt) const;
375
381 const Priority& priorityByPos(Size index) const;
382
386 void clear();
387
399 const HashTable< Val, Size >& allValues() const noexcept;
400
405 std::string toString() const;
406
408 // ============================================================================
410 // ============================================================================
412
419 Size capacity() const noexcept;
420
426 void resize(Size new_size);
427
429
430 private:
432 std::vector< std::pair< Priority, const Val* > > _heap_;
433
436
439
442 };
443
444#ifndef DOXYGEN_SHOULD_SKIP_THIS
445
446 // ===========================================================================
447 // === SCALAR IMPLEMENTATION OF PRIORITY QUEUES ===
448 // ===========================================================================
449
469 template < typename Val, typename Priority, typename Cmp >
470 class PriorityQueueImplementation< Val, Priority, Cmp, true > {
472 friend class PriorityQueue< Val, Priority, Cmp >;
473
474 public:
477 using value_type = Val;
478 using reference = Val&;
479 using const_reference = const Val&;
480 using pointer = Val*;
481 using const_pointer = const Val*;
482 using difference_type = std::ptrdiff_t;
484
485 private:
486 // ============================================================================
488 // ============================================================================
490
500 explicit PriorityQueueImplementation(Cmp compare, Size capacity);
501
510 explicit PriorityQueueImplementation(std::initializer_list< std::pair< Val, Priority > > list);
511
518
524
529
531
532 public:
533 // ============================================================================
535 // ============================================================================
537
551
560
568 const Val& operator[](Size index_elt) const;
569
571 // ============================================================================
573 // ============================================================================
575
580 Size size() const noexcept;
581
586 bool empty() const noexcept;
587
593 bool contains(Val val) const;
594
596
597 const Val& top() const;
598
604 const Priority& topPriority() const;
605
611 Val pop();
612
625 Size insert(Val val, const Priority& priority);
626
639 Size insert(Val val, Priority&& priority);
640
653 template < typename... Args >
654 Size emplace(Args&&... args);
655
662 void eraseTop();
663
682 void eraseByPos(Size index);
683
696 void erase(Val val);
697
707 Size setPriorityByPos(Size index, const Priority& new_priority);
708
718 Size setPriorityByPos(Size index, Priority&& new_priority);
719
726 void setPriority(Val elt, const Priority& new_priority);
727
734 void setPriority(Val elt, Priority&& new_priority);
735
745 const Priority& priority(Val elt) const;
746
752 const Priority& priorityByPos(Size index) const;
753
757 void clear();
758
770 const HashTable< Val, Size >& allValues() const noexcept;
771
776 std::string toString() const;
777
779 // ============================================================================
781 // ============================================================================
783
790 Size capacity() const noexcept;
791
797 void resize(Size new_size);
798
800
801 private:
803 std::vector< std::pair< Priority, Val > > _heap_;
804
807
809 Size _nb_elements_{0};
810
812 Cmp _cmp_;
813 };
814
815#endif /* DOXYGEN_SHOULD_SKIP_THIS */
816
817 // ===========================================================================
818 // === PRIORITY QUEUES ===
819 // ===========================================================================
882 template < typename Val, typename Priority = int, typename Cmp = std::less< Priority > >
884 public PriorityQueueImplementation< Val, Priority, Cmp, std::is_scalar< Val >::value > {
885 public:
888 using value_type = Val;
889 using reference = Val&;
890 using const_reference = const Val&;
891 using pointer = Val*;
892 using const_pointer = const Val*;
893 using difference_type = std::ptrdiff_t;
895
898
899 // ============================================================================
901 // ============================================================================
903
913 explicit PriorityQueue(Cmp compare = Cmp(),
915
924 explicit PriorityQueue(std::initializer_list< std::pair< Val, Priority > > list);
925
931
937
942
944 // ============================================================================
946 // ============================================================================
948
961
968
970 };
971
972} /* namespace gum */
973
974#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
975extern template class gum::PriorityQueue< std::string >;
976extern template class gum::PriorityQueue< int, int >;
977#endif
978
979// always include the implementation of the templates
981
982#endif /* GUM_PRIORITY_QUEUE_H */
The class for generic Hash Tables.
Definition hashTable.h:637
The internal class for representing priority queues.
const Val & operator[](Size index_elt) const
Returns the element at index "index_elt" from the priority queue.
std::ptrdiff_t difference_type
Types for STL compliance.
Size size() const noexcept
Returns the number of elements in the priority queue.
PriorityQueueImplementation(PriorityQueueImplementation< Val, Priority, Cmp, Gen > &&from)
Move constructor.
PriorityQueueImplementation< Val, Priority, Cmp, Gen > & operator=(PriorityQueueImplementation< Val, Priority, Cmp, Gen > &&from)
Move operator.
~PriorityQueueImplementation()
Class destructor.
const Val * const_pointer
Types for STL compliance.
PriorityQueueImplementation(std::initializer_list< std::pair< Val, Priority > > list)
Initializer list constructor.
PriorityQueueImplementation< Val, Priority, Cmp, Gen > & operator=(const PriorityQueueImplementation< Val, Priority, Cmp, Gen > &from)
Copy operator.
Val value_type
Types for STL compliance.
PriorityQueueImplementation(const PriorityQueueImplementation< Val, Priority, Cmp, Gen > &from)
Copy constructor.
Val & reference
Types for STL compliance.
PriorityQueueImplementation(Cmp compare, Size capacity)
Basic constructor.
const Val & const_reference
Types for STL compliance.
Val * pointer
Types for STL compliance.
A priorityQueue is a heap in which each element has a mutable priority.
PriorityQueue< Val, Priority, Cmp > & operator=(const PriorityQueue< Val, Priority, Cmp > &from)
Copy operator.
PriorityQueue(Cmp compare=Cmp(), Size capacity=GUM_PRIORITY_QUEUE_DEFAULT_CAPACITY)
Basic constructor.
PriorityQueueImplementation< Val, Priority, Cmp, std::is_scalar< Val >::value > Implementation
const Val * const_pointer
Types for STL compliance.
Val & reference
Types for STL compliance.
const Val & const_reference
Types for STL compliance.
std::ptrdiff_t difference_type
Types for STL compliance.
~PriorityQueue()
Class destructor.
Val value_type
Types for STL compliance.
Val * pointer
Types for STL compliance.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Class hash tables iterators.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
std::ostream & operator<<(std::ostream &stream, const AVLTree< Val, Cmp > &tree)
display the content of a tree
Definition AVLTree.h:913
STL namespace.
#define GUM_PRIORITY_QUEUE_DEFAULT_CAPACITY
template implementations of priority queues
static constexpr Size default_size
The default number of slots in hashtables.
Definition hashTable.h:101