foobar2000 SDK  2015-08-03
Public Types | Public Member Functions | Static Public Member Functions | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes
pfc::avltree_t< t_storage, t_comparator >

#include <avltree.h>

Public Types

typedef pfc::const_iterator< t_storage > const_iterator
 
typedef pfc::iterator< t_storage > iterator
 
typedef t_storage t_item
 
typedef avltree_t< t_storage, t_comparator > t_self
 

Public Member Functions

 avltree_t ()
 
 avltree_t (const t_self &p_other)
 
template<typename t_other >
 avltree_t (const t_other &p_other)
 
 ~avltree_t ()
 
template<typename t_callback >
void _enumerate_var (t_callback &p_callback)
 
iterator _first_var ()
 
iterator _last_var ()
 
template<typename t_param >
t_storage & add (const t_param &p_item)
 
template<typename t_param >
t_storage & add_ex (const t_param &p_item, bool &p_isnew)
 
template<typename t_param >
t_storage & add_item (t_param const &p_item)
 
template<typename t_param >
bool add_item_check (t_param const &item)
 
template<typename t_param >
t_storage & add_item_ex (t_param const &p_item, bool &p_isnew)
 
template<typename t_param >
bool contains (const t_param &p_item) const
 
template<typename t_callback >
void enumerate (t_callback &p_callback) const
 
template<typename t_param >
bool exists (t_param const &p_item) const
 
template<typename t_param >
const_iterator find (t_param const &item) const
 
template<typename t_param >
iterator find (t_param const &item)
 
template<typename t_param >
const t_storage * find_item_ptr (t_param const &p_item) const
 
template<typename t_param >
t_storage * find_item_ptr (t_param const &p_item)
 
template<bool inclusive, bool above, typename t_search >
const t_storage * find_nearest_item (const t_search &p_search) const
 
template<bool inclusive, bool above, typename t_search >
t_storage * find_nearest_item (const t_search &p_search)
 
template<typename t_param >
const t_storage * find_ptr (t_param const &p_item) const
 
template<typename t_param >
t_storage * find_ptr (t_param const &p_item)
 
const_iterator first () const throw ()
 
t_size get_count () const throw ()
 
template<typename t_param >
bool get_first (t_param &p_item) const throw ()
 
template<typename t_param >
bool get_last (t_param &p_item) const throw ()
 
template<typename t_param >
bool have_item (const t_param &p_item) const
 
template<typename t_param >
iterator insert (const t_param &p_item)
 
const_iterator last () const throw ()
 
bool operator!= (const t_self &other) const
 
template<typename t_param >
t_selfoperator+= (const t_param &p_item)
 
template<typename t_param >
t_selfoperator-= (const t_param &p_item)
 
const t_selfoperator= (const t_self &p_other)
 
template<typename t_other >
const t_selfoperator= (const t_other &p_other)
 
bool operator== (const t_self &other) const
 
bool remove (const_iterator const &iter)
 
void remove_all () throw ()
 
template<typename t_param >
bool remove_item (t_param const &p_item)
 
void reset ()
 
template<typename t_param >
void set_item (const t_param &p_item)
 

Static Public Member Functions

static bool equals (const t_self &v1, const t_self &v2)
 

Private Types

typedef _avltree_node< t_storage > t_node
 
typedef refcounted_object_ptr_t< t_nodet_nodeptr
 
typedef t_node::t_ptr t_nodeptr
 
typedef t_nodet_noderawptr
 
typedef t_node::t_rawptr t_noderawptr
 

Private Member Functions

void __copy (const t_self &p_other)
 
template<bool inclusive, bool above, typename t_search >
t_storage * __find_nearest (const t_search &p_search) const
 
template<typename t_param >
t_storage * _find_item_ptr (t_param const &p_item) const
 
template<typename t_param >
t_node_find_node_ptr (t_param const &p_item) const
 
t_node_firstlast (bool which) const throw ()
 

Static Private Member Functions

static t_nodeptr __copy_recur (t_node *p_source, t_node *parent)
 
template<typename t_nodewalk , typename t_callback >
static void __enum_items_recur (t_nodewalk *p_node, t_callback &p_callback)
 
static void _unlink_recur (t_nodeptr &node)
 
static void assert_children (t_nodeptr ptr)
 
static t_size calc_count (const t_node *p_node) throw ()
 
static t_size calc_depth (const t_nodeptr &ptr)
 
template<typename t_item1 , typename t_item2 >
static int compare (const t_item1 &p_item1, const t_item2 &p_item2)
 
static t_nodeptr extract_left_leaf (t_nodeptr &p_base)
 
static t_nodeptr extract_right_leaf (t_nodeptr &p_base)
 
template<typename t_search >
static t_storage * g_find_or_add (t_nodeptr &p_base, t_node *parent, t_search const &p_search, bool &p_new)
 
template<typename t_search >
static t_nodeg_find_or_add_node (t_nodeptr &p_base, t_node *parent, t_search const &p_search, bool &p_new)
 
static void g_rebalance (t_nodeptr &p_node)
 
template<typename t_search >
static bool g_remove (t_nodeptr &p_node, t_search const &p_search)
 
static void g_rotate_left (t_nodeptr &oldroot)
 
static void g_rotate_right (t_nodeptr &oldroot)
 
static bool is_ptr_valid (t_nodeptr const &p)
 
static bool is_ptr_valid (t_node const *p)
 
static void recalc_depth (t_nodeptr const &ptr)
 
static void remove_internal (t_nodeptr &p_node)
 
static void selftest (t_nodeptr const &p_node)
 
static t_ssize test_depth (t_nodeptr const &ptr)
 

Private Attributes

t_nodeptr m_root
 

Detailed Description

template<typename t_storage, typename t_comparator = comparator_default>
class pfc::avltree_t< t_storage, t_comparator >

Definition at line 73 of file avltree.h.

Member Typedef Documentation

template<typename t_storage, typename t_comparator = comparator_default>
typedef pfc::const_iterator<t_storage> pfc::avltree_t< t_storage, t_comparator >::const_iterator

