Move callback functions into the same file
[TD_C.git] / TP_13 / exo1 / lib / array.c
index c9a824676c26d89ca5acd337742cb57e8324b9ec..2077b3fde188848225829cb065ee2d01e8bdaeae 100644 (file)
@@ -1,48 +1,62 @@
 #include <stdlib.h>
+#include <stdio.h>
 
 #include "array.h"
 
-int create_tab(int tab[], unsigned tab_size) {
-    tab = malloc(sizeof(unsigned) * tab_size);
-    if (tab == NULL) {
-        return -1;
-    } else {
-        return 0;
+int* create_tab(int tab[], unsigned tab_size) {
+    tab = (int*)malloc(sizeof(int) * tab_size);
+    if (tab != NULL) {
+        /* initialize to zero the integer array */
+        for (unsigned i = 0; i < tab_size; i++) {
+            tab[i] = 0;
+        }
     }
+    return tab;
 }
 
 void free_tab(int tab[]) {
-    free(tab);
+    if (!tab)
+        free(tab);
 }
 
-/* we suppose both tab are already created */
-static void copy_tab(int src_tab[], int dest_tab[], unsigned min_tab_size, unsigned index_offset) {
-    /* FIXME: I think it's worth doing some sanity check on the array size */
-    for (unsigned i = 0; i < min_tab_size; i++) {
+/* we suppose both tabs are already created */
+static void copy_tab(int src_tab[], int dest_tab[], unsigned src_tab_size, unsigned index_offset) {
+    /* FIXME: I think it's worth doing some sanity checks on the array size:
+     * dest_tab_size >= src_tab_size */
+    if (src_tab == NULL || dest_tab == NULL) {
+        printf("Please ensure you have created both arrays beforehand\n");
+        return;
+    }
+    for (unsigned i = 0; i < src_tab_size; i++) {
         dest_tab[i + index_offset] = src_tab[i];
     }
 }
 
-int concat_tab(int tab1[], unsigned tab_size1, int tab2[], unsigned tab_size2, int tab_dest[]) {
-    int rt = create_tab(tab_dest, tab_size1 + tab_size2);
+/* one must free the two source tabs in case they will be unused after to concatenation  */
+int* concat_tab(int tab1[], unsigned tab_size1, int tab2[], unsigned tab_size2) {
+    int* tab_dest = NULL;
+    tab_dest = create_tab(tab_dest, tab_size1 + tab_size2);
 
     copy_tab(tab1, tab_dest, tab_size1, 0);
     copy_tab(tab2, tab_dest, tab_size2, tab_size1);
-    return rt;
+    return tab_dest;
 }
 
-int resize_tab(int tab[], unsigned tab_size) {
-    tab = realloc(tab, sizeof(int) * tab_size);
-    if (tab == NULL) {
-        return -1;
-    } else {
-        return 0;
+int* resize_tab(int tab[], unsigned old_tab_size, unsigned new_tab_size) {
+    tab = (int*)realloc(tab, sizeof(int) * new_tab_size);
+    /* zero by default the added cells */
+    if (old_tab_size < new_tab_size) {
+        for (unsigned i = old_tab_size; i < new_tab_size; i++) {
+            tab[i] = 0;
+        }
     }
+    return tab;
 }
 
 /* number of occurences of an element in an unsorted array  */
-int count_tab_element(int tab[], unsigned tab_size, int element) {
+unsigned count_tab_element(int tab[], unsigned tab_size, int element) {
     unsigned el_count = 0;
+
     for (unsigned i = 0; i < tab_size; i++) {
         if (tab[i] == element) {
             el_count++;
@@ -51,6 +65,17 @@ int count_tab_element(int tab[], unsigned tab_size, int element) {
     return el_count;
 }
 
-void sort_tab(int tab[], unsigned tab_size, criteria_cb criteria) {
-    sort_bubble_array(tab, tab_size, criteria);
+unsigned count_tab_criteria(int tab[], unsigned tab_size, c_criteria_cb c_criteria) {
+    unsigned cr_count = 0;
+
+    for (unsigned i = 0; i < tab_size; i++) {
+        if (c_criteria(tab[i])) {
+            cr_count++;
+        }
+    }
+    return cr_count;
+}
+
+void sort_tab(int tab[], unsigned tab_size, s_criteria_cb sort_criteria) {
+    sort_bubble_array(tab, tab_size, sort_criteria);
 }