aboutsummaryrefslogtreecommitdiffstats
path: root/libparc/parc/algol/parc_List.c
blob: 0e62cda6fa4680d60394841acb22050c3295c360 (plain)
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
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 */
#include <config.h>

#include <LongBow/runtime.h>

#include <stdio.h>
#include <stdarg.h>

#include <parc/algol/parc_Object.h>

#include <parc/algol/parc_List.h>
#include <parc/algol/parc_Memory.h>

struct parc_list {
    void *instance;
    const PARCListInterface *interface;
};

static void
_destroy(PARCList **listPtr)
{
    PARCList *list = *listPtr;

    (list->interface->Destroy)(&list->instance);
}

parcObject_ExtendPARCObject(PARCList, _destroy, parcList_Copy, NULL, parcList_Equals, NULL, parcList_HashCode, NULL);

PARCList *
parcList(void *instance, PARCListInterface *interface)
{
    PARCList *result = parcObject_CreateInstance(PARCList);
    if (result != NULL) {
        result->instance = instance;
        result->interface = interface;
    }

    return result;
}

PARCList *
parcList_Create(PARCObject *instance, PARCListInterface *interface)
{
    PARCList *result = parcObject_CreateInstance(PARCList);
    if (result != NULL) {
        result->instance = parcObject_Acquire(instance);
        result->interface = interface;
    }

    return result;
}

parcObject_ImplementAcquire(parcList, PARCList);

parcObject_ImplementRelease(parcList, PARCList);

PARCList *
parcList_Copy(const PARCList *list)
{
    PARCList *result = parcObject_CreateInstance(PARCList);
    if (result != NULL) {
        result->instance = (list->interface->Copy)(list->instance);
        result->interface = list->interface;
    }

    return result;
}

bool
parcList_IsEmpty(const PARCList *list)
{
    bool result = false;
    if (list->interface->IsEmpty) {
        result = (list->interface->IsEmpty)(list->instance);
    } else {
        result = (parcList_Size(list) == 0);
    }
    return result;
}

bool
parcList_Add(PARCList *list, void *element)
{
    return (list->interface->Add)(list->instance, element);
}

bool
parcList_AddAll(PARCList *list, size_t argc, void *argv[argc])
{
    for (int i = 0; i < argc; i++) {
        (list->interface->Add)(list->instance, argv[i]);
    }
    return true;
}

void
parcList_AddAtIndex(PARCList *list, int index, void *element)
{
    (list->interface->AddAtIndex)(list->instance, index, element);
}

bool
parcList_AddCollection(PARCList *list, PARCCollection *collection)
{
    return (list->interface->AddCollection)(list->instance, collection);
}

bool
parcList_AddCollectionAtIndex(PARCList *list, int index, PARCCollection *collection)
{
    return (list->interface->AddCollectionAtIndex)(list->instance, index, collection);
}

void
parcList_Clear(PARCList *list)
{
    if (!list->interface->Clear) {
        for (size_t i = 0; i < parcList_Size(list); i++) {
            parcList_RemoveAtIndex(list, i);
        }
    } else {
        (list->interface->Clear)(list->instance);
    }
}

bool
parcList_Contains(const PARCList *list, void *element)
{
    return (list->interface->Contains)(list->instance, element);
}

bool
parcList_ContainsCollection(PARCList *list, PARCCollection *collection)
{
    return (list->interface->ContainsCollection)(list->instance, collection);
}

bool
parcList_Equals(const PARCList *x, const PARCList *y)
{
    return (x->interface->Equals)(x->instance, y->instance);
}

void *
parcList_GetAtIndex(const PARCList *list, size_t index)
{
    return (list->interface->GetAtIndex)(list->instance, index);
}

int
parcList_HashCode(const PARCList *list)
{
    return (list->interface->HashCode)(list->instance);
}

ssize_t
parcList_IndexOf(const PARCList *list, PARCObject *element)
{
    ssize_t result = -1;

    if (list->interface->IndexOf) {
        result = (list->interface->IndexOf)(list->instance, element);
    } else {
        for (ssize_t i = 0; i < parcList_Size(list); i++) {
            PARCObject *e = parcList_GetAtIndex(list, i);
            if (parcObject_Equals(e, element)) {
                result = i;
                break;
            }
        }
    }

    return result;
}

ssize_t
parcList_LastIndexOf(const PARCList *list, PARCObject *element)
{
    ssize_t result = -1;

    if (list->interface->LastIndexOf) {
        result = (list->interface->LastIndexOf)(list->instance, element);
    } else {
        for (ssize_t i = parcList_Size(list) - 1; i >= 0; i--) {
            PARCObject *e = parcList_GetAtIndex(list, i);
            if (parcObject_Equals(e, element)) {
                result = i;
                break;
            }
        }
    }

    return result;
}

PARCObject *
parcList_RemoveAtIndex(PARCList *list, size_t index)
{
    if (list->interface->RemoveAtIndex) {
        return (list->interface->RemoveAtIndex)(list->instance, index);
    } else {
        return NULL;
    }
}

bool
parcList_Remove(PARCList *list, PARCObject *element)
{
    bool result = false;

    if (list->interface->Remove != NULL) {
        result = (list->interface->Remove)(list->instance, element);
    } else {
        for (size_t i = 0; i < parcList_Size(list); i++) {
            void *e = parcList_GetAtIndex(list, i);
            if (parcObject_Equals(e, element)) {
                parcList_RemoveAtIndex(list, i);
                result = true;
                break;
            }
        }
    }

    return result;
}

bool
parcList_RemoveCollection(PARCList *list, PARCCollection *collection)
{
    return (list->interface->RemoveCollection)(list->instance, collection);
}

bool
parcList_RetainCollection(PARCList *list, PARCCollection *collection)
{
    return (list->interface->RetainCollection)(list->instance, collection);
}

PARCObject *
parcList_SetAtIndex(PARCList *list, size_t index, void *element)
{
    return (list->interface->SetAtIndex)(list->instance, index, element);
}

size_t
parcList_Size(const PARCList *list)
{
    return (list->interface->Size)(list->instance);
}

PARCList *
parcList_SubList(PARCList *list, size_t fromIndex, size_t toIndex)
{
    return (list->interface->SubList)(list->instance, fromIndex, toIndex);
}

void**
parcList_ToArray(PARCList *list)
{
    return (list->interface->ToArray)(list->instance);
}