Revert "Remove GTS sources in favour of the libgts package."
[geda-pcb/pcjc2.git] / gts / fifo.c
blob8b3d2b668686240dad3323b541483a1e1e1ab07b
1 /* GTS - Library for the manipulation of triangulated surfaces
2 * Copyright (C) 1999 Stéphane Popinet
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #include "gts.h"
22 struct _GtsFifo {
23 GList * head;
24 GList * tail;
27 /**
28 * gts_fifo_new:
30 * Returns: a new #GtsFifo.
32 GtsFifo * gts_fifo_new ()
34 GtsFifo * fifo = g_malloc (sizeof (GtsFifo));
36 fifo->head = fifo->tail = NULL;
37 return fifo;
40 /**
41 * gts_fifo_write:
42 * @fifo: a #GtsFifo.
43 * @fp: a file pointer.
45 * Writes the content of @fifo in @fp.
47 void gts_fifo_write (GtsFifo * fifo, FILE * fp)
49 GList * i;
51 g_return_if_fail (fifo != NULL);
52 g_return_if_fail (fp != NULL);
54 fprintf (fp, "[");
55 i = fifo->head;
56 while (i) {
57 fprintf (fp, "%p ", i->data);
58 i = i->next;
60 fprintf (fp, "]");
63 /**
64 * gts_fifo_push:
65 * @fifo: a #GtsFifo.
66 * @data: data to add to @fifo.
68 * Push @data into @fifo.
70 void gts_fifo_push (GtsFifo * fifo, gpointer data)
72 g_return_if_fail (fifo != NULL);
74 fifo->head = g_list_prepend (fifo->head, data);
75 if (fifo->tail == NULL)
76 fifo->tail = fifo->head;
79 /**
80 * gts_fifo_pop:
81 * @fifo: a #GtsFifo.
83 * Removes the first element from @fifo.
85 * Returns: the first element in @fifo or %NULL if @fifo is empty.
87 gpointer gts_fifo_pop (GtsFifo * fifo)
89 gpointer data;
90 GList * tail;
92 g_return_val_if_fail (fifo != NULL, NULL);
94 if (fifo->tail == NULL)
95 return NULL;
96 tail = fifo->tail->prev;
97 data = fifo->tail->data;
98 fifo->head = g_list_remove_link (fifo->head, fifo->tail);
99 g_list_free_1 (fifo->tail);
100 fifo->tail = tail;
101 return data;
105 * gts_fifo_top:
106 * @fifo: a #GtsFifo.
108 * Returns: the first element in @fifo or %NULL if @fifo is empty.
110 gpointer gts_fifo_top (GtsFifo * fifo)
112 g_return_val_if_fail (fifo != NULL, NULL);
114 if (fifo->tail == NULL)
115 return NULL;
116 return fifo->tail->data;
120 * gts_fifo_size:
121 * @fifo: a #GtsFifo.
123 * Returns: the number of elements in @fifo.
125 guint gts_fifo_size (GtsFifo * fifo)
127 g_return_val_if_fail (fifo != NULL, 0);
129 return g_list_length (fifo->head);
133 * gts_fifo_destroy:
134 * @fifo: a #GtsFifo.
136 * Frees all the memory allocated for @fifo.
138 void gts_fifo_destroy (GtsFifo * fifo)
140 g_return_if_fail (fifo != NULL);
141 g_list_free (fifo->head);
142 g_free (fifo);
146 * gts_fifo_is_empty:
147 * @fifo: a #GtsFifo.
149 * Returns: %TRUE if @fifo is empty, %FALSE otherwise.
151 gboolean gts_fifo_is_empty (GtsFifo * fifo)
153 g_return_val_if_fail (fifo != NULL, TRUE);
155 return (fifo->head == NULL);
159 * gts_fifo_foreach:
160 * @fifo: a #GtsFifo.
161 * @func: a #GtsFunc.
162 * @data: user data to be passed to @func.
164 * Calls @func in order for each item in @fifo, passing @data.
166 void gts_fifo_foreach (GtsFifo * fifo, GtsFunc func, gpointer data)
168 GList * i;
170 g_return_if_fail (fifo != NULL);
171 g_return_if_fail (func != NULL);
173 i = fifo->tail;
174 while (i) {
175 (* func) (i->data, data);
176 i = i->prev;
181 * gts_fifo_reverse:
182 * @fifo: a #GtsFifo.
184 * Reverses the order of elements in @fifo.
186 void gts_fifo_reverse (GtsFifo * fifo)
188 g_return_if_fail (fifo != NULL);
190 fifo->tail = fifo->head;
191 fifo->head = g_list_reverse (fifo->head);