Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

intstack.c

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr
00003  *cr            (C) Copyright 1995-2019 The Board of Trustees of the
00004  *cr                        University of Illinois
00005  *cr                         All Rights Reserved
00006  *cr
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  * RCS INFORMATION:
00011  *
00012  *      $RCSfile: intstack.c,v $
00013  *      $Author: johns $        $Locker:  $             $State: Exp $
00014  *      $Revision: 1.10 $      $Date: 2019/01/17 21:21:03 $
00015  *
00016  ***************************************************************************
00017  * DESCRIPTION:
00018  *   Trivial stack implementation for use in eliminating recursion
00019  *   in molecule graph traversal algorithms.
00020  *
00021  ***************************************************************************/
00022 
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include "intstack.h"
00026 
00027 typedef struct {
00028   long growthrate;
00029   long size;
00030   long top;
00031   int *s;
00032 } intstack;
00033 
00034 IntStackHandle intstack_create(long size) {
00035   intstack *s;
00036 
00037   s = (intstack *) malloc(sizeof(intstack));
00038   if (s == NULL)
00039     return NULL;
00040 
00041   s->growthrate = 32768;
00042   s->top = -1;
00043 
00044   if (size > 0) {
00045     s->size = size;
00046     s->s = (int *) malloc(s->size * sizeof(int));
00047   } else {
00048     s->size = 0;
00049     s->s = NULL;
00050   }
00051 
00052   return s;
00053 }
00054 
00055 
00056 void intstack_destroy(IntStackHandle voidhandle) {
00057   intstack *s = (intstack *) voidhandle;
00058   free(s->s);
00059   s->s = NULL; /* prevent access after free */
00060   free(s);
00061 }
00062 
00063 
00064 int intstack_compact(IntStackHandle voidhandle) {
00065   intstack *s = (intstack *) voidhandle;
00066 
00067   if (s->size > (s->top + 1)) {
00068     long newsize = s->top + 1L;
00069     int *tmp = (int *) realloc(s->s, newsize * sizeof(int));
00070     if (tmp == NULL)
00071       return -1;
00072     s->s = tmp;
00073     s->size = newsize; 
00074   }
00075 
00076   return 0;
00077 }
00078 
00079 int intstack_push(IntStackHandle voidhandle, int i) {
00080   intstack *s = (intstack *) voidhandle;
00081 
00082   s->top++;
00083   if (s->top >= s->size) {
00084     long newsize = s->size + s->growthrate; 
00085     int *tmp = (int *) realloc(s->s, newsize * sizeof(int));
00086     if (tmp == NULL) {
00087       s->top--;
00088       return -1; /* out of space! */
00089     }
00090     s->s = tmp;
00091     s->size = newsize;
00092   }  
00093 
00094   s->s[s->top] = i; /* push onto the stack */
00095 
00096   return 0;
00097 }
00098 
00099 
00100 int intstack_pop(IntStackHandle voidhandle, int *i) {
00101   intstack *s = (intstack *) voidhandle;
00102   if (s->top < 0)
00103     return -1;
00104 
00105   *i = s->s[s->top];
00106   s->top--;
00107 
00108   return 0;
00109 }
00110 
00111 int intstack_popall(IntStackHandle voidhandle) {
00112   intstack *s = (intstack *) voidhandle;
00113   s->top = -1;
00114 
00115   return 0;
00116 }
00117 
00118 int intstack_empty(IntStackHandle voidhandle) {
00119   intstack *s = (intstack *) voidhandle;
00120   if (s->top < 0) return 1;
00121   else return 0;
00122 }
00123 
00124 #if 0
00125 
00126 #include <stdio.h>
00127 
00128 int main() {
00129   int i;
00130   IntStackHandle stack;
00131 
00132 printf("allocating stack...\n");
00133   stack = intstack_create(0);
00134 
00135 printf("pushing data values onto the stack...\n");
00136   intstack_push(stack, 5);
00137   intstack_compact(stack);
00138   intstack_push(stack, 3);
00139   intstack_compact(stack);
00140   intstack_push(stack, 5);
00141   intstack_compact(stack);
00142   intstack_push(stack, 2);
00143   intstack_compact(stack);
00144   intstack_push(stack, 9);
00145   intstack_compact(stack);
00146   intstack_push(stack, 5);
00147   intstack_compact(stack);
00148   intstack_push(stack, 1);
00149   intstack_compact(stack);
00150   intstack_push(stack, 4);
00151   intstack_compact(stack);
00152   intstack_push(stack, 1);
00153   intstack_compact(stack);
00154   intstack_push(stack, 3);
00155 
00156 printf("popping data values off the stack...\n");
00157   while (!intstack_pop(stack, &i)) {
00158     printf("%d\n", i);
00159   }
00160 
00161   intstack_destroy(stack);
00162  
00163   return 0;
00164 }
00165 
00166 #endif
00167 

Generated on Fri Nov 8 02:44:41 2024 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002