pcb 4.1.1
An interactive printed circuit board layout editor.

fifo.c

Go to the documentation of this file.
00001 /* GTS - Library for the manipulation of triangulated surfaces
00002  * Copyright (C) 1999 Stéphane Popinet
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Library General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Library General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Library General Public
00015  * License along with this library; if not, write to the
00016  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017  * Boston, MA 02111-1307, USA.
00018  */
00019 
00020 #include "gts.h"
00021 
00022 struct _GtsFifo {
00023   GList * head;
00024   GList * tail;
00025 };
00026 
00032 GtsFifo * gts_fifo_new ()
00033 {
00034   GtsFifo * fifo = g_malloc (sizeof (GtsFifo));
00035 
00036   fifo->head = fifo->tail = NULL;
00037   return fifo;
00038 }
00039 
00047 void gts_fifo_write (GtsFifo * fifo, FILE * fp)
00048 {
00049   GList * i;
00050 
00051   g_return_if_fail (fifo != NULL);
00052   g_return_if_fail (fp != NULL);
00053 
00054   fprintf (fp, "[");
00055   i = fifo->head;
00056   while (i) {
00057     fprintf (fp, "%p ", i->data);
00058     i = i->next;
00059   }
00060   fprintf (fp, "]");
00061 }
00062 
00070 void gts_fifo_push (GtsFifo * fifo, gpointer data)
00071 {
00072   g_return_if_fail (fifo != NULL);
00073 
00074   fifo->head = g_list_prepend (fifo->head, data);
00075   if (fifo->tail == NULL)
00076     fifo->tail = fifo->head;
00077 }
00078 
00087 gpointer gts_fifo_pop (GtsFifo * fifo)
00088 {
00089   gpointer data;
00090   GList * tail;
00091 
00092   g_return_val_if_fail (fifo != NULL, NULL);
00093 
00094   if (fifo->tail == NULL)
00095     return NULL;
00096   tail = fifo->tail->prev;
00097   data = fifo->tail->data;
00098   fifo->head = g_list_remove_link (fifo->head, fifo->tail);
00099   g_list_free_1 (fifo->tail);
00100   fifo->tail = tail;
00101   return data;
00102 }
00103 
00110 gpointer gts_fifo_top (GtsFifo * fifo)
00111 {
00112   g_return_val_if_fail (fifo != NULL, NULL);
00113 
00114   if (fifo->tail == NULL)
00115     return NULL;
00116   return fifo->tail->data;
00117 }
00118 
00125 guint gts_fifo_size (GtsFifo * fifo)
00126 {
00127   g_return_val_if_fail (fifo != NULL, 0);
00128 
00129   return g_list_length (fifo->head);
00130 }
00131 
00138 void gts_fifo_destroy (GtsFifo * fifo)
00139 {
00140   g_return_if_fail (fifo != NULL);
00141   g_list_free (fifo->head);
00142   g_free (fifo);
00143 }
00144 
00151 gboolean gts_fifo_is_empty (GtsFifo * fifo)
00152 {
00153   g_return_val_if_fail (fifo != NULL, TRUE);
00154 
00155   return (fifo->head == NULL);
00156 }
00157 
00166 void gts_fifo_foreach (GtsFifo * fifo, GtsFunc func, gpointer data)
00167 {
00168   GList * i;
00169 
00170   g_return_if_fail (fifo != NULL);
00171   g_return_if_fail (func != NULL);
00172 
00173   i = fifo->tail;
00174   while (i) {
00175     (* func) (i->data, data);
00176     i = i->prev;
00177   }
00178 }
00179 
00186 void gts_fifo_reverse (GtsFifo * fifo)
00187 {
00188   g_return_if_fail (fifo != NULL);
00189 
00190   fifo->tail = fifo->head;
00191   fifo->head = g_list_reverse (fifo->head);
00192 }