6 int* create_tab(int tab
[], unsigned tab_size
) {
7 tab
= malloc(sizeof(int) * tab_size
);
9 /* initialize to zero the integer array */
10 for (unsigned i
= 0; i
< tab_size
; i
++) {
17 void free_tab(int tab
[]) {
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 */
26 if (src_tab
== NULL
|| dest_tab
== NULL
) {
27 printf("Please ensure you have created both arrays beforehand\n");
30 for (unsigned i
= 0; i
< src_tab_size
; i
++) {
31 dest_tab
[i
+ index_offset
] = src_tab
[i
];
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
) {
38 tab_dest
= create_tab(tab_dest
, tab_size1
+ tab_size2
);
40 copy_tab(tab1
, tab_dest
, tab_size1
, 0);
41 copy_tab(tab2
, tab_dest
, tab_size2
, tab_size1
);
45 int* resize_tab(int tab
[], unsigned old_tab_size
, unsigned new_tab_size
) {
46 tab
= realloc(tab
, sizeof(int) * new_tab_size
);
47 /* zero by default the added cells */
48 if (old_tab_size
< new_tab_size
) {
49 for (unsigned i
= old_tab_size
; i
< new_tab_size
; i
++) {
56 /* number of occurences of an element in an unsorted array */
57 unsigned count_tab_element(int tab
[], unsigned tab_size
, int element
) {
58 unsigned el_count
= 0;
60 for (unsigned i
= 0; i
< tab_size
; i
++) {
61 if (tab
[i
] == element
) {
68 unsigned count_tab_criteria(int tab
[], unsigned tab_size
, c_criteria_cb c_criteria
) {
69 unsigned cr_count
= 0;
71 for (unsigned i
= 0; i
< tab_size
; i
++) {
72 if (c_criteria(tab
[i
])) {
88 void sort_tab(int tab
[], unsigned tab_size
, s_criteria_cb sort_criteria
) {
89 sort_bubble_array(tab
, tab_size
, sort_criteria
);