#ifndef CODE_DLIST_H
#define CODE_DLIST_H

/*-----------------------------------------------------------------------
 * Xclasses code file
 * ($Id: dlist.cxx,v 1.6 2000/01/21 22:22:27 schmitzj Exp $)
 * 
 * Copyright (C) 1998 Jürgen Schmitz
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.
 *-----------------------------------------------------------------------
 */

#include "types.h"
#include <Xclasses/dlist.h>
#include <string.h>

dlist::dlist()
{
	terminator=NULL;
	head=(node*)&terminator;
	tail=(node*)&head;
	numbered=0;
	last_number=0;
	last_node=head;
}
dlist::~dlist()
{
//	if (!IsEmpty()) eprintf("dlist closed which was not empty!");
}
void dlist::AddHead(struct node *n)
{
//	n->prev=head->prev;
//	n->next=head;
//	head->prev=n; 	// aendert tail
//	head=n; 		// aendert head
	
	Insert(n,head);
}
void dlist::AddTail(struct node *n)
{
//	n->next=tail->next;
//	n->prev=tail;
//	tail->next=n; 	// aendert head
//	tail=n; 		// aendert tail
	
	Insert(n,tail->next);
}
void dlist::Insert(struct node *n,struct node *before)
{
	n->next=before;
	n->prev=before->prev;
	before->prev=n;
	n->prev->next=n;
	numbered=0;
}
void dlist::AddAlpha(struct node *n)
{
	int fertig=0;
	struct node *n2;
	n2=head;
	while (n2->next && n2->text && !fertig)
	{
		if (strcasecmp(n->text,n2->text)<0)
		{
			fertig=1;
			Insert(n,n2);
		}
		n2=n2->next;
	}
	if (!fertig) AddTail(n);
}
struct node *dlist::RemHead(void)
{
	struct node *n;
	if (IsEmpty())
	{
		n=NULL;
	}
	else
	{
		n=head;
		Remove(head);
//		head->next->prev=head->prev; // aendert tail
//		head->prev->next=head->next; // aendert head
	}
	return n;
}	
struct node *dlist::RemTail(void)
{
	struct node *n;
	if (IsEmpty())
	{
		n=NULL;
	}
	else
	{
		n=tail;
		Remove(tail);
//		tail->next->prev=tail->prev; // aendert tail
//		tail->prev->next=tail->next; // aendert head
	}
	return n;
}
void dlist::Remove(struct node *n)
{
	n->next->prev=n->prev; // aendert tail
	n->prev->next=n->next; // aendert head
	numbered=0;
}
int dlist::Find(struct node *cn)
{
	if (!cn) return 0;
	if (!numbered)
	{
		Count();
	}
	return cn->number;
}
struct node *dlist::Find(int c)
{
	struct node *n;
	if (!numbered)
	{
		Count();
	}
	if (Count()==0) return NULL;

	if (c>last_number)
	{ // last_node -> next
		n=last_node;
		while(n->next && n->number!=c)
		{
			n=n->next;
		}
		if (n->next)
		{
			last_node=n;
			last_number=c;
		}
		else n=NULL;
	}
	else if (c<last_number)
	{ // last_node -> prev
		n=last_node;
		while(n->prev && n->number!=c)
		{
			n=n->prev;
		}
		if (n->prev)
		{
			last_node=n;
			last_number=c;
		}
		else n=NULL;
	}
	else
	{ // c==last_number
		n=last_node;
	}
	return n;
}
struct node *dlist::FindText(char *txt,struct node *n)
{
	while(n->next)
	{
		if (!strcmp(n->text,txt)) return n;
		n=n->next;
	}
	return NULL;
}
struct node *dlist::FindText(char *txt)
{
	return FindText(txt,head);
}
struct node *dlist::FindTextNC(char *txt,struct node *n)
{
	while(n->next)
	{
		if (!strcasecmp(n->text,txt)) return n;
		n=n->next;
	}
	return NULL;
}
struct node *dlist::FindTextNC(char *txt)
{
	return FindTextNC(txt,head);
}
int dlist::Count(void)
{
	if (!numbered)
	{
		int nr=0;
		struct node *n=head;

		while(n->next)
		{
			nr++;
			n->number=nr;
			n=n->next;
		}
		numbered=1;
		count=nr;
		last_number=1;
		last_node=head;
	}
	return count;
}
int dlist::IsMember(struct node *c)
{
	struct node *n=head;
	while (n->next)
	{
		if (n==c) return 1;
		n=n->next;
	}
	return 0;
}
int dlist::IsEmpty(void)
{
	return !(head->next);
}

int (*sortfunc)(struct node*,struct node*);

int cmp(struct node **n1,struct node **n2)
{
	return sortfunc(*n1,*n2);
}

void dlist::Sort(int (*func)(struct node*,struct node*))
{
	int t=Count();
	struct node *nodes[t];
	struct node *n;
	int i=0;

	sortfunc=func;

	while (n=RemHead())
	{
		nodes[i]=n;
		i++;
	}

	qsort(nodes,t,sizeof(struct node*),(int (*)(const void *, const void *))cmp);

	for(i=0;i<t;i++)
	{
		AddTail(nodes[i]);
	}
	numbered=0;
}

#endif
