/* Frame based linked list V2.14 31/1/08
   Copyright 2008 Jeffrey Lee
   This file is part of WOUM.
   WOUM 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 3 of the License, or
   (at your option) any later version.
   WOUM 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 WOUM.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _FBLL_H
#define _FBLL_H

typedef struct _fbll_frame {
	struct _fbll_frame *next,*prev;
	void *items[0];
} fbll_frame;

typedef struct {
	fbll_frame *head,*tail;
	signed long top,bottom,size,fsize; /* top = next free position, bot = lowets pos containing value, size = number of items, fsize = size of each frame */
} fbll;

extern fbll *fbll_new(int fsize); /* Create new list where fsize is the number of items per frame. Returns 0 on failure. */
extern void fbll_delete(fbll *h); /* Delete the list h */
extern int fbll_addhead(fbll *h,void *i); /* Add item 'i' to the head of the list, returning !0 on failure */
extern int fbll_addtail(fbll *h,void *i); /* Add item 'i' to the tail of the list, returning !0 on failure */
extern void fbll_removehead(fbll *h); /* Remove the head item from the list */
extern void fbll_removetail(fbll *h); /* Remove the tail item from the list */
extern void *fbll_head(fbll *h); /* Return the head item on the list, or 0 for failure */
extern void *fbll_tail(fbll *h); /* Return the tail item on the list, or 0 for failure */
extern unsigned long fbll_size(fbll *h); /* Return the number of items in the lst */
extern void *fbll_peekhead(fbll *h,signed long ofs); /* Return item 'ofs' from the head of the list, where 0 is the head itself. Returns 0 on failure */
extern void *fbll_peektail(fbll *h,signed long ofs); /* Return item 'ofs' from the tail of the list, where 0 is the tail itself. Returns 0 on failure */
extern int fbll_findhead(fbll *h,void *i); /* Return index of 'i' in 'h', relative to the list head. Returns -1 on failure */
extern int fbll_findtail(fbll *h,void *i); /* Return index of 'i' in 'h', relative to the list tail. Returns -1 on failure */
extern int fbll_sort(fbll *h,int (*func)(const void *,const void *)); /* Sort the list, using the supplied comparison function (which accepts item pointers). Returns !0 on failure */
extern void *fbll_removeheadn(fbll *h,int i); /* Remove and return nth element from head */
extern void *fbll_removetailn(fbll *h,int i); /* Remove and return nth element from tail */

#endif
