Browse Source

Update lub/list from birq project

Serj Kalichev 10 years ago
parent
commit
752d8aca88
3 changed files with 47 additions and 37 deletions
  1. 10 37
      lub/list.h
  2. 36 0
      lub/list/list.c
  3. 1 0
      lub/list/private.h

+ 10 - 37
lub/list.h

@@ -4,58 +4,31 @@
 #include <stddef.h>
 #include "lub/c_decl.h"
 
-/****************************************************************
- * TYPE DEFINITIONS
- **************************************************************** */
-
 typedef struct lub_list_node_s lub_list_node_t;
-
-/**
- * This type defines a callback function which will compare two nodes
- * with each other
- *
- * \param clientnode 	the client node to compare
- * \param clientkey 	the key to compare with a node
- *
- * \return
- *     <0 if clientnode  < clientkey;
- *      0 if clientnode == clientkey;
- *     >0 if clientnode  > clientkey
- */
 typedef int lub_list_compare_fn(const void *first, const void *second);
-
-/**
- * This type represents a list instance
- */
 typedef struct lub_list_s lub_list_t;
-
-/**
- * This is used to perform iterations of a list
- */
 typedef struct lub_list_node_s lub_list_iterator_t;
 
 _BEGIN_C_DECL
-/****************************************************************
- * LIST OPERATIONS
- **************************************************************** */
-/**
- * This operation initialises an instance of a list.
- */
-lub_list_t *lub_list_new(lub_list_compare_fn compareFn);
+
 lub_list_node_t *lub_list_node_new(void *data);
-void lub_list_free(lub_list_t *list);
-void lub_list_node_free(lub_list_node_t *node);
-lub_list_node_t *lub_list__get_head(lub_list_t *list);
-lub_list_node_t *lub_list__get_tail(lub_list_t *list);
 lub_list_node_t *lub_list_node__get_prev(lub_list_node_t *node);
 lub_list_node_t *lub_list_node__get_next(lub_list_node_t *node);
 void *lub_list_node__get_data(lub_list_node_t *node);
+void lub_list_node_free(lub_list_node_t *node);
+void lub_list_node_copy(lub_list_node_t *dst, lub_list_node_t *src);
+
+lub_list_t *lub_list_new(lub_list_compare_fn compareFn);
+void lub_list_free(lub_list_t *list);
+lub_list_node_t *lub_list__get_head(lub_list_t *list);
+lub_list_node_t *lub_list__get_tail(lub_list_t *list);
 lub_list_node_t *lub_list_iterator_init(lub_list_t *list);
 lub_list_node_t *lub_list_iterator_next(lub_list_node_t *node);
 lub_list_node_t *lub_list_iterator_prev(lub_list_node_t *node);
 lub_list_node_t *lub_list_add(lub_list_t *list, void *data);
 void lub_list_del(lub_list_t *list, lub_list_node_t *node);
-void lub_list_node_copy(lub_list_node_t *dst, lub_list_node_t *src);
+lub_list_node_t *lub_list_search(lub_list_t *list, void *data);
+unsigned int lub_list_len(lub_list_t *list);
 
 _END_C_DECL
 #endif				/* _lub_list_h */

+ 36 - 0
lub/list/list.c

@@ -11,6 +11,7 @@ static inline void lub_list_init(lub_list_t * this,
 	this->head = NULL;
 	this->tail = NULL;
 	this->compareFn = compareFn;
+	this->len = 0;
 }
 
 /*--------------------------------------------------------- */
@@ -111,6 +112,8 @@ lub_list_node_t *lub_list_add(lub_list_t *this, void *data)
 	lub_list_node_t *node = lub_list_node_new(data);
 	lub_list_node_t *iter;
 
+	this->len++;
+
 	/* Empty list */
 	if (!this->head) {
 		this->head = node;
@@ -164,10 +167,43 @@ void lub_list_del(lub_list_t *this, lub_list_node_t *node)
 		node->next->prev = node->prev;
 	else
 		this->tail = node->prev;
+
+	this->len--;
 }
 
+/*--------------------------------------------------------- */
 inline void lub_list_node_copy(lub_list_node_t *dst, lub_list_node_t *src)
 {
 	memcpy(dst, src, sizeof(lub_list_node_t));
 }
+
+/*--------------------------------------------------------- */
+lub_list_node_t *lub_list_search(lub_list_t *this, void *data)
+{
+	lub_list_node_t *iter;
+
+	/* Empty list */
+	if (!this->head)
+		return NULL;
+	/* Not sorted list. Can't search. */
+	if (!this->compareFn)
+		return NULL;
+
+	/* Sorted list */
+	iter = this->head;
+	while (iter) {
+		if (!this->compareFn(data, iter->data))
+			return iter;
+		iter = iter->next;
+	}
+
+	return NULL;
+}
+
+/*--------------------------------------------------------- */
+inline unsigned int lub_list_len(lub_list_t *this)
+{
+	return this->len;
+}
+
 /*--------------------------------------------------------- */

+ 1 - 0
lub/list/private.h

@@ -10,4 +10,5 @@ struct lub_list_s {
 	lub_list_node_t *head;
 	lub_list_node_t *tail;
 	lub_list_compare_fn *compareFn;
+	unsigned int len;
 };