aboutsummaryrefslogtreecommitdiff
path: root/src/mtx_qhull/list.c
blob: 754516f8e05fb5d3edbd4f8b08c3481cce3a5ade (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
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
334
335
336
337
338
339
340
341
342
343
#include "list.h"
#include <stdlib.h>
#include <stdio.h>

/*
 *  list operations for zhull
 *
 * Copyright (c) 2012, Franz Zotter,
 * with friendly help from
 * IOhannes zmoelnig
 * for variable entry types
 * in entry.h
 * IEM, Graz, Austria
 * 
 *
 */


// memory things:
list_t emptyList(void) {
    list_t generated_list;
    generated_list.length=0;
    generated_list.entries=0;
    return generated_list;
}

list_t allocateList(const size_t length) {
    list_t generated_list = emptyList();
    if (length>0) {
        generated_list.entries= (entry_t*) malloc(length*sizeof(entry_t));
        if (generated_list.entries!=0) {
            generated_list.length=length;
        } 
    }
    return generated_list;
}

void reallocateList(list_t *list,
        const size_t length) {
    entry_t *old_entries = list->entries;
    if (length>0) {
        if (getLength(*list)==0) 
            *list = allocateList(length);
        else {
            if (list->length != length)
                list->entries = (entry_t*) realloc(list->entries,length*sizeof(entry_t));
            if (list->entries!=0) 
                list->length=length;
            else 
                *list=emptyList();
        }
    } 
    else 
        freeList(list);
}


void freeList(list_t *list) {
    if (list->entries!=0) {
        free(list->entries);
    }
    list->entries=0;
    list->length=0;
}

// programming interface:

size_t getLength(const list_t list) {
    return list.length;
}

entry_t getEntry(const list_t list, const index_t index) {
    if ((index>=0)&&(index<getLength(list)))
        return list.entries[index];
    else {
      entry_t result={0};
        return result;
    }
}

void setEntry(const list_t list, const index_t index, 
        const entry_t entry) {
    if ((index>=0)&&(index<getLength(list)))
        list.entries[index]=entry;
}


list_t initList(const entry_t *entries, 
        const size_t length) {
    index_t i;
    list_t l = allocateList(length);
    if (getLength(l)!=0) 
        for (i=0; i<(index_t)length; i++) 
            setEntry(l,i,entries[i]);
    return l;
}

list_t initListIndex(const index_t *entries, const size_t length) {
  index_t i;
  list_t l = allocateList(length);
  if (getLength(l)!=0) 
    for (i=0; i<(index_t)length; i++) 
      setEntry(l,i,entry_makeIndex(entries[i]));
  return l;
}



list_t initListFromTo(const index_t start, const index_t stop) {
    entry_t e;
    index_t i;
    size_t length;
    index_t c;
    int incr;
    if (stop>=start) {
      length=(size_t) (stop-start+1);
        incr=1;
    } else {
      length=(size_t) (start-stop+1);
      incr=-1;
    }
    list_t l = allocateList(length);
    if (getLength(l)!=0) {
      for (i=0,c=start; i<length; i++, c+=incr) {
        entry_setIndex(&e, c);
        setEntry(l,i,e);
      }
    }
    return l;
}

list_t initConstantList(const entry_t c, const size_t length){
    index_t i;
    list_t l = allocateList(length);
    if (getLength(l)!=0) 
        for (i=0; i<length; i++)
            setEntry(l,i,c);
    return l;
}


list_t duplicateList(const list_t list_in) {
    index_t i;
    list_t list_out=emptyList();
    list_out = allocateList(getLength(list_in));
    for (i=0; i<getLength(list_out); i++) 
        setEntry(list_out,i,getEntry(list_in,i));
    return list_out;
}

list_t mergeLists(const list_t list1, const list_t list2) {
    list_t list_out;
    index_t i,j;
    list_out = allocateList(getLength(list1)+ getLength(list2));
    if (getLength(list_out)>=getLength(list1)) {
        for (i=0; i<getLength(list1); i++) 
            setEntry(list_out,i,getEntry(list1,i));
        for (j=0; i<getLength(list_out); i++, j++) 
            setEntry(list_out,i,getEntry(list2,j));
    }
    return list_out;
}

list_t getSubList(const list_t list, const list_t indices) {
    index_t i;
    list_t new_list = allocateList(getLength(indices));
    for (i=0; i<getLength(new_list); i++) {
        entry_t e1=getEntry(indices,i);
        setEntry(new_list,i,
                 getEntry(list,entry_getIndex(&e1))
                 );
    }
}

list_t getSubListFromTo(const list_t list, const index_t start, 
        const index_t stop)  {
    list_t new_list=emptyList();
    index_t i,j;
    int incr;
    if ((start>0)&&(stop>0)&&(start<getLength(list))&&(stop<getLength(list))) {
        if (start>stop) {
            incr=-1;
            new_list=allocateList(start-stop+1);
        } else {
            incr=1;
            new_list=allocateList(start-stop+1);
        }
        for (j=start,i=0; i<getLength(new_list); i++, j+=incr) {
            setEntry(new_list,i,getEntry(list,j));
        }
    }
    return new_list;
}

void appendToList(list_t *list, const entry_t entry) {
    const size_t i=getLength(*list);
    reallocateList(list,getLength(*list)+1);
    if (getLength(*list)>i) {
        setEntry(*list,i,entry);
    }
}

void removeIndexFromList(list_t *list, const index_t index) {
    index_t i,j;
    for (i=j=0; i<getLength(*list); i++) {
        if (i!=index) 
            setEntry(*list,j++,getEntry(*list,i));
    }
    reallocateList(list,j);
}


void removeValueFromList(list_t *list, const entry_t entry) {
    index_t i,j;
    for (j=i=0; i<getLength(*list); i++) {
      entry_t e1=getEntry(*list,i);
      if (!entry_equals(&e1, &entry)) 
        setEntry(*list, j++, getEntry(*list,i));
    }
    reallocateList(list,j);
}

void appendListToList(list_t *list1, const list_t list2) {
    index_t i,j;
    const size_t siz_old = getLength(*list1);
    reallocateList(list1, getLength(*list1) + getLength(list2));
    for (i=siz_old, j=0; i<getLength(*list1); i++, j++)  
        setEntry(*list1,i,getEntry(list2,j));
}

void removeEntryListFromList(list_t *list, const list_t indices) {
    index_t i,j;
    for (i=j=0; i<getLength(*list); i++) {
      entry_t e={i};
      if (notInList(e,indices)) 
        setEntry(*list, j++, getEntry(*list,i));
    }
    reallocateList(list,j);
}

void removeValueListFromList(list_t *list, const list_t excl_list) {
    index_t i,j,k;
    int keep;
    for (j=i=0; i<getLength(*list); i++) {
        keep=1;
        for (k=0; k<getLength(excl_list); k++) {
          entry_t e1=getEntry(*list, i);
          entry_t e2=getEntry( excl_list, k);
          keep=(keep)&&(!entry_equals(&e1, &e2));
        }
        if (keep)
            setEntry(*list, j++, getEntry(*list,i));
    }
    reallocateList(list,j);
}

void reverseList(list_t * const list) {
    index_t i,j;
    entry_t v;
    const cnt = getLength(*list)/ 2;
    if (cnt>0)
        for (i=0, j=getLength(*list)-1; i<cnt; i++, j--) {
            v=getEntry(*list,i);
            setEntry(*list,i,getEntry(*list,j));
            setEntry(*list,j,v);
        }
}

int inList(const entry_t entry, const list_t list) {
    index_t i;
    for (i=0; i<getLength(list); i++) {
      entry_t e1=getEntry(list,i);
      if(entry_equals(&e1, &entry))
        return 1;
    }
    return 0;
}

index_t findValueInList(const entry_t entry, const list_t list) {
    index_t i;
    for (i=0; i<getLength(list); i++) {
      entry_t e1=getEntry(list,i);
      if(entry_equals(&e1, &entry))
        return i;
    }
    return i;
}

void uniquefyListEntries(list_t *list)  {
    index_t i,j,k;
    k=0;
    for (j=0; j<getLength(*list); j++) {
        for (i=0; i<k; i++) {
          entry_t e1=list->entries[j];
          entry_t e2=list->entries[i];
          if(entry_equals(&e1, &e2))
            break;
        }
        if (i==k) {
            list->entries[i++]=list->entries[j];
            k++;
        }
    }
    reallocateList(list, k);
}

list_t findValueListInList(const list_t value_list,
        const list_t list) {
    list_t l=emptyList();
    index_t i,j;
    for (i=0; i<getLength(value_list); i++) {
      index_t idx=findValueInList(getEntry(value_list,i),list);
      entry_t e;
      entry_setIndex(&e, idx);
      appendToList(&l, e);
    }
    return l;
}

int notInList(const entry_t entry, const list_t list) {
    index_t i;
    for (i=0; i<getLength(list); i++) {
      entry_t e=getEntry(list, i);
      if (entry_equals(&e, &entry))
        return 0;
    }
    return 1;
}

void printList(list_t const list) {
    index_t i;
    const size_t len=getLength(list);
    printf("[list]_%d=[",len);
    if (len>0) {
      print_entry(getEntry(list,0));
    } 
    for (i=1; i<len; i++) {
      printf(", ");
      print_entry(getEntry(list,i));
    }
    printf("]\n");
}