ucx/list.c

changeset 152
62921b370c60
parent 124
80609f9675f1
child 157
0b33b9396851
--- a/ucx/list.c	Wed Nov 22 12:59:13 2017 +0100
+++ b/ucx/list.c	Sun Jan 21 12:13:09 2018 +0100
@@ -1,7 +1,7 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright 2015 Olaf Wintermann. All rights reserved.
+ * Copyright 2016 Olaf Wintermann. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:
@@ -76,6 +76,13 @@
     }
 }
 
+void ucx_list_free_content(UcxList* list, ucx_destructor destr) {
+    while (list != NULL) {
+        destr(list->data);
+        list = list->next;
+    }
+}
+
 UcxList *ucx_list_append(UcxList *l, void *data)  {
     return ucx_list_append_a(ucx_default_allocator(), l, data);
 }
@@ -99,6 +106,41 @@
     }
 }
 
+UcxList *ucx_list_append_once(UcxList *l, void *data,
+        cmp_func cmpfnc, void *cmpdata) {
+    return ucx_list_append_once_a(ucx_default_allocator(), l,
+            data, cmpfnc, cmpdata);
+}
+
+UcxList *ucx_list_append_once_a(UcxAllocator *alloc, UcxList *l, void *data,
+        cmp_func cmpfnc, void *cmpdata) {
+
+    UcxList *last = NULL;
+    {
+        UcxList *e = l;
+        while (e) {
+            if (cmpfnc(e->data, data, cmpdata) == 0) {
+                return l;
+            }
+            last = e;
+            e = e->next;
+        }
+    }
+    
+    UcxList *nl = ucx_list_append_a(alloc, NULL, data);
+    if (!nl) {
+        return NULL;
+    }
+
+    if (last == NULL) {
+        return nl;
+    } else {
+        nl->prev = last;
+        last->next = nl;
+        return l;
+    }
+}
+
 UcxList *ucx_list_prepend(UcxList *l, void *data) {
     return ucx_list_prepend_a(ucx_default_allocator(), l, data);
 }

mercurial