File: AGArray.c

package info (click to toggle)
agsync 0.2-pre-9
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,044 kB
  • ctags: 1,183
  • sloc: ansic: 9,979; sh: 8,390; makefile: 86
file content (333 lines) | stat: -rw-r--r-- 7,741 bytes parent folder | download | duplicates (12)
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* The contents of this file are subject to the Mozilla Public License
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * The Original Code is Mobile Application Link.
 *
 * The Initial Developer of the Original Code is AvantGo, Inc.
 * Portions created by AvantGo, Inc. are Copyright (C) 1997-1999
 * AvantGo, Inc. All Rights Reserved.
 *
 * Contributor(s):
 */

// Owner: linus

#include <AGArray.h>
#include <AGUtil.h>

#define MIN_CAPACITY 8

ExportFunc AGArray *AGArrayNew(AGElementType elemType,
                               int32 initialCapacity)
{
    AGArray *array;

    array = (AGArray *)malloc(sizeof(AGArray));
    return AGArrayInit(array, elemType, initialCapacity);
}

ExportFunc AGArray *AGArrayInit(AGArray *array,
                                AGElementType elemType,
                                int32 initialCapacity)
{
    bzero(array, sizeof(*array));

    if (initialCapacity > 0) {
        AGArrayEnsureCapacity(array, initialCapacity);
    }

    AGCollectionCallbacksInit(&(array->callbacks), elemType);

    return array;
}

ExportFunc void AGArrayFinalize(AGArray *array)
{
    AGArrayRemoveAll(array);
    if(array->elements)
        free(array->elements);
    bzero(array, sizeof(*array));
}

ExportFunc void AGArrayFree(AGArray *array)
{
    AGArrayFinalize(array);
    free(array);
}

ExportFunc void AGArrayEnsureCapacity(AGArray *array, int32 minCapacity)
{
    int32 count, capacity, newCapacity;
    void **newElements;

    capacity = array->capacity;
    if (capacity >= minCapacity) {
        return;
    }

    newCapacity = capacity;
    if (newCapacity < MIN_CAPACITY) {
        newCapacity = MIN_CAPACITY;
    }

    while (newCapacity < minCapacity) {
        newCapacity *= 2;
    }

    newElements = (void**)malloc(newCapacity * sizeof(void *));

    count = array->count;
    if (count > 0) {
        bcopy(array->elements, newElements, count * sizeof(void *));
        free(array->elements);
    }

    bzero(newElements + count, (newCapacity - count) * sizeof(void *));
    array->elements = newElements;
    array->capacity = newCapacity;
}

ExportFunc void AGArraySetCallbacks(AGArray *array,
                                    AGCompareCallback compareFunc,
                                    AGInsertCallback insertFunc,
                                    AGRemoveCallback removeFunc)
{
    array->callbacks.compareFunc = compareFunc;
    array->callbacks.hashFunc = NULL;
    array->callbacks.insertFunc = insertFunc;
    array->callbacks.removeFunc = removeFunc;
}

ExportFunc int32 AGArrayCount(AGArray *array)
{
    if (NULL == array)
        return 0;
    else
        return array->count;
}

ExportFunc void *AGArrayElementAt(AGArray *array, int32 index)
{
    if (index >= array->count) {
        return NULL;
    }

    return array->elements[index];
}

ExportFunc void AGArrayInsertAt(AGArray *array, int32 index, void *elem)
{
    int32 count = array->count;
    void **elements;
    AGInsertCallback insertFunc;

    if (index >= count + 1) {
        return;
    }

    if (count >= array->capacity) {
        AGArrayEnsureCapacity(array, count + 1);
    }

    elements = array->elements;

    if (count - index > 0) {
        bcopy(elements + index, elements + index + 1, (count - index) * sizeof(void *));
    }

    insertFunc = array->callbacks.insertFunc;
    if (insertFunc != NULL) {
        elem = (*insertFunc)(elem);
    }

    elements[index] = elem;
    array->count = count + 1;
}