Definition at line 76 of file avltree.h.

template<typename t_storage, typename t_comparator = comparator_default>
typedef pfc::iterator<t_storage> pfc::avltree_t< t_storage, t_comparator >::iterator

Definition at line 77 of file avltree.h.

template<typename t_storage, typename t_comparator = comparator_default>
typedef t_storage pfc::avltree_t< t_storage, t_comparator >::t_item

Definition at line 78 of file avltree.h.

template<typename t_storage, typename t_comparator = comparator_default>
typedef _avltree_node<t_storage> pfc::avltree_t< t_storage, t_comparator >::t_node
private

Definition at line 80 of file avltree.h.

template<typename t_storage, typename t_comparator = comparator_default>
typedef refcounted_object_ptr_t<t_node> pfc::avltree_t< t_storage, t_comparator >::t_nodeptr
private

Definition at line 82 of file avltree.h.

template<typename t_storage, typename t_comparator = comparator_default>
typedef t_node::t_ptr pfc::avltree_t< t_storage, t_comparator >::t_nodeptr
private

Definition at line 85 of file avltree.h.

template<typename t_storage, typename t_comparator = comparator_default>
typedef t_node* pfc::avltree_t< t_storage, t_comparator >::t_noderawptr
private

Definition at line 83 of file avltree.h.

template<typename t_storage, typename t_comparator = comparator_default>
typedef t_node::t_rawptr pfc::avltree_t< t_storage, t_comparator >::t_noderawptr
private

Definition at line 86 of file avltree.h.

template<typename t_storage, typename t_comparator = comparator_default>
typedef avltree_t<t_storage,t_comparator> pfc::avltree_t< t_storage, t_comparator >::t_self

Definition at line 75 of file avltree.h.

Constructor & Destructor Documentation

template<typename t_storage, typename t_comparator = comparator_default>
pfc::avltree_t< t_storage, t_comparator >::avltree_t ( )
inline

Definition at line 362 of file avltree.h.

362 : m_root(NULL) {}
t_nodeptr m_root
Definition: avltree.h:97
template<typename t_storage, typename t_comparator = comparator_default>
pfc::avltree_t< t_storage, t_comparator >::~avltree_t ( )
inline

Definition at line 363 of file avltree.h.

363 {reset();}
void reset()
Definition: avltree.h:480
template<typename t_storage, typename t_comparator = comparator_default>
pfc::avltree_t< t_storage, t_comparator >::avltree_t ( const t_self p_other)
inline

Definition at line 365 of file avltree.h.

365 : m_root(NULL) {try{__copy(p_other);} catch(...) {remove_all(); throw;}}
void __copy(const t_self &p_other)
Definition: avltree.h:541
t_nodeptr m_root
Definition: avltree.h:97
void remove_all()
Definition: avltree.h:435
template<typename t_storage, typename t_comparator = comparator_default>
template<typename t_other >
pfc::avltree_t< t_storage, t_comparator >::avltree_t ( const t_other &  p_other)
inline

Definition at line 368 of file avltree.h.

368 : m_root(NULL) {try{copy_list_enumerated(*this,p_other);}catch(...){remove_all(); throw;}}
void copy_list_enumerated(t_receiver &p_receiver, const t_giver &p_giver)
Definition: primitives.h:819
t_nodeptr m_root
Definition: avltree.h:97
void remove_all()
Definition: avltree.h:435

Member Function Documentation

template<typename t_storage, typename t_comparator = comparator_default>
void pfc::avltree_t< t_storage, t_comparator >::__copy ( const t_self p_other)
inlineprivate

Definition at line 541 of file avltree.h.

541  {
542  reset();
543  m_root = __copy_recur(p_other.m_root.get_ptr(),NULL);
544  selftest(m_root);
545  }
void reset()
Definition: avltree.h:480
static void selftest(t_nodeptr const &p_node)
Definition: avltree.h:284
static t_nodeptr __copy_recur(t_node *p_source, t_node *parent)
Definition: avltree.h:528
t_nodeptr m_root
Definition: avltree.h:97
template<typename t_storage, typename t_comparator = comparator_default>
static t_nodeptr pfc::avltree_t< t_storage, t_comparator >::__copy_recur ( t_node p_source,
t_node parent 
)
inlinestaticprivate

Definition at line 528 of file avltree.h.

528  {
529  if (p_source == NULL) {
530  return NULL;
531  } else {
532  t_nodeptr newnode = new t_node(p_source->m_content);
533  newnode->m_depth = p_source->m_depth;
534  newnode->m_left = __copy_recur(p_source->m_left.get_ptr(),newnode.get_ptr());
535  newnode->m_right = __copy_recur(p_source->m_right.get_ptr(),newnode.get_ptr());
536  newnode->m_parent = parent;
537  return newnode;
538  }
539  }
refcounted_object_ptr_t< t_node > t_nodeptr
Definition: avltree.h:82
t_rawptr m_parent
Definition: avltree.h:14
static t_nodeptr __copy_recur(t_node *p_source, t_node *parent)
Definition: avltree.h:528
_avltree_node< t_storage > t_node
Definition: avltree.h:80
template<typename t_storage, typename t_comparator = comparator_default>
template<typename t_nodewalk , typename t_callback >
static void pfc::avltree_t< t_storage, t_comparator >::__enum_items_recur ( t_nodewalk *  p_node,
t_callback &  p_callback 
)
inlinestaticprivate

Definition at line 173 of file avltree.h.

173  {
174  if (is_ptr_valid(p_node)) {
175  __enum_items_recur<t_nodewalk>(p_node->m_left.get_ptr(),p_callback);
176  p_callback (p_node->m_content);
177  __enum_items_recur<t_nodewalk>(p_node->m_right.get_ptr(),p_callback);
178  }
179  }
static bool is_ptr_valid(t_nodeptr const &p)
Definition: avltree.h:89
template<typename t_storage, typename t_comparator = comparator_default>
template<bool inclusive, bool above, typename t_search >
t_storage* pfc::avltree_t< t_storage, t_comparator >::__find_nearest ( const t_search &  p_search) const
inlineprivate

