aGrUM  0.14.2
splay.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
27 #ifndef GUM_SPLAY_H
28 #define GUM_SPLAY_H
29 
30 #include <iostream>
31 //#include <stdlib.h>
32 
33 #include <agrum/agrum.h>
34 #include <agrum/core/hashTable.h>
35 
36 namespace gum {
37 
38  template < class Element >
40  template < class Element >
41  class SplayTree;
42 
44 
45  template < typename Element >
46  INLINE std::ostream& operator<<(std::ostream& out,
48 
50 
51  template < typename Element >
52  INLINE std::ostream& operator<<(std::ostream& out,
53  const SplayTree< Element >& s);
54 
55  // =========================================================================
56  // === NODE ===
57  // =========================================================================
69  template < class Element >
70  class SplayBinaryNode {
71  public:
72  // ============================================================================
74  // ============================================================================
76 
81  int position() const;
82 
87  const Element& getElement() const;
88 
94  const SplayBinaryNode< Element >* getFg() const;
95 
101  const SplayBinaryNode< Element >* getFd() const;
102 
104 
105  protected:
106  // ============================================================================
108  // ============================================================================
110 
120  SplayBinaryNode(const Element& e,
121  HashTable< Element, SplayBinaryNode< Element >* >& addr,
122  SplayBinaryNode* g = 0,
123  SplayBinaryNode* d = 0,
124  SplayBinaryNode* p = 0);
125 
132  HashTable< Element, SplayBinaryNode< Element >* >& addr);
133 
139  void _copy(const SplayBinaryNode< Element >& from,
140  HashTable< Element, SplayBinaryNode< Element >* >& addr);
141 
146 
148  // ============================================================================
150  // ============================================================================
152 
158 
164 
170 
179  HashTable< Element, SplayBinaryNode< Element >* >& addr);
180 
182  // ============================================================================
184  // ============================================================================
186 
188  Element elt;
189 
192 
195 
198 
201 
203 
205  friend class SplayTree< Element >;
206 
208  friend std::ostream& operator<<<>(std::ostream& out,
210  };
211 
212  // ============================================================================
213  // === SPLAY TREE ===
214  // ============================================================================
258  template < class Element >
259  class SplayTree {
260  public:
261  // ============================================================================
263  // ============================================================================
265 
269  SplayTree();
270 
275  SplayTree(const Element& e);
276 
281  SplayTree(const SplayTree& from);
282 
286  ~SplayTree();
287 
289  // ============================================================================
291  // ============================================================================
293 
299  SplayTree< Element >& operator=(const SplayTree< Element >& from);
300 
302  // ============================================================================
304  // ============================================================================
306 
313  Element& operator[](const unsigned int i);
314 
321  const Element& operator[](const unsigned int i) const;
322 
323 
328  Element& front();
329 
334  Element& back();
335 
339  void popFront();
340 
344  void popBack();
345 
350  void pushFront(const Element& e);
351 
356  void pushBack(const Element& e);
357 
362  void insert(const Element& e);
363 
368  void join(const SplayTree< Element >& s);
369 
370 
380  SplayTree< Element > split(const int i);
381 
382 
391  SplayTree< Element > split_by_val(const Element& e);
392 
397  Size size() const;
398 
404  bool contains(const Element& e) const;
405 
407 
408  protected:
409  // ============================================================================
411  // ============================================================================
413 
416 
419 
421 
423  void _copy(const SplayTree< Element >&);
424 
426  friend std::ostream& operator<<<>(std::ostream& out,
427  const SplayTree< Element >&);
428  };
429 
430 } /* namespace gum */
431 
432 // always include the _tpl.h as it contains only templates
433 #include <agrum/core/splay_tpl.h>
434 
435 #endif /* GUM_SPLAY_H */
SplayBinaryNode * pere
The father, nullptr for the root.
Definition: splay.h:200
SplayBinaryNode< Element > * zag()
A left rotation, the node must hava a father.
Definition: splay_tpl.h:165
const Element & getElement() const
Returns the element in the node.
Definition: splay_tpl.h:317
const SplayBinaryNode< Element > * getFg() const
Returns the left child.
Definition: splay_tpl.h:328
SplayBinaryNode< Element > * splay()
A splay rotation, the node will be the root of the tree.
Definition: splay_tpl.h:215
Template implementation of splay trees.
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:70
~SplayBinaryNode()
Class destructor.
Definition: splay_tpl.h:101
void _copy(const SplayBinaryNode< Element > &from, HashTable< Element, SplayBinaryNode< Element > * > &addr)
A function used to perform copies.
Definition: splay_tpl.h:38
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
std::vector< std::string > split(const std::string &str, const std::string &delim)
Split str using the delimiter.
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:415
the nodes of splay trees
Definition: splay.h:39
The class for generic Hash Tables.
Definition: hashTable.h:676
Size size
The size of the sub-tree.
Definition: splay.h:191
SplayBinaryNode * fg
The left child.
Definition: splay.h:194
Element elt
The content.
Definition: splay.h:188
std::ostream & operator<<(std::ostream &output, const BayesNet< GUM_SCALAR > &bn)
Prints map&#39;s DAG in output using the Graphviz-dot format.
Definition: BayesNet_tpl.h:583
A splay tree.
Definition: splay.h:41
const SplayBinaryNode< Element > * getFd() const
Returns the right child.
Definition: splay_tpl.h:339
SplayBinaryNode< Element > * join(const SplayBinaryNode< Element > *e, HashTable< Element, SplayBinaryNode< Element > * > &addr)
Concatenation of two trees.
Definition: splay_tpl.h:260
SplayBinaryNode * fd
The right child.
Definition: splay.h:197
SplayBinaryNode< Element > * zig()
A right rotation, the node must have a father.
Definition: splay_tpl.h:114
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
int position() const
Position of the node.
Definition: splay_tpl.h:290
Class hash tables iterators.
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:418