aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
gum::SplayTree< Element > Class Template Reference

A splay tree. More...

#include <agrum/tools/core/splay.h>

+ Collaboration diagram for gum::SplayTree< Element >:

Public Member Functions

Constructors / Destructors
 SplayTree ()
 Basic constructor, make an empty splay tree. More...
 
 SplayTree (const Element &e)
 Basic constructor, make a splay tree with one element. More...
 
 SplayTree (const SplayTree &from)
 Copy constructor. More...
 
 ~SplayTree ()
 Class destructor. More...
 
Operators
SplayTree< Element > & operator= (const SplayTree< Element > &from)
 Assignment operator. More...
 
Methods
Element & operator[] (const unsigned int i)
 Get the element at the position n. More...
 
const Element & operator[] (const unsigned int i) const
 Get the element at the position n. More...
 
Element & front ()
 Get the first element. More...
 
Element & back ()
 Get the last element. More...
 
void popFront ()
 Remove the first element. More...
 
void popBack ()
 Remove the last element. More...
 
void pushFront (const Element &e)
 Add an element in the first position. More...
 
void pushBack (const Element &e)
 Add an element in the last position. More...
 
void insert (const Element &e)
 Add an element to the tree. More...
 
void join (const SplayTree< Element > &s)
 Concatenation of two trees. More...
 
SplayTree< Element > split (const int i)
 Divide the tree at the position. More...
 
SplayTree< Element > split_by_val (const Element &e)
 Divide the tree at the position. More...
 
Size size () const
 The number of elements in the tree. More...
 
bool contains (const Element &e) const
 Test if the tree contains the element. More...
 

Protected Attributes

Data Menbers
SplayBinaryNode< Element > * root
 Root of the tree. More...
 
HashTable< Element, SplayBinaryNode< Element > *> addr
 The hash table to find quickly the position of a node. More...
 

Protected Member Functions

void copy_ (const SplayTree< Element > &)
 a function used to perform copies More...
 

Friends

std::ostream & operator<< (std::ostream &out, const SplayTree< Element > &)
 Friendly to display. More...
 

Detailed Description

template<class Element>
class gum::SplayTree< Element >

A splay tree.

Warning
an Element must be in just one Splay Tree, the behavior is unspecified else.
Usage example:
// Creating empty trees
SplayTree<string> a;
// Make a copy
SplayTree<string> b(a);
// Add an element
a.insert("toto");
a.insert("titi");
// Get the first element
a.front();
// And the last
a.back();
// concatenate two trees
a.join(b);
// divide a tree at the third position, the first part is placed into a
// the second into b.
b = a.split(3);
// Display the splay tree
a.printTree();
// Get the size
a.size();
Template Parameters
ElementThe elements type.

Definition at line 43 of file splay.h.

Constructor & Destructor Documentation

◆ SplayTree() [1/3]

template<class Element >
INLINE gum::SplayTree< Element >::SplayTree ( )

Basic constructor, make an empty splay tree.

Definition at line 365 of file splay_tpl.h.

References gum::Set< Key, Alloc >::emplace().

