Wrapper for Clustal Omega.
[jabaws.git] / binaries / src / clustalo / src / squid / stack.c
1 /*****************************************************************
2  * SQUID - a library of functions for biological sequence analysis
3  * Copyright (C) 1992-2002 Washington University School of Medicine
4  * 
5  *     This source code is freely distributed under the terms of the
6  *     GNU General Public License. See the files COPYRIGHT and LICENSE
7  *     for details.
8  *****************************************************************/
9
10 /* stack.c
11  * SRE, Thu Mar  3 10:08:48 1994
12  * 
13  * Implementation of generic stack structures.
14  * RCS $Id: stack.c 217 2011-03-19 10:27:10Z andreas $ (Original squid RCS Id: stack.c,v 1.2 1998/10/09 18:07:16 eddy Exp)
15  */
16
17 #include <stdlib.h>
18 #include "squid.h"
19
20 #ifdef MEMDEBUG
21 #include "dbmalloc.h"
22 #endif
23
24
25 /************************************************************
26  * intstack_s implementation.
27  * 
28  * Functions: InitIntStack() - returns ptr to new stack
29  *            PushIntStack() - (void)
30  *            PopIntStack()  - returns 1 on success, 0 if stack empty
31  *            FreeIntStack() - returns number of elements free'd, or 0 if 
32  *                             stack was empty.
33  *            
34  * Implementation of the pushdown stack for storing single
35  * integers.
36  *************************************************************/  
37 struct intstack_s *
38 InitIntStack(void)
39 {
40   struct intstack_s *stack;
41
42   if ((stack = (struct intstack_s *) malloc (sizeof(struct intstack_s))) == NULL)
43     Die("Memory allocation failure at %s line %d", __FILE__, __LINE__);
44   stack->nxt = NULL;
45   return stack;
46 }
47 void 
48 PushIntStack(struct intstack_s *stack, int data)
49 {
50   struct intstack_s *new;
51
52   if ((new = (struct intstack_s *) malloc (sizeof(struct intstack_s))) == NULL)
53     Die("Memory allocation failure at %s line %d", __FILE__, __LINE__);
54   new->data = data;
55
56   new->nxt     = stack->nxt;
57   stack->nxt   = new;
58 }
59
60 int
61 PopIntStack(struct intstack_s  *stack, int *ret_data)
62 {
63   struct intstack_s *old;
64
65   if (stack->nxt == NULL) return 0;
66
67   old = stack->nxt;
68   stack->nxt = old->nxt;
69
70   *ret_data = old->data;
71   free(old); 
72   return 1;
73 }
74
75 void
76 ReverseIntStack(struct intstack_s *stack)
77 {
78   struct intstack_s *old;
79   struct intstack_s *new;
80
81   old        = stack->nxt;
82   stack->nxt = NULL;
83   while (old != NULL)
84     {
85       new        = old;         /* remove one from top of old stack */
86       old        = old->nxt;
87       new->nxt   = stack->nxt;  /* push it onto new stack */
88       stack->nxt = new;
89     }
90 }
91
92 int
93 FreeIntStack( struct intstack_s *stack )
94 {
95   int data;
96   int count = 0;
97
98   while (PopIntStack(stack, &data))
99     count++;
100   free(stack);
101   return count;
102 }