-
Notifications
You must be signed in to change notification settings - Fork 0
/
grow_array.c
68 lines (49 loc) · 1.22 KB
/
grow_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
57
58
59
60
61
62
63
64
65
66
67
68
#include "grow_array.h"
static double *ga_location(ga_array *ga_array_ptr, size_t index) {
size_t skips = index/GARRAYBLOCK;
size_t subindex = index%GARRAYBLOCK;
size_t i;
ga_array *subarray;
subarray = ga_array_ptr;
for(i=0;i<skips;i++) {
if(subarray->next==NULL) {
subarray->next = malloc(sizeof(ga_array));
if(subarray->next==NULL) return NULL;
ga_init(subarray->next);
}
subarray=subarray->next;
}
return &(subarray->data[subindex]);
}
double ga_get(ga_array *array_ptr, size_t index) {
double *loc;
loc = ga_location(array_ptr,index);
return *loc;
}
int ga_set(ga_array *array_ptr, size_t index, double data) {
double *loc;
loc = ga_location(array_ptr,index);
if(loc==NULL) return -1;
*loc = data;
return 0;
}
double *ga_regularize(ga_array *array_ptr, size_t length) {
double *result;
size_t i;
result = malloc(length*sizeof(double));
if(result==NULL) return NULL;
for(i=0;i<length;i++) {
result[i] = ga_get(array_ptr,i);
}
return result;
}
void ga_free(ga_array *array_ptr) {
ga_array *thisone;
ga_array *nextone;
thisone = array_ptr->next;
while(thisone!=NULL) {
nextone=thisone->next;
free(thisone);
thisone=nextone;
}
}