365  : root(0), addr() {
366  // for debugging purposes
367  GUM_CONSTRUCTOR(SplayTree);
368  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:417
SplayTree()
Basic constructor, make an empty splay tree.
Definition: splay_tpl.h:365
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:420
+ Here is the call graph for this function:

◆ SplayTree() [2/3]

template<class Element >
INLINE gum::SplayTree< Element >::SplayTree ( const Element &  e)

Basic constructor, make a splay tree with one element.

Parameters
eThe element of the tree.

Definition at line 376 of file splay_tpl.h.

References gum::Set< Key, Alloc >::emplace().

376  : root(0), addr() {
377  root = new SplayBinaryNode< Element >(e, addr);
378  // for debugging purposes
379  GUM_CONSTRUCTOR(SplayTree);
380  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:417
SplayTree()
Basic constructor, make an empty splay tree.
Definition: splay_tpl.h:365
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:420
+ Here is the call graph for this function:

◆ SplayTree() [3/3]

template<class Element >
INLINE gum::SplayTree< Element >::SplayTree ( const SplayTree< Element > &  from)

Copy constructor.

Parameters
fromThe gum::SplayTree to copy.

Definition at line 385 of file splay_tpl.h.

References gum::Set< Key, Alloc >::emplace().

385  : addr() {
386  copy_(from);
387  // for debugging purposes
388  GUM_CONS_CPY(SplayTree);
389  }
void copy_(const SplayTree< Element > &)
a function used to perform copies
Definition: splay_tpl.h:354
SplayTree()
Basic constructor, make an empty splay tree.
Definition: splay_tpl.h:365
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:420
+ Here is the call graph for this function:

◆ ~SplayTree()

template<class Element >
INLINE gum::SplayTree< Element >::~SplayTree ( )

Class destructor.

Definition at line 415 of file splay_tpl.h.

References gum::Set< Key, Alloc >::emplace().

415  {
416  // for debugging purposes
417  GUM_DESTRUCTOR(SplayTree);
418 
419  if (root) delete (root);
420  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:417
SplayTree()
Basic constructor, make an empty splay tree.
Definition: splay_tpl.h:365
+ Here is the call graph for this function:

Member Function Documentation

◆ back()

template<class Element >
INLINE Element & gum::SplayTree< Element >::back ( )

Get the last element.

Exceptions
NotFoundRaised if the splay tree is empty.

Definition at line 518 of file splay_tpl.h.

References gum::Set< Key, Alloc >::emplace().

518  {
519  SplayBinaryNode< Element >* act = root;
520 
521  if (!root) { GUM_ERROR(NotFound, "The splay tree is empty"); }
522 
523  if (act->fd)
524  for (; act->fd; act = act->fd)
525  ;
526 
527  root = act->splay();
528 
529  return root->elt;
530  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:417
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ contains()

template<class Element >
INLINE bool gum::SplayTree< Element >::contains ( const Element &  e) const

Test if the tree contains the element.

Parameters
eThe element to test if it is in the splay tree.
Returns
Returns true if e is in this splay tree.

Definition at line 787 of file splay_tpl.h.

References gum::Set< Key, Alloc >::emplace().

787  {
788  return addr.exists(e);
789  }
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:420
+ Here is the call graph for this function:

◆ copy_()

template<class Element >
INLINE void gum::SplayTree< Element >::copy_ ( const SplayTree< Element > &  from)
protected

a function used to perform copies

Definition at line 354 of file splay_tpl.h.

References gum::Set< Key, Alloc >::emplace().

354  {
355  if (from.root) {
356  root = new SplayBinaryNode< Element >(*from.root, addr);
357  } else {
358  root = 0;
359  }
360  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:417
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:420
+ Here is the call graph for this function:

◆ front()

template<class Element >
INLINE Element & gum::SplayTree< Element >::front ( )

Get the first element.

Exceptions
NotFoundRaised if the splay tree is empty.

Definition at line 501 of file splay_tpl.h.

References gum::Set< Key, Alloc >::emplace().

501  {
502  SplayBinaryNode< Element >* act = root;
503 
504  if (!root) { GUM_ERROR(NotFound, "The splay tree is empty"); }
505 
506  if (act->fg)
507  for (; act->fg; act = act->fg)
508  ;
509 
510  root = act->splay();
511 
512  return root->elt;
513  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:417
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ insert()

template<class Element >
INLINE void gum::SplayTree< Element >::insert ( const Element &  e)

Add an element to the tree.

Parameters
eThe element to add.

Definition at line 619 of file splay_tpl.h.

References gum::Set< Key, Alloc >::emplace().

619  {
620  SplayBinaryNode< Element >* act = root;
621 
622  if (!root) {
623  root = new SplayBinaryNode< Element >(e, addr);
624  } else {
625  while (act->fd) {
626  ++act->size;
627  act = act->fd;
628  }
629 
630  // act->fd == nullptr
631  act->fd = new SplayBinaryNode< Element >(e, addr, 0, 0, act);
632 
633  ++act->size;
634 
635  root = act->fd->splay();
636  }
637  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:417
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:420
+ Here is the call graph for this function:

◆ join()

template<class Element >
INLINE void gum::SplayTree< Element >::join ( const SplayTree< Element > &  s)

Concatenation of two trees.

Parameters
sThe tree to add.

Definition at line 645 of file splay_tpl.h.

References gum::Set< Key, Alloc >::emplace().

645  {
646  if (s.root) {
647  if (root) {
648  root = root->join(s.root, addr);
649  } else {
650  root = new SplayBinaryNode< Element >(*s.root, addr);
651  }
652  }
653  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:417
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:420
+ Here is the call graph for this function:

◆ operator=()

template<class Element >
INLINE SplayTree< Element > & gum::SplayTree< Element >::operator= ( const SplayTree< Element > &  from)

Assignment operator.

Parameters
fromThe gum::SplayTree to copy.
Returns
This gum::SplayTree.

Definition at line 395 of file splay_tpl.h.

References gum::Set< Key, Alloc >::emplace().

395  {
396  // avoid self assignment
397  if (this != &from) {
398  // for debugging purposes
399  GUM_OP_CPY(SplayTree);
400  // delete the old contents
401 
402  if (root) delete root;
403 
404  addr.clear();
405 
406  copy_(from);
407  }
408 
409  return *this;
410  }
void copy_(const SplayTree< Element > &)
a function used to perform copies
Definition: splay_tpl.h:354
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:417
SplayTree()
Basic constructor, make an empty splay tree.
Definition: splay_tpl.h:365
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:420
+ Here is the call graph for this function:

◆ operator[]() [1/2]

template<class Element >
Element & gum::SplayTree< Element >::operator[] ( const unsigned int  i)

Get the element at the position n.

Parameters
iThe position of the element to return.
Exceptions
NotFoundRaised if no element was found.
Returns
Returns the element at the position n.

Definition at line 425 of file splay_tpl.h.

References gum::Set< Key, Alloc >::emplace().

425  {
426  int val = i;
427 
428  if (!root) {
429  GUM_ERROR(NotFound, "The tree is empty !");
430  } else if (val >= root->size) {
431  GUM_ERROR(NotFound, "The index is too large !");
432  } else {
433  // The element exists
434  // Find it
435  SplayBinaryNode< Element >* act = root;
436  int pos_act = val - 1;
437  bool next = true;
438 
439  while (next) {
440  if (!act->fg)
441  pos_act = 0;
442  else
443  pos_act = act->fg->size;
444 
445  if (pos_act > val) {
446  act = act->fg;
447  } else if (pos_act < val) {
448  act = act->fd;
449  val -= (pos_act + 1);
450  } else {
451  next = false;
452  }
453  }
454 
455  root = act->splay();
456 
457  return root->elt;
458  }
459  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:417
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ operator[]() [2/2]

template<class Element >
const Element & gum::SplayTree< Element >::operator[] ( const unsigned int  i) const

Get the element at the position n.

Parameters
iThe position of the element to return.
Exceptions
NotFoundRaised if no element was found.
Returns
Returns the element at the position n.

Definition at line 462 of file splay_tpl.h.

References gum::Set< Key, Alloc >::emplace().

462  {
463  int val = i;
464 
465  if (!root) {
466  GUM_ERROR(NotFound, "The tree is empty !");
467  } else if (val >= root->size) {
468  GUM_ERROR(NotFound, "The index is too large !");
469  } else {
470  // The element exists
471  // Find it
472  SplayBinaryNode< Element >* act = root;
473  int pos_act = val - 1;
474  bool next = true;
475 
476  while (next) {
477  if (!act->fg)
478  pos_act = 0;
479  else
480  pos_act = act->fg->size;
481 
482  if (pos_act > val) {
483  act = act->fg;
484  } else if (pos_act < val) {
485  act = act->fd;
486  val -= (pos_act + 1);
487  } else {
488  next = false;
489  }
490  }
491 
492  root = act->splay();
493 
494  return root->elt;
495  }
496  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:417
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ popBack()

template<class Element >
INLINE void gum::SplayTree< Element >::popBack ( )

Remove the last element.

Definition at line 558 of file splay_tpl.h.

References gum::Set< Key, Alloc >::emplace().

558  {
559  SplayBinaryNode< Element >* act = root;
560 
561  if (root) {
562  if (root->fd)
563  for (; act->fd; act = act->fd)
564  ;
565 
566  act = act->splay();
567 
568  root = act->fg;
569 
570  if (root) root->pere = 0;
571 
572  act->fg = 0;
573 
574  delete act;
575  }
576  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:417
+ Here is the call graph for this function:

◆ popFront()

template<class Element >
INLINE void gum::SplayTree< Element >::popFront ( )

Remove the first element.

Definition at line 535 of file splay_tpl.h.

References gum::Set< Key, Alloc >::emplace().

535  {
536  SplayBinaryNode< Element >* act = root;
537 
538  if (root) {
539  if (root->fg)
540  for (; act->fg; act = act->fg)
541  ;
542 
543  act = act->splay();
544 
545  root = act->fd;
546 
547  if (root) root->pere = 0;
548 
549  act->fd = 0;
550 
551  delete act;
552  }
553  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:417
+ Here is the call graph for this function:

◆ pushBack()

template<class Element >
INLINE void gum::SplayTree< Element >::pushBack ( const Element &  e)

Add an element in the last position.

Parameters
eThe element to push.

Definition at line 600 of file splay_tpl.h.

References gum::Set< Key, Alloc >::emplace().

600  {
601  SplayBinaryNode< Element >* act = root;
602 
603  if (root) {
604  if (root->fd)
605  for (; act->fd; act = act->fd)
606  ;
607 
608  root = act->splay();
609 
610  root->fd = new SplayBinaryNode< Element >(e, addr, 0, 0, root);
611  } else {
612  root = new SplayBinaryNode< Element >(e, addr);
613  }
614  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:417
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:420
+ Here is the call graph for this function:

◆ pushFront()

template<class Element >
INLINE void gum::SplayTree< Element >::pushFront ( const Element &  e)

Add an element in the first position.

Parameters
eThe element to push.

Definition at line 581 of file splay_tpl.h.

References gum::Set< Key, Alloc >::emplace().

581  {
582  SplayBinaryNode< Element >* act = root;
583 
584  if (root) {
585  if (root->fg)
586  for (; act->fg; act = act->fg)
587  ;
588 
589  root = act->splay();
590 
591  root->fg = new SplayBinaryNode< Element >(e, addr, 0, 0, root);
592  } else {
593  root = new SplayBinaryNode< Element >(e, addr);
594  }
595  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:417
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:420
+ Here is the call graph for this function:

◆ size()

template<class Element >
INLINE Size gum::SplayTree< Element >::size ( ) const

The number of elements in the tree.

Returns
the size of the tree

Definition at line 777 of file splay_tpl.h.

References gum::Set< Key, Alloc >::emplace().

777  {
778  if (root)
779  return root->size;
780  else
781  return Size(0);
782  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:417
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
+ Here is the call graph for this function:

◆ split()

template<class Element >
INLINE SplayTree< Element > gum::SplayTree< Element >::split ( const int  i)

Divide the tree at the position.

Warning
all the elements less than e are stored into the tree and those who are greater than e in the returned tree.
Parameters
iThe position of the element (e) for split.
Returns
Returns a tree with all the elements greater than i.

Definition at line 672 of file splay_tpl.h.

References gum::Set< Key, Alloc >::emplace().

672  {
673  GUM_ASSERT(i >= 0 && i < root->size);
674  GUM_ASSERT(root != 0);
675 
676  if (i == 0) {
677  // the tree will be empty
678  // the returned tree is a copy of the present tree
679  SplayTree< Element > s = *this;
680  addr.clear();
681  delete root;
682  root = 0;
683  return s;
684  } else if (i == root->size - 1) {
685  SplayTree< Element > s;
686  return s;
687  } else {
688  // We will find the node at position i
689  SplayBinaryNode< Element >* act = root;
690  int pos = root->position();
691 
692  while (pos != i) {
693  GUM_ASSERT(act != 0);
694 
695  if (i < pos) {
696  act = act->fg;
697  } else {
698  act = act->fd;
699  }
700 
701  pos = act->position();
702  }
703 
704  // We reorganize the tree
705  act->splay();
706 
707  // We take the first part
708  root = act->fg;
709 
710  if (root) root->pere = 0;
711 
712  // We take the second part
713  SplayTree< Element > s;
714 
715  s.root = act;
716 
717  s.root->fg = 0;
718 
719  Size size_ = 1;
720 
721  if (s.root->fd) size_ += s.root->fd->size;
722 
723  s.root->size = size_;
724 
725  // We remove the old nodes in the hash table
726  // Complexity O(n) => very expensive
727  removeInfo(act, addr);
728 
729  return s;
730  }
731  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:417
Size size() const
The number of elements in the tree.
Definition: splay_tpl.h:777
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
static INLINE void removeInfo(const SplayBinaryNode< Element > *e, HashTable< Element, SplayBinaryNode< Element > * > &addr)
Definition: splay_tpl.h:659
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:420
+ Here is the call graph for this function:

◆ split_by_val()

template<typename Element >
INLINE SplayTree< Element > gum::SplayTree< Element >::split_by_val ( const Element &  e)

Divide the tree at the position.

Parameters
ethe element for split
Warning
All the elements less than e are stored into the tree and those greater than e are in the returned tree
The element e is neither in the trees.
Returns
Returns the tree with all value greather than e.

Definition at line 737 of file splay_tpl.h.

References gum::Set< Key, Alloc >::emplace().

737  {
738  GUM_ASSERT(root != 0);
739 
740  if (!addr.exists(e)) {
741  GUM_ERROR(NotFound, "not enough elements in the splay tree");
742  }
743 
744  // We will find the node at position i
745  SplayBinaryNode< Element >* act = addr[e];
746 
747  // We reorganize the tree
748  act->splay();
749 
750  // We take the two parts
751  SplayTree< Element > s;
752 
753  s.root = act->fd;
754 
755  if (s.root) { s.root->pere = 0; }
756 
757  root = act->fg;
758 
759  if (root) root->pere = 0;
760 
761  act->fg = 0;
762 
763  // We remove the old nodes in the hash table
764  // Complexity O(n) => very expensive
765  removeInfo(act, addr);
766 
767  act->fd = 0;
768 
769  delete act;
770 
771  return s;
772  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:417
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
static INLINE void removeInfo(const SplayBinaryNode< Element > *e, HashTable< Element, SplayBinaryNode< Element > * > &addr)
Definition: splay_tpl.h:659
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:420
+ Here is the call graph for this function:

Friends And Related Function Documentation

◆ operator<<

template<class Element >
std::ostream& operator<< ( std::ostream &  out,
const SplayTree< Element > &  s 
)
friend

Friendly to display.

Definition at line 808 of file splay_tpl.h.

809  {
810  out << "|[";
811 
812  if (s.root) out << *s.root;
813 
814  out << "]|";
815 
816  return out;
817  }

Member Data Documentation

◆ addr

template<class Element >
HashTable< Element, SplayBinaryNode< Element >* > gum::SplayTree< Element >::addr
protected

The hash table to find quickly the position of a node.

Definition at line 420 of file splay.h.

◆ root

template<class Element >
SplayBinaryNode< Element >* gum::SplayTree< Element >::root
protected

Root of the tree.

Definition at line 417 of file splay.h.


The documentation for this class was generated from the following files: