Commit | Line | Data |
---|---|---|
e4001676 | 1 | #include <stdlib.h> |
475ee86d | 2 | #include <stdio.h> |
e4001676 | 3 | |
884e9557 | 4 | #include "array.h" |
e4001676 | 5 | |
34f864c6 | 6 | int* create_tab(int tab[], unsigned tab_size) { |
0b8ccced | 7 | tab = (int*)malloc(sizeof(int) * tab_size); |
34dd19e9 | 8 | if (tab != NULL) { |
34f864c6 JB |
9 | /* initialize to zero the integer array */ |
10 | for (unsigned i = 0; i < tab_size; i++) { | |
11 | tab[i] = 0; | |
12 | } | |
e4001676 | 13 | } |
34dd19e9 | 14 | return tab; |
e4001676 JB |
15 | } |
16 | ||
17 | void free_tab(int tab[]) { | |
1c0d8256 JB |
18 | if (!tab) |
19 | free(tab); | |
e4001676 JB |
20 | } |
21 | ||
34f864c6 JB |
22 | /* we suppose both tabs are already created */ |
23 | static void copy_tab(int src_tab[], int dest_tab[], unsigned src_tab_size, unsigned index_offset) { | |
24 | /* FIXME: I think it's worth doing some sanity checks on the array size: | |
25 | * dest_tab_size >= src_tab_size */ | |
475ee86d | 26 | if (src_tab == NULL || dest_tab == NULL) { |
a4e0f903 | 27 | printf("Please ensure you have created both arrays beforehand\n"); |
475ee86d JB |
28 | return; |
29 | } | |
34f864c6 | 30 | for (unsigned i = 0; i < src_tab_size; i++) { |
e4001676 JB |
31 | dest_tab[i + index_offset] = src_tab[i]; |
32 | } | |
33 | } | |
34 | ||
475ee86d JB |
35 | /* one must free the two source tabs in case they will be unused after to concatenation */ |
36 | int* concat_tab(int tab1[], unsigned tab_size1, int tab2[], unsigned tab_size2) { | |
37 | int* tab_dest = NULL; | |
38 | tab_dest = create_tab(tab_dest, tab_size1 + tab_size2); | |
e4001676 JB |
39 | |
40 | copy_tab(tab1, tab_dest, tab_size1, 0); | |
41 | copy_tab(tab2, tab_dest, tab_size2, tab_size1); | |
475ee86d | 42 | return tab_dest; |
e4001676 JB |
43 | } |
44 | ||
59941dc1 | 45 | int* resize_tab(int tab[], unsigned old_tab_size, unsigned new_tab_size) { |
0b8ccced | 46 | tab = (int*)realloc(tab, sizeof(int) * new_tab_size); |
ac5c12a4 | 47 | /* zero by default the added cells */ |
59941dc1 JB |
48 | if (old_tab_size < new_tab_size) { |
49 | for (unsigned i = old_tab_size; i < new_tab_size; i++) { | |
50 | tab[i] = 0; | |
51 | } | |
52 | } | |
34dd19e9 | 53 | return tab; |
e4001676 JB |
54 | } |
55 | ||
56 | /* number of occurences of an element in an unsorted array */ | |
889d5862 | 57 | unsigned count_tab_element(int tab[], unsigned tab_size, int element) { |
e4001676 | 58 | unsigned el_count = 0; |
889d5862 | 59 | |
e4001676 JB |
60 | for (unsigned i = 0; i < tab_size; i++) { |
61 | if (tab[i] == element) { | |
62 | el_count++; | |
63 | } | |
64 | } | |
65 | return el_count; | |
66 | } | |
67 | ||
210f7f05 | 68 | unsigned count_tab_criteria(int tab[], unsigned tab_size, c_criteria_cb c_criteria) { |
889d5862 JB |
69 | unsigned cr_count = 0; |
70 | ||
71 | for (unsigned i = 0; i < tab_size; i++) { | |
72 | if (c_criteria(tab[i])) { | |
73 | cr_count++; | |
74 | } | |
75 | } | |
76 | return cr_count; | |
77 | } | |
78 | ||
210f7f05 JB |
79 | void sort_tab(int tab[], unsigned tab_size, s_criteria_cb sort_criteria) { |
80 | sort_bubble_array(tab, tab_size, sort_criteria); | |
e4001676 | 81 | } |