summaryrefslogtreecommitdiff
path: root/src/list.c
blob: 1b9f4d4794f7feb604959a05808595d23691a24e (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
#include "list.h"
#include "util.h"
#include <string.h>
#include <stdio.h>

struct List_s {
  void **array;                 /* the array of elements */
  NUM len;                      /* the length of the array */
  NUM capacity;                 /* the number of pre-allocated bytes
                                   for the array */
};

List *
new_list()
{
  void **array = MYALLOC(void*, LIST_INIT_AMOUNT);

  if (array == NULL) return NULL;

  for (NUM i = 0; i < LIST_INIT_AMOUNT; i++)
    *(array+i) = NULL;

  List *list = MYALLOC(List, 1);

  if (list == NULL) return NULL;

  list->array = array;
  list->len = 0;
  list->capacity = LIST_INIT_AMOUNT;

  return list;
}

H_ATTR
BOOL
add_to_list(List *ls, void *element)
{
  if (!(ls->capacity)) {
    /* The capacity can be zero only when the list has been destroyed,
       in which case adding an element to that list is definitely an
       error. */
    fleprintf0("Adding an element to a destroyed list.\n");
  }

  (ls->len)++;

  if (ls->len >= ls->capacity) {
    NUM new_capacity = ((ls->capacity) * 3) >> 1;

    if (new_capacity < ls->capacity + LIST_INIT_AMOUNT)
      new_capacity = ls->capacity + LIST_INIT_AMOUNT;

    NUM max_diff = 1 << 20;
    if (new_capacity >= ls->capacity + max_diff)
      new_capacity = ls->capacity + max_diff;

    /* The new_capacity will not be zero, so upon failure it returns
       NULL. */
    void **newarr = realloc(ls->array, sizeof(void*) * new_capacity);

    if (newarr == NULL) {
      fleprintf0("Fail to realloc\n");
      ls->len -= 1;
      return 1;
    }

    ls->array = newarr;

    for (NUM i = ls->capacity; i < new_capacity; i++)
      *(ls->array+i) = NULL;

    ls->capacity = new_capacity;
  }

  *(ls->array+ls->len-1) = element;

  return 0;
}

H_ATTR
void *
pop_from_list(List *ls)
{
  if (!(ls->len)) return NULL;

  return *(ls->array+--(ls->len));
}

H_ATTR
void
map_list(List *ls, acter f)
{
  for (NUM i = 0; i < ls->len; i++)
    f(*(ls->array+i));
}

H_ATTR
void
map_list_between(List *ls, acter f, doer d)
{
  for (NUM i = 0; i < ls->len; i++) {
    f(*(ls->array+i));
    if (i + 1 < ls->len) d();
  }
}

void
print_list(List *ls, printer prt)
{
  printf("printing a list with length = %lu and capacity = %lu\n",
         ls->len, ls->capacity);

  map_list(ls, (acter) prt);

  printf("printing terminated\n");
}

void *
copy_num(void *p)
{
  NUM *pointer = malloc(sizeof *pointer);

  if (pointer == NULL) return NULL;

  *pointer = *((NUM *) p);

  return pointer;
}

BOOL
copy_list(List *dest, List *source, copyer copyf)
{
  BOOL sign = 0;

  if ((sign = list_assure_size(dest, source->len)))
    return sign;

  dest->len = source->len;

  for (NUM i = 0; i < source->len; i++) {
    void *pointer = copyf(*(source->array+i));
    if (pointer == NULL) return 1;
    *(dest->array+i) = pointer;
  }

  return 0;
}

H_ATTR
void *
list_nth(const List * const restrict ls, NUM n)
{
  return *(ls->array+n);
}

H_ATTR
NUM
list_length(const List * const restrict ls)
{
  return ls->len;
}

BOOL
list_assure_size(List *ls, NUM size)
{
  if (ls->capacity >= size) return 0;  /* we are good */

  void **newarray = realloc(ls->array, sizeof(void*) * size);

  if (newarray == NULL) {
    fleprintf0("Fail to realloc\n");
    return 1;
  }

  ls->array = newarray;

  for (NUM i = ls->capacity; i < size; i++)
    *(ls->array+i) = NULL;

  ls->capacity = size;

  return 0;
}

BOOL
list_set_length(List *ls, NUM len)
{
  list_assure_size(ls, len);

  if (ls->len >= len) return 0;

  NUM *array = MYALLOC(NUM, len - ls->len);

  if (array == NULL) return 1;

  for (NUM i = ls->len; i < len; i++) {
    *(array + i - ls->len) = -1;
    *(ls->array+i) = array + i - ls->len;
  }

  ls->len = len;

  return 0;
}

void *
list_to_array(List *ls, NUM element_bytes, NUM *num)
{
  /* We shall not use a void pointer here, since standard C does not
     allow doing arithmetic on void pointers.  So we use a char
     pointer here, since the size of char is 1. */
  char *array = malloc(element_bytes * ls->len);

  if (array == NULL) return NULL;

  *num = ls->len;

  for (NUM i = 0; i < *num; i++)
    memcpy(array+(element_bytes*i), *(ls->array+i), element_bytes);

  return array;
}

void **
list_array(List *ls)
{
  return ls->array;
}

List *
array_to_list(void **array, NUM size)
{
  List *ls = NULL;
  
  SAFE_MALLOC(List, ls, 1, return NULL;);

  ls->array = array;
  ls->len = ls->capacity = size;

  return ls;
}

void
destroy_list(List *ls, BOOL all_free_p)
{
  if (all_free_p == 1)
    for (NUM i = 0; i < ls->len; i++)
      free(*(ls->array+i));

  if (all_free_p == 2)
    free(*(ls->array));

  ls->len = (ls->capacity = 0);

  free(ls->array);
  free(ls);
}

void
destroy_list_free_all(void *ls)
{
  List *list = (List *) ls;
  destroy_list(list, 1);
}

void
destroy_list_free_first(void *ls)
{
  List *list = (List *) ls;
  destroy_list(list, 2);
}

void
destroy_list_no_free(void *ls)
{
  List *list = (List *) ls;
  destroy_list(list, 0);
}