/* lp_queue.c
 * Neil Moore <neil@cs.uky.edu>
 * 24 Feb 2000
 *
 * Part of liblp.  See COPYRIGHT for copyright information.
 */
#include <lp_util.h>
#include <lp_queue.h>


struct lp_aqueue *lp_aq_new(void) 
{
	return lp_calloc(1,sizeof(struct lp_aqueue));
}


void lp_aq_free(struct lp_aqueue *aq) 
{
	if(aq->head)
		lp_al_free(aq->head);
	free(aq);
}


void lp_aq_enqueue(struct lp_aqueue *aq, struct lp_atom *a) 
{
	struct lp_alist *curr = lp_malloc(sizeof(struct lp_alist));
	curr->next = NULL;
	curr->atom = a;

	
	if(aq->tail == NULL) {
		aq->tail = curr;
	} else {
		aq->tail->next = curr;
		aq->tail = curr;
	}
	
		
	if(aq->head == NULL)
		aq->head = curr;
}


struct lp_atom *lp_aq_head(const struct lp_aqueue *aq) 
{
	if(aq->head == NULL)
		return NULL;
	return aq->head->atom;
}


struct lp_atom *lp_aq_dequeue(struct lp_aqueue *aq) 
{
	struct lp_atom *a;
	struct lp_alist *old = aq->head;
	
	if(old == NULL)
		return NULL;
	
	a = old->atom;

	aq->head = old->next;
	free(old);
	
	if(aq->head == NULL)
		aq->tail = NULL;
	
	return a;
}


int lp_aq_empty(const struct lp_aqueue *aq) 
{
	return (aq->head == NULL);
}


int lp_aq_size(const struct lp_aqueue *aq) 
{
	struct lp_alist *j;
	int size=0;

	for(j=aq->head; j!=NULL; j=j->next)
		size++;
	
	return size;
}
