flist.h 4.31 KB
Newer Older
1
2
3
4
5
/**
 * \file flist.h 
 * \brief Library for handling linked lists where each node is identified by an integer ID
 */

Arne Øslebø's avatar
Arne Øslebø committed
6
7
8
9
#ifndef _FLIST_H
#define _FLIST_H 1


10
11
12
13
/**
 * \struct flist_node flist.h
 * \brief Structure containing the data for one single node in an flist
 */
Arne Øslebø's avatar
Arne Øslebø committed
14
15
typedef struct flist_node
{
16
17
18
  int id; //!< ID of the node
  void *data; //!< Pointer to the data stored in the node
  struct flist_node* next;  //!< Pointer to the next node. NULL if no other nodes exits
Arne Øslebø's avatar
Arne Øslebø committed
19
20
} flist_node_t;

21
22
23
24
/**
 * \struct flist flist.h
 * \brief Structure containing control information about an flist
 */
Arne Øslebø's avatar
Arne Øslebø committed
25
26
typedef struct flist
{
27
28
29
  flist_node_t *head; //!< Pointer to head of list
  flist_node_t *tail; //!< Pointer to tail of list
  unsigned size; //!< Number of elements in the list
Arne Øslebø's avatar
Arne Øslebø committed
30
31
32
33
34
35
36
37
} flist_t;

//! Macro to get the head node of a list l
#define flist_head(l) (l)->head
//! Macro to get the tail node of a list l
#define flist_tail(l) (l)->tail
//! Macro to get the size of a list l
#define flist_size(l) (l)->size
38
//! Macro to get the next node after n
Arne Øslebø's avatar
Arne Øslebø committed
39
#define flist_next(n) (n)->next
40
//! Macro to get the data of node n
Arne Øslebø's avatar
Arne Øslebø committed
41
#define flist_data(n) (n)->data
42
/// Macro to get the ID of node n
Arne Øslebø's avatar
Arne Øslebø committed
43
44
#define flist_id(n) (n)->id

45

Arne Øslebø's avatar
Arne Øslebø committed
46
47
typedef enum { FLIST_LEAVE_DATA = 0, FLIST_FREE_DATA } flist_destroy_t;

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
/**
 * \brief Initialise an flist
 * \param list the list to initialize
 */
void flist_init(flist_t *list);

/** \brief Destroy and de-allocate the memory hold by a list
  * \param list a pointer to an existing list
  * \param dealloc flag that indicates whether stored data should also be de-allocated
  */
void flist_destroy(flist_t *list,flist_destroy_t dealloc);

/** \brief Remove a node from the list
  * \param list a pointer to an existing list
  * \param dealloc flag that indicates whether stored data should also be de-allocated
  */
void* flist_remove(flist_t *list,int,flist_destroy_t dealloc);

/** \brief Insert a new node into the list
  * \param list a pointer to an existing list
  * \param id ID of the new node
  * \param data data to be stored in the node
  * \param index specifies the exact index of the list where the node should be inserted
  * \return 0 on success, or -1 on failure
  */
's avatar
committed
73
int  flist_insert(flist_t *list, int id, void *data, int list_index);
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

/** \brief Reverse the order of the list
  * \param list a pointer to an existing list
  */
void flist_reverse(flist_t *list);

/** \brief Moves one node before another one in the list
  * \param list a pointer to an existing list
  * \param before ID of the node that the other node should be moved before
  * \param id ID of the node that should be moved
  */
void flist_move_before(flist_t *list,int before,int id);

/** \brief Pop the first element in the list
  * \param list a pointer to a list
  * \return a pointer to the element, or NULL if the list is empty
  */
Arne Øslebø's avatar
Arne Øslebø committed
91
extern inline void *flist_pop_first(flist_t *);
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

/** \brief Append data to list
  * \param list a pointer to a list
  * \param data the data to place in the list
  * \return 0 on success, or -1 on failure 
  */
extern inline int flist_append(flist_t *list,int id,void *data);

/** \brief Prepend data to list
  * \param list a pointer to list
  * \param data the data to place in the list
  * \return 0 on success, or -1 on failure
  */
extern inline int flist_prepend(flist_t *list,int id,void *data);

/** \brief Get the data of a node from the list
  * \param list a pointer to list
  * \param id ID of the node to get
  * \return pointer to the data of the node. 0 if the node do not exist
  */
extern inline void* flist_get(flist_t *list,int id);

/** \brief Get the data of the next node in the list
  * \param list a pointer to list
  * \param id ID of the previous node
  * \return pointer to the data of the node. 0 if the node do not exist
  */
extern inline int flist_get_next_id(flist_t *list,int id);
Arne Øslebø's avatar
Arne Øslebø committed
120

's avatar
nits    
committed
121
122
123
extern int flist_default_free_data;
#define FLIST_FREE_DATA_ADDR ((void *)&flist_default_free_data)

's avatar
committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#ifdef DIMAPI
/** \brief Searches for a mathcing node and returns it
	Returns only the first matching node.

	\param list a pointer to an existing list
	\param comp function reference to be used for comparison of nodes
	\param cdata 2nd argument to be passed to comparison function

	\return Reference to the matching node's data, or NULL if no node 
	was matched
*/
extern inline void *flist_search(flist_t *, int (*compar)(void *, void *), void *);
#endif

Arne Øslebø's avatar
Arne Øslebø committed
138
#endif