1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
/*
GOCR Copyright (C) 2000 Joerg Schulenburg Joerg.Schulenburg@physik.uni-magdeburg.de
GOCR API Copyright (C) 2001 Bruno Barberi Gnecco <brunobg@sourceforge.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 _GOCR_LIST_H
#define _GOCR_LIST_H
/*! \file list.h
\brief This is the linked list header
\author Bruno Barberi Gnecco <brunobg@sourceforge.net>
*/
/** @name List
*/
/*@{*/
/**
\brief Node structure.
*/
struct node {
struct node *next; /**< Pointer to next node. */
struct node *previous; /**< Pointer to prev node. */
void *data; /**< Pointer to data. */
};
typedef struct node Node;
/**
\brief List structure.
*/
struct list {
Node *header; /**< List header */
Node *tail; /**< List tail */
Node **fix; /**< fixes the list_del header problem */
Node **current; /**< for(each_element) data */
int n; /**< number of elements */
int level; /**< level of nested fors */
};
typedef struct list List;
/*
* Functions
*/
void list_init ( List *l );
int list_app ( List *l, void *data );
int list_ins ( List *l, void *data_after, void *data);
int list_ins_order ( List *l, void *data,
int (*say_when) (const void *, const void *) );
Node* list_node_from_data ( List *l, void *data );
int list_del ( List *l, void *data );
void list_free ( List *l );
int list_higher_level ( List *l );
void list_lower_level ( List *l );
void * list_next ( List *l, void *data );
void * list_prev ( List *l, void *data );
void list_sort ( List *l, int (*compare)(const void *, const void *) );
/**
\brief Is the list empty?
\retval 0 No.
\retval 1 Yes.
*/
#define list_empty(l) ((l)->header == NULL ? 1 : 0)
/**
\brief Get header data
\return The header data.
*/
#define list_get_header(l) ((l)->header->data)
/**
\brief Get tail data
\return The tail data.
*/
#define list_get_tail(l) ((l)->tail->data)
/**
\brief Get current data
\sa for_each_data.
\return The current data.
*/
#define list_get_current(l) ((l)->current[(l)->level]->data)
#define list_get_cur_prev(l) ((l)->current[(l)->level]->previous == NULL ? \
NULL : (l)->current[(l)->level]->previous->data )
#define list_get_cur_next(l) ((l)->current[(l)->level]->next == NULL ? \
NULL : (l)->current[(l)->level]->next->data )
/**
\brief Get number of nodes
\return The number of nodes of the list.
*/
#define list_total(l) ((l)->n)
#define for_each_data(l) \
if (list_higher_level(l) == 0) { \
for ( ; (l)->current[(l)->level]; (l)->current[(l)->level] = \
(l)->current[(l)->level]->next ) { \
if ( (l)->fix[(l)->level] ) { /* fix level */\
int i; \
for ( i = (l)->level - 1; i >= 0; i-- ) { \
/* check if some other copy of (l)->fix[(l)->level] exists */ \
\
if ( (l)->fix[i] == (l)->fix[(l)->level] ) break; \
} \
if ( i < 0 ) { /* no, it doesn't. Free it */ \
free((l)->fix[(l)->level]); \
} \
(l)->fix[(l)->level] = NULL; \
}
#define end_for_each(l) \
} \
list_lower_level(l); \
}
/*@}*/
#endif
|