summaryrefslogtreecommitdiffabout
path: root/src/list.c
blob: 4a5d3f6822043139b11269a5e03ed15d11cea9cd (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
/* grecs - Gray's Extensible Configuration System
   Copyright (C) 2007-2011 Sergey Poznyakoff

   Grecs 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.

   Grecs 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 Grecs. If not, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <grecs.h>
#include <grecs-gram.h>
#include <stdlib.h>
#include <string.h>

struct grecs_list *
grecs_list_create ()
{
  struct grecs_list *lp = grecs_malloc (sizeof (*lp));
  memset (lp, 0, sizeof (*lp));
  return lp;
}

size_t
grecs_list_size (struct grecs_list *lp)
{
  return lp ? lp->count : 0;
}

void
grecs_list_append (struct grecs_list *lp, void *val)
{
  struct grecs_list_entry *ep = grecs_malloc (sizeof (*ep));
  ep->data = val;
  ep->next = NULL;
  if (lp->tail)
    lp->tail->next = ep;
  else
    lp->head = ep;
  lp->tail = ep;
  lp->count++;
}

void
grecs_list_push (struct grecs_list *lp, void *val)
{
  struct grecs_list_entry *ep = grecs_malloc (sizeof (*ep));
  ep->data = val;
  ep->next = lp->head;
  lp->head = ep;
  lp->count++;
}

void *
grecs_list_pop (struct grecs_list *lp)
{
  void *data;
  struct grecs_list_entry *ep = lp->head;
  if (ep)
    {
      data = ep->data;
      lp->head = ep->next;
      if (!lp->head)
	lp->tail = NULL;
      lp->count--;
      free (ep);
    }
  else
    data = NULL;
  return data;
}

void
grecs_list_clear (struct grecs_list *lp)
{
  struct grecs_list_entry *ep = lp->head;

  while (ep)
    {
      struct grecs_list_entry *next = ep->next;
      if (lp->free_entry)
	lp->free_entry (ep->data);
      free (ep);
      ep = next;
    }
  lp->head = lp->tail = NULL;
  lp->count = 0;
}

void
grecs_list_free (struct grecs_list *lp)
{
  grecs_list_clear (lp);
  free (lp);
}

static int
_ptrcmp (const void *a, const void *b)
{
  return a != b;
}

void *
grecs_list_locate (struct grecs_list *lp, void *data)
{
  struct grecs_list_entry *ep;
  int (*cmp)(const void *, const void *) = lp->cmp ? lp->cmp : _ptrcmp;

  for (ep = lp->head; ep; ep = ep->next)
    {
      if (cmp (ep->data, data) == 0)
	return ep->data;
    }
  return NULL;
}

void *
grecs_list_index (struct grecs_list *lp, size_t idx)
{
  struct grecs_list_entry *ep;

  for (ep = lp->head; ep && idx; ep = ep->next, idx--)
    ;
  return ep ? ep->data : NULL;
}

void *
grecs_list_remove_tail (struct grecs_list *lp)
{
  void *data;
  
  if (!lp->head)
    return NULL;
  data = lp->tail;
  if (lp->head == lp->tail)
    {
      free (lp->tail);
      lp->head = lp->tail = NULL;
      lp->count = 0;
    }
  else
    {
      struct grecs_list_entry *ep;
      
      for (ep = lp->head; ep->next != lp->tail; ep = ep->next)
	;
      free (lp->tail);
      ep->next = NULL;
      lp->tail = ep;
    }
  return data;
}

Return to:

Send suggestions and report system problems to the System administrator.