1 /* 2 * stack.c 3 * 4 * Author: mozman 5 * Copyright (c) 2010-2013 by Manfred Moitzi 6 * License: MIT-License 7 */ 8 9 #include "ctrees.h" 10 #include "stack.h" 11 12 extern node_stack_t * 13 stack_init(int size) 14 { 15 node_stack_t *stack; 16 17 stack = PyMem_Malloc(sizeof(node_stack_t)); 18 stack->stack = PyMem_Malloc(sizeof(node_t *) * size); 19 stack->size = size; 20 stack->stackptr = 0; 21 return stack; 22 } 23 24 extern void 25 stack_delete(node_stack_t *stack) 26 { 27 PyMem_Free(stack->stack); 28 PyMem_Free(stack); 29 } 30 31 extern void 32 stack_push(node_stack_t *stack, node_t *node) 33 { 34 stack->stack[stack->stackptr++] = node; 35 if (stack->stackptr >= stack->size) { 36 stack->size *= 2; 37 stack->stack = PyMem_Realloc(stack->stack, 38 sizeof(node_t *) * stack->size); 39 } 40 } 41 42 extern node_t * 43 stack_pop(node_stack_t *stack) 44 { 45 return (stack->stackptr > 0) ? stack->stack[--stack->stackptr] : NULL; 46 } 47 48 extern int 49 stack_is_empty(node_stack_t *stack) 50 { 51 return (stack->stackptr == 0); 52 } 53 54 extern void 55 stack_reset(node_stack_t *stack) 56 { 57 stack->stackptr = 0; 58 } 59