-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.h
69 lines (53 loc) · 1.73 KB
/
common.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "acutest.h"
#define calc_time(cur_time) (((double)(clock() - cur_time))/CLOCKS_PER_SEC)
static inline int* allocate_array(unsigned int num_of_elements)
{
int* arr = malloc(sizeof(int) * num_of_elements);
assert(arr != NULL); // allocation failure
return arr;
}
// creates an ordered array eg. [0, 1, 2.. num_of_elements-1]
static inline int* create_ordered_array(unsigned int num_of_elements)
{
int* arr = allocate_array(num_of_elements);
for (unsigned int i = 0; i < num_of_elements; i++)
arr[i] = i;
return arr;
}
// creates a completely random array (duplicates allowed)
static inline int* create_random_array(unsigned int num_of_elements)
{
int* arr = allocate_array(num_of_elements);
for (unsigned int i = 0; i < num_of_elements; i++)
arr[i] = rand() % RAND_MAX;
return arr;
}
// creates a shuffled random array with elements in range [0, num_of_elements-1]
static inline int* create_shuffled_array(unsigned int num_of_elements)
{
int* arr = create_ordered_array(num_of_elements);
// shuffle the array
for (unsigned int i = 0; i < num_of_elements; i++)
{
// create random spot for the elements to swap places
unsigned int new_spot = i + rand() / (RAND_MAX / (num_of_elements-i) + 1);
int tmp = arr[i];
arr[i] = arr[new_spot];
arr[new_spot] = tmp;
}
return arr;
}
// allocates memory for an integer
int* createData(int a)
{
int* val = malloc(sizeof(int));
assert(val != NULL); // allocation failure
*val = a;
return val;
}
// compare function
int compareFunction(void* v1, void* v2) { return *((int*)v1) - *((int*)v2); }