aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
splay.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_SPLAY_H
50#define GUM_SPLAY_H
51
52#include <iostream>
53// #include <stdlib.h>
54
55#include <agrum/agrum.h>
56
58
59namespace gum {
60
61 template < class Element >
62 class SplayBinaryNode;
63 template < class Element >
64 class SplayTree;
65
67
68 template < typename Element >
69 INLINE std::ostream& operator<<(std::ostream& out, const SplayBinaryNode< Element >& e);
70
72
73 template < typename Element >
74 INLINE std::ostream& operator<<(std::ostream& out, const SplayTree< Element >& s);
75
76 // =========================================================================
77 // === NODE ===
78 // =========================================================================
90 template < class Element >
92 public:
93 // ============================================================================
95 // ============================================================================
97
102 int position() const;
103
108 const Element& getElement() const;
109
115 const SplayBinaryNode< Element >* getFg() const;
116
122 const SplayBinaryNode< Element >* getFd() const;
123
125
126 protected:
127 // ============================================================================
129 // ============================================================================
131
141 SplayBinaryNode(const Element& e,
142 HashTable< Element, SplayBinaryNode< Element >* >& addr,
143 SplayBinaryNode* g = 0,
144 SplayBinaryNode* d = 0,
145 SplayBinaryNode* p = 0);
146
153 HashTable< Element, SplayBinaryNode< Element >* >& addr);
154
160 void copy_(const SplayBinaryNode< Element >& from,
161 HashTable< Element, SplayBinaryNode< Element >* >& addr);
162
167
169 // ============================================================================
171 // ============================================================================
173
179
185
191
199 HashTable< Element, SplayBinaryNode< Element >* >& addr);
200
202 // ============================================================================
204 // ============================================================================
206
208 Element elt;
209
212
215
218
221
223
225 friend class SplayTree< Element >;
226
228 friend std::ostream& operator<< <>(std::ostream& out, const SplayBinaryNode< Element >&);
229 };
230
231 // ============================================================================
232 // === SPLAY TREE ===
233 // ============================================================================
277 template < class Element >
278 class SplayTree {
279 public:
280 // ============================================================================
282 // ============================================================================
284
288 SplayTree();
289
294 SplayTree(const Element& e);
295
300 SplayTree(const SplayTree& from);
301
305 ~SplayTree();
306
308 // ============================================================================
310 // ============================================================================
312
319
321 // ============================================================================
323 // ============================================================================
325
332 Element& operator[](const unsigned int i);
333
340 const Element& operator[](const unsigned int i) const;
341
342
347 Element& front();
348
353 Element& back();
354
358 void popFront();
359
363 void popBack();
364
369 void pushFront(const Element& e);
370
375 void pushBack(const Element& e);
376
381 void insert(const Element& e);
382
387 void join(const SplayTree< Element >& s);
388
389
399 SplayTree< Element > split(const int i);
400
401
410 SplayTree< Element > split_by_val(const Element& e);
411
416 Size size() const;
417
423 bool contains(const Element& e) const;
424
426
427 protected:
428 // ============================================================================
430 // ============================================================================
432
435
438
440
442 void copy_(const SplayTree< Element >&);
443
445 friend std::ostream& operator<< <>(std::ostream& out, const SplayTree< Element >&);
446 };
447
448} /* namespace gum */
449
450// always include the tpl_.h as it contains only templates
452
453#endif /* GUM_SPLAY_H */
The class for generic Hash Tables.
Definition hashTable.h:637
the nodes of splay trees
Definition splay.h:91
SplayBinaryNode * fg
The left child.
Definition splay.h:214
SplayBinaryNode< Element > * zag()
A left rotation, the node must hava a father.
Definition splay_tpl.h:182
const Element & getElement() const
Returns the element in the node.
Definition splay_tpl.h:332
SplayBinaryNode(const Element &e, HashTable< Element, SplayBinaryNode< Element > * > &addr, SplayBinaryNode *g=0, SplayBinaryNode *d=0, SplayBinaryNode *p=0)
Basic constructor: creates a node with a reference to the element.
Definition splay_tpl.h:91
int position() const
Position of the node.
Definition splay_tpl.h:307
SplayBinaryNode * pere
The father, nullptr for the root.
Definition splay.h:220
void copy_(const SplayBinaryNode< Element > &from, HashTable< Element, SplayBinaryNode< Element > * > &addr)
A function used to perform copies.
Definition splay_tpl.h:62
SplayBinaryNode * fd
The right child.
Definition splay.h:217
const SplayBinaryNode< Element > * getFg() const
Returns the left child.
Definition splay_tpl.h:342
~SplayBinaryNode()
Class destructor.
Definition splay_tpl.h:118
Element elt
The content.
Definition splay.h:208
SplayBinaryNode< Element > * join(const SplayBinaryNode< Element > *e, HashTable< Element, SplayBinaryNode< Element > * > &addr)
Concatenation of two trees.
Definition splay_tpl.h:278
SplayBinaryNode< Element > * splay()
A splay rotation, the node will be the root of the tree.
Definition splay_tpl.h:232
const SplayBinaryNode< Element > * getFd() const
Returns the right child.
Definition splay_tpl.h:352
SplayBinaryNode< Element > * zig()
A right rotation, the node must have a father.
Definition splay_tpl.h:131
Size size
The size of the sub-tree.
Definition splay.h:211
A splay tree.
Definition splay.h:278
Element & operator[](const unsigned int i)
Get the element at the position n.
Definition splay_tpl.h:435
Element & front()
Get the first element.
Definition splay_tpl.h:507
~SplayTree()
Class destructor.
Definition splay_tpl.h:425
void pushFront(const Element &e)
Add an element in the first position.
Definition splay_tpl.h:587
bool contains(const Element &e) const
Test if the tree contains the element.
Definition splay_tpl.h:787
SplayBinaryNode< Element > * root
Root of the tree.
Definition splay.h:434
SplayTree()
Basic constructor, make an empty splay tree.
Definition splay_tpl.h:376
void popBack()
Remove the last element.
Definition splay_tpl.h:564
void popFront()
Remove the first element.
Definition splay_tpl.h:541
Size size() const
The number of elements in the tree.
Definition splay_tpl.h:779
SplayTree< Element > & operator=(const SplayTree< Element > &from)
Assignment operator.
Definition splay_tpl.h:405
Element & back()
Get the last element.
Definition splay_tpl.h:524
void join(const SplayTree< Element > &s)
Concatenation of two trees.
Definition splay_tpl.h:651
HashTable< Element, SplayBinaryNode< Element > * > addr
The hash table to find quickly the position of a node.
Definition splay.h:437
SplayTree< Element > split(const int i)
Divide the tree at the position.
Definition splay_tpl.h:677
void copy_(const SplayTree< Element > &)
a function used to perform copies
Definition splay_tpl.h:365
void insert(const Element &e)
Add an element to the tree.
Definition splay_tpl.h:625
void pushBack(const Element &e)
Add an element in the last position.
Definition splay_tpl.h:606
SplayTree< Element > split_by_val(const Element &e)
Divide the tree at the position.
Definition splay_tpl.h:741
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
Template implementation of splay trees.