Commit f201a8182628158121838232e609e9e8bded0d70

Thomas de Grivel 2024-11-19T11:46:20

list_unique => List.unique

diff --git a/lib/kc3/0.1/list.kc3 b/lib/kc3/0.1/list.kc3
index 682c4dc..3060f05 100644
--- a/lib/kc3/0.1/list.kc3
+++ b/lib/kc3/0.1/list.kc3
@@ -46,4 +46,6 @@ defmodule List do
 
   def sort_by = cfn List "list_sort_by" (List, Callable, Result)
 
+  def unique = cfn List "list_unique" (List, Result)
+
 end
diff --git a/libkc3/list.c b/libkc3/list.c
index 869bd24..29d05ba 100644
--- a/libkc3/list.c
+++ b/libkc3/list.c
@@ -520,3 +520,30 @@ s_array * list_to_array (s_list *list, const s_sym *array_type,
   free(tmp.dimensions);
   return NULL;
 }
+
+s_list ** list_unique (s_list **list, s_list **dest)
+{
+  bool found;
+  s_list *l;
+  s_list **tail;
+  s_list *tmp = NULL;
+  assert(list);
+  assert(dest);
+  tail = &tmp;
+  l = *list;
+  while (l) {
+    if (! list_has((const s_list * const *) &tmp, &l->tag, &found))
+      goto ko;
+    if (! found) {
+      if (! (*tail = list_new_tag_copy(&l->tag, NULL)))
+        goto ko;
+      tail = &(*tail)->next.data.list;
+    }
+    l = list_next(l);
+  }
+  *dest = tmp;
+  return dest;
+ ko:
+  list_delete_all(tmp);
+  return NULL;
+}
diff --git a/libkc3/list.h b/libkc3/list.h
index 1893803..0f81a5a 100644
--- a/libkc3/list.h
+++ b/libkc3/list.h
@@ -69,5 +69,6 @@ s_list ** list_sort_by (s_list **list, p_callable *compare,
                         s_list **dest);
 s_array * list_to_array (s_list *list, const s_sym *type,
                          s_array *dest);
+s_list ** list_unique (s_list **list, s_list **dest);
 
 #endif /* LIBKC3_LIST_H */