foobar2000 SDK  2015-01-14
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 &p_node)
 
static void g_rotate_right (t_nodeptr &p_node)
 
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:95
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:95
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:95
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,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:95
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,&*newnode);
535  newnode->m_right = __copy_recur(&*p_source->m_right,&*newnode);
536  newnode->m_parent = parent;
537  return newnode;
538  }
539  }
refcounted_object_ptr_t< t_node > t_nodeptr
Definition: avltree.h:82
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 171 of file avltree.h.

171  {
172  if (p_node != NULL) {
173  __enum_items_recur<t_nodewalk>(&*p_node->m_left,p_callback);
174  p_callback (p_node->m_content);
175  __enum_items_recur<t_nodewalk>(&*p_node->m_right,p_callback);
176  }
177  }
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;
342  t_storage * found = NULL;
343  while(ptr != NULL) {
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  }
_avltree_node< t_storage > t_node
Definition: avltree.h:80
t_nodeptr m_root
Definition: avltree.h:95
static int compare(const t_item1 &p_item1, const t_item2 &p_item2)
Definition: avltree.h:91
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,p_callback); }
t_nodeptr m_root
Definition: avltree.h:95
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;
319  while(ptr != NULL) {
320  int result = compare(ptr->m_content,p_item);
321  if (result > 0) ptr=&*ptr->m_left;
322  else if (result < 0) ptr=&*ptr->m_right;
323  else return &ptr->m_content;
324  }
325  return NULL;
326  }
_avltree_node< t_storage > t_node
Definition: avltree.h:80
t_nodeptr m_root
Definition: avltree.h:95
static int compare(const t_item1 &p_item1, const t_item2 &p_item2)
Definition: avltree.h:91
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;
331  while(ptr != NULL) {
332  int result = compare(ptr->m_content,p_item);
333  if (result > 0) ptr=&*ptr->m_left;
334  else if (result < 0) ptr=&*ptr->m_right;
335  else return ptr;
336  }
337  return NULL;
338  }
_avltree_node< t_storage > t_node
Definition: avltree.h:80
t_nodeptr m_root
Definition: avltree.h:95
static int compare(const t_item1 &p_item1, const t_item2 &p_item2)
Definition: avltree.h:91
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; ; ) {
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:95
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:214
t_nodeptr m_root
Definition: avltree.h:95
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:214
t_nodeptr m_root
Definition: avltree.h:95
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 106 of file avltree.h.

106  {
107  PFC_ASSERT(ptr->m_depth == pfc::max_t(calc_depth(ptr->m_left),calc_depth(ptr->m_right)) );
108  }
static t_size calc_depth(const t_nodeptr &ptr)
Definition: avltree.h:97
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 (p_node != NULL) {
310  return 1 + calc_count(&*p_node->m_left) + calc_count(&*p_node->m_right);
311  } else {
312  return 0;
313  }
314  }
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 97 of file avltree.h.

98  {
99  return ptr.is_valid() ? 1+ptr->m_depth : 0;
100  }
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 91 of file avltree.h.

91  {
92  return t_comparator::compare(p_item1,p_item2);
93  }
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,p_callback);
460  }
t_nodeptr m_root
Definition: avltree.h:95
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 116 of file avltree.h.

116  {
117  if (p_base->m_left != NULL) {
118  t_nodeptr ret = extract_left_leaf(p_base->m_left);
119  recalc_depth(p_base);
120  g_rebalance(p_base);
121  return ret;
122  } else {
123  t_nodeptr node = p_base;
124  p_base = node->m_right;
125  if (p_base.is_valid()) p_base->m_parent = node->m_parent;
126  node->m_right.release();
127  node->m_depth = 0;
128  node->m_parent = NULL;
129  return node;
130  }
131  }
refcounted_object_ptr_t< t_node > t_nodeptr
Definition: avltree.h:82
static void recalc_depth(t_nodeptr const &ptr)
Definition: avltree.h:102
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:116
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 133 of file avltree.h.

133  {
134  if (p_base->m_right != NULL) {
135  t_nodeptr ret = extract_right_leaf(p_base->m_right);
136  recalc_depth(p_base);
137  g_rebalance(p_base);
138  return ret;
139  } else {
140  t_nodeptr node = p_base;
141  p_base = node->m_left;
142  if (p_base.is_valid()) p_base->m_parent = node->m_parent;
143  node->m_left.release();
144  node->m_depth = 0;
145  node->m_parent = NULL;
146  return node;
147  }
148  }
refcounted_object_ptr_t< t_node > t_nodeptr
Definition: avltree.h:82
static void recalc_depth(t_nodeptr const &ptr)
Definition: avltree.h:102
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:133
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 214 of file avltree.h.

214  {
215  return &g_find_or_add_node(p_base,parent,p_search,p_new)->m_content;
216  }
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:179
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 179 of file avltree.h.