ExportFunc void AGArrayAppend(AGArray *array, void *elem)
{
    int32 count = array->count;
    AGInsertCallback insertFunc;

    if (count >= array->capacity) {
        AGArrayEnsureCapacity(array, count + 1);
    }

    insertFunc = array->callbacks.insertFunc;
    if (insertFunc != NULL) {
        elem = (*insertFunc)(elem);
    }

    array->elements[count] = elem;
    array->count++;
}

ExportFunc void AGArrayRemoveAt(AGArray *array, int32 index)
{
    int32 count = array->count;
    void **elements;
    AGRemoveCallback removeFunc;

    if (index >= count) {
        return;
    }

    elements = array->elements;

    removeFunc = array->callbacks.removeFunc;
    if (removeFunc != NULL) {
        (*removeFunc)(elements[index]);
    }

    if (count - index - 1 > 0) {
        bcopy(elements + index + 1, elements + index, 
            (count - index - 1) * sizeof(void *));
    }

    count--;
    elements[count] = NULL;
    array->count = count;
}

ExportFunc void AGArrayRemoveAll(AGArray *array)
{
    int32 i, count = array->count;
    void **elements;
    AGRemoveCallback removeFunc;

    if (count <= 0) {
        return;
    }

    elements = array->elements;

    removeFunc = array->callbacks.removeFunc;
    if (removeFunc != NULL) {
        for (i = 0; i < count; i++) {
            (*removeFunc)(elements[i]);
        }
    }

    bzero(elements, count * sizeof(void *));
    array->count = 0;
}

ExportFunc AGBool AGArrayAppendIfAbsent(AGArray *array, void *elem)
{
    int32 index;

    index = AGArrayIndexOf(array, elem, 0);
    if (index < 0) {
        AGArrayAppend(array, elem);
        return TRUE;
    }

    return FALSE;
}

ExportFunc void AGArrayAppendArray(AGArray *array, AGArray *other)
{
    int32 i, count = other->count;
    void **elements;

    elements = other->elements;

    for (i = 0; i < count; i++) {
        AGArrayAppend(array, elements[i]);
    }
}

ExportFunc void AGArrayReplaceAt(AGArray *array, int32 index, void *elem)
{
    int32 count = array->count;
    void **elements;
    AGRemoveCallback removeFunc;
    AGInsertCallback insertFunc;

    if (index >= count) {
        return;
    }

    elements = array->elements;

    // If we are replacing an element with itself, don't
    // invoke the callbacks.

    if (elem != elements[index]) {
        insertFunc = array->callbacks.insertFunc;
        if (insertFunc != NULL) {
            elem = (*insertFunc)(elem);
        }

        removeFunc = array->callbacks.removeFunc;
        if (removeFunc != NULL) {
            (*removeFunc)(elements[index]);
        }
    }

    elements[index] = elem;
}

ExportFunc int32 AGArrayIndexOf(AGArray *array, void *elem, int32 startIndex)
{
    int32 i, count = array->count;
    void **elements;
    AGCompareCallback compareFunc;

    elements = array->elements;
    compareFunc = array->callbacks.compareFunc;

    if (compareFunc != NULL) {
        for (i = startIndex; i < count; i++) {
            if ((*compareFunc)(elem, elements[i]) == 0) {
                return i;
            }
        }
    } else {
        for (i = startIndex; i < count; i++) {
            if (elem == elements[i]) {
                return i;
            }
        }
    }

    return -1;
}

ExportFunc int32 AGArrayLastIndexOf(AGArray *array, void *elem, int32 startIndex)
{
    int32 i, count = array->count;
    void **elements;
    AGCompareCallback compareFunc;

    if (startIndex >= count) {
        return -1;
    }

    elements = array->elements;
    compareFunc = array->callbacks.compareFunc;

    if (compareFunc != NULL) {
        for (i = startIndex; i >= 0; i--) {
            if ((*compareFunc)(elem, elements[i]) == 0) {
                return i;
            }
        }
    } else {
        for (i = startIndex; i >= 0; i--) {
            if (elem == elements[i]) {
                return i;
            }
        }
    }

    return -1;
}