/*
 * CINELERRA
 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 */


#ifndef LINKLIST_H
#define LINKLIST_H

template<class TYPE> class List;

template<class TYPE>
class ListItem {
public:
	TYPE *previous, *next;
	List<TYPE> *list;

	int get_item_number() { return !list ? -1 : list->number_of((TYPE*)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);
	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);
	TYPE *append(TYPE *new_item);
	TYPE *append() { return append(new TYPE()); }
	void destroy() { while(last) delete last; }
	TYPE *insert_before(TYPE *here, TYPE *item);
	TYPE *insert_before(TYPE *here) { return insert_before(here, new TYPE()); }
	TYPE *insert_after(TYPE *here, TYPE *item);
	TYPE *insert_after(TYPE *here) { return insert_after(here, new TYPE()); }
	int total() { TYPE *p=first; int n=0; while(p) { p=p->next; ++n; } return n; }
	TYPE *get_item_number(int i) { TYPE *p=first;
		while(p && i>0) { --i; p=p->next; }
		return p;
	}
	TYPE &operator[](int i) { return *get_item_number(i); }
	int number_of(TYPE *item) { TYPE *p=first; int i=0;
		while(p && p!=item) { ++i; p=p->next; }
		return p ? i : -1;
	}
	void swap(TYPE *item1, TYPE *item2);
	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);
	void concat(List<TYPE> &b);
	List() { first = last = 0; }
	virtual ~List() { destroy(); }
};

// convenience macros
#define PREVIOUS current->previous
#define NEXT current->next

template<class TYPE>
TYPE* List<TYPE>::append(TYPE *item)
{
	item->list = this;  item->next = 0;
	if( !last ) { item->previous = 0; first = item; }
	else { item->previous = last;  last->next = item; }
	return last = item;
}

template<class TYPE>
TYPE* List<TYPE>::insert_before(TYPE *here, TYPE *item)
{
	if( !here || !last ) return append(item);
	item->list = this;  item->next = here;
	*( !(item->previous=here->previous) ? &first : &here->previous->next ) = item;
	return here->previous = item;
}

template<class TYPE>
TYPE* List<TYPE>::insert_after(TYPE *here, TYPE *item)
{
	if( !here || !last ) return append(item);
	item->list = this;  item->previous = here;
	*( !(item->next=here->next) ? &last : &here->next->previous ) = item;
	return here->next = item;
}


template<class TYPE>
void List<TYPE>::remove_pointer(ListItem<TYPE> *item)
{
	if( !item ) return;
	TYPE *previous = item->previous, *next = item->next;
	*( previous ? &previous->next : &first ) = next;
	*( next ? &next->previous : &last ) = previous;
	item->list = 0;
}

template<class TYPE>
void List<TYPE>::swap(TYPE *item1, TYPE *item2)
{
	if( item1 == item2 ) return;
	TYPE *item1_previous = item1->previous, *item1_next = item1->next;
	TYPE *item2_previous = item2->previous, *item2_next = item2->next;
	*( item1_previous ? &item1_previous->next : &first ) = item2;
	*( item1_next     ? &item1_next->previous : &last  ) = item2;
	*( item2_previous ? &item2_previous->next : &first ) = item1;
	*( item2_next     ? &item2_next->previous : &last  ) = item1;
	item1->previous = item2_previous == item1 ? item2 : item2_previous;
	item1->next     = item2_next     == item1 ? item2 : item2_next;
	item2->previous = item1_previous == item2 ? item1 : item1_previous;
	item2->next     = item1_next     == item2 ? item1 : item1_next;
}

template<class TYPE>
TYPE *List<TYPE>::split(int (*cmpr)(TYPE *a, TYPE *b), TYPE *l, TYPE *r)
{
	for(;;) {
		while( cmpr(r,l) >= 0 ) if( (l=l->next) == r ) return r;
		swap(l, r);
		while( cmpr(l,r) >= 0 ) if( r == (l=l->previous) ) return r;
		swap(l, r);
	}
}

template<class TYPE>
void List<TYPE>::sort(int (*cmpr)(TYPE *a, TYPE *b), TYPE *ll, TYPE *rr)
{
	for(;;) {
		TYPE *l = !ll ? first : ll->next;
		if( l == rr ) return;
		TYPE *r = !rr ? last  : rr->previous;
		if( l == r ) return;
		TYPE *m = split(cmpr, l, r);
		sort(cmpr, ll, m);
		ll = m;
	}
}

template<class TYPE>
void List<TYPE>::concat(List<TYPE> &b)
{
	if( !b.first ) return;
	*(last ? &last->next : &first) = b.first;
	b.first->previous = last;  last = b.last;
	TYPE *bp = b.first;  b.first = b.last = 0;
	while( bp ) { bp->list = this;  bp = bp->next; }
}

#endif
