summaryrefslogtreecommitdiffstats
path: root/osi/src/list.c
blob: 6fbb69cd7903cdcaea12f0c9e9ed07f35cfa6d56 (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
#include <assert.h>

#include "osi/include/allocator.h"
#include "osi/include/list.h"
#include "osi/include/osi.h"

struct list_node_t {
  struct list_node_t *next;
  void *data;
};

typedef struct list_t {
  list_node_t *head;
  list_node_t *tail;
  size_t length;
  list_free_cb free_cb;
  const allocator_t *allocator;
} list_t;

static list_node_t *list_free_node_(list_t *list, list_node_t *node);

// Hidden constructor, only to be used by the hash map for the allocation tracker.
// Behaves the same as |list_new|, except you get to specify the allocator.
list_t *list_new_internal(list_free_cb callback, const allocator_t *zeroed_allocator) {
  list_t *list = (list_t *)zeroed_allocator->alloc(sizeof(list_t));
  if (!list)
    return NULL;

  list->free_cb = callback;
  list->allocator = zeroed_allocator;
  return list;
}

list_t *list_new(list_free_cb callback) {
  return list_new_internal(callback, &allocator_calloc);
}

void list_free(list_t *list) {
  if (!list)
    return;

  list_clear(list);
  list->allocator->free(list);
}

bool list_is_empty(const list_t *list) {
  assert(list != NULL);
  if (list)
    return (list->length == 0);
  else
    return true;
}

bool list_contains(const list_t *list, const void *data) {
  assert(list != NULL);
  assert(data != NULL);

  for (const list_node_t *node = list_begin(list); node != list_end(list); node = list_next(node)) {
    if (list_node(node) == data)
      return true;
  }

  return false;
}

size_t list_length(const list_t *list) {
  assert(list != NULL);
  if (list)
    return list->length;
  else
    return 0;
}

void *list_front(const list_t *list) {
  assert(list != NULL);
  assert(!list_is_empty(list));
  if (list && list->head)
    return list->head->data;
  return NULL;
}

void *list_back(const list_t *list) {
  assert(list != NULL);
  assert(!list_is_empty(list));
  if (list && list->tail)
    return list->tail->data;
  return NULL;
}

bool list_insert_after(list_t *list, list_node_t *prev_node, void *data) {
  assert(list != NULL);
  assert(prev_node != NULL);
  assert(data != NULL);
  if (!list || !prev_node)
    return false;
  list_node_t *node = (list_node_t *)list->allocator->alloc(sizeof(list_node_t));
  if (!node)
    return false;

  node->next = prev_node->next;
  node->data = data;
  prev_node->next = node;
  if (list->tail == prev_node)
    list->tail = node;
  ++list->length;
  return true;
}

bool list_prepend(list_t *list, void *data) {
  assert(list != NULL);
  assert(data != NULL);
  if (!list)
    return false;

  list_node_t *node = (list_node_t *)list->allocator->alloc(sizeof(list_node_t));
  if (!node)
    return false;
  node->next = list->head;
  node->data = data;
  list->head = node;
  if (list->tail == NULL)
    list->tail = list->head;
  ++list->length;
  return true;
}

bool list_append(list_t *list, void *data) {
  assert(list != NULL);
  assert(data != NULL);
  if (!list)
    return false;

  list_node_t *node = (list_node_t *)list->allocator->alloc(sizeof(list_node_t));
  if (!node)
    return false;
  node->next = NULL;
  node->data = data;
  if (list->tail == NULL) {
    list->head = node;
    list->tail = node;
  } else {
    list->tail->next = node;
    list->tail = node;
  }
  ++list->length;
  return true;
}

bool list_remove(list_t *list, void *data) {
  assert(list != NULL);
  assert(data != NULL);

  if (!list || list_is_empty(list))
    return false;

  if (list->head->data == data) {
    list_node_t *next = list_free_node_(list, list->head);
    if (list->tail == list->head)
      list->tail = next;
    list->head = next;
    return true;
  }

  for (list_node_t *prev = list->head, *node = list->head->next; node; prev = node, node = node->next)
    if (node->data == data) {
      prev->next = list_free_node_(list, node);
      if (list->tail == node)
        list->tail = prev;
      return true;
    }

  return false;
}

void list_clear(list_t *list) {
  assert(list != NULL);
  if (list)
    for (list_node_t *node = list->head; node; )
      node = list_free_node_(list, node);
  list->head = NULL;
  list->tail = NULL;
  list->length = 0;
}

void list_foreach(const list_t *list, list_iter_cb callback) {
  assert(list != NULL);
  assert(callback != NULL);
  if (list)
    for (list_node_t *node = list->head; node; ) {
      list_node_t *next = node->next;
      callback(node->data);
      node = next;
    }
}

// Iterates through the entire |list| and calls |callback| for each data element.
// Passes the caller provided data along with node
// If the list is empty, |callback| will never be called. It is safe to mutate the
// list inside the callback. If an element is added before the node being visited,
// there will be no callback for the newly-inserted node. Neither |list| nor
// |callback| may be NULL.
void list_foreach_ext(const list_t *list, list_iter_cb_ext callback, void *cb_data) {
  assert(list != NULL);
  assert(callback != NULL);
  if (list)
    for (list_node_t *node = list->head; node; ) {
      list_node_t *next = node->next;
      callback(node->data, cb_data);
      node = next;
    }
}

list_node_t *list_begin(const list_t *list) {
  assert(list != NULL);
  if (list)
    return list->head;
  else
    return NULL;
}

list_node_t *list_end(UNUSED_ATTR const list_t *list) {
  assert(list != NULL);
  return NULL;
}

list_node_t *list_next(const list_node_t *node) {
  assert(node != NULL);
  if (node)
    return node->next;
  else
    return NULL;
}

void *list_node(const list_node_t *node) {
  assert(node != NULL);
  if (node)
    return node->data;
  return false;
}

static list_node_t *list_free_node_(list_t *list, list_node_t *node) {
  assert(list != NULL);
  assert(node != NULL);

  list_node_t *next = node ?node->next: NULL;
  if (list)
  {
    if (list->free_cb)
      list->free_cb(node->data);
    list->allocator->free(node);
    --list->length;
  }

  return next;
}