formatted code.
This commit is contained in:
@@ -20,7 +20,8 @@
|
||||
*/
|
||||
|
||||
list_node_t *
|
||||
list_node_new(void *val) {
|
||||
list_node_new(void *val)
|
||||
{
|
||||
list_node_t *self;
|
||||
if (!(self = LIST_MALLOC(sizeof(list_node_t))))
|
||||
return NULL;
|
||||
@@ -35,7 +36,8 @@ list_node_new(void *val) {
|
||||
*/
|
||||
|
||||
list_t *
|
||||
list_new(void) {
|
||||
list_new(void)
|
||||
{
|
||||
list_t *self;
|
||||
if (!(self = LIST_MALLOC(sizeof(list_t))))
|
||||
return NULL;
|
||||
@@ -49,18 +51,20 @@ list_new(void) {
|
||||
|
||||
/*
|
||||
* Remove all the node in list.
|
||||
* @self: Pointer to the list
|
||||
* @self: Pointer to the list
|
||||
*/
|
||||
|
||||
void
|
||||
list_clear(list_t *self) {
|
||||
void list_clear(list_t *self)
|
||||
{
|
||||
uint32_t len = self->len;
|
||||
list_node_t *next;
|
||||
list_node_t *curr = self->head;
|
||||
|
||||
while (len) {
|
||||
while (len)
|
||||
{
|
||||
next = curr->next;
|
||||
if (self->free) self->free(curr->val);
|
||||
if (self->free)
|
||||
self->free(curr->val);
|
||||
LIST_FREE(curr);
|
||||
curr = next;
|
||||
len--;
|
||||
@@ -69,11 +73,11 @@ list_clear(list_t *self) {
|
||||
|
||||
/*
|
||||
* Free the list.
|
||||
* @self: Pointer to the list
|
||||
* @self: Pointer to the list
|
||||
*/
|
||||
|
||||
void
|
||||
list_destroy(list_t *self) {
|
||||
void list_destroy(list_t *self)
|
||||
{
|
||||
list_clear(self);
|
||||
LIST_FREE(self);
|
||||
}
|
||||
@@ -86,15 +90,20 @@ list_destroy(list_t *self) {
|
||||
*/
|
||||
|
||||
list_node_t *
|
||||
list_rpush(list_t *self, list_node_t *node) {
|
||||
if (!node) return NULL;
|
||||
list_rpush(list_t *self, list_node_t *node)
|
||||
{
|
||||
if (!node)
|
||||
return NULL;
|
||||
|
||||
if (self->len) {
|
||||
if (self->len)
|
||||
{
|
||||
node->prev = self->tail;
|
||||
node->next = NULL;
|
||||
self->tail->next = node;
|
||||
self->tail = node;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
self->head = self->tail = node;
|
||||
node->prev = node->next = NULL;
|
||||
}
|
||||
@@ -109,14 +118,19 @@ list_rpush(list_t *self, list_node_t *node) {
|
||||
*/
|
||||
|
||||
list_node_t *
|
||||
list_rpop(list_t *self) {
|
||||
if (!self->len) return NULL;
|
||||
list_rpop(list_t *self)
|
||||
{
|
||||
if (!self->len)
|
||||
return NULL;
|
||||
|
||||
list_node_t *node = self->tail;
|
||||
|
||||
if (--self->len) {
|
||||
if (--self->len)
|
||||
{
|
||||
(self->tail = node->prev)->next = NULL;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
self->tail = self->head = NULL;
|
||||
}
|
||||
|
||||
@@ -130,14 +144,19 @@ list_rpop(list_t *self) {
|
||||
*/
|
||||
|
||||
list_node_t *
|
||||
list_lpop(list_t *self) {
|
||||
if (!self->len) return NULL;
|
||||
list_lpop(list_t *self)
|
||||
{
|
||||
if (!self->len)
|
||||
return NULL;
|
||||
|
||||
list_node_t *node = self->head;
|
||||
|
||||
if (--self->len) {
|
||||
if (--self->len)
|
||||
{
|
||||
(self->head = node->next)->prev = NULL;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
self->head = self->tail = NULL;
|
||||
}
|
||||
|
||||
@@ -153,15 +172,20 @@ list_lpop(list_t *self) {
|
||||
*/
|
||||
|
||||
list_node_t *
|
||||
list_lpush(list_t *self, list_node_t *node) {
|
||||
if (!node) return NULL;
|
||||
list_lpush(list_t *self, list_node_t *node)
|
||||
{
|
||||
if (!node)
|
||||
return NULL;
|
||||
|
||||
if (self->len) {
|
||||
if (self->len)
|
||||
{
|
||||
node->next = self->head;
|
||||
node->prev = NULL;
|
||||
self->head->prev = node;
|
||||
self->head = node;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
self->head = self->tail = node;
|
||||
node->prev = node->next = NULL;
|
||||
}
|
||||
@@ -173,22 +197,29 @@ list_lpush(list_t *self, list_node_t *node) {
|
||||
/*
|
||||
* Return the node associated to val or NULL.
|
||||
* @self: Pointer to the list for finding given value
|
||||
* @val: Value to find
|
||||
* @val: Value to find
|
||||
*/
|
||||
|
||||
list_node_t *
|
||||
list_find(list_t *self, void *val) {
|
||||
list_find(list_t *self, void *val)
|
||||
{
|
||||
list_iterator_t *it = list_iterator_new(self, LIST_HEAD);
|
||||
list_node_t *node;
|
||||
|
||||
while ((node = list_iterator_next(it))) {
|
||||
if (self->compare) {
|
||||
if (self->compare(val, node->val) == 0) {
|
||||
while ((node = list_iterator_next(it)))
|
||||
{
|
||||
if (self->compare)
|
||||
{
|
||||
if (self->compare(val, node->val) == 0)
|
||||
{
|
||||
list_iterator_destroy(it);
|
||||
return node;
|
||||
}
|
||||
} else {
|
||||
if (val == node->val) {
|
||||
}
|
||||
else
|
||||
{
|
||||
if (val == node->val)
|
||||
{
|
||||
list_iterator_destroy(it);
|
||||
return node;
|
||||
}
|
||||
@@ -201,23 +232,27 @@ list_find(list_t *self, void *val) {
|
||||
|
||||
/*
|
||||
* Return the node at the given index or NULL.
|
||||
* @self: Pointer to the list for finding given index
|
||||
* @self: Pointer to the list for finding given index
|
||||
* @index: the index of node in the list
|
||||
*/
|
||||
|
||||
list_node_t *
|
||||
list_at(list_t *self, int index) {
|
||||
list_at(list_t *self, int index)
|
||||
{
|
||||
list_direction_t direction = LIST_HEAD;
|
||||
|
||||
if (index < 0) {
|
||||
if (index < 0)
|
||||
{
|
||||
direction = LIST_TAIL;
|
||||
index = ~index;
|
||||
}
|
||||
|
||||
if ((unsigned)index < self->len) {
|
||||
if ((unsigned)index < self->len)
|
||||
{
|
||||
list_iterator_t *it = list_iterator_new(self, direction);
|
||||
list_node_t *node = list_iterator_next(it);
|
||||
while (index--) node = list_iterator_next(it);
|
||||
while (index--)
|
||||
node = list_iterator_next(it);
|
||||
list_iterator_destroy(it);
|
||||
return node;
|
||||
}
|
||||
@@ -227,21 +262,22 @@ list_at(list_t *self, int index) {
|
||||
|
||||
/*
|
||||
* Remove the given node from the list, freeing it and it's value.
|
||||
* @self: Pointer to the list to delete a node
|
||||
* @self: Pointer to the list to delete a node
|
||||
* @node: Pointer the node to be deleted
|
||||
*/
|
||||
|
||||
void
|
||||
list_remove(list_t *self, list_node_t *node) {
|
||||
void list_remove(list_t *self, list_node_t *node)
|
||||
{
|
||||
node->prev
|
||||
? (node->prev->next = node->next)
|
||||
: (self->head = node->next);
|
||||
? (node->prev->next = node->next)
|
||||
: (self->head = node->next);
|
||||
|
||||
node->next
|
||||
? (node->next->prev = node->prev)
|
||||
: (self->tail = node->prev);
|
||||
? (node->next->prev = node->prev)
|
||||
: (self->tail = node->prev);
|
||||
|
||||
if (self->free) self->free(node->val);
|
||||
if (self->free)
|
||||
self->free(node->val);
|
||||
|
||||
LIST_FREE(node);
|
||||
--self->len;
|
||||
@@ -253,10 +289,11 @@ list_remove(list_t *self, list_node_t *node) {
|
||||
*/
|
||||
|
||||
list_iterator_t *
|
||||
list_iterator_new(list_t *list, list_direction_t direction) {
|
||||
list_iterator_new(list_t *list, list_direction_t direction)
|
||||
{
|
||||
list_node_t *node = direction == LIST_HEAD
|
||||
? list->head
|
||||
: list->tail;
|
||||
? list->head
|
||||
: list->tail;
|
||||
return list_iterator_new_from_node(node, direction);
|
||||
}
|
||||
|
||||
@@ -266,7 +303,8 @@ 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_iterator_new_from_node(list_node_t *node, list_direction_t direction)
|
||||
{
|
||||
list_iterator_t *self;
|
||||
if (!(self = LIST_MALLOC(sizeof(list_iterator_t))))
|
||||
return NULL;
|
||||
@@ -281,12 +319,14 @@ list_iterator_new_from_node(list_node_t *node, list_direction_t direction) {
|
||||
*/
|
||||
|
||||
list_node_t *
|
||||
list_iterator_next(list_iterator_t *self) {
|
||||
list_iterator_next(list_iterator_t *self)
|
||||
{
|
||||
list_node_t *curr = self->next;
|
||||
if (curr) {
|
||||
if (curr)
|
||||
{
|
||||
self->next = self->direction == LIST_HEAD
|
||||
? curr->next
|
||||
: curr->prev;
|
||||
? curr->next
|
||||
: curr->prev;
|
||||
}
|
||||
return curr;
|
||||
}
|
||||
@@ -295,8 +335,8 @@ list_iterator_next(list_iterator_t *self) {
|
||||
* Free the list iterator.
|
||||
*/
|
||||
|
||||
void
|
||||
list_iterator_destroy(list_iterator_t *self) {
|
||||
void list_iterator_destroy(list_iterator_t *self)
|
||||
{
|
||||
LIST_FREE(self);
|
||||
self = NULL;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user