File: list.c

package info (click to toggle)
quickplot 0.10.3-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 2,464 kB
  • sloc: ansic: 16,640; sh: 11,163; makefile: 395
file content (311 lines) | stat: -rw-r--r-- 6,057 bytes parent folder | download | duplicates (4)
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
/*
  Quickplot - an interactive 2D plotter

  Copyright (C) 1998-2011  Lance Arsenault


  This file is part of Quickplot.

  Quickplot is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published
  by the Free Software Foundation, either version 3 of the License,
  or (at your option) any later version.

  Quickplot 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
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with Quickplot.  If not, see <http://www.gnu.org/licenses/>.

*/

#include <stdlib.h>

#include "config.h"
#include "debug.h"
#include "list.h"

#ifdef DMALLOC
#  include "dmalloc.h"
#endif



struct qp_sllist *qp_sllist_create(struct qp_sllist *orig)
{
  struct qp_sllist *l;
  l = (struct qp_sllist *) qp_malloc(sizeof(struct qp_sllist));
  if(orig)
  {
    ASSERT(*(orig->ref_count) > 0);

    l->first = orig->first;
    l->last = orig->last;
    l->length = orig->length;
    l->current = NULL;
    l->ref_count = orig->ref_count;
    ++(*(l->ref_count));
  }
  else
  {
    l->first = l->last = l->current = NULL;
    l->length = 0;
    l->ref_count = (int *)qp_malloc(sizeof(int));
    *(l->ref_count) = 1;
  }
  return l;
}

struct qp_dllist *qp_dllist_create(struct qp_dllist *orig)
{
  struct qp_dllist *l;
  l = (struct qp_dllist *) qp_malloc(sizeof(struct qp_dllist));
  if(orig)
  {
    ASSERT(*(orig->ref_count) > 0);

    l->first = orig->first;
    l->last = orig->last;
    l->length = orig->length;
    l->current = NULL;
    l->ref_count = orig->ref_count;
    ++(*(l->ref_count));
  }
  else
  {
    l->first = l->last = l->current = NULL;
    l->length = 0;
    l->ref_count = (int *)qp_malloc(sizeof(int));
    *(l->ref_count) = 1;
  }
  return l;
}

/** add val to the end */
void qp_sllist_append(struct qp_sllist *l, void *val)
{
  struct qp_sllist_entry *new_e;
  ASSERT(l);
  ASSERT(*(l->ref_count) == 1);
  new_e = (struct qp_sllist_entry *)
    qp_malloc(sizeof(struct qp_sllist_entry));
  new_e->next = NULL;
  new_e->val = val;

  if(l->last)
  {
    l->last->next = new_e;
    l->last = new_e;
  }
  else
  {
    l->current = l->first = l->last = new_e;
  }
  ++(l->length);
}

/** add val to the end */
void qp_dllist_append(struct qp_dllist *l, void *val)
{
  struct qp_dllist_entry *new_e;
  ASSERT(l);
  ASSERT(*(l->ref_count) == 1);
  new_e = (struct qp_dllist_entry *)
    qp_malloc(sizeof(struct qp_dllist_entry));
  new_e->next = NULL;
  new_e->prev = l->last;
  new_e->val = val;

  if(l->last)
  {
    l->last->next = new_e;
    l->last = new_e;
  }
  else
  {
    l->current = l->first = l->last = new_e;
  }
  ++(l->length);
}

void qp_sllist_destroy(struct qp_sllist *l, int free_vals)
{
  ASSERT(l);
  ASSERT(*(l->ref_count) > 0);

  if(!l) return;


  if(*(l->ref_count) == 1)
  {
    /* This is the last reader of this
     * list so we may free the entries */
    struct qp_sllist_entry *e;
    e = l->first;
    while(e)
    {
      struct qp_sllist_entry *prev_e;
      prev_e = e;
      e = e->next;
      if(free_vals && prev_e->val)
        free(prev_e->val);
      free(prev_e);
    }
    free(l->ref_count);
  }
  else
    --(*(l->ref_count));

  free(l);
}


void qp_dllist_destroy(struct qp_dllist *l, int free_vals)
{
  ASSERT(l);
  ASSERT(*(l->ref_count) > 0);

  if(!l) return;

  if(*(l->ref_count) == 1)
  {
    struct qp_dllist_entry *e;
    e = l->first;
    while(e)
    {
      struct qp_dllist_entry *prev_e;
      prev_e = e;
      e = e->next;
      if(free_vals && prev_e->val)
        free(prev_e->val);
      free(prev_e);
    }
    free(l->ref_count);
  }
  else
    --(*(l->ref_count));

  free(l);
}

/* returns the number found */
int qp_sllist_find(struct qp_sllist *l, void *val)
{
  void *v;
  int ret = 0;
  ASSERT(l);
  ASSERT(*(l->ref_count) > 0);

  for(v=qp_sllist_begin(l); v;
      v=qp_sllist_next(l))
    if(v == val)
      ++ret;
  return ret;
}

/* returns the number found */
int qp_dllist_find(struct qp_dllist *l, void *val)
{
  void *v;
  int ret = 0;
  ASSERT(l);
  ASSERT(*(l->ref_count) > 0);

  for(v=qp_dllist_begin(l); v;
      v=qp_dllist_next(l))
    if(v == val)
      ++ret;
  return ret;
}

/* remove by pointer */
int qp_sllist_remove(struct qp_sllist *l,
    const void *val, int free_val)
{
  struct qp_sllist_entry *e, *prev = NULL;
  int ret = 0;
  ASSERT(l);
  ASSERT(*(l->ref_count) == 1);

  for(e=l->first; e;)
  {
    struct qp_sllist_entry *next;
    
    next = e->next;
    if(e->val == val)
    {
      if(prev)
      {
        prev->next = e->next;
        if(l->last == e)
          l->last = prev;
      }
      else /* it is the first entry */
      {
        l->first = e->next;
        if(l->last == e)
          l->last = NULL;
      }
      if(l->current == e)
        /* reset iterators */
        l->current = NULL;

      /* free it once */
      if(free_val && !ret)
        free(e->val);
      free(e);
      --(l->length);
      ASSERT(l->length != (size_t) -1);
      ++ret;
    }
    else
      prev = e;

    e = next;
  }
  return ret;
}

/* remove by pointer */
int qp_dllist_remove(struct qp_dllist *l,
    const void *val, int free_val)
{
  struct qp_dllist_entry *e;
  int ret = 0;
  ASSERT(l);
  ASSERT(*(l->ref_count) == 1);
  for(e=l->first; e;)
  {
    struct qp_dllist_entry *next;
    next = e->next;
    if(e->val == val)
    {
      if(e->prev)
        e->prev->next = e->next;
      if(e->next)
        e->next->prev = e->prev;

      if(l->first == e)
        l->first = e->next;
      if(l->last == e)
        l->last = e->prev;

      if(l->current == e)
        /* reset iterators */
        l->current = NULL;

      /* free it once */
      if(free_val && !ret)
        free(e->val);
      free(e);
      --(l->length);
      ASSERT(l->length != (size_t) -1);
      ++ret;
    }
    e = next;
  }
  return ret;
}