180  {
181  if (p_base.is_empty()) {
182  p_base = new t_node(p_search);
183  p_base->m_parent = parent;
184  p_new = true;
185  return p_base.get_ptr();
186  }
187 
188  PFC_ASSERT( p_base->m_parent == parent );
189 
190  int result = compare(p_base->m_content,p_search);
191  if (result > 0) {
192  t_node * ret = g_find_or_add_node<t_search>(p_base->m_left,&*p_base,p_search,p_new);
193  if (p_new) {
194  recalc_depth(p_base);
195  g_rebalance(p_base);
196  }
197  return ret;
198  } else if (result < 0) {
199  t_node * ret = g_find_or_add_node<t_search>(p_base->m_right,&*p_base,p_search,p_new);
200  if (p_new) {
201  recalc_depth(p_base);
202  g_rebalance(p_base);
203  }
204  return ret;
205  } else {
206  p_new = false;
207  return p_base.get_ptr();
208  }
209  }
static void recalc_depth(t_nodeptr const &ptr)
Definition: avltree.h:102
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:91
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:110
static void g_rotate_left(t_nodeptr &p_node)
Definition: avltree.h:231
pfc::sized_int_t< sizeof(size_t) >::t_signed t_ssize
Definition: int_types.h:49
static void g_rotate_right(t_nodeptr &p_node)
Definition: avltree.h:219
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 (p_node != NULL) {
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 void recalc_depth(t_nodeptr const &ptr)
Definition: avltree.h:102
static void remove_internal(t_nodeptr &p_node)
Definition: avltree.h:150
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:91
template<typename t_storage, typename t_comparator = comparator_default>
static void pfc::avltree_t< t_storage, t_comparator >::g_rotate_left ( t_nodeptr p_node)
inlinestaticprivate

Definition at line 231 of file avltree.h.

231  {
232  t_nodeptr oldroot = p_node;
233  t_nodeptr newroot = oldroot->m_left;
234  oldroot->link_child(false, &*newroot->m_right);
235  newroot->m_right = oldroot;
236  newroot->m_parent = oldroot->m_parent;
237  oldroot->m_parent = &*newroot;
238  recalc_depth(oldroot);
239  recalc_depth(newroot);
240  p_node = 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:102
template<typename t_storage, typename t_comparator = comparator_default>
static void pfc::avltree_t< t_storage, t_comparator >::g_rotate_right ( t_nodeptr p_node)
inlinestaticprivate

Definition at line 219 of file avltree.h.

219  {
220  t_nodeptr oldroot = p_node;
221  t_nodeptr newroot = oldroot->m_right;
222  oldroot->link_child(true, &*newroot->m_left);
223  newroot->m_left = oldroot;
224  newroot->m_parent = oldroot->m_parent;
225  oldroot->m_parent = &*newroot;
226  recalc_depth(oldroot);
227  recalc_depth(newroot);
228  p_node = newroot;
229  }
refcounted_object_ptr_t< t_node > t_nodeptr
Definition: avltree.h:82
static void recalc_depth(t_nodeptr const &ptr)
Definition: avltree.h:102
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);
455  }
static t_size calc_count(const t_node *p_node)
Definition: avltree.h:308
t_nodeptr m_root
Definition: avltree.h:95
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:95
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:179
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 102 of file avltree.h.

102  {
103  ptr->m_depth = pfc::max_t(calc_depth(ptr->m_left), calc_depth(ptr->m_right));
104  }
static t_size calc_depth(const t_nodeptr &ptr)
Definition: avltree.h:97
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:95
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 150 of file avltree.h.

150  {
151  t_nodeptr oldval = p_node;
152  if (p_node->m_left.is_empty()) {
153  p_node = p_node->m_right;
154  if (p_node.is_valid()) p_node->m_parent = oldval->m_parent;
155  } else if (p_node->m_right.is_empty()) {
156  p_node = p_node->m_left;
157  if (p_node.is_valid()) p_node->m_parent = oldval->m_parent;
158  } else {
159  t_nodeptr swap = extract_left_leaf(p_node->m_right);
160 
161  swap->link_left(&*oldval->m_left);
162  swap->link_right(&*oldval->m_right);
163  swap->m_parent = oldval->m_parent;
164  recalc_depth(swap);
165  p_node = swap;
166  }
167  oldval->unlink();
168  }
refcounted_object_ptr_t< t_node > t_nodeptr
Definition: avltree.h:82
static void recalc_depth(t_nodeptr const &ptr)
Definition: avltree.h:102
static t_nodeptr extract_left_leaf(t_nodeptr &p_base)
Definition: avltree.h:116
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:95
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 (p_node != NULL) {
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 (p_node->m_parent != NULL) {
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 void selftest(t_nodeptr const &p_node)
Definition: avltree.h:284
static t_ssize test_depth(t_nodeptr const &ptr)
Definition: avltree.h:110
static void assert_children(t_nodeptr ptr)
Definition: avltree.h:106
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 110 of file avltree.h.

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

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 95 of file avltree.h.


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