Definition at line 340 of file avltree.h.

340  {
341  t_node * ptr = m_root.get_ptr();
342  t_storage * found = NULL;
343  while(is_ptr_valid(ptr)) {
344  int result = compare(ptr->m_content,p_search);
345  if (above) result = -result;
346  if (inclusive && result == 0) {
347  //direct hit
348  found = &ptr->m_content;
349  break;
350  } else if (result < 0) {
351  //match
352  found = &ptr->m_content;
353  ptr = ptr->child(!above);
354  } else {
355  //mismatch
356  ptr = ptr->child(above);
357  }
358  }
359  return found;
360  }
static bool is_ptr_valid(t_nodeptr const &p)
Definition: avltree.h:89
_avltree_node< t_storage > t_node
Definition: avltree.h:80
t_nodeptr m_root
Definition: avltree.h:97
static int compare(const t_item1 &p_item1, const t_item2 &p_item2)
Definition: avltree.h:93
template<typename t_storage, typename t_comparator = comparator_default>
template<typename t_callback >
void pfc::avltree_t< t_storage, t_comparator >::_enumerate_var ( t_callback &  p_callback)
inline

Allows callback to modify the tree content. Unsafe! Caller must not modify items in a way that changes sort order!

Definition at line 465 of file avltree.h.

465 { __enum_items_recur<t_node>(m_root.get_ptr(),p_callback); }
t_nodeptr m_root
Definition: avltree.h:97
template<typename t_storage, typename t_comparator = comparator_default>
template<typename t_param >
t_storage* pfc::avltree_t< t_storage, t_comparator >::_find_item_ptr ( t_param const &  p_item) const
inlineprivate

Definition at line 317 of file avltree.h.

317  {
318  t_node* ptr = m_root.get_ptr();
319  while(is_ptr_valid(ptr)) {
320  int result = compare(ptr->m_content,p_item);
321  if (result > 0) ptr=ptr->m_left.get_ptr();
322  else if (result < 0) ptr=ptr->m_right.get_ptr();
323  else return &ptr->m_content;
324  }
325  return NULL;
326  }
static bool is_ptr_valid(t_nodeptr const &p)
Definition: avltree.h:89
_avltree_node< t_storage > t_node
Definition: avltree.h:80
t_nodeptr m_root
Definition: avltree.h:97
static int compare(const t_item1 &p_item1, const t_item2 &p_item2)
Definition: avltree.h:93
template<typename t_storage, typename t_comparator = comparator_default>
template<typename t_param >
t_node* pfc::avltree_t< t_storage, t_comparator >::_find_node_ptr ( t_param const &  p_item) const
inlineprivate

Definition at line 329 of file avltree.h.

329  {
330  t_node* ptr = m_root.get_ptr();
331  while(is_ptr_valid(ptr)) {
332  int result = compare(ptr->m_content,p_item);
333  if (result > 0) ptr=ptr->m_left.get_ptr();
334  else if (result < 0) ptr=ptr->m_right.get_ptr();
335  else return ptr;
336  }
337  return NULL;
338  }
static bool is_ptr_valid(t_nodeptr const &p)
Definition: avltree.h:89
_avltree_node< t_storage > t_node
Definition: avltree.h:80
t_nodeptr m_root
Definition: avltree.h:97
static int compare(const t_item1 &p_item1, const t_item2 &p_item2)
Definition: avltree.h:93
template<typename t_storage, typename t_comparator = comparator_default>
iterator pfc::avltree_t< t_storage, t_comparator >::_first_var ( )
inline

Unsafe! Caller must not modify items in a way that changes sort order!

Definition at line 488 of file avltree.h.

488 { return _firstlast(false); }
t_node * _firstlast(bool which) const
Definition: avltree.h:519
template<typename t_storage, typename t_comparator = comparator_default>
t_node* pfc::avltree_t< t_storage, t_comparator >::_firstlast ( bool  which) const
throw (
)
inlineprivate

Definition at line 519 of file avltree.h.

519  {
520  if (m_root.is_empty()) return NULL;
521  for(t_node * walk = m_root.get_ptr(); ; ) {
522  t_node * next = walk->child(which);
523  if (next == NULL) return walk;
524  PFC_ASSERT( next->m_parent == walk );
525  walk = next;
526  }
527  }
_avltree_node< t_storage > t_node
Definition: avltree.h:80
t_nodeptr m_root
Definition: avltree.h:97
template<typename t_storage, typename t_comparator = comparator_default>
iterator pfc::avltree_t< t_storage, t_comparator >::_last_var ( )
inline

Unsafe! Caller must not modify items in a way that changes sort order!

Definition at line 490 of file avltree.h.

490 { return _firstlast(true); }
t_node * _firstlast(bool which) const
Definition: avltree.h:519
template<typename t_storage, typename t_comparator = comparator_default>
static void pfc::avltree_t< t_storage, t_comparator >::_unlink_recur ( t_nodeptr node)
inlinestaticprivate

Definition at line 512 of file avltree.h.

512  {
513  if (node.is_valid()) {
514  _unlink_recur(node->m_left);
515  _unlink_recur(node->m_right);
516  node->unlink();
517  }
518  }
static void _unlink_recur(t_nodeptr &node)
Definition: avltree.h:512
template<typename t_storage, typename t_comparator = comparator_default>
template<typename t_param >
t_storage& pfc::avltree_t< t_storage, t_comparator >::add ( const t_param &  p_item)
inline

Definition at line 475 of file avltree.h.

475 {return add_item(p_item);}
t_storage & add_item(t_param const &p_item)
Definition: avltree.h:380
template<typename t_storage, typename t_comparator = comparator_default>
template<typename t_param >
t_storage& pfc::avltree_t< t_storage, t_comparator >::add_ex ( const t_param &  p_item,
bool &  p_isnew 
)
inline

