3 template<
typename t_storage>
20 if (ptr != NULL) ptr->
m_parent =
this;
24 if (ptr != NULL) ptr->
m_parent =
this;
28 (which ? m_right :
m_left) = ptr;
29 if (ptr != NULL) ptr->m_parent =
this;
39 inline t_rawptr
child(
bool which)
const throw() {
return which ? &*m_right : &*
m_left;}
44 t_rawptr
step(
bool direction)
throw() {
47 t_self* t = walk->
child(direction);
48 if (t != NULL)
return t->
peakchild(!direction);
51 if (t == NULL)
return NULL;
61 if (next == NULL)
return walk;
72 template<
typename t_storage,
typename t_comparator = comparator_default>
90 template<
typename t_item1,
typename t_item2>
91 inline static int compare(
const t_item1 & p_item1,
const t_item2 & p_item2) {
112 if (ptr==0)
return 0;
117 if (p_base->
m_left != NULL) {
123 t_nodeptr node = p_base;
140 t_nodeptr node = p_base;
151 t_nodeptr oldval = p_node;
170 template<
typename t_nodewalk,
typename t_callback>
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);
178 template<
typename t_search>
179 static t_node *
g_find_or_add_node(t_nodeptr & p_base,t_node * parent,t_search
const & p_search,
bool & p_new)
182 p_base =
new t_node(p_search);
188 PFC_ASSERT( p_base->
m_parent == parent );
190 int result =
compare(p_base->m_content,p_search);
192 t_node * ret = g_find_or_add_node<t_search>(p_base->
m_left,&*p_base,p_search,p_new);
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);
213 template<
typename t_search>
214 static t_storage *
g_find_or_add(t_nodeptr & p_base,t_node * parent,t_search
const & p_search,
bool & p_new) {
220 t_nodeptr oldroot = p_node;
221 t_nodeptr newroot = oldroot->
m_right;
223 newroot->
m_left = oldroot;
232 t_nodeptr oldroot = p_node;
233 t_nodeptr newroot = oldroot->
m_left;
251 }
else if (balance < -1) {
261 template<
typename t_search>
262 static bool g_remove(t_nodeptr & p_node,t_search
const & p_search) {
263 if (p_node.
is_empty())
return false;
265 int result =
compare(p_node->m_content,p_search);
268 if (p_node != NULL) {
274 if (g_remove<t_search>(result > 0 ? p_node->
m_left : p_node->
m_right,p_search)) {
285 #if 0 //def _DEBUG//SLOW!
286 if (p_node != NULL) {
291 PFC_ASSERT(delta >= -1 && delta <= 1);
309 if (p_node != NULL) {
316 template<
typename t_param>
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;
328 template<
typename t_param>
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;
340 template<
bool inclusive,
bool above,
typename t_search> t_storage *
__find_nearest(
const t_search & p_search)
const {
342 t_storage * found = NULL;
344 int result =
compare(ptr->m_content,p_search);
345 if (above) result = -result;
346 if (inclusive && result == 0) {
348 found = &ptr->m_content;
350 }
else if (result < 0) {
352 found = &ptr->m_content;
353 ptr = ptr->
child(!above);
356 ptr = ptr->
child(above);
371 template<
bool inclusive,
bool above,
typename t_search>
const t_storage *
find_nearest_item(
const t_search & p_search)
const {
372 return __find_nearest<inclusive,above>(p_search);
375 template<
bool inclusive,
bool above,
typename t_search> t_storage *
find_nearest_item(
const t_search & p_search) {
376 return __find_nearest<inclusive,above>(p_search);
379 template<
typename t_param>
385 template<
typename t_param>
388 template<
typename t_param>
392 template<
typename t_param>
399 template<
typename t_param>
406 template<
typename t_param>
410 if (isnew) found = p_item;
413 template<
typename t_param>
417 template<
typename t_param>
420 template<
typename t_param> const_iterator
find(t_param
const & item)
const {
return _find_node_ptr(item);}
426 template<
typename t_param>
432 template<
typename t_param>
440 bool remove(const_iterator
const& iter) {
441 PFC_ASSERT(iter.is_valid());
446 template<
typename t_param>
448 bool ret = g_remove<t_param>(
m_root,p_item);
457 template<
typename t_callback>
459 __enum_items_recur<const t_node>(&*
m_root,p_callback);
464 template<
typename t_callback>
467 template<
typename t_param> iterator
insert(
const t_param & p_item) {
475 template<
typename t_param> t_storage &
add(
const t_param & p_item) {
return add_item(p_item);}
476 template<
typename t_param> t_storage &
add_ex(
const t_param & p_item,
bool & p_isnew) {
return add_item_ex(p_item,p_isnew);}
479 template<
typename t_param>
bool exists(t_param
const & p_item)
const {
return have_item(p_item);}
492 template<
typename t_param>
bool get_first(t_param & p_item)
const throw() {
493 const_iterator iter =
first();
498 template<
typename t_param>
bool get_last(t_param & p_item)
const throw() {
499 const_iterator iter =
last();
505 static bool equals(
const t_self & v1,
const t_self & v2) {
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 );
529 if (p_source == NULL) {
532 t_nodeptr newnode =
new t_node(p_source->m_content);
549 template<
typename t_storage,
typename t_comparator>
bool add_item_check(t_param const &item)
Returns true when the list has been altered, false when the item was already present before...
Base class for list nodes. Implemented by list implementers.
bool exists(t_param const &p_item) const
t_storage & add(const t_param &p_item)
bool get_first(t_param &p_item) const
refcounted_object_ptr_t< t_self > t_ptr
static bool listEquals(const t_list1 &p_list1, const t_list2 &p_list2)
void _enumerate_var(t_callback &p_callback)
Allows callback to modify the tree content. Unsafe! Caller must not modify items in a way that change...
bool operator!=(const t_self &other) const
t_node * _firstlast(bool which) const
void copy_list_enumerated(t_receiver &p_receiver, const t_giver &p_giver)
const t_storage * find_nearest_item(const t_search &p_search) const
void link_child(bool which, t_self *ptr)
refcounted_object_ptr_t< t_node > t_nodeptr
t_rawptr child(bool which) const
void set_item(const t_param &p_item)
int compare(t1 const &p1, t2 const &p2)
t_storage * __find_nearest(const t_search &p_search) const
const t_self & operator=(const t_self &p_other)
iterator _first_var()
Unsafe! Caller must not modify items in a way that changes sort order!
static void recalc_depth(t_nodeptr const &ptr)
static bool equals(const t_self &v1, const t_self &v2)
t_node * _find_node_ptr(t_param const &p_item) const
void __copy(const t_self &p_other)
static t_size calc_count(const t_node *p_node)
static bool g_remove(t_nodeptr &p_node, t_search const &p_search)
t_self * walk(bool forward)
static void selftest(t_nodeptr const &p_node)
static t_size calc_depth(const t_nodeptr &ptr)
t_self & operator+=(const t_param &p_item)
bool have_item(const t_param &p_item) const
Same as contains().
static void remove_internal(t_nodeptr &p_node)
t_storage & add_ex(const t_param &p_item, bool &p_isnew)
_avltree_node< t_storage > t_self
t_node::t_rawptr t_noderawptr
t_storage * find_ptr(t_param const &p_item)
pfc::iterator< t_storage > iterator
static void g_rebalance(t_nodeptr &p_node)
t_rawptr peakchild(bool direction)
t_storage * _find_item_ptr(t_param const &p_item) const
iterator _last_var()
Unsafe! Caller must not modify items in a way that changes sort order!
const_iterator find(t_param const &item) const
const_iterator first() const
void enumerate(t_callback &p_callback) const
static void __enum_items_recur(t_nodewalk *p_node, t_callback &p_callback)
iterator find(t_param const &item)
Unsafe! Caller must not modify items in a way that changes sort order!
avltree_t(const t_self &p_other)
static t_ssize test_depth(t_nodeptr const &ptr)
static t_nodeptr __copy_recur(t_node *p_source, t_node *parent)
_avltree_node< t_storage > t_node
avltree_t(const t_other &p_other)
pfc::const_iterator< t_storage > const_iterator
static t_storage * g_find_or_add(t_nodeptr &p_base, t_node *parent, t_search const &p_search, bool &p_new)
static void g_rotate_left(t_nodeptr &p_node)
bool which_child(const t_self *ptr) const
void link_left(t_self *ptr)
t_self & operator-=(const t_param &p_item)
iterator insert(const t_param &p_item)
_avltree_node(t_param const ¶m)
static void assert_children(t_nodeptr ptr)
t_storage & add_item(t_param const &p_item)
t_storage & add_item_ex(t_param const &p_item, bool &p_isnew)
pfc::sized_int_t< sizeof(size_t) >::t_signed t_ssize
static int compare(const t_item1 &p_item1, const t_item2 &p_item2)
const t_storage * find_ptr(t_param const &p_item) const
static void _unlink_recur(t_nodeptr &node)
avltree_t< t_storage, t_comparator > t_self
t_rawptr step(bool direction)
static t_nodeptr extract_left_leaf(t_nodeptr &p_base)
const t_storage * find_item_ptr(t_param const &p_item) const
static t_nodeptr extract_right_leaf(t_nodeptr &p_base)
bool operator==(const t_self &other) const
t_storage * find_item_ptr(t_param const &p_item)
Unsafe! Caller must not modify items in a way that changes sort order!
T max_t(const T &item1, const T &item2)
static void g_rotate_right(t_nodeptr &p_node)
_list_node< t_storage > t_node
bool get_last(t_param &p_item) const
const_iterator last() const
static t_node * g_find_or_add_node(t_nodeptr &p_base, t_node *parent, t_search const &p_search, bool &p_new)
t_storage * find_nearest_item(const t_search &p_search)
void link_right(t_self *ptr)
const t_self & operator=(const t_other &p_other)
bool contains(const t_param &p_item) const
bool remove_item(t_param const &p_item)