aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
sharedAVLTree.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_SHARED_AVL_TREE_H
51#define GUM_SHARED_AVL_TREE_H
52
53#include <agrum/agrum.h>
54
56
57#ifndef DOXYGEN_SHOULD_SKIP_THIS
58
59namespace gum {
60
61 template < typename Val, typename Cmp >
62 class SharedAVLTreeIterator;
63 template < typename Val, typename Cmp >
64 class SharedAVLTreeIteratorSafe;
65
66 template < typename Val, typename Cmp >
67 class SharedAVLTreeReverseIterator;
68 template < typename Val, typename Cmp >
69 class SharedAVLTreeReverseIteratorSafe;
70
81 template < typename Val, typename Cmp = std::less< Val > >
82 class SharedAVLTree: private AVLTree< Val, Cmp > {
83 public:
86 using value_type = Val;
87 using reference = Val&;
88 using const_reference = const Val&;
89 using pointer = Val*;
90 using const_pointer = const Val*;
91 using iterator = SharedAVLTreeIterator< Val, Cmp >;
92 using iterator_safe = SharedAVLTreeIteratorSafe< Val, Cmp >;
93 using reverse_iterator = SharedAVLTreeReverseIterator< Val, Cmp >;
94 using reverse_iterator_safe = SharedAVLTreeReverseIteratorSafe< Val, Cmp >;
95 using AVLNode = AVLTreeNode< Val >;
97
98 // ============================================================================
100 // ============================================================================
102
110 explicit SharedAVLTree(const Cmp& compare = Cmp());
111
116 SharedAVLTree(SharedAVLTree< Val, Cmp >&& from) noexcept;
117
121 ~SharedAVLTree();
122
124
125 // ============================================================================
127 // ============================================================================
129
136 SharedAVLTree< Val, Cmp >& operator=(SharedAVLTree< Val, Cmp >&& from);
137
139
140 // ============================================================================
142 // ============================================================================
144
149 using AVLTree< Val, Cmp >::size;
150
155 using AVLTree< Val, Cmp >::empty;
156
162 using AVLTree< Val, Cmp >::contains;
163
169 using AVLTree< Val, Cmp >::exists;
170
172
173 AVLNode* highestNode() const noexcept;
174
176
177 using AVLTree< Val, Cmp >::highestValue;
178
180
181 AVLNode* lowestNode() const noexcept;
182
184
185 using AVLTree< Val, Cmp >::lowestValue;
186
188 void insert(AVLNode* node);
189
191
192 void erase(AVLNode* node);
193
195
201 void erase(iterator_safe& iter);
202
204
210 void erase(reverse_iterator_safe& iter);
211
213 using AVLTree< Val, Cmp >::clear;
214
218 using AVLTree< Val, Cmp >::toString;
219
221
222 // ============================================================================
224 // ============================================================================
226
228 iterator begin() const;
229
231 constexpr const iterator& end() const;
232
234 reverse_iterator rbegin() const;
235
237 constexpr const reverse_iterator& rend() const;
238
240 iterator_safe beginSafe();
241
243 constexpr const iterator_safe& endSafe() const;
244
246 reverse_iterator_safe rbeginSafe();
247
249 constexpr const reverse_iterator_safe& rendSafe() const;
250
252
253
254 private:
256 SharedAVLTree(const SharedAVLTree< Val, Cmp >& from) = delete;
257 SharedAVLTree< Val, Cmp >& operator=(const SharedAVLTree< Val, Cmp >& from) = delete;
258
259
261 friend iterator;
262 friend reverse_iterator;
263 friend iterator_safe;
264 friend reverse_iterator_safe;
265 };
266
277 template < typename Val, typename Cmp = std::less< Val > >
278 class SharedAVLTreeIterator: protected AVLTreeIterator< Val, Cmp > {
279 public:
282 using iterator_category = std::bidirectional_iterator_tag;
283 using AVLNode = AVLTreeNode< Val >;
284 using value_type = AVLNode;
285 using reference = value_type&;
286 using const_reference = const value_type&;
287 using pointer = value_type*;
288 using const_pointer = const value_type*;
290
291
292 // ============================================================================
294 // ============================================================================
296
303 explicit SharedAVLTreeIterator(const SharedAVLTree< Val, Cmp >& tree,
304 const bool begin = true) noexcept;
305
306# ifndef DOXYGEN_SHOULD_SKIP_THIS
307 // constructor for the static end iterator
308 // only AVLTree.cpp should use this constructor
309 explicit consteval SharedAVLTreeIterator(StaticInitializer init) noexcept :
310 AVLTreeIterator< Val, Cmp >(init) {}
311# endif // DOXYGEN_SHOULD_SKIP_THIS
312
314 SharedAVLTreeIterator(const SharedAVLTreeIterator< Val, Cmp >& from) noexcept;
315
317 SharedAVLTreeIterator(SharedAVLTreeIterator< Val, Cmp >&& from) noexcept;
318
320 ~SharedAVLTreeIterator() noexcept;
321
323
324
325 // ============================================================================
327 // ============================================================================
329
331 SharedAVLTreeIterator< Val, Cmp >&
332 operator=(const SharedAVLTreeIterator< Val, Cmp >& from) noexcept;
333
335 SharedAVLTreeIterator< Val, Cmp >& operator=(SharedAVLTreeIterator< Val, Cmp >&& from) noexcept;
336
338 bool operator==(const SharedAVLTreeIterator< Val, Cmp >& from) const;
339
341 bool operator!=(const SharedAVLTreeIterator< Val, Cmp >& from) const;
342
344
346 SharedAVLTreeIterator< Val, Cmp >& operator++() noexcept;
347
349
351 SharedAVLTreeIterator< Val, Cmp >& operator+=(const Size k) noexcept;
352
354
356 SharedAVLTreeIterator< Val, Cmp >& operator--() noexcept;
357
359
361 SharedAVLTreeIterator< Val, Cmp >& operator-=(const Size k) noexcept;
362
369 const_reference operator*() const;
370
372 const_pointer operator->() const;
373
375
377 friend AVLTree< Val, Cmp >;
378 friend SharedAVLTree< Val, Cmp >;
379 };
380
391 template < typename Val, typename Cmp = std::less< Val > >
392 class SharedAVLTreeIteratorSafe: protected AVLTreeIteratorSafe< Val, Cmp > {
393 public:
396 using iterator_category = std::bidirectional_iterator_tag;
397 using AVLNode = AVLTreeNode< Val >;
398 using value_type = AVLNode;
399 using reference = value_type&;
400 using const_reference = const value_type&;
401 using pointer = value_type*;
402 using const_pointer = const value_type*;
404
405
406 // ============================================================================
408 // ============================================================================
410
417 explicit SharedAVLTreeIteratorSafe(SharedAVLTree< Val, Cmp >& tree, const bool rbegin = true);
418
419# ifndef DOXYGEN_SHOULD_SKIP_THIS
420 // constructor for the static endSafe iterator
421 // only AVLTree.cpp should use this constructor
422 explicit consteval SharedAVLTreeIteratorSafe(StaticInitializer init) noexcept :
423 AVLTreeIteratorSafe< Val, Cmp >(init) {}
424# endif // DOXYGEN_SHOULD_SKIP_THIS
425
427 SharedAVLTreeIteratorSafe(const SharedAVLTreeIteratorSafe< Val, Cmp >& from);
428
430 SharedAVLTreeIteratorSafe(SharedAVLTreeIteratorSafe< Val, Cmp >&& from);
431
433 ~SharedAVLTreeIteratorSafe() noexcept;
434
436
437 // ============================================================================
439 // ============================================================================
441
443 SharedAVLTreeIteratorSafe< Val, Cmp >&
444 operator=(const SharedAVLTreeIteratorSafe< Val, Cmp >& from);
445
447 SharedAVLTreeIteratorSafe< Val, Cmp >& operator=(SharedAVLTreeIteratorSafe< Val, Cmp >&& from);
448
450 bool operator==(const SharedAVLTreeIteratorSafe< Val, Cmp >& from) const;
451
453 bool operator!=(const SharedAVLTreeIteratorSafe< Val, Cmp >& from) const;
454
456
458 SharedAVLTreeIteratorSafe< Val, Cmp >& operator++() noexcept;
459
461
463 SharedAVLTreeIteratorSafe< Val, Cmp >& operator+=(const Size k) noexcept;
464
466
468 SharedAVLTreeIteratorSafe< Val, Cmp >& operator--() noexcept;
469
471
473 SharedAVLTreeIteratorSafe< Val, Cmp >& operator-=(const Size k) noexcept;
474
481 const_reference operator*() const;
482
484 const_pointer operator->() const;
485
487
488
489 protected:
491 friend AVLTree< Val, Cmp >;
492 friend SharedAVLTree< Val, Cmp >;
493 };
494
505 template < typename Val, typename Cmp = std::less< Val > >
506 class SharedAVLTreeReverseIterator: protected SharedAVLTreeIterator< Val, Cmp > {
507 public:
510 using iterator_category = std::bidirectional_iterator_tag;
511 using AVLNode = AVLTreeNode< Val >;
512 using value_type = AVLNode;
513 using reference = value_type&;
514 using const_reference = const value_type&;
515 using pointer = value_type*;
516 using const_pointer = const value_type*;
518
519
520 // ============================================================================
522 // ============================================================================
524
531 explicit SharedAVLTreeReverseIterator(const SharedAVLTree< Val, Cmp >& tree,
532 const bool rbegin = true) noexcept;
533
534# ifndef DOXYGEN_SHOULD_SKIP_THIS
535 // constructor for the static rend iterator
536 // only AVLTree.cpp should use this constructor
537 explicit consteval SharedAVLTreeReverseIterator(StaticInitializer init) noexcept :
538 SharedAVLTreeIterator< Val, Cmp >(init) {}
539# endif // DOXYGEN_SHOULD_SKIP_THIS
540
542 SharedAVLTreeReverseIterator(const SharedAVLTreeReverseIterator< Val, Cmp >& from) noexcept;
543
545 SharedAVLTreeReverseIterator(SharedAVLTreeReverseIterator< Val, Cmp >&& from) noexcept;
546
548 ~SharedAVLTreeReverseIterator() noexcept;
549
551
552 // ============================================================================
554 // ============================================================================
556
558 SharedAVLTreeReverseIterator< Val, Cmp >&
559 operator=(const SharedAVLTreeReverseIterator< Val, Cmp >& from) noexcept;
560
562 SharedAVLTreeReverseIterator< Val, Cmp >&
563 operator=(SharedAVLTreeReverseIterator< Val, Cmp >&& from) noexcept;
564
566 bool operator==(const SharedAVLTreeReverseIterator< Val, Cmp >& from) const;
567
569 bool operator!=(const SharedAVLTreeReverseIterator< Val, Cmp >& from) const;
570
572
574 SharedAVLTreeReverseIterator< Val, Cmp >& operator++() noexcept;
575
577
579 SharedAVLTreeReverseIterator< Val, Cmp >& operator+=(const Size k) noexcept;
580
582
584 SharedAVLTreeReverseIterator< Val, Cmp >& operator--() noexcept;
585
587
589 SharedAVLTreeReverseIterator< Val, Cmp >& operator-=(const Size k) noexcept;
590
597 using SharedAVLTreeIterator< Val, Cmp >::operator*;
598
600 using SharedAVLTreeIterator< Val, Cmp >::operator->;
601
603
604
605 protected:
607 friend AVLTree< Val, Cmp >;
608 friend SharedAVLTree< Val, Cmp >;
609 };
610
621 template < typename Val, typename Cmp = std::less< Val > >
622 class SharedAVLTreeReverseIteratorSafe: protected SharedAVLTreeIteratorSafe< Val, Cmp > {
623 public:
626 using iterator_category = std::bidirectional_iterator_tag;
627 using AVLNode = AVLTreeNode< Val >;
628 using value_type = AVLNode;
629 using reference = value_type&;
630 using const_reference = const value_type&;
631 using pointer = value_type*;
632 using const_pointer = const value_type*;
634
635
636 // ============================================================================
638 // ============================================================================
640
647 explicit SharedAVLTreeReverseIteratorSafe(SharedAVLTree< Val, Cmp >& tree,
648 const bool rbegin = true);
649
650# ifndef DOXYGEN_SHOULD_SKIP_THIS
651 // constructor for the static rendSafe iterator
652 // only AVLTree.cpp should use this constructor
653 explicit consteval SharedAVLTreeReverseIteratorSafe(StaticInitializer init) noexcept :
654 SharedAVLTreeIteratorSafe< Val, Cmp >(init) {}
655# endif // DOXYGEN_SHOULD_SKIP_THIS
656
658 SharedAVLTreeReverseIteratorSafe(const SharedAVLTreeReverseIteratorSafe< Val, Cmp >& from);
659
661 SharedAVLTreeReverseIteratorSafe(SharedAVLTreeReverseIteratorSafe< Val, Cmp >&& from);
662
664 ~SharedAVLTreeReverseIteratorSafe() noexcept;
665
667
668 // ============================================================================
670 // ============================================================================
672
674 SharedAVLTreeReverseIteratorSafe< Val, Cmp >&
675 operator=(const SharedAVLTreeReverseIteratorSafe< Val, Cmp >& from);
676
678 SharedAVLTreeReverseIteratorSafe< Val, Cmp >&
679 operator=(SharedAVLTreeReverseIteratorSafe< Val, Cmp >&& from);
680
682 bool operator==(const SharedAVLTreeReverseIteratorSafe< Val, Cmp >& from) const;
683
685 bool operator!=(const SharedAVLTreeReverseIteratorSafe< Val, Cmp >& from) const;
686
688
690 SharedAVLTreeReverseIteratorSafe< Val, Cmp >& operator++() noexcept;
691
693
695 SharedAVLTreeReverseIteratorSafe< Val, Cmp >& operator+=(const Size k) noexcept;
696
698
700 SharedAVLTreeReverseIteratorSafe< Val, Cmp >& operator--() noexcept;
701
703
705 SharedAVLTreeReverseIteratorSafe< Val, Cmp >& operator-=(const Size k) noexcept;
706
713 using SharedAVLTreeIteratorSafe< Val, Cmp >::operator*;
714
716 using SharedAVLTreeIteratorSafe< Val, Cmp >::operator->;
717
719
720 protected:
722 friend AVLTree< Val, Cmp >;
723 friend SharedAVLTree< Val, Cmp >;
724 };
725
727 template < typename Val, typename Cmp >
728 std::ostream& operator<<(std::ostream& stream, const SharedAVLTree< Val, Cmp >& tree) {
729 return stream << tree.toString();
730 }
731
732
733# ifndef DOXYGEN_SHOULD_SKIP_THIS
734 // _static_SharedAVLTree_end_ is a 'constant' iterator initialized at compile time
735 // that represents the end iterators for all shared AVL trees (whatever their
736 // type). This global variable avoids creating the same iterators within every
737 // shared AVL tree instance (this would be quite inefficient as end is precisely
738 // identical for all AVL trees). The same hold for reverse and safe end iterators.
739 // The type of _SharedAVLTree_end_ is a pointer to void because C++ allows
740 // pointers to void to be cast into pointers to other types (and conversely).
741 // This avoids the painful strict-aliasing rule warning
742 extern const SharedAVLTreeIterator< int, std::less< int > > _static_SharedAVLTree_end_;
743 extern const SharedAVLTreeReverseIterator< int, std::less< int > > _static_SharedAVLTree_rend_;
744 extern const SharedAVLTreeIteratorSafe< int, std::less< int > > _static_SharedAVLTree_end_safe_;
745 extern const SharedAVLTreeReverseIteratorSafe< int, std::less< int > >
746 _static_SharedAVLTree_rend_safe_;
747
748 inline constexpr void* const _SharedAVLTree_end_ = (void* const)&_static_SharedAVLTree_end_;
749 inline constexpr void* const _SharedAVLTree_rend_ = (void* const)&_static_SharedAVLTree_rend_;
750 inline constexpr void* const _SharedAVLTree_end_safe_
751 = (void* const)&_static_SharedAVLTree_end_safe_;
752 inline constexpr void* const _SharedAVLTree_rend_safe_
753 = (void* const)&_static_SharedAVLTree_rend_safe_;
754# endif // DOXYGEN_SHOULD_SKIP_THIS
755
756
757} // namespace gum
758
759// always include the implementation of the templates
761
762#endif // DOXYGEN_SHOULD_SKIP_THIS
763
764#endif // GUM_SHARED_AVL_TREE_H
AVL binary search trees.
AVL binary search tree.
Definition AVLTree.h:168
gum is the global namespace for all aGrUM entities
Definition agrum.h:46