+#include <stdlib.h>
+
+#include "sort.h"
+
+int create_tab(int tab[], unsigned tab_size) {
+ tab = malloc(sizeof(unsigned) * tab_size);
+ if (tab == NULL) {
+ return -1;
+ } else {
+ return 0;
+ }
+}
+
+void free_tab(int 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) {
+ for (unsigned i = 0; i < min_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);
+
+ copy_tab(tab1, tab_dest, tab_size1, 0);
+ copy_tab(tab2, tab_dest, tab_size2, tab_size1);
+ return rt;
+}
+
+int resize_tab(int tab[], unsigned tab_size) {
+ tab = realloc(tab, sizeof(int) * tab_size);
+ if (tab == NULL) {
+ return -1;
+ } else {
+ return 0;
+ }
+}
+
+/* number of occurences of an element in an unsorted array */
+int 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++;
+ }
+ }
+ return el_count;
+}
+
+void sort_tab(int tab[], unsigned tab_size, criteria_cb criteria) {
+ sort_bubble_array(tab, tab_size, criteria);
+}
#ifndef ARRAY_H
#define ARRAY_H
-
+int create_tab(int tab[], unsigned tab_size);
+void free_tab(int tab[]);
+int concat_tab(int tab1[], unsigned tab_size1, int tab2[], unsigned tab_size2, int tab_dest[]);
+int resize_tab(int tab[], unsigned tab_size);
+int count_tab_element(int tab[], unsigned tab_size, int element);
+void sort_tab(int tab[], unsigned tab_size, criteria_cb criteria);
#endif /* ARRAY_H */
#ifndef MACROS_H
#define MACROS_H
+#include <stdlib.h>
+
/* definition to expand macro then apply to pragma message */
#define VALUE_TO_STRING(x) #x
#define VALUE(x) VALUE_TO_STRING(x)
#define VAR_NAME_VALUE(var) #var "=" VALUE(var)
-#define ARRAY_SIZE(arr) ({typeof (arr) arr ## _is_a_pointer __attribute__((unused)) = {}; \
- sizeof(arr) / sizeof(arr[0]);})
+/* FIXME: ensure we manipulate real array */
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
#endif /* MACROS_H */
return a < b;
}
-static bool sort_first(int* array, int length, criteria_cb criteria) {
+bool ascending_and_even(int a, int b) {
+ return (ascending(a, b) && (a % 2 == 0));
+}
+
+bool ascending_and_odd(int a, int b) {
+ return (ascending(a, b) && (a % 2 != 0));
+}
+
+static bool sort_first(int* array, unsigned length, criteria_cb criteria) {
bool rt = false;
- for (int i = 0; i < length-1; i++) {
+ for (unsigned i = 0; i < length-1; i++) {
if (criteria(array[i], array[i+1])) {
swap_int(&array[i], &array[i+1]);
if (!rt) { rt = true; };
}
/* this function is awaited in the array.c file */
-void sort_array(int* array, int length, criteria_cb criteria) {
+void sort_bubble_array(int* array, unsigned length, criteria_cb criteria) {
bool rt;
do {
rt = sort_first(array, length, criteria);
/* sort criteria */
bool ascending(int a, int b);
bool descending(int a, int b);
+bool ascending_and_even(int a, int b);
+bool ascending_and_odd(int a, int b);
-void sort_array(int* array, int length, criteria_cb criteria);
+void sort_bubble_array(int* array, unsigned length, criteria_cb criteria);
#endif /* SORT_H */
+#include <stdlib.h>
#include <stdio.h>
int main() {
printf("Hello world\n");
- return 0;
+ exit(EXIT_SUCCESS);
}