-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.c
56 lines (51 loc) · 1.53 KB
/
array.c
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
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "data.h"
#include "array.h"
array_t *make_array(){
array_t *arr = malloc(sizeof(*arr));
int temp_size = ORI_SIZE;
arr->size = temp_size;
arr->multi_datapoint= malloc(temp_size * sizeof(*(arr->multi_datapoint)));
arr->n = 0;
return arr;
};
void arrayAppend(array_t *arr, datapoint_t* data){
if(arr->size == arr->n){
arr->size *= 2;
arr->multi_datapoint = realloc(arr->multi_datapoint, arr->size * sizeof(*(arr->multi_datapoint)));
}
if (arr->n < arr->size){
arr->multi_datapoint[arr->n] = data;
(arr->n)++;
}
}
void array_shrink(array_t *arr){
if (arr->size != arr->n) {
arr->size = (arr->n > ORI_SIZE) ? arr->n : ORI_SIZE;
arr->multi_datapoint = realloc(arr->multi_datapoint, arr->size * sizeof(*(arr->multi_datapoint)));
assert(arr->multi_datapoint);
}
}
void array_sort(array_t* arr){
for(int i = 0; i < arr->n; i++){
for(int j = i + 1; j < arr->n; j++){
if(arr->multi_datapoint[i]->record->footpath_id > arr->multi_datapoint[j]->record->footpath_id){
datapoint_t* temp = arr->multi_datapoint[i];
arr->multi_datapoint[i] = arr->multi_datapoint[j];
arr->multi_datapoint[j] = temp;
}
}
}
}
void array_free(array_t* arr){
for(int i = 0; i < arr->n; i++){
printf("Run2\n");
free(arr->multi_datapoint[i]->point);
free(arr->multi_datapoint[i]->record);
}
free(arr->multi_datapoint);
free(arr);
}