class ListItem {
public:
TYPE *previous, *next;
- List<TYPE> *owner;
+ List<TYPE> *list;
- int get_item_number() { return !owner ? -1 : owner->number_of(this); }
- ListItem() { owner = 0; previous = next = 0; }
- ListItem(List<TYPE> &me) { owner = me; previous = next = 0; }
- virtual ~ListItem() { if( owner ) owner->remove_pointer(this); }
+ int get_item_number() { return !list ? -1 : list->number_of(this); }
+ ListItem() { list = 0; previous = next = 0; }
+ ListItem(List<TYPE> &me) { list = me; previous = next = 0; }
+ virtual ~ListItem() { if( list ) list->remove_pointer(this); }
};
template<class TYPE>
class List {
TYPE *split(int (*cmpr)(TYPE *a, TYPE *b),TYPE *l, TYPE *r);
- void sort(int (*cmpr)(TYPE *a, TYPE *b),TYPE *ll, TYPE *rr);
static int cmpr(TYPE *a, TYPE *b) {
if( *a == *b ) return 0;
return *a > *b ? 1 : -1;
public:
TYPE *first, *last;
void remove(TYPE *item) { if(item) delete item; }
- void remove_pointer(ListItem<TYPE> *item);
+ void remove_pointer(ListItem<TYPE> *item);
TYPE *append(TYPE *new_item);
TYPE *append() { return append(new TYPE()); }
TYPE *insert_before(TYPE *here, TYPE *item);
return p ? i : -1;
}
void swap(TYPE *item1, TYPE *item2);
- void sort(int (*cmp)(TYPE *a, TYPE *b) = 0) {
- return sort(cmp ? cmp : cmpr,0,0); }
+ void sort(TYPE *ap=0, TYPE *bp=0) { return sort(cmpr,ap,bp); }
+ void sort(int (*cmp)(TYPE *a, TYPE *b), TYPE *ap=0, TYPE *bp=0);
List() { first = last = 0; }
virtual ~List() { while(last) delete last; }
};
template<class TYPE>
TYPE* List<TYPE>::append(TYPE *item)
{
- item->owner = this; item->next = 0;
+ item->list = this; item->next = 0;
if( !last ) { item->previous = 0; first = item; }
else { item->previous = last; last->next = item; }
return last = item;
TYPE* List<TYPE>::insert_before(TYPE *here, TYPE *item)
{
if( !here || !last ) return append(item);
- item->owner = this; item->next = here;
+ item->list = this; item->next = here;
*( !(item->previous=here->previous) ? &first : &here->previous->next ) = item;
return here->previous = item;
}
TYPE* List<TYPE>::insert_after(TYPE *here, TYPE *item)
{
if( !here || !last ) return append(item);
- item->owner = this; item->previous = here;
+ item->list = this; item->previous = here;
*( !(item->next=here->next) ? &last : &here->next->previous ) = item;
return here->next = item;
}
TYPE *previous = item->previous, *next = item->next;
*( previous ? &previous->next : &first ) = next;
*( next ? &next->previous : &last ) = previous;
- item->owner = 0;
+ item->list = 0;
}
template<class TYPE>