Definition at line 476 of file avltree.h.

476 {return add_item_ex(p_item,p_isnew);}
t_storage & add_item_ex(t_param const &p_item, bool &p_isnew)
Definition: avltree.h:400
template<typename t_storage, typename t_comparator = comparator_default>
template<typename t_param >
t_storage& pfc::avltree_t< t_storage, t_comparator >::add_item ( t_param const &  p_item)
inline

Definition at line 380 of file avltree.h.

380  {
381  bool dummy;
382  return add_item_ex(p_item,dummy);
383  }
t_storage & add_item_ex(t_param const &p_item, bool &p_isnew)
Definition: avltree.h:400
template<typename t_storage, typename t_comparator = comparator_default>
template<typename t_param >
bool pfc::avltree_t< t_storage, t_comparator >::add_item_check ( t_param const &  item)
inline

Returns true when the list has been altered, false when the item was already present before.

Definition at line 393 of file avltree.h.

393  {
394  bool isNew = false;
395  g_find_or_add(m_root,NULL,item,isNew);
396  selftest(m_root);
397  return isNew;
398  }
static void selftest(t_nodeptr const &p_node)
Definition: avltree.h:284
static t_storage * g_find_or_add(t_nodeptr &p_base, t_node *parent, t_search const &p_search, bool &p_new)
Definition: avltree.h:216
t_nodeptr m_root
Definition: avltree.h:97
template<typename t_storage, typename t_comparator = comparator_default>
template<typename t_param >
t_storage& pfc::avltree_t< t_storage, t_comparator >::add_item_ex ( t_param const &  p_item,
bool &  p_isnew 
)
inline

Definition at line 400 of file avltree.h.

400  {
401  t_storage * ret = g_find_or_add(m_root,NULL,p_item,p_isnew);
402  selftest(m_root);
403  return *ret;
404  }
static void selftest(t_nodeptr const &p_node)
Definition: avltree.h:284
static t_storage * g_find_or_add(t_nodeptr &p_base, t_node *parent, t_search const &p_search, bool &p_new)
Definition: avltree.h:216
t_nodeptr m_root
Definition: avltree.h:97
template<typename t_storage, typename t_comparator = comparator_default>
static void pfc::avltree_t< t_storage, t_comparator >::assert_children ( t_nodeptr  ptr)
inlinestaticprivate

Definition at line 108 of file avltree.h.

108  {
109  PFC_ASSERT(ptr->m_depth == pfc::max_t(calc_depth(ptr->m_left),calc_depth(ptr->m_right)) );
110  }
static t_size calc_depth(const t_nodeptr &ptr)
Definition: avltree.h:99
T max_t(const T &item1, const T &item2)
Definition: primitives.h:553
template<typename t_storage, typename t_comparator = comparator_default>
static t_size pfc::avltree_t< t_storage, t_comparator >::calc_count ( const t_node p_node)
throw (
)
inlinestaticprivate

Definition at line 308 of file avltree.h.

308  {
309  if (is_ptr_valid(p_node)) {
310  return 1 + calc_count(p_node->m_left.get_ptr()) + calc_count(p_node->m_right.get_ptr());
311  } else {
312  return 0;
313  }
314  }
static bool is_ptr_valid(t_nodeptr const &p)
Definition: avltree.h:89
static t_size calc_count(const t_node *p_node)
Definition: avltree.h:308
template<typename t_storage, typename t_comparator = comparator_default>
static t_size pfc::avltree_t< t_storage, t_comparator >::calc_depth ( const t_nodeptr ptr)
inlinestaticprivate

Definition at line 99 of file avltree.h.

100  {
101  return ptr.is_valid() ? 1+ptr->m_depth : 0;
102  }
template<typename t_storage, typename t_comparator = comparator_default>
template<typename t_item1 , typename t_item2 >
static int pfc::avltree_t< t_storage, t_comparator >::compare ( const t_item1 &  p_item1,
const t_item2 &  p_item2 
)
inlinestaticprivate

Definition at line 93 of file avltree.h.

93  {
94  return t_comparator::compare(p_item1,p_item2);
95  }
int compare(t1 const &p1, t2 const &p2)
Definition: pathUtils.h:29
template<typename t_storage, typename t_comparator = comparator_default>
template<typename t_param >
bool pfc::avltree_t< t_storage, t_comparator >::contains ( const t_param &  p_item) const
inline

Definition at line 427 of file avltree.h.

427  {
428  return find_item_ptr(p_item) != NULL;
429  }
const t_storage * find_item_ptr(t_param const &p_item) const
Definition: avltree.h:414
template<typename t_storage, typename t_comparator = comparator_default>
template<typename t_callback >
void pfc::avltree_t< t_storage, t_comparator >::enumerate ( t_callback &  p_callback) const
inline

Definition at line 458 of file avltree.h.

458  {
459  __enum_items_recur<const t_node>(m_root.get_ptr(),p_callback);
460  }
t_nodeptr m_root
Definition: avltree.h:97
template<typename t_storage, typename t_comparator = comparator_default>
static bool pfc::avltree_t< t_storage, t_comparator >::equals ( const t_self v1,
const t_self v2 
)
inlinestatic

Definition at line 505 of file avltree.h.

505  {
506  return listEquals(v1,v2);
507  }
static bool listEquals(const t_list1 &p_list1, const t_list2 &p_list2)
Definition: iterators.h:105
template<typename t_storage, typename t_comparator = comparator_default>
template<typename t_param >
bool pfc::avltree_t< t_storage, t_comparator >::exists ( t_param const &  p_item) const
inline

Definition at line 479 of file avltree.h.

479 {return have_item(p_item);}
bool have_item(const t_param &p_item) const
Same as contains().
Definition: avltree.h:433
template<typename t_storage, typename t_comparator = comparator_default>
static t_nodeptr pfc::avltree_t< t_storage, t_comparator >::extract_left_leaf ( t_nodeptr p_base)
inlinestaticprivate

Definition at line 118 of file avltree.h.

