aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
multiPriorityQueue.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
48
49#ifndef GUM_MULTI_PRIORITY_QUEUE_H
50#define GUM_MULTI_PRIORITY_QUEUE_H
51
52#include <functional>
53#include <sstream>
54#include <string>
55#include <utility>
56#include <vector>
57
58#include <agrum/agrum.h>
59
61
62#include <initializer_list>
63
64namespace gum {
65
66#define GUM_MULTIPLE_PRIORITY_QUEUE_DEFAULT_CAPACITY 10
67
68#ifndef DOXYGEN_SHOULD_SKIP_THIS
69 // templates provided by this file
70 template < typename Val, typename Priority, typename Cmp >
71 class MultiPriorityQueue;
72
73 template < typename Val, typename Priority, typename Cmp >
74 std::ostream& operator<<(std::ostream&, const MultiPriorityQueue< Val, Priority, Cmp >&);
75
76#endif // DOXYGEN_SHOULD_SKIP_THIS
77
78 // ===========================================================================
79 // === PRIORITY QUEUES ===
80 // ===========================================================================
142 template < typename Val, typename Priority = int, typename Cmp = std::less< Priority > >
144 public:
147 using value_type = Val;
148 using reference = Val&;
149 using const_reference = const Val&;
150 using pointer = Val*;
151 using const_pointer = const Val*;
152 using difference_type = std::ptrdiff_t;
154
155
156 // ============================================================================
158 // ============================================================================
160
170 explicit MultiPriorityQueue(Cmp compare = Cmp(),
172
181 explicit MultiPriorityQueue(std::initializer_list< std::pair< Val, Priority > > list);
182
188
194
199
201 // ============================================================================
203 // ============================================================================
205
218
225
233 const Val& operator[](Size index_elt) const;
234
236 // ============================================================================
238 // ============================================================================
240
245 Size size() const noexcept;
246
251 bool empty() const noexcept;
252
258 bool contains(const Val& val) const;
259
265 const Val& top() const;
266
271 const Priority& topPriority() const;
272
278 Val pop();
279
291 Size insert(const Val& val, const Priority& priority);
292
304 Size insert(Val&& val, Priority&& priority);
305
316 template < typename... Args >
317 Size emplace(Args&&... args);
318
325 void eraseTop();
326
345 void eraseByPos(Size index);
346
359 void erase(const Val& val);
360
370 Size setPriorityByPos(Size index, const Priority& new_priority);
371
381 Size setPriorityByPos(Size index, Priority&& new_priority);
382
389 void setPriority(const Val& elt, const Priority& new_priority);
390
403 const Priority& priority(const Val& elt) const;
404
408 void clear();
409
421 const HashTable< Val, std::vector< Size > >& allValues() const;
422
427 std::string toString() const;
428
430 // ============================================================================
432 // ============================================================================
434
441 Size capacity() const noexcept;
442
450 void resize(Size new_size);
451
453
454 private:
456 std::vector< std::pair< Priority, const Val* > > _heap_;
457
459 HashTable< Val, std::vector< Size > > _indices_;
460
463
466 };
467
468} /* namespace gum */
469
470
471#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
472extern template class gum::MultiPriorityQueue< std::string >;
473extern template class gum::MultiPriorityQueue< int, int >;
474#endif
475
476
477// always include the implementation of the templates
479
480#endif /* GUM_MULTI_PRIORITY_QUEUE_H */
The class for generic Hash Tables.
Definition hashTable.h:637
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.
Val value_type
types for STL compliance
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.
const Val & const_reference
types for STL compliance
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.
std::ptrdiff_t difference_type
types for STL compliance
Size setPriorityByPos(Size index, const Priority &new_priority)
Modifies the priority of the element at position "index" of the queue.
const Val * const_pointer
types for STL compliance
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.
Val * pointer
types for STL compliance
Size emplace(Args &&... args)
Emplace a new element into the priority queue.
Val & reference
types for STL compliance
MultiPriorityQueue(Cmp compare=Cmp(), Size capacity=GUM_MULTIPLE_PRIORITY_QUEUE_DEFAULT_CAPACITY)
Basic constructor.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Class hash tables iterators.
#define GUM_MULTIPLE_PRIORITY_QUEUE_DEFAULT_CAPACITY
Template implementations of multi priority queues.
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.