aGrUM  0.20.3
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 363 of file splay_tpl.h.

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

363  : root(0), addr() {
364  // for debugging purposes
365  GUM_CONSTRUCTOR(SplayTree);
366  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:413
SplayTree()
Basic constructor, make an empty splay tree.
Definition: splay_tpl.h:363
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:416
+ 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 374 of file splay_tpl.h.

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

374  : root(0), addr() {
375  root = new SplayBinaryNode< Element >(e, addr);
376  // for debugging purposes
377  GUM_CONSTRUCTOR(SplayTree);
378  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:413
SplayTree()
Basic constructor, make an empty splay tree.
Definition: splay_tpl.h:363
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:416
+ 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 383 of file splay_tpl.h.

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

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

◆ ~SplayTree()

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

Class destructor.

Definition at line 412 of file splay_tpl.h.

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

412  {
413  // for debugging purposes
414  GUM_DESTRUCTOR(SplayTree);
415 
416  if (root) delete (root);
417  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:413
SplayTree()
Basic constructor, make an empty splay tree.
Definition: splay_tpl.h:363
+ 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 515 of file splay_tpl.h.

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

515  {
516  SplayBinaryNode< Element >* act = root;
517 
518  if (!root) { GUM_ERROR(NotFound, "The splay tree is empty") }
519 
520  if (act->fd)
521  for (; act->fd; act = act->fd)
522  ;
523 
524  root = act->splay();
525 
526  return root->elt;
527  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:413
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ 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 780 of file splay_tpl.h.

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

780  {
781  return addr.exists(e);
782  }
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:416
+ 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 352 of file splay_tpl.h.

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

352  {
353  if (from.root) {
354  root = new SplayBinaryNode< Element >(*from.root, addr);
355  } else {
356  root = 0;
357  }
358  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:413
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:416
+ 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 498 of file splay_tpl.h.

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

498  {
499  SplayBinaryNode< Element >* act = root;
500 
501  if (!root) { GUM_ERROR(NotFound, "The splay tree is empty") }
502 
503  if (act->fg)
504  for (; act->fg; act = act->fg)
505  ;
506 
507  root = act->splay();
508 
509  return root->elt;
510  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:413
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ 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 616 of file splay_tpl.h.

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

616  {
617  SplayBinaryNode< Element >* act = root;
618 
619  if (!root) {
620  root = new SplayBinaryNode< Element >(e, addr);
621  } else {
622  while (act->fd) {
623  ++act->size;
624  act = act->fd;
625  }
626 
627  // act->fd == nullptr
628  act->fd = new SplayBinaryNode< Element >(e, addr, 0, 0, act);
629 
630  ++act->size;
631 
632  root = act->fd->splay();
633  }
634  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:413
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:416
+ 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 642 of file splay_tpl.h.

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

642  {
643  if (s.root) {
644  if (root) {
645  root = root->join(s.root, addr);
646  } else {
647  root = new SplayBinaryNode< Element >(*s.root, addr);
648  }
649  }
650  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:413
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:416
+ 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 392 of file splay_tpl.h.

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

392  {
393  // avoid self assignment
394  if (this != &from) {
395  // for debugging purposes
396  GUM_OP_CPY(SplayTree);
397  // delete the old contents
398 
399  if (root) delete root;
400 
401  addr.clear();
402 
403  copy_(from);
404  }
405 
406  return *this;
407  }
void copy_(const SplayTree< Element > &)
a function used to perform copies
Definition: splay_tpl.h:352
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:413
SplayTree()
Basic constructor, make an empty splay tree.
Definition: splay_tpl.h:363
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:416
+ 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 422 of file splay_tpl.h.

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

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

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

459  {
460  int val = i;
461 
462  if (!root) {
463  GUM_ERROR(NotFound, "The tree is empty !")
464  } else if (val >= root->size) {
465  GUM_ERROR(NotFound, "The index is too large !")
466  } else {
467  // The element exists
468  // Find it
469  SplayBinaryNode< Element >* act = root;
470  int pos_act = val - 1;
471  bool next = true;
472 
473  while (next) {
474  if (!act->fg)
475  pos_act = 0;
476  else
477  pos_act = act->fg->size;
478 
479  if (pos_act > val) {
480  act = act->fg;
481  } else if (pos_act < val) {
482  act = act->fd;
483  val -= (pos_act + 1);
484  } else {
485  next = false;
486  }
487  }
488 
489  root = act->splay();
490 
491  return root->elt;
492  }
493  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:413
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ 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 555 of file splay_tpl.h.

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

555  {
556  SplayBinaryNode< Element >* act = root;
557 
558  if (root) {
559  if (root->fd)
560  for (; act->fd; act = act->fd)
561  ;
562 
563  act = act->splay();
564 
565  root = act->fg;
566 
567  if (root) root->pere = 0;
568 
569  act->fg = 0;
570 
571  delete act;
572  }
573  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:413
+ 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 532 of file splay_tpl.h.

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

532  {
533  SplayBinaryNode< Element >* act = root;
534 
535  if (root) {
536  if (root->fg)
537  for (; act->fg; act = act->fg)
538  ;
539 
540  act = act->splay();
541 
542  root = act->fd;
543 
544  if (root) root->pere = 0;
545 
546  act->fd = 0;
547 
548  delete act;
549  }
550  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:413
+ 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 597 of file splay_tpl.h.

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

597  {
598  SplayBinaryNode< Element >* act = root;
599 
600  if (root) {
601  if (root->fd)
602  for (; act->fd; act = act->fd)
603  ;
604 
605  root = act->splay();
606 
607  root->fd = new SplayBinaryNode< Element >(e, addr, 0, 0, root);
608  } else {
609  root = new SplayBinaryNode< Element >(e, addr);
610  }
611  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:413
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:416
+ 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 578 of file splay_tpl.h.

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

578  {
579  SplayBinaryNode< Element >* act = root;
580 
581  if (root) {
582  if (root->fg)
583  for (; act->fg; act = act->fg)
584  ;
585 
586  root = act->splay();
587 
588  root->fg = new SplayBinaryNode< Element >(e, addr, 0, 0, root);
589  } else {
590  root = new SplayBinaryNode< Element >(e, addr);
591  }
592  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:413
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:416
+ 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 770 of file splay_tpl.h.

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

770  {
771  if (root)
772  return root->size;
773  else
774  return Size(0);
775  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:413
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 668 of file splay_tpl.h.

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

668  {
669  GUM_ASSERT(i >= 0 && i < root->size);
670  GUM_ASSERT(root != 0);
671 
672  if (i == 0) {
673  // the tree will be empty
674  // the returned tree is a copy of the present tree
675  SplayTree< Element > s = *this;
676  addr.clear();
677  delete root;
678  root = 0;
679  return s;
680  } else if (i == root->size - 1) {
681  SplayTree< Element > s;
682  return s;
683  } else {
684  // We will find the node at position i
685  SplayBinaryNode< Element >* act = root;
686  int pos = root->position();
687 
688  while (pos != i) {
689  GUM_ASSERT(act != 0);
690 
691  if (i < pos) {
692  act = act->fg;
693  } else {
694  act = act->fd;
695  }
696 
697  pos = act->position();
698  }
699 
700  // We reorganize the tree
701  act->splay();
702 
703  // We take the first part
704  root = act->fg;
705 
706  if (root) root->pere = 0;
707 
708  // We take the second part
709  SplayTree< Element > s;
710 
711  s.root = act;
712 
713  s.root->fg = 0;
714 
715  Size size_ = 1;
716 
717  if (s.root->fd) size_ += s.root->fd->size;
718 
719  s.root->size = size_;
720 
721  // We remove the old nodes in the hash table
722  // Complexity O(n) => very expensive
723  removeInfo(act, addr);
724 
725  return s;
726  }
727  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:413
Size size() const
The number of elements in the tree.
Definition: splay_tpl.h:770
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:655
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:416
+ 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 732 of file splay_tpl.h.

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

732  {
733  GUM_ASSERT(root != 0);
734 
735  if (!addr.exists(e)) { GUM_ERROR(NotFound, "not enough elements in the splay tree") }
736 
737  // We will find the node at position i
738  SplayBinaryNode< Element >* act = addr[e];
739 
740  // We reorganize the tree
741  act->splay();
742 
743  // We take the two parts
744  SplayTree< Element > s;
745 
746  s.root = act->fd;
747 
748  if (s.root) { s.root->pere = 0; }
749 
750  root = act->fg;
751 
752  if (root) root->pere = 0;
753 
754  act->fg = 0;
755 
756  // We remove the old nodes in the hash table
757  // Complexity O(n) => very expensive
758  removeInfo(act, addr);
759 
760  act->fd = 0;
761 
762  delete act;
763 
764  return s;
765  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:413
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
static INLINE void removeInfo(const SplayBinaryNode< Element > *e, HashTable< Element, SplayBinaryNode< Element > * > &addr)
Definition: splay_tpl.h:655
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:416
+ 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 800 of file splay_tpl.h.

800  {
801  out << "|[";
802 
803  if (s.root) out << *s.root;
804 
805  out << "]|";
806 
807  return out;
808  }

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 416 of file splay.h.

◆ root

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

Root of the tree.

Definition at line 413 of file splay.h.


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