118  {
119  if (is_ptr_valid(p_base->m_left)) {
120  t_nodeptr ret = extract_left_leaf(p_base->m_left);
121  recalc_depth(p_base);
122  g_rebalance(p_base);
123  return ret;
124  } else {
125  t_nodeptr node = p_base;
126  p_base = node->m_right;
127  if (p_base.is_valid()) p_base->m_parent = node->m_parent;
128  node->m_right.release();
129  node->m_depth = 0;
130  node->m_parent = NULL;
131  return node;
132  }
133  }
static bool is_ptr_valid(t_nodeptr const &p)
Definition: avltree.h:89
refcounted_object_ptr_t< t_node > t_nodeptr
Definition: avltree.h:82
static void recalc_depth(t_nodeptr const &ptr)
Definition: avltree.h:104
static void g_rebalance(t_nodeptr &p_node)
Definition: avltree.h:243
static t_nodeptr extract_left_leaf(t_nodeptr &p_base)
Definition: avltree.h:118
template<typename t_storage, typename t_comparator = comparator_default>
static t_nodeptr pfc::avltree_t< t_storage, t_comparator >::extract_right_leaf ( t_nodeptr p_base)
inlinestaticprivate

Definition at line 135 of file avltree.h.

135  {
136  if (is_ptr_valid(p_base->m_right)) {
137  t_nodeptr ret = extract_right_leaf(p_base->m_right);
138  recalc_depth(p_base);
139  g_rebalance(p_base);
140  return ret;
141  } else {
142  t_nodeptr node = p_base;
143  p_base = node->m_left;
144  if (p_base.is_valid()) p_base->m_parent = node->m_parent;
145  node->m_left.release();
146  node->m_depth = 0;
147  node->m_parent = NULL;
148  return node;
149  }
150  }
static bool is_ptr_valid(t_nodeptr const &p)
Definition: avltree.h:89
refcounted_object_ptr_t< t_node > t_nodeptr
Definition: avltree.h:82
static void recalc_depth(t_nodeptr const &ptr)
Definition: avltree.h:104
static void g_rebalance(t_nodeptr &p_node)
Definition: avltree.h:243
static t_nodeptr extract_right_leaf(t_nodeptr &p_base)
Definition: avltree.h:135
template<typename t_storage, typename t_comparator = comparator_default>
template<typename t_param >
const_iterator pfc::avltree_t< t_storage, t_comparator >::find ( t_param const &  item) const
inline

Definition at line 420 of file avltree.h.

420 { return _find_node_ptr(item);}
t_node * _find_node_ptr(t_param const &p_item) const
Definition: avltree.h:329
template<typename t_storage, typename t_comparator = comparator_default>
template<typename t_param >
iterator pfc::avltree_t< t_storage, t_comparator >::find ( t_param const &  item)
inline

Unsafe! Caller must not modify items in a way that changes sort order!

Definition at line 423 of file avltree.h.

423 { return _find_node_ptr(item);}
t_node * _find_node_ptr(t_param const &p_item) const
Definition: avltree.h:329
template<typename t_storage, typename t_comparator = comparator_default>
template<typename t_param >
const t_storage* pfc::avltree_t< t_storage, t_comparator >::find_item_ptr ( t_param const &  p_item) const
inline

Definition at line 414 of file avltree.h.

414 {return _find_item_ptr(p_item);}
t_storage * _find_item_ptr(t_param const &p_item) const
Definition: avltree.h:317
template<typename t_storage, typename t_comparator = comparator_default>
template<typename t_param >
t_storage* pfc::avltree_t< t_storage, t_comparator >::find_item_ptr ( t_param const &  p_item)
inline

Unsafe! Caller must not modify items in a way that changes sort order!

Definition at line 418 of file avltree.h.

418 { return _find_item_ptr(p_item); }
t_storage * _find_item_ptr(t_param const &p_item) const
Definition: avltree.h:317
template<typename t_storage, typename t_comparator = comparator_default>
template<bool inclusive, bool above, typename t_search >
const t_storage* pfc::avltree_t< t_storage, t_comparator >::find_nearest_item ( const t_search &  p_search) const
inline

Definition at line 371 of file avltree.h.

371  {
372  return __find_nearest<inclusive,above>(p_search);
373  }
template<typename t_storage, typename t_comparator = comparator_default>
template<bool inclusive, bool above, typename t_search >
t_storage* pfc::avltree_t< t_storage, t_comparator >::find_nearest_item ( const t_search &  p_search)
inline

Definition at line 375 of file avltree.h.

375  {
376  return __find_nearest<inclusive,above>(p_search);
377  }
template<typename t_storage, typename t_comparator = comparator_default>
template<typename t_param >
const t_storage* pfc::avltree_t< t_storage, t_comparator >::find_ptr ( t_param const &  p_item) const
inline

Definition at line 477 of file avltree.h.

477 {return find_item_ptr(p_item);}
const t_storage * find_item_ptr(t_param const &p_item) const
Definition: avltree.h:414
template<typename t_storage, typename t_comparator = comparator_default>
template<typename t_param >
t_storage* pfc::avltree_t< t_storage, t_comparator >::find_ptr ( t_param const &  p_item)
inline

Definition at line 478 of file avltree.h.

478 {return find_item_ptr(p_item);}
const t_storage * find_item_ptr(t_param const &p_item) const
Definition: avltree.h:414
template<typename t_storage, typename t_comparator = comparator_default>
const_iterator pfc::avltree_t< t_storage, t_comparator >::first ( ) const
throw (
)
inline

Definition at line 485 of file avltree.h.

485 {return _firstlast(false);}
t_node * _firstlast(bool which) const
Definition: avltree.h:519
template<typename t_storage, typename t_comparator = comparator_default>
template<typename t_search >
static t_storage* pfc::avltree_t< t_storage, t_comparator >::g_find_or_add ( t_nodeptr p_base,
t_node parent,
t_search const &  p_search,
bool &  p_new 
)
inlinestaticprivate

