File: list.c

package info (click to toggle)
libi8x 0.0.5-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 7,192 kB
  • ctags: 1,365
  • sloc: ansic: 6,874; python: 1,339; makefile: 146; sh: 102
file content (261 lines) | stat: -rw-r--r-- 5,286 bytes parent folder | download
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
/* Copyright (C) 2016-17 Red Hat, Inc.
   This file is part of the Infinity Note Execution Library.

   The Infinity Note Execution Library is free software; you can
   redistribute it and/or modify it under the terms of the GNU Lesser
   General Public License as published by the Free Software
   Foundation; either version 2.1 of the License, or (at your option)
   any later version.

   The Infinity Note Execution Library is distributed in the hope that
   it will be useful, but WITHOUT ANY WARRANTY; without even the
   implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
   PURPOSE.  See the GNU Lesser General Public License for more
   details.

   You should have received a copy of the GNU Lesser General Public
   License along with the Infinity Note Execution Library; if not, see
   <http://www.gnu.org/licenses/>.  */

#include "libi8x-private.h"

struct i8x_list
{
  I8X_OBJECT_FIELDS;

  /* The head of the list.  */
  struct i8x_listitem *head;

  /* True if listitems should own references to their objects.  */
  bool manage_references;
};

struct i8x_listitem
{
  I8X_OBJECT_FIELDS;

  struct i8x_listitem *next, *prev;

  struct i8x_object *ob;
};

static struct i8x_list *
i8x_listitem_get_list (struct i8x_listitem *li)
{
  return (struct i8x_list *)
    i8x_ob_get_parent ((struct i8x_object *) li);

}

static void
i8x_list_unlink (struct i8x_object *ob)
{
  struct i8x_list *list = (struct i8x_list *) ob;

  list->head = i8x_listitem_unref (list->head);
}

static void
i8x_listitem_unlink (struct i8x_object *ob)
{
  struct i8x_listitem *li = (struct i8x_listitem *) ob;
  struct i8x_list *list = i8x_listitem_get_list (li);

  if (list->manage_references)
    li->ob = i8x_ob_unref (li->ob);

  if (li->next != list->head)
    li->next = i8x_listitem_unref (li->next);
}

const struct i8x_object_ops i8x_list_ops =
  {
    "list",				/* Object name.  */
    sizeof (struct i8x_list),		/* Object size.  */
    i8x_list_unlink,			/* Unlink function.  */
    NULL,				/* Free function.  */
  };

const struct i8x_object_ops i8x_listitem_ops =
  {
    "listitem",				/* Object name.  */
    sizeof (struct i8x_listitem),	/* Object size.  */
    i8x_listitem_unlink,		/* Unlink function.  */
    NULL,				/* Free function.  */
  };

static i8x_err_e
i8x_listitem_new (struct i8x_list *list,
		  struct i8x_object *ob,
		  struct i8x_listitem **li)
{
  struct i8x_listitem *l;
  i8x_err_e err;

  err = i8x_ob_new (list, &i8x_listitem_ops, &l);
  if (err != I8X_OK)
    return err;

  if (list->manage_references)
    ob = i8x_ob_ref (ob);

  l->ob = ob;

  *li = l;

  return I8X_OK;
}

i8x_err_e
i8x_list_new (struct i8x_ctx *ctx, bool manage_references,
	      struct i8x_list **list)
{
  struct i8x_list *l;
  i8x_err_e err;

  err = i8x_ob_new (ctx, &i8x_list_ops, &l);
  if (err != I8X_OK)
    return err;

  l->manage_references = manage_references;

  err = i8x_listitem_new (l, NULL, &l->head);
  if (err != I8X_OK)
    {
      l = i8x_list_unref (l);

      return err;
    }

  l->head->next = l->head->prev = l->head;

  *list = l;

  return I8X_OK;
}

i8x_err_e
i8x_list_append (struct i8x_list *list, struct i8x_object *ob)
{
  struct i8x_listitem *head = list->head;
  struct i8x_listitem *last = head->prev;
  struct i8x_listitem *item;
  i8x_err_e err;

  err = i8x_listitem_new (list, ob, &item);
  if (err != I8X_OK)
    return err;

  item->prev = last;
  item->next = list->head;
  last->next = item;
  head->prev = item;

  return I8X_OK;
}

struct i8x_listitem *
i8x_list_get_item (struct i8x_list *list, struct i8x_object *ob)
{
  struct i8x_listitem *li;

  i8x_list_foreach (list, li)
    if (li->ob == ob)
      return li;

  return NULL;
}

void
i8x_listitem_remove (struct i8x_listitem *item)
{
  i8x_assert (item != NULL);

  item->prev->next = item->next;
  item->next->prev = item->prev;

  item->next = NULL;
  item = i8x_listitem_unref (item);
}

void
i8x_list_remove (struct i8x_list *list, struct i8x_object *ob)
{
  i8x_listitem_remove (i8x_list_get_item (list, ob));
}

I8X_EXPORT size_t
i8x_list_size (struct i8x_list *list)
{
  struct i8x_listitem *li;
  size_t count = 0;

  i8x_list_foreach (list, li)
    {
      count++;
      i8x_assert (count > 0);
    }

  return count;
}

I8X_EXPORT struct i8x_listitem *
i8x_list_get_first (struct i8x_list *list)
{
  if (list == NULL)
    return NULL;

  return i8x_list_get_next (list, list->head);
}

struct i8x_listitem *
i8x_list_get_last (struct i8x_list *list)
{
  if (list == NULL)
    return NULL;

  return i8x_list_get_prev (list, list->head);
}

I8X_EXPORT struct i8x_listitem *
i8x_list_get_next (struct i8x_list *list, struct i8x_listitem *li)
{
  li = li->next;
  if (li == list->head)
    return NULL;

  return li;
}

struct i8x_listitem *
i8x_list_get_prev (struct i8x_list *list, struct i8x_listitem *li)
{
  li = li->prev;
  if (li == list->head)
    return NULL;

  return li;
}

I8X_EXPORT struct i8x_listitem *
i8x_list_get_item_by_index (struct i8x_list *list, size_t index)
{
  struct i8x_listitem *li;
  size_t count = 0;

  i8x_list_foreach (list, li)
    {
      if (count == index)
	return li;

      count++;
    }

  return NULL;
}

I8X_EXPORT struct i8x_object *
i8x_listitem_get_object (struct i8x_listitem *li)
{
  return li->ob;
}