Browse Source

UNFINISHED. Work on lub_list

Serj Kalichev 6 years ago
parent
commit
7b5b31254a
3 changed files with 36 additions and 12 deletions
  1. 2 10
      clish/shell/shell_new.c
  2. 3 0
      lub/list.h
  3. 31 2
      lub/list/list.c

+ 2 - 10
clish/shell/shell_new.c

@@ -35,7 +35,7 @@ static void clish_shell_init(clish_shell_t * this,
 		clish_var_bt_compare, clish_var_bt_getkey);
 
 	/* Initialize plugin list */
-	this->plugins = lub_list_new(NULL, NULL);
+	this->plugins = lub_list_new(NULL, clish_plugin_free);
 
 	/* Initialise the list of unresolved (yet) symbols */
 	this->syms = lub_list_new(clish_sym_compare, clish_sym_free);
@@ -103,15 +103,7 @@ static void clish_shell_fini(clish_shell_t *this)
 	lub_list_node_t *iter;
 
 	/* Free all loaded plugins */
-	while ((iter = lub_list__get_head(this->plugins))) {
-		/* Remove the symbol from the list */
-		lub_list_del(this->plugins, iter);
-		/* Free the instance */
-		clish_plugin_free((clish_plugin_t *)lub_list_node__get_data(iter),
-			(void *)this);
-		lub_list_node_free(iter);
-	}
-	lub_list_free(this->plugins);
+	lub_list_free_all(this->plugins);
 
 	/* Delete each VIEW  */
 	lub_list_free_all(this->view_tree);

+ 3 - 0
lub/list.h

@@ -3,6 +3,7 @@
 
 #include <stddef.h>
 #include "lub/c_decl.h"
+#include "types.h"
 
 typedef struct lub_list_node_s lub_list_node_t;
 typedef int lub_list_compare_fn(const void *first, const void *second);
@@ -29,6 +30,8 @@ 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);
+lub_list_node_t *lub_list_add_uniq(lub_list_t *list, void *data);
+lub_list_node_t *lub_list_find_add(lub_list_t *list, void *data);
 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);

+ 31 - 2
lub/list/list.c

@@ -129,7 +129,13 @@ inline void *lub_list_node__get_data(lub_list_node_t *this)
 }
 
 /*--------------------------------------------------------- */
-lub_list_node_t *lub_list_add(lub_list_t *this, void *data)
+/* uniq - true/false Don't add entry with identical order
+ *	key (when the compareFn() returns 0)
+ * find - true/false Function returns list_node if there is
+ *	identical entry. Or NULL if find is false.
+ */
+static lub_list_node_t *lub_list_add_generic(lub_list_t *this, void *data,
+	bool_t uniq, bool_t find)
 {
 	lub_list_node_t *node = lub_list_node_new(data);
 	lub_list_node_t *iter;
@@ -155,7 +161,12 @@ lub_list_node_t *lub_list_add(lub_list_t *this, void *data)
 	/* Sorted list */
 	iter = this->tail;
 	while (iter) {
-		if (this->compareFn(node->data, iter->data) >= 0) {
+		int res = this->compareFn(node->data, iter->data);
+		if (uniq && (res == 0)) {
+			this->len--; // Revert previous increment
+			return (find ? iter : NULL);
+		}
+		if (res >= 0) {
 			node->next = iter->next;
 			node->prev = iter;
 			iter->next = node;
@@ -178,6 +189,24 @@ lub_list_node_t *lub_list_add(lub_list_t *this, void *data)
 	return node;
 }
 
+/*--------------------------------------------------------- */
+lub_list_node_t *lub_list_add(lub_list_t *this, void *data)
+{
+	return lub_list_add_generic(this, data, BOOL_FALSE, BOOL_FALSE);
+}
+
+/*--------------------------------------------------------- */
+lub_list_node_t *lub_list_add_uniq(lub_list_t *this, void *data)
+{
+	return lub_list_add_generic(this, data, BOOL_TRUE, BOOL_FALSE);
+}
+
+/*--------------------------------------------------------- */
+lub_list_node_t *lub_list_find_add(lub_list_t *this, void *data)
+{
+	return lub_list_add_generic(this, data, BOOL_TRUE, BOOL_TRUE);
+}
+
 /*--------------------------------------------------------- */
 void lub_list_del(lub_list_t *this, lub_list_node_t *node)
 {