Definition at line 216 of file avltree.h.

216  {
217  return &g_find_or_add_node(p_base,parent,p_search,p_new)->m_content;
218  }
static t_node * g_find_or_add_node(t_nodeptr &p_base, t_node *parent, t_search const &p_search, bool &p_new)
Definition: avltree.h:181
template<typename t_storage, typename t_comparator = comparator_default>
template<typename t_search >
static t_node* pfc::avltree_t< t_storage, t_comparator >::g_find_or_add_node ( t_nodeptr p_base,
t_node parent,
t_search const &  p_search,
bool &  p_new 
)
inlinestaticprivate

Definition at line 181 of file avltree.h.

182  {
183  if (p_base.is_empty()) {
184  p_base = new t_node(p_search);
185  p_base->m_parent = parent;
186  p_new = true;
187  return p_base.get_ptr();
188  }
189 
190  PFC_ASSERT( p_base->m_parent == parent );
191 
192  int result = compare(p_base->m_content,p_search);
193  if (result > 0) {
194  t_node * ret = g_find_or_add_node<t_search>(p_base->m_left,p_base.get_ptr(),p_search,p_new);
195  if (p_new) {
196  recalc_depth(p_base);
197  g_rebalance(p_base);
198  }
199  return ret;
200  } else if (result < 0) {
201  t_node * ret = g_find_or_add_node<t_search>(p_base->m_right,p_base.get_ptr(),p_search,p_new);
202  if (p_new) {
203  recalc_depth(p_base);
204  g_rebalance(p_base);
205  }
206  return ret;
207  } else {
208  p_new = false;
209  return p_base.get_ptr();
210  }
211  }
static void recalc_depth(t_nodeptr const &ptr)
Definition: avltree.h:104
static void g_rebalance(t_nodeptr &p_node)
Definition: avltree.h:243
_avltree_node< t_storage > t_node
Definition: avltree.h:80
static int compare(const t_item1 &p_item1, const t_item2 &p_item2)
Definition: avltree.h:93
template<typename t_storage, typename t_comparator = comparator_default>
static void pfc::avltree_t< t_storage, t_comparator >::g_rebalance ( t_nodeptr p_node)
inlinestaticprivate

Definition at line 243 of file avltree.h.

243  {
244  t_ssize balance = test_depth(p_node);
245  if (balance > 1) {
246  //right becomes root
247  if (test_depth(p_node->m_right) < 0) {
248  g_rotate_left(p_node->m_right);
249  }
250  g_rotate_right(p_node);
251  } else if (balance < -1) {
252  //left becomes root
253  if (test_depth(p_node->m_left) > 0) {
254  g_rotate_right(p_node->m_left);
255  }
256  g_rotate_left(p_node);
257  }
258  selftest(p_node);
259  }
static void selftest(t_nodeptr const &p_node)
Definition: avltree.h:284
static t_ssize test_depth(t_nodeptr const &ptr)
Definition: avltree.h:112
static void g_rotate_left(t_nodeptr &oldroot)
Definition: avltree.h:232
pfc::sized_int_t< sizeof(size_t) >::t_signed t_ssize
Definition: int_types.h:49
static void g_rotate_right(t_nodeptr &oldroot)
Definition: avltree.h:221
template<typename t_storage, typename t_comparator = comparator_default>
template<typename t_search >
static bool pfc::avltree_t< t_storage, t_comparator >::g_remove ( t_nodeptr p_node,
t_search const &  p_search 
)
inlinestaticprivate

Definition at line 262 of file avltree.h.

262  {
263  if (p_node.is_empty()) return false;
264 
265  int result = compare(p_node->m_content,p_search);
266  if (result == 0) {
267  remove_internal(p_node);
268  if (is_ptr_valid(p_node)) {
269  recalc_depth(p_node);
270  g_rebalance(p_node);
271  }
272  return true;
273  } else {
274  if (g_remove<t_search>(result > 0 ? p_node->m_left : p_node->m_right,p_search)) {
275  recalc_depth(p_node);
276  g_rebalance(p_node);
277  return true;
278  } else {
279  return false;
280  }
281  }
282  }
static bool is_ptr_valid(t_nodeptr const &p)
Definition: avltree.h:89
static void recalc_depth(t_nodeptr const &ptr)
Definition: avltree.h:104
static void remove_internal(t_nodeptr &p_node)
Definition: avltree.h:152
static void g_rebalance(t_nodeptr &p_node)
Definition: avltree.h:243
static int compare(const t_item1 &p_item1, const t_item2 &p_item2)
Definition: avltree.h:93
template<typename t_storage, typename t_comparator = comparator_default>
static void pfc::avltree_t< t_storage, t_comparator >::g_rotate_left ( t_nodeptr oldroot)
inlinestaticprivate

Definition at line 232 of file avltree.h.

232  {
233  t_nodeptr newroot ( oldroot->m_left );
234  oldroot->link_child(false, newroot->m_right.get_ptr());
235  newroot->m_right = oldroot;
236  newroot->m_parent = oldroot->m_parent;
237  oldroot->m_parent = newroot.get_ptr();
238  recalc_depth(oldroot);
239  recalc_depth(newroot);
240  oldroot = newroot;
241  }
refcounted_object_ptr_t< t_node > t_nodeptr
Definition: avltree.h:82
static void recalc_depth(t_nodeptr const &ptr)
Definition: avltree.h:104
template<typename t_storage, typename t_comparator = comparator_default>
static void pfc::avltree_t< t_storage, t_comparator >::g_rotate_right ( t_nodeptr oldroot)
inlinestaticprivate

Definition at line 221 of file avltree.h.

221  {
222  t_nodeptr newroot ( oldroot->m_right );
223  oldroot->link_child(true, newroot->m_left.get_ptr());
224  newroot->m_left = oldroot;
225  newroot->m_parent = oldroot->m_parent;
226  oldroot->m_parent = newroot.get_ptr();
227  recalc_depth(oldroot);
228  recalc_depth(newroot);
229  oldroot = newroot;
230  }
refcounted_object_ptr_t< t_node > t_nodeptr
Definition: avltree.h:82
static void recalc_depth(t_nodeptr const &ptr)
Definition: avltree.h:104
template<typename t_storage, typename t_comparator = comparator_default>
t_size pfc::avltree_t< t_storage, t_comparator >::get_count ( ) const
throw (
)
inline

