File: fifo.c

package info (click to toggle)
pcb 20140316-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 22,212 kB
  • ctags: 16,012
  • sloc: ansic: 123,955; sh: 7,306; yacc: 5,087; pascal: 4,118; makefile: 1,559; perl: 552; lex: 438; awk: 157; lisp: 86; tcl: 63; xml: 20
file content (192 lines) | stat: -rw-r--r-- 3,928 bytes parent folder | download | duplicates (16)
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
/* GTS - Library for the manipulation of triangulated surfaces
 * Copyright (C) 1999 Stphane Popinet
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "gts.h"

struct _GtsFifo {
  GList * head;
  GList * tail;
};

/**
 * gts_fifo_new:
 *
 * Returns: a new #GtsFifo.
 */
GtsFifo * gts_fifo_new ()
{
  GtsFifo * fifo = g_malloc (sizeof (GtsFifo));

  fifo->head = fifo->tail = NULL;
  return fifo;
}

/**
 * gts_fifo_write:
 * @fifo: a #GtsFifo.
 * @fp: a file pointer.
 *
 * Writes the content of @fifo in @fp.
 */
void gts_fifo_write (GtsFifo * fifo, FILE * fp)
{
  GList * i;

  g_return_if_fail (fifo != NULL);
  g_return_if_fail (fp != NULL);

  fprintf (fp, "[");
  i = fifo->head;
  while (i) {
    fprintf (fp, "%p ", i->data);
    i = i->next;
  }
  fprintf (fp, "]");
}

/**
 * gts_fifo_push:
 * @fifo: a #GtsFifo.
 * @data: data to add to @fifo.
 *
 * Push @data into @fifo.
 */
void gts_fifo_push (GtsFifo * fifo, gpointer data)
{
  g_return_if_fail (fifo != NULL);

  fifo->head = g_list_prepend (fifo->head, data);
  if (fifo->tail == NULL)
    fifo->tail = fifo->head;
}

/**
 * gts_fifo_pop:
 * @fifo: a #GtsFifo.
 *
 * Removes the first element from @fifo.
 *
 * Returns: the first element in @fifo or %NULL if @fifo is empty.
 */
gpointer gts_fifo_pop (GtsFifo * fifo)
{
  gpointer data;
  GList * tail;

  g_return_val_if_fail (fifo != NULL, NULL);

  if (fifo->tail == NULL)
    return NULL;
  tail = fifo->tail->prev;
  data = fifo->tail->data;
  fifo->head = g_list_remove_link (fifo->head, fifo->tail);
  g_list_free_1 (fifo->tail);
  fifo->tail = tail;
  return data;
}

/**
 * gts_fifo_top:
 * @fifo: a #GtsFifo.
 *
 * Returns: the first element in @fifo or %NULL if @fifo is empty.
 */
gpointer gts_fifo_top (GtsFifo * fifo)
{
  g_return_val_if_fail (fifo != NULL, NULL);

  if (fifo->tail == NULL)
    return NULL;
  return fifo->tail->data;
}

/**
 * gts_fifo_size:
 * @fifo: a #GtsFifo.
 *
 * Returns: the number of elements in @fifo.
 */
guint gts_fifo_size (GtsFifo * fifo)
{
  g_return_val_if_fail (fifo != NULL, 0);

  return g_list_length (fifo->head);
}

/**
 * gts_fifo_destroy:
 * @fifo: a #GtsFifo.
 *
 * Frees all the memory allocated for @fifo.
 */
void gts_fifo_destroy (GtsFifo * fifo)
{
  g_return_if_fail (fifo != NULL);
  g_list_free (fifo->head);
  g_free (fifo);
}

/**
 * gts_fifo_is_empty:
 * @fifo: a #GtsFifo.
 * 
 * Returns: %TRUE if @fifo is empty, %FALSE otherwise.
 */
gboolean gts_fifo_is_empty (GtsFifo * fifo)
{
  g_return_val_if_fail (fifo != NULL, TRUE);

  return (fifo->head == NULL);
}

/**
 * gts_fifo_foreach:
 * @fifo: a #GtsFifo.
 * @func: a #GtsFunc.
 * @data: user data to be passed to @func.
 *
 * Calls @func in order for each item in @fifo, passing @data.
 */
void gts_fifo_foreach (GtsFifo * fifo, GtsFunc func, gpointer data)
{
  GList * i;

  g_return_if_fail (fifo != NULL);
  g_return_if_fail (func != NULL);

  i = fifo->tail;
  while (i) {
    (* func) (i->data, data);
    i = i->prev;
  }
}

/**
 * gts_fifo_reverse:
 * @fifo: a #GtsFifo.
 *
 * Reverses the order of elements in @fifo.
 */
void gts_fifo_reverse (GtsFifo * fifo)
{
  g_return_if_fail (fifo != NULL);

  fifo->tail = fifo->head;
  fifo->head = g_list_reverse (fifo->head);
}