aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
heap.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_HEAP_H
51#define GUM_HEAP_H
52
53#include <functional>
54#include <utility>
55#include <vector>
56
57#include <agrum/agrum.h>
58
59#include <initializer_list>
60
61namespace gum {
62
63#define GUM_HEAP_DEFAULT_CAPACITY 10
64
65 // templates provided by this file
66
67 template < typename Val, typename Cmp >
68 class Heap;
69 template < typename Val, typename Cmp >
70 std::ostream& operator<<(std::ostream&, const Heap< Val, Cmp >&);
71
72 // ===========================================================================
73 // === SIMPLE HEAP DATA STRUCTURE ===
74 // ===========================================================================
75
140 template < typename Val, typename Cmp = std::less< Val > >
141 class Heap {
142 public:
145 using value_type = Val;
146 using reference = Val&;
147 using const_reference = const Val&;
148 using pointer = Val*;
149 using const_pointer = const Val*;
150 using size_type = std::size_t;
151 using difference_type = std::ptrdiff_t;
153
154 // ============================================================================
156 // ============================================================================
158
167 explicit Heap(Cmp compare = Cmp(), Size capacity = GUM_HEAP_DEFAULT_CAPACITY);
168
173 explicit Heap(std::initializer_list< Val > list);
174
179 Heap(const Heap< Val, Cmp >& from);
180
185 Heap(Heap< Val, Cmp >&& from) noexcept;
186
190 ~Heap();
191
193 // ============================================================================
195 // ============================================================================
197
209
221
230 const Val& operator[](Size index_elt) const;
231
233 // ============================================================================
235 // ============================================================================
237
243 const Val& top() const;
244
250 Val pop();
251
258 void eraseTop();
259
277 void eraseByPos(Size index);
278
288 void erase(const Val& val);
289
297 Size insert(const Val& val);
298
306 Size insert(Val&& val);
307
315 template < typename... Args >
316 Size emplace(Args&&... args);
317
322 Size size() const noexcept;
323
328 bool empty() const noexcept;
329
334 bool contains(const Val&) const;
335
340 std::string toString() const;
341
343 // ============================================================================
345 // ============================================================================
347
352 Size capacity() const noexcept;
353
362 void resize(Size new_size);
363
365
366 private:
368 std::vector< Val > _heap_;
369
372
375
378 };
379
380} /* namespace gum */
381
382
383#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
384extern template class gum::Heap< int >;
385extern template class gum::Heap< long >;
386extern template class gum::Heap< double >;
387#endif
388
389
390// always include the implementation of the templates
392
393#endif /* GUM_HEAP_H */
Heap data structure.
Definition heap.h:141
Size size() const noexcept
Returns the number of elements in the heap.
Definition heap_tpl.h:148
void eraseTop()
Removes the top of the heap (but does not return it).
Definition heap_tpl.h:205
std::size_t size_type
Types for STL compliance.
Definition heap.h:150
Val pop()
Removes the top element from the heap and return it.
Definition heap_tpl.h:213
bool contains(const Val &) const
Indicates whether the heap contains a given value.
Definition heap_tpl.h:273
Size _nb_elements_
The number of elements in the heap.
Definition heap.h:371
const Val & operator[](Size index_elt) const
Returns the element at index index_elt from the heap.
Definition heap_tpl.h:282
void resize(Size new_size)
Changes the size of the the internal structure storing the heap.
Definition heap_tpl.h:160
Val * pointer
Types for STL compliance.
Definition heap.h:148
Size emplace(Args &&... args)
Emplace a new element in the heap and returns its index.
Definition heap_tpl.h:258
~Heap()
Class destructor.
Definition heap_tpl.h:95
Size insert(const Val &val)
inserts a new element (actually a copy) in the heap and returns its index
Definition heap_tpl.h:239
Val value_type
Types for STL compliance.
Definition heap.h:145
Heap< Val, Cmp > & operator=(const Heap< Val, Cmp > &from)
Copy operator.
Definition heap_tpl.h:102
Size capacity() const noexcept
Returns the size of the internal structure storing the heap.
Definition heap_tpl.h:154
Heap(Cmp compare=Cmp(), Size capacity=GUM_HEAP_DEFAULT_CAPACITY)
Basic constructor: creates an empty heap.
Definition heap_tpl.h:59
Val & reference
Types for STL compliance.
Definition heap.h:146
const Val & top() const
Returns the element at the top of the heap.
Definition heap_tpl.h:140
std::ptrdiff_t difference_type
Types for STL compliance.
Definition heap.h:151
bool empty() const noexcept
Indicates whether the heap is empty.
Definition heap_tpl.h:267
const Val & const_reference
Types for STL compliance.
Definition heap.h:147
const Val * const_pointer
Types for STL compliance.
Definition heap.h:149
std::string toString() const
Definition heap_tpl.h:291
void eraseByPos(Size index)
Removes the element positioned at "index" from the heap.
Definition heap_tpl.h:166
Cmp _cmp_
Comparison function.
Definition heap.h:374
void erase(const Val &val)
Removes a given element from the heap (but does not return it).
Definition heap_tpl.h:194
Size _restoreHeap_()
After inserting an element at the end of the heap, restore heap property.
Definition heap_tpl.h:223
std::vector< Val > _heap_
An array storing all the elements of the heap.
Definition heap.h:368
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
#define GUM_HEAP_DEFAULT_CAPACITY
Definition heap.h:63
template implementations of heaps
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.