Definition at line 453 of file avltree.h.

453  {
454  return calc_count(m_root.get_ptr());
455  }
static t_size calc_count(const t_node *p_node)
Definition: avltree.h:308
t_nodeptr m_root
Definition: avltree.h:97
template<typename t_storage, typename t_comparator = comparator_default>
template<typename t_param >
bool pfc::avltree_t< t_storage, t_comparator >::get_first ( t_param &  p_item) const
throw (
)
inline

Definition at line 492 of file avltree.h.

492  {
493  const_iterator iter = first();
494  if (!iter.is_valid()) return false;
495  p_item = *iter;
496  return true;
497  }
const_iterator first() const
Definition: avltree.h:485
pfc::const_iterator< t_storage > const_iterator
Definition: avltree.h:76
template<typename t_storage, typename t_comparator = comparator_default>
template<typename t_param >
bool pfc::avltree_t< t_storage, t_comparator >::get_last ( t_param &  p_item) const
throw (
)
inline

Definition at line 498 of file avltree.h.

498  {
499  const_iterator iter = last();
500  if (!iter.is_valid()) return false;
501  p_item = *iter;
502  return true;
503  }
pfc::const_iterator< t_storage > const_iterator
Definition: avltree.h:76
const_iterator last() const
Definition: avltree.h:486
template<typename t_storage, typename t_comparator = comparator_default>
template<typename t_param >
bool pfc::avltree_t< t_storage, t_comparator >::have_item ( const t_param &  p_item) const
inline

Same as contains().

Definition at line 433 of file avltree.h.

433 {return contains(p_item);}
bool contains(const t_param &p_item) const
Definition: avltree.h:427
template<typename t_storage, typename t_comparator = comparator_default>
template<typename t_param >
iterator pfc::avltree_t< t_storage, t_comparator >::insert ( const t_param &  p_item)
inline

Definition at line 467 of file avltree.h.

467  {
468  bool isNew;
469  t_node * ret = g_find_or_add_node(m_root,NULL,p_item,isNew);
470  selftest(m_root);
471  return ret;
472  }
static void selftest(t_nodeptr const &p_node)
Definition: avltree.h:284
_avltree_node< t_storage > t_node
Definition: avltree.h:80
t_nodeptr m_root
Definition: avltree.h:97
static t_node * g_find_or_add_node(t_nodeptr &p_base, t_node *parent, t_search const &p_search, bool &p_new)
Definition: avltree.h:181
template<typename t_storage, typename t_comparator = comparator_default>
static bool pfc::avltree_t< t_storage, t_comparator >::is_ptr_valid ( t_nodeptr const &  p)
inlinestaticprivate

Definition at line 89 of file avltree.h.

89 { return p.is_valid(); }
template<typename t_storage, typename t_comparator = comparator_default>
static bool pfc::avltree_t< t_storage, t_comparator >::is_ptr_valid ( t_node const *  p)
inlinestaticprivate

Definition at line 90 of file avltree.h.

90 { return p != NULL; }
template<typename t_storage, typename t_comparator = comparator_default>
const_iterator pfc::avltree_t< t_storage, t_comparator >::last ( ) const
throw (
)
inline

Definition at line 486 of file avltree.h.

486 {return _firstlast(true);}
t_node * _firstlast(bool which) const
Definition: avltree.h:519
template<typename t_storage, typename t_comparator = comparator_default>
bool pfc::avltree_t< t_storage, t_comparator >::operator!= ( const t_self other) const
inline

Definition at line 509 of file avltree.h.

509 {return !equals(*this,other);}
static bool equals(const t_self &v1, const t_self &v2)
Definition: avltree.h:505
template<typename t_storage, typename t_comparator = comparator_default>
template<typename t_param >
t_self& pfc::avltree_t< t_storage, t_comparator >::operator+= ( const t_param &  p_item)
inline

Definition at line 386 of file avltree.h.

386 {add_item(p_item);return *this;}
t_storage & add_item(t_param const &p_item)
Definition: avltree.h:380
template<typename t_storage, typename t_comparator = comparator_default>
template<typename t_param >
t_self& pfc::avltree_t< t_storage, t_comparator >::operator-= ( const t_param &  p_item)
inline

Definition at line 389 of file avltree.h.

389 {remove_item(p_item);return *this;}
bool remove_item(t_param const &p_item)
Definition: avltree.h:447
template<typename t_storage, typename t_comparator = comparator_default>
const t_self& pfc::avltree_t< t_storage, t_comparator >::operator= ( const t_self p_other)
inline

Definition at line 364 of file avltree.h.

364 {__copy(p_other);return *this;}
void __copy(const t_self &p_other)
Definition: avltree.h:541
template<typename t_storage, typename t_comparator = comparator_default>
template<typename t_other >
const t_self& pfc::avltree_t< t_storage, t_comparator >::operator= ( const t_other &  p_other)
inline

Definition at line 367 of file avltree.h.

367 {copy_list_enumerated(*this,p_other);return *this;}
void copy_list_enumerated(t_receiver &p_receiver, const t_giver &p_giver)
Definition: primitives.h:819
template<typename t_storage, typename t_comparator = comparator_default>
bool pfc::avltree_t< t_storage, t_comparator >::operator== ( const t_self other) const
inline

Definition at line 508 of file avltree.h.

508 {return equals(*this,other);}
static bool equals(const t_self &v1, const t_self &v2)
Definition: avltree.h:505
template<typename t_storage, typename t_comparator = comparator_default>
static void pfc::avltree_t< t_storage, t_comparator >::recalc_depth ( t_nodeptr const &  ptr)
inlinestaticprivate

Definition at line 104 of file avltree.h.

104  {
105  ptr->m_depth = pfc::max_t(calc_depth(ptr->m_left), calc_depth(ptr->m_right));
106  }
static t_size calc_depth(const t_nodeptr &ptr)
Definition: avltree.h:99
T max_t(const T &item1, const T &item2)
Definition: primitives.h:553
template<typename t_storage, typename t_comparator = comparator_default>
bool pfc::avltree_t< t_storage, t_comparator >::remove ( const_iterator const &  iter)
inline

Definition at line 440 of file avltree.h.

440  {
441  PFC_ASSERT(iter.is_valid());
442  return remove_item(*iter);//OPTIMIZEME
443  //should never return false unless there's a bug in calling code
444  }
bool remove_item(t_param const &p_item)
Definition: avltree.h:447
template<typename t_storage, typename t_comparator = comparator_default>
void pfc::avltree_t< t_storage, t_comparator >::remove_all ( )
throw (
)
inline

Definition at line 435 of file avltree.h.

435  {
437  m_root.release();
438  }
t_nodeptr m_root
Definition: avltree.h:97
static void _unlink_recur(t_nodeptr &node)
Definition: avltree.h:512
template<typename t_storage, typename t_comparator = comparator_default>
static void pfc::avltree_t< t_storage, t_comparator >::remove_internal ( t_nodeptr p_node)
inlinestaticprivate

Definition at line 152 of file avltree.h.

152  {
153  t_nodeptr oldval = p_node;
154  if (p_node->m_left.is_empty()) {
155  p_node = p_node->m_right;
156  if (p_node.is_valid()) p_node->m_parent = oldval->m_parent;
157  } else if (p_node->m_right.is_empty()) {
158  p_node = p_node->m_left;
159  if (p_node.is_valid()) p_node->m_parent = oldval->m_parent;
160  } else {
161  t_nodeptr swap = extract_left_leaf(p_node->m_right);
162 
163  swap->link_left(oldval->m_left.get_ptr());
164  swap->link_right(oldval->m_right.get_ptr());
165  swap->m_parent = oldval->m_parent;
166  recalc_depth(swap);
167  p_node = swap;
168  }
169  oldval->unlink();
170  }
refcounted_object_ptr_t< t_node > t_nodeptr
Definition: avltree.h:82
static void recalc_depth(t_nodeptr const &ptr)
Definition: avltree.h:104
static t_nodeptr extract_left_leaf(t_nodeptr &p_base)
Definition: avltree.h:118
template<typename t_storage, typename t_comparator = comparator_default>
template<typename t_param >
bool pfc::avltree_t< t_storage, t_comparator >::remove_item ( t_param const &  p_item)
inline

Definition at line 447 of file avltree.h.

447  {
448  bool ret = g_remove<t_param>(m_root,p_item);
449  selftest(m_root);
450  return ret;
451  }
static void selftest(t_nodeptr const &p_node)
Definition: avltree.h:284
t_nodeptr m_root
Definition: avltree.h:97
template<typename t_storage, typename t_comparator = comparator_default>
void pfc::avltree_t< t_storage, t_comparator >::reset ( )
inline

Definition at line 480 of file avltree.h.

480 {remove_all();}
void remove_all()
Definition: avltree.h:435
template<typename t_storage, typename t_comparator = comparator_default>
static void pfc::avltree_t< t_storage, t_comparator >::selftest ( t_nodeptr const &  p_node)
inlinestaticprivate

Definition at line 284 of file avltree.h.

284  {
285  #if 0 //def _DEBUG//SLOW!
286  if (is_ptr_valid(p_node)) {
287  selftest(p_node->m_left);
288  selftest(p_node->m_right);
289  assert_children(p_node);
290  t_ssize delta = test_depth(p_node);
291  PFC_ASSERT(delta >= -1 && delta <= 1);
292 
293  if (p_node->m_left.is_valid()) {
294  PFC_ASSERT( p_node.get_ptr() == p_node->m_left->m_parent );
295  }
296  if (p_node->m_right.is_valid()) {
297  PFC_ASSERT( p_node.get_ptr() == p_node->m_right->m_parent );
298  }
299 
300  if (is_ptr_valid(p_node->m_parent)) {
301  PFC_ASSERT(p_node == p_node->m_parent->m_left || p_node == p_node->m_parent->m_right);
302  }
303  }
304  #endif
305  }
static bool is_ptr_valid(t_nodeptr const &p)
Definition: avltree.h:89
static void selftest(t_nodeptr const &p_node)
Definition: avltree.h:284
static t_ssize test_depth(t_nodeptr const &ptr)
Definition: avltree.h:112
static void assert_children(t_nodeptr ptr)
Definition: avltree.h:108
pfc::sized_int_t< sizeof(size_t) >::t_signed t_ssize
Definition: int_types.h:49
template<typename t_storage, typename t_comparator = comparator_default>
template<typename t_param >
void pfc::avltree_t< t_storage, t_comparator >::set_item ( const t_param &  p_item)
inline

Definition at line 407 of file avltree.h.

407  {
408  bool isnew;
409  t_storage & found = add_item_ex(p_item,isnew);
410  if (isnew) found = p_item;
411  }
t_storage & add_item_ex(t_param const &p_item, bool &p_isnew)
Definition: avltree.h:400
template<typename t_storage, typename t_comparator = comparator_default>
static t_ssize pfc::avltree_t< t_storage, t_comparator >::test_depth ( t_nodeptr const &  ptr)
inlinestaticprivate

Definition at line 112 of file avltree.h.

113  {
114  if (ptr==0) return 0;
115  else return calc_depth(ptr->m_right) - calc_depth(ptr->m_left);
116  }
static t_size calc_depth(const t_nodeptr &ptr)
Definition: avltree.h:99

Field Documentation

template<typename t_storage, typename t_comparator = comparator_default>
t_nodeptr pfc::avltree_t< t_storage, t_comparator >::m_root
private

Definition at line 97 of file avltree.h.


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