-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patharray.c
195 lines (142 loc) · 4.13 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <string.h>
#include "types.h"
#include "array.h"
#include "toy.h"
static Array* array_realloc(Array*);
static Array* array_realloc_size(Array*, int);
Array*
new_array() {
Array *array;
array = GC_MALLOC(sizeof(Array));
ALLOC_SAFE(array);
array->array = GC_MALLOC(sizeof(struct _toy_type) * ARRAY_INIT_SIZE);
ALLOC_SAFE(array->array);
memset(array->array, 0, sizeof(struct _toy_type) * ARRAY_INIT_SIZE);
array->alloc_size = ARRAY_INIT_SIZE;
array->cur_size = 0;
return array;
}
Array*
array_append(Array *array, struct _toy_type *item) {
if (NULL == array) return NULL;
if ((array->cur_size + 1) > array->alloc_size) {
if (NULL == array_realloc(array)) return NULL;
}
array->array[array->cur_size] = *item;
array->cur_size++;
return array;
}
Array*
array_set(Array *array, struct _toy_type *item, int pos) {
if (NULL == array) return NULL;
if ((pos < 0) || (pos >= array->cur_size)) return NULL;
array->array[pos] = *item;
return array;
}
struct _toy_type*
array_get(Array *array, int pos) {
struct _toy_type *item;
if (NULL == array) return NULL;
if ((pos < 0) || (pos >= array->cur_size)) return NULL;
item = GC_MALLOC(sizeof(struct _toy_type));
ALLOC_SAFE(item);
*item = array->array[pos];
return item;
}
int
array_get_size(Array *array) {
if (NULL == array) return -1;
return array->cur_size;
}
int
array_swap(Array *array, int pos1, int pos2) {
struct _toy_type t;
if (NULL == array) return 0;
if ((pos1 < 0) || (pos1 >= array->cur_size)) return 0;
if ((pos2 < 0) || (pos2 >= array->cur_size)) return 0;
t = array->array[pos1];
array->array[pos1] = array->array[pos2];
array->array[pos2] = t;
return 1;
}
Array*
array_resize(Array *array, int new_size) {
if (NULL == array) return NULL;
if (new_size < 0) return NULL;
return array_realloc_size(array, new_size);
}
static Array*
array_realloc(Array* array) {
struct _toy_type *orig_array, *new_array;
int alloc_size;
int i;
alloc_size = array->alloc_size * 2;
orig_array = array->array;
new_array = GC_MALLOC(sizeof(struct _toy_type) * alloc_size);
ALLOC_SAFE(new_array);
memset(new_array, 0, sizeof(struct _toy_type) * alloc_size);
for (i=0; i<array->cur_size; i++) {
new_array[i] = orig_array[i];
}
array->alloc_size = alloc_size;
array->array = new_array;
return array;
}
static Array*
array_realloc_size(Array* array, int size) {
struct _toy_type *new_t;
int i;
if (size < 0) return NULL;
if (size <= array->alloc_size) {
array->cur_size = size;
for (i=size; i<array->alloc_size; i++) {
memset(&array->array[i], 0, sizeof(struct _toy_type));
}
return array;
}
new_t = GC_MALLOC(sizeof(struct _toy_type) * ((size>0) ? size : ARRAY_INIT_SIZE));
ALLOC_SAFE(new_t);
memset(new_t, 0, sizeof(struct _toy_type) * ((size>0) ? size : ARRAY_INIT_SIZE));
for (i=0; i<size; i++) {
if (i >= array->cur_size) break;
new_t[i] = array->array[i];
}
array->array = new_t;
array->cur_size = size;
array->alloc_size = ((size>0) ? size : ARRAY_INIT_SIZE);
return array;
}
Array*
array_insert(Array *array, int pos, struct _toy_type *item) {
int i;
if (NULL == array) return NULL;
if (pos < 0) return NULL;
if (pos >= array->cur_size) {
return array_append(array, item);
}
if ((array->cur_size + 1) > array->alloc_size) {
if (NULL == array_realloc(array)) return NULL;
}
array->cur_size ++;
for (i=array->cur_size - 1; i>pos; i--) {
array->array[i] = array->array[i-1];
}
array->array[pos] = *item;
return array;
}
struct _toy_type*
array_delete(Array *array, int pos) {
int i;
struct _toy_type *item;
if (NULL == array) return NULL;
if (pos < 0) return NULL;
if (pos >= array->cur_size) return NULL;
item = GC_MALLOC(sizeof(struct _toy_type));
ALLOC_SAFE(item);
*item = array->array[pos];
array->cur_size --;
for (i=pos; i<array->cur_size; i++) {
array->array[i] = array->array[i+1];
}
return item;
}