rename clist.

This commit is contained in:
Gatsby Van
2024-01-21 13:35:55 +08:00
parent 709619d5bf
commit 726d52c674
3 changed files with 174 additions and 174 deletions
@@ -16,14 +16,14 @@
#include "list.h"
/*
* Allocates a new list_node_t. NULL on failure.
* Allocates a new clist_node_t. NULL on failure.
*/
list_node_t *
list_node_new(void *val)
clist_node_t *
clist_node_new(void *val)
{
list_node_t *self;
if (!(self = LIST_MALLOC(sizeof(list_node_t))))
clist_node_t *self;
if (!(self = LIST_MALLOC(sizeof(clist_node_t))))
return NULL;
self->prev = NULL;
self->next = NULL;
@@ -32,14 +32,14 @@ list_node_new(void *val)
}
/*
* Allocate a new list_t. NULL on failure.
* Allocate a new clist_t. NULL on failure.
*/
list_t *
list_new(void)
clist_t *
clist_new(void)
{
list_t *self;
if (!(self = LIST_MALLOC(sizeof(list_t))))
clist_t *self;
if (!(self = LIST_MALLOC(sizeof(clist_t))))
return NULL;
self->head = NULL;
self->tail = NULL;
@@ -54,11 +54,11 @@ list_new(void)
* @self: Pointer to the list
*/
void list_clear(list_t *self)
void clist_clear(clist_t *self)
{
uint32_t len = self->len;
list_node_t *next;
list_node_t *curr = self->head;
clist_node_t *next;
clist_node_t *curr = self->head;
while (len)
{
@@ -76,9 +76,9 @@ void list_clear(list_t *self)
* @self: Pointer to the list
*/
void list_destroy(list_t *self)
void clist_destroy(clist_t *self)
{
list_clear(self);
clist_clear(self);
LIST_FREE(self);
}
@@ -89,8 +89,8 @@ void list_destroy(list_t *self)
* @node: the node to push
*/
list_node_t *
list_rpush(list_t *self, list_node_t *node)
clist_node_t *
clist_rpush(clist_t *self, clist_node_t *node)
{
if (!node)
return NULL;
@@ -117,13 +117,13 @@ list_rpush(list_t *self, list_node_t *node)
* @self: Pointer to the list for popping node
*/
list_node_t *
list_rpop(list_t *self)
clist_node_t *
clist_rpop(clist_t *self)
{
if (!self->len)
return NULL;
list_node_t *node = self->tail;
clist_node_t *node = self->tail;
if (--self->len)
{
@@ -143,13 +143,13 @@ list_rpop(list_t *self)
* @self: Pointer to the list for popping node
*/
list_node_t *
list_lpop(list_t *self)
clist_node_t *
clist_lpop(clist_t *self)
{
if (!self->len)
return NULL;
list_node_t *node = self->head;
clist_node_t *node = self->head;
if (--self->len)
{
@@ -171,8 +171,8 @@ list_lpop(list_t *self)
* @node: the node to push
*/
list_node_t *
list_lpush(list_t *self, list_node_t *node)
clist_node_t *
clist_lpush(clist_t *self, clist_node_t *node)
{
if (!node)
return NULL;
@@ -200,19 +200,19 @@ list_lpush(list_t *self, list_node_t *node)
* @val: Value to find
*/
list_node_t *
list_find(list_t *self, void *val)
clist_node_t *
clist_find(clist_t *self, void *val)
{
list_iterator_t *it = list_iterator_new(self, LIST_HEAD);
list_node_t *node;
clist_iterator_t *it = clist_iterator_new(self, LIST_HEAD);
clist_node_t *node;
while ((node = list_iterator_next(it)))
while ((node = clist_iterator_next(it)))
{
if (self->compare)
{
if (self->compare(val, node->val) == 0)
{
list_iterator_destroy(it);
clist_iterator_destroy(it);
return node;
}
}
@@ -220,13 +220,13 @@ list_find(list_t *self, void *val)
{
if (val == node->val)
{
list_iterator_destroy(it);
clist_iterator_destroy(it);
return node;
}
}
}
list_iterator_destroy(it);
clist_iterator_destroy(it);
return NULL;
}
@@ -236,10 +236,10 @@ list_find(list_t *self, void *val)
* @index: the index of node in the list
*/
list_node_t *
list_at(list_t *self, int index)
clist_node_t *
clist_at(clist_t *self, int index)
{
list_direction_t direction = LIST_HEAD;
clist_direction_t direction = LIST_HEAD;
if (index < 0)
{
@@ -249,11 +249,11 @@ list_at(list_t *self, int index)
if ((unsigned)index < self->len)
{
list_iterator_t *it = list_iterator_new(self, direction);
list_node_t *node = list_iterator_next(it);
clist_iterator_t *it = clist_iterator_new(self, direction);
clist_node_t *node = clist_iterator_next(it);
while (index--)
node = list_iterator_next(it);
list_iterator_destroy(it);
node = clist_iterator_next(it);
clist_iterator_destroy(it);
return node;
}
@@ -266,7 +266,7 @@ list_at(list_t *self, int index)
* @node: Pointer the node to be deleted
*/
void list_remove(list_t *self, list_node_t *node)
void clist_remove(clist_t *self, clist_node_t *node)
{
node->prev
? (node->prev->next = node->next)
@@ -284,29 +284,29 @@ void list_remove(list_t *self, list_node_t *node)
}
/*
* Allocate a new list_iterator_t. NULL on failure.
* Allocate a new clist_iterator_t. NULL on failure.
* Accepts a direction, which may be LIST_HEAD or LIST_TAIL.
*/
list_iterator_t *
list_iterator_new(list_t *list, list_direction_t direction)
clist_iterator_t *
clist_iterator_new(clist_t *list, clist_direction_t direction)
{
list_node_t *node = direction == LIST_HEAD
clist_node_t *node = direction == LIST_HEAD
? list->head
: list->tail;
return list_iterator_new_from_node(node, direction);
return clist_iterator_new_from_node(node, direction);
}
/*
* Allocate a new list_iterator_t with the given start
* Allocate a new clist_iterator_t with the given start
* node. NULL on failure.
*/
list_iterator_t *
list_iterator_new_from_node(list_node_t *node, list_direction_t direction)
clist_iterator_t *
clist_iterator_new_from_node(clist_node_t *node, clist_direction_t direction)
{
list_iterator_t *self;
if (!(self = LIST_MALLOC(sizeof(list_iterator_t))))
clist_iterator_t *self;
if (!(self = LIST_MALLOC(sizeof(clist_iterator_t))))
return NULL;
self->next = node;
self->direction = direction;
@@ -314,14 +314,14 @@ list_iterator_new_from_node(list_node_t *node, list_direction_t direction)
}
/*
* Return the next list_node_t or NULL when no more
* Return the next clist_node_t or NULL when no more
* nodes remain in the list.
*/
list_node_t *
list_iterator_next(list_iterator_t *self)
clist_node_t *
clist_iterator_next(clist_iterator_t *self)
{
list_node_t *curr = self->next;
clist_node_t *curr = self->next;
if (curr)
{
self->next = self->direction == LIST_HEAD
@@ -335,7 +335,7 @@ list_iterator_next(list_iterator_t *self)
* Free the list iterator.
*/
void list_iterator_destroy(list_iterator_t *self)
void clist_iterator_destroy(clist_iterator_t *self)
{
LIST_FREE(self);
self = NULL;
+118
View File
@@ -0,0 +1,118 @@
//
// list.h
//
// Copyright (c) 2010 TJ Holowaychuk <[email protected]>
//
#ifndef __CLIST_H__
#define __CLIST_H__
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdlib.h>
#include <stdint.h>
/*
* clist_t iterator direction.
*/
typedef enum
{
LIST_HEAD,
LIST_TAIL
} clist_direction_t;
/*
* clist_t node struct.
*/
typedef struct clist_node
{
struct clist_node *prev;
struct clist_node *next;
void *val;
} clist_node_t;
/*
* clist_t struct.
*/
typedef struct
{
clist_node_t *head;
clist_node_t *tail;
uint32_t len;
void (*free)(void *val);
int8_t (*compare)(void *a, void *b);
} clist_t;
/*
* clist_t iterator struct.
*/
typedef struct
{
clist_node_t *next;
clist_direction_t direction;
} clist_iterator_t;
// Node prototypes.
clist_node_t *
clist_node_new(void *val);
// clist_t prototypes.
clist_t *
clist_new(void);
clist_node_t *
clist_rpush(clist_t *self, clist_node_t *node);
clist_node_t *
clist_lpush(clist_t *self, clist_node_t *node);
clist_node_t *
clist_find(clist_t *self, void *val);
clist_node_t *
clist_at(clist_t *self, int index);
clist_node_t *
clist_rpop(clist_t *self);
clist_node_t *
clist_lpop(clist_t *self);
void
clist_remove(clist_t *self, clist_node_t *node);
void
clist_clear(clist_t *self);
void
clist_destroy(clist_t *self);
// clist_t iterator prototypes.
clist_iterator_t *
clist_iterator_new(clist_t *list, clist_direction_t direction);
clist_iterator_t *
clist_iterator_new_from_node(clist_node_t *node, clist_direction_t direction);
clist_node_t *
clist_iterator_next(clist_iterator_t *self);
void
clist_iterator_destroy(clist_iterator_t *self);
#ifdef __cplusplus
}
#endif
#endif /* __CLIBS_LIST_H__ */
-118
View File
@@ -1,118 +0,0 @@
//
// list.h
//
// Copyright (c) 2010 TJ Holowaychuk <[email protected]>
//
#ifndef __LIST_H__
#define __LIST_H__
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdlib.h>
#include <stdint.h>
/*
* list_t iterator direction.
*/
typedef enum
{
LIST_HEAD,
LIST_TAIL
} list_direction_t;
/*
* list_t node struct.
*/
typedef struct list_node
{
struct list_node *prev;
struct list_node *next;
void *val;
} list_node_t;
/*
* list_t struct.
*/
typedef struct
{
list_node_t *head;
list_node_t *tail;
uint32_t len;
void (*free)(void *val);
int8_t (*compare)(void *a, void *b);
} list_t;
/*
* list_t iterator struct.
*/
typedef struct
{
list_node_t *next;
list_direction_t direction;
} list_iterator_t;
// Node prototypes.
list_node_t *
list_node_new(void *val);
// list_t prototypes.
list_t *
list_new(void);
list_node_t *
list_rpush(list_t *self, list_node_t *node);
list_node_t *
list_lpush(list_t *self, list_node_t *node);
list_node_t *
list_find(list_t *self, void *val);
list_node_t *
list_at(list_t *self, int index);
list_node_t *
list_rpop(list_t *self);
list_node_t *
list_lpop(list_t *self);
void
list_remove(list_t *self, list_node_t *node);
void
list_clear(list_t *self);
void
list_destroy(list_t *self);
// list_t iterator prototypes.
list_iterator_t *
list_iterator_new(list_t *list, list_direction_t direction);
list_iterator_t *
list_iterator_new_from_node(list_node_t *node, list_direction_t direction);
list_node_t *
list_iterator_next(list_iterator_t *self);
void
list_iterator_destroy(list_iterator_t *self);
#ifdef __cplusplus
}
#endif
#endif /* __CLIBS_LIST_H__ */