-
Notifications
You must be signed in to change notification settings - Fork 3
/
LCArray.c
311 lines (271 loc) · 9.57 KB
/
LCArray.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include "LCArray.h"
typedef struct arrayData* arrayDataRef;
LCCompare arrayCompare(LCObjectRef object1, LCObjectRef object2);
void arrayDealloc(LCObjectRef object);
void arrayWalkChildren(LCObjectRef object, void *cookie, childCallback cb);
void arrayStoreChildren(LCObjectRef object, char *key, LCObjectRef objects[], size_t length);
static void* arrayInitData();
bool resizeBuffer(arrayDataRef array, size_t size);
void mutableArraySerialize(LCObjectRef object, void* cookie, callback flush, FILE* fd);
struct arrayData {
size_t length;
size_t bufferLength;
LCObjectRef* objects;
};
struct LCType typeArray = {
.name = "LCArray",
.serializationFormat = LCText,
.immutable = true,
.dealloc = arrayDealloc,
.compare = arrayCompare,
.initData = arrayInitData,
.walkChildren = arrayWalkChildren,
.storeChildren = arrayStoreChildren
};
struct LCType typeMutableArray = {
.name = "LCMutableArray",
.serializationFormat = LCText,
.immutable = false,
.dealloc = arrayDealloc,
.compare = arrayCompare,
.initData = arrayInitData,
.walkChildren = arrayWalkChildren,
.storeChildren = arrayStoreChildren
};
LCTypeRef LCTypeArray = &typeArray;
LCTypeRef LCTypeMutableArray = &typeMutableArray;
static void* arrayInitData() {
arrayDataRef newArray = malloc(sizeof(struct arrayData));
if (newArray) {
newArray->length = 0;
newArray->objects = NULL;
}
return newArray;
}
static void arraySetObjects(arrayDataRef data, LCObjectRef objects[], size_t length) {
for(LCInteger i=0; i<length; i++) {
objectRetain(objects[i]);
}
resizeBuffer(data, length);
memcpy(data->objects, objects, length * sizeof(LCObjectRef));
data->length = length;
}
LCArrayRef LCArrayCreate(LCObjectRef objects[], size_t length) {
arrayDataRef newArray = arrayInitData();
newArray->objects = NULL;
arraySetObjects(newArray, objects, length);
return objectCreate(LCTypeArray, newArray);
};
LCArrayRef LCArrayCreateAppendingObject(LCArrayRef array, LCObjectRef object) {
return LCArrayCreateAppendingObjects(array, &object, 1);
}
LCArrayRef LCArrayCreateAppendingObjects(LCArrayRef object, LCObjectRef objects[], size_t length) {
if (!objectsImmutable(objects, length)) {
perror(ErrorObjectImmutable);
return NULL;
}
arrayDataRef array = objectData(object);
size_t totalLength = array->length + length;
LCObjectRef* newObjects = malloc(totalLength * sizeof(LCObjectRef));
if(newObjects) {
memcpy(newObjects, array->objects, array->length * sizeof(LCObjectRef));
memcpy(&(newObjects[array->length]), objects, length * sizeof(LCObjectRef));
arrayDataRef data = arrayInitData();
data->objects = newObjects;
data->length = totalLength;
for (LCInteger i=0; i<totalLength; i++) {
objectRetain(newObjects[i]);
}
return objectCreate(LCTypeArray, data);
} else {
return NULL;
}
}
LCArrayRef LCArrayCreateFromArrays(LCArrayRef arrays[], size_t length) {
size_t totalLength = 0;
for (LCInteger i=0; i<length; i++) {
totalLength = totalLength + LCArrayLength(arrays[i]);
}
LCObjectRef* newObjects = malloc(totalLength * sizeof(LCObjectRef));
if (newObjects) {
arrayDataRef newArray = arrayInitData();
newArray->objects = newObjects;
newArray->length = totalLength;
size_t copyPos = 0;
for (LCInteger i=0; i<length; i++) {
size_t copyLength = LCArrayLength(arrays[i]);
memcpy(&(newArray->objects[copyPos]), LCArrayObjects(arrays[i]), copyLength * sizeof(void*));
copyPos = copyPos+copyLength;
}
for (LCInteger i=0; i<totalLength; i++) {
objectRetain(newArray->objects[i]);
}
return objectCreate(LCTypeArray, newArray);
} else {
return NULL;
}
}
LCObjectRef* LCArrayObjects(LCArrayRef object) {
arrayDataRef array = objectData(object);
return array->objects;
}
LCObjectRef LCArrayObjectAtIndex(LCArrayRef object, LCInteger index) {
arrayDataRef array = objectData(object);
return array->objects[index];
}
size_t LCArrayLength(LCArrayRef object) {
arrayDataRef array = objectData(object);
return array->length;
}
LCArrayRef LCArrayCreateSubArray(LCArrayRef object, LCInteger start, size_t length) {
size_t arrayLength = LCArrayLength(object);
LCObjectRef* arrayObjects = LCArrayObjects(object);
if (start >= arrayLength) {
return LCArrayCreate(NULL, 0);
}
if (length == -1) {
length = arrayLength - start;
}
return LCArrayCreate(&(arrayObjects[start]), length);
}
LCArrayRef LCArrayCreateArrayWithMap(LCArrayRef array, void* info, LCCreateEachCb each) {
size_t arrayLength = LCArrayLength(array);
LCArrayRef* arrayObjects = LCArrayObjects(array);
LCArrayRef newObjects[arrayLength];
for (LCInteger i=0; i<arrayLength; i++) {
newObjects[i] = each(i, info, arrayObjects[i]);
}
LCArrayRef newArray = LCArrayCreate(newObjects, arrayLength);
for (LCInteger i=0; i<arrayLength; i++) {
objectRelease(newObjects[i]);
}
return newArray;
}
LCCompare arrayCompare(LCObjectRef array1, LCObjectRef array2) {
LCCompare result;
size_t array1Length = LCArrayLength(array1);
size_t array2Length = LCArrayLength(array2);
size_t checkLength;
if (LCArrayLength(array1) == LCArrayLength(array2)) {
checkLength = array1Length;
result = LCEqual;
} else if (LCArrayLength(array1) > LCArrayLength(array2)) {
checkLength = array2Length;
result = LCGreater;
} else {
checkLength = array1Length;
result = LCSmaller;
}
for (LCInteger i=0; i<checkLength; i++) {
LCCompare eachResult = objectCompare(LCArrayObjectAtIndex(array1, i), LCArrayObjectAtIndex(array2, i));
if (eachResult != LCEqual) {
return eachResult;
}
}
return result;
}
void arrayDealloc(LCObjectRef object) {
for (LCInteger i=0; i<LCArrayLength(object); i++) {
objectRelease(LCArrayObjectAtIndex(object, i));
}
lcFree(objectData(object));
}
void arrayWalkChildren(LCObjectRef object, void *cookie, childCallback cb) {
cb(cookie, "objects", LCArrayObjects(object), LCArrayLength(object), false);
}
void arrayStoreChildren(LCObjectRef object, char *key, LCObjectRef objects[], size_t length) {
if (strcmp(key, "objects")==0) {
arraySetObjects(objectData(object), objects, length);
}
}
// LCMutableArray
LCMutableArrayRef LCMutableArrayCreate(LCObjectRef objects[], size_t length) {
arrayDataRef newArray = arrayInitData();
newArray->objects = NULL;
if(length > 0) {
arraySetObjects(newArray, objects, length);
} else {
resizeBuffer(newArray, 10);
}
return objectCreate(LCTypeMutableArray, newArray);
};
inline LCObjectRef* LCMutableArrayObjects(LCMutableArrayRef array) {
return LCArrayObjects(array);
}
inline LCObjectRef LCMutableArrayObjectAtIndex(LCMutableArrayRef array, LCInteger index) {
return LCArrayObjectAtIndex(array, index);
}
inline size_t LCMutableArrayLength(LCMutableArrayRef array) {
return LCArrayLength(array);
}
inline LCMutableArrayRef LCMutableArrayCreateSubArray(LCMutableArrayRef array, LCInteger start, size_t length) {
return LCArrayCreateSubArray(array, start, length);
}
LCMutableArrayRef LCMutableArrayCreateFromArray(LCArrayRef array) {
return LCMutableArrayCreate(LCArrayObjects(array), LCArrayLength(array));
}
LCArrayRef LCMutableArrayCreateArray(LCMutableArrayRef array) {
return LCArrayCreate(LCMutableArrayObjects(array), LCMutableArrayLength(array));
}
LCMutableArrayRef LCMutableArrayCopy(LCMutableArrayRef array) {
return LCMutableArrayCreate(LCMutableArrayObjects(array), LCMutableArrayLength(array));
}
void LCMutableArrayAddObject(LCMutableArrayRef array, LCObjectRef object) {
arrayDataRef arrayData = objectData(array);
size_t arrayLength = LCMutableArrayLength(array);
if(arrayLength+1 > arrayData->bufferLength) {
resizeBuffer(arrayData, arrayData->bufferLength*2);
}
objectRetain(object);
LCMutableArrayObjects(array)[arrayLength] = object;
arrayData->length = arrayLength + 1;
}
void LCMutableArrayAddObjects(LCMutableArrayRef array, LCObjectRef objects[], size_t length) {
for (LCInteger i=0; i<length; i++) {
LCMutableArrayAddObject(array, objects[i]);
}
}
void LCMutableArrayRemoveIndex(LCMutableArrayRef array, LCInteger index) {
LCObjectRef* arrayObjects = LCMutableArrayObjects(array);
size_t arrayLength = LCMutableArrayLength(array);
arrayDataRef arrayData = objectData(array);
objectRelease(arrayObjects[index]);
if (index < (arrayLength-1)) {
size_t objectsToCopy = arrayLength - (index+1);
memmove(&(arrayObjects[index]), &(arrayObjects[index+1]), objectsToCopy*sizeof(LCObjectRef));
}
arrayData->length = arrayLength-1;
}
void LCMutableArrayRemoveObject(LCMutableArrayRef array, LCObjectRef object) {
for (LCInteger i=0; i<LCMutableArrayLength(object); i++) {
if(LCMutableArrayObjectAtIndex(array, i) == object) {
return LCMutableArrayRemoveIndex(array, i);
}
}
}
void LCMutableArraySort(LCMutableArrayRef array) {
objectsSort(LCMutableArrayObjects(array), LCMutableArrayLength(array));
}
LCMutableArrayRef LCArrayCreateMutableArrayWithMap(LCArrayRef array, void* info, LCCreateEachCb each) {
size_t arrayLength = LCArrayLength(array);
LCArrayRef* arrayObjects = LCArrayObjects(array);
LCArrayRef newObjects[arrayLength];
for (LCInteger i=0; i<arrayLength; i++) {
newObjects[i] = each(i, info, arrayObjects[i]);
}
LCArrayRef newArray = LCMutableArrayCreate(newObjects, arrayLength);
for (LCInteger i=0; i<arrayLength; i++) {
objectRelease(newObjects[i]);
}
return newArray;
}
bool resizeBuffer(arrayDataRef array, size_t length) {
void* buffer = realloc(array->objects, sizeof(void*) * length);
if(buffer) {
array->objects = buffer;
array->bufferLength = length;
return true;
} else {
return false;
}
}