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