Commit f4706782 authored by 's avatar
Browse files

Removed redundant flist files.


git-svn-id: file:///home/svn/mapi/trunk@901 8d5bb341-7cf1-0310-8cf6-ba355fef3186
parent cb93166a
#include "stdlib.h"
#include "flist.h"
int
flist_append(flist_t *list,int id,void *data)
{
flist_node_t *node;
if ( (node = malloc(sizeof(flist_node_t))) == NULL )
return -1;
node->id=id;
node->data = data;
node->next = NULL;
if ( flist_head(list) == NULL )
flist_head(list) = node;
else
flist_next(flist_tail(list)) = node;
flist_tail(list) = node;
++flist_size(list);
return 0;
}
int
flist_prepend(flist_t *list,int id,void *data)
{
flist_node_t *node;
if ( (node = malloc(sizeof(flist_node_t))) == NULL )
return -1;
node->id=id;
flist_data(node) = data;
flist_next(node) = flist_head(list);
flist_head(list) = node;
if ( flist_tail(list) == NULL )
flist_tail(list) = node;
++flist_size(list);
return 0;
}
void *
flist_pop_first(flist_t *list)
{
void *d;
flist_node_t *node;
if ( flist_head(list) == NULL )
return NULL;
d = flist_data((node = flist_head(list)));
flist_head(list) = flist_next(node);
free(node);
if ( --flist_size(list) == 0 )
flist_tail(list) = NULL;
return d;
}
void
flist_init(flist_t *list)
{
flist_head(list) = flist_tail(list) = NULL;
flist_size(list) = 0;
}
void
flist_destroy(flist_t *list,flist_destroy_t dealloc)
{
flist_node_t *node;
while( (node = flist_head(list)) )
{
flist_head(list) = flist_next(node);
if ( dealloc == FLIST_FREE_DATA )
free(flist_data(node));
free(node);
}
flist_tail(list) = NULL;
flist_size(list) = 0;
}
void* flist_get(flist_t *list, int id)
{
flist_node_t *node=flist_head(list);
while(node!=NULL) {
if(node->id==id)
return node->data;
else
node=flist_next(node);
}
return NULL;
}
int flist_get_next_id(flist_t *list, int id)
{
flist_node_t *node=flist_head(list);
while(node!=NULL) {
if(node->id==id) {
node=flist_next(node);
if(node==NULL)
return 0;
else
return node->id;
}
else
node=flist_next(node);
}
return 0;
}
void flist_move_before(flist_t *list,int before,int id) {
void *data=flist_remove(list,id);
flist_insert(list,id,data,before);
}
void* flist_remove(flist_t *list,int id,flist_destroy_t dealloc)
{
flist_node_t *node=flist_head(list);
flist_node_t *p=NULL;
void *data;
while(node!=NULL) {
if(node->id==id) {
--flist_size(list);
data=node->data;
if(p==NULL)
flist_head(list)=node->next;
else
p->next=node->next;
if(flist_tail(list)==node)
flist_tail(list)=p;
if(dealloc==FLIST_FREE_DATA)
free(flist_data(node));
free(node);
return data;
}
else {
p=node;
node=flist_next(node);
}
}
return NULL;
}
int
flist_insert(flist_t *list, int id, void* data, int index)
{
int i;
flist_node_t* head = flist_head(list);
flist_node_t* prev=NULL;
flist_node_t* node;
if (index == 0)
return flist_prepend(list, id, data);
if ((unsigned int)index >= flist_size(list))
return flist_append(list, id, data);
if ( (node = malloc(sizeof(flist_node_t))) == NULL )
return -1;
node->id = id;
node->data = data;
for (i=0; head != NULL; head = flist_next(head), i++)
{
if (i == index)
{
node->next = head;
flist_next(prev) = node;
}
prev = head;
}
flist_size(list)++;
return 0;
}
void flist_reverse(flist_t* list)
{
flist_node_t* node=NULL;
flist_node_t* prev=NULL;
flist_node_t* next=NULL;
if (flist_head(list) == NULL)
return;
for (node=flist_head(list); node != NULL; node = next)
{
next = flist_next(node);
flist_next(node) = prev;
prev = node;
}
node = flist_head(list);
flist_head(list) = flist_tail(list);
flist_tail(list) = node;
return;
}
#ifdef DIMAPI
void *flist_search(flist_t *list, int (*comp)(void *, void *), void *user)
{
flist_node_t *n;
void *dataCopy;
//pthread_mutex_lock(&list->lock);
for(n = flist_head(list) ; n != NULL ; n = flist_next(n)) {
if ( comp(flist_data(n), user) == 0 ) {
dataCopy = flist_data(n);
// pthread_mutex_unlock(&list->lock);
return dataCopy;
}
}
//pthread_mutex_unlock(&list->lock);
return NULL;
}
#endif
/**
* \file flist.h
* \brief Library for handling linked lists where each node is identified by an integer ID
*/
#ifndef _FLIST_H
#define _FLIST_H 1
/**
* \struct flist_node flist.h
* \brief Structure containing the data for one single node in an flist
*/
typedef struct flist_node
{
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
} flist_node_t;
/**
* \struct flist flist.h
* \brief Structure containing control information about an flist
*/
typedef struct flist
{
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
} 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
//! Macro to get the next node after n
#define flist_next(n) (n)->next
//! Macro to get the data of node n
#define flist_data(n) (n)->data
/// Macro to get the ID of node n
#define flist_id(n) (n)->id
typedef enum { FLIST_LEAVE_DATA = 0, FLIST_FREE_DATA } flist_destroy_t;
/**
* \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
*/
int flist_insert(flist_t *list, int id, void *data, int index);
/** \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
*/
extern inline void *flist_pop_first(flist_t *);
/** \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);
#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
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment