Browse Source

UNFINISHED: lub_list_match() funcs

Serj Kalichev 5 years ago
parent
commit
f00883166d
2 changed files with 46 additions and 0 deletions
  1. 7 0
      lub/list.h
  2. 39 0
      lub/list/list.c

+ 7 - 0
lub/list.h

@@ -8,6 +8,7 @@
 typedef struct lub_list_node_s lub_list_node_t;
 typedef int lub_list_compare_fn(const void *first, const void *second);
 typedef void lub_list_free_fn(void *data);
+typedef int lub_list_match_fn(const void *key, const void *data);
 typedef struct lub_list_s lub_list_t;
 typedef struct lub_list_node_s lub_list_iterator_t;
 
@@ -36,6 +37,12 @@ void lub_list_del(lub_list_t *list, lub_list_node_t *node);
 lub_list_node_t *lub_list_search_node(lub_list_t *list, void *data);
 void *lub_list_search(lub_list_t *list, void *data);
 unsigned int lub_list_len(lub_list_t *list);
+lub_list_node_t *lub_list_match_node(lub_list_t *list,
+	lub_list_match_fn matchFn, const void *userkey,
+	lub_list_node_t **saveptr);
+void *lub_list_match(lub_list_t *list,
+	lub_list_match_fn matchFn, const void *userkey,
+	lub_list_node_t **saveptr);
 
 _END_C_DECL
 #endif				/* _lub_list_h */

+ 39 - 0
lub/list/list.c

@@ -228,6 +228,45 @@ 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_match_node(lub_list_t *this,
+	lub_list_match_fn matchFn, const void *userkey,
+	lub_list_node_t **saveptr)
+{
+	lub_list_node_t *iter;
+	if (!this || !matchFn || !this->head)
+		return NULL;
+	if (saveptr)
+		iter = *saveptr;
+	if (!iter)
+		iter = this->head;
+	while (iter) {
+		int res;
+		lub_list_node_t *node = iter;
+		iter = lub_list_node__get_next(iter);
+		if (saveptr)
+			*saveptr = iter;
+		res = matchFn(userkey, lub_list_node__get_data(node));
+		if (!res)
+			return node;
+		if (res < 0) // No chances to find match
+			return NULL;
+	}
+
+	return NULL;
+}
+
+/*--------------------------------------------------------- */
+void *lub_list_match(lub_list_t *this,
+	lub_list_match_fn matchFn, const void *userkey,
+	lub_list_node_t **saveptr)
+{
+	lub_list_node_t *res = lub_list_match_node(this, matchFn, userkey, saveptr);
+	if (!res)
+		return NULL;
+	return lub_list_node__get_data(res);
+}
+
 /*--------------------------------------------------------- */
 lub_list_node_t *lub_list_search_node(lub_list_t *this, void *data)
 {