An intermediate implementation

Lets look at the int_stack with all the bells and whistles (but without checksum).


int_stack.h


	#ifndef H_int_stack
	#define H_int_stack
	
	#include "int_stack-private.h"
	
	typedef struct int_stack * int_stack;
	
	/* basic functions */
	int_stack	make_int_stack(void);
	void		destroy_int_stack(int_stack);
	void		push_int_stack(int_stack, int);
	int		top_int_stack(const int_stack);
	void		pop_int_stack(int_stack);
	int		is_empty_int_stack(const int_stack);
	
	/* debug functions */
	void		verify_int_stack(const char * file, int line, const int_stack);
	void		set_trace_int_stack(int_stack);
	
	#endif
	


int_stack-private.h


	#ifndef H_int_stack_private
	#define H_int_stack_private
	
	#ifndef H_common
	#include "common.h"
	#endif
	
	#define INIT_INT_STACK	16
	#define INCR_INT_STACK	16
	
	struct int_stack {
	  int		is_max;
	  int		is_len;
	  int *		is_vals;
	};
	
	#define deref_int_stack(_s)	(_s)
	
	#define x_max_int_stack(_s)	(deref_int_stack(_s)->is_max)
	#define x_len_int_stack(_s)	(deref_int_stack(_s)->is_len)
	#define x_vals_int_stack(_s)	(deref_int_stack(_s)->is_vals)
	#define x_val_int_stack(_s,_i)	\
		(x_vals_int_stack(_s)[bounds(_i, x_len_int_stack(_s))])
	
	#define max_int_stack(_s)	((void)0, x_max_int_stack(_s))
	#define len_int_stack(_s)	((void)0, x_len_int_stack(_s))
	#define vals_int_stack(_s)	((void)0, x_vals_int_stack(_s))
	#define val_int_stack(_s,_i)	((void)0, x_val_int_stack(_s, _i))
	
	#endif
	


int_stack.c


	#include "int_stack.h"
	
	#ifndef H_common
	#include "common.h"
	#endif
	
	
	#if( NVERIFY )
	  #define VERIFY_INT_STACK(_stack)	((void)0)
	#else
	  #define VERIFY_INT_STACK(_stack) \
	  	verify_int_stack(__FILE__, __LINE__, _stack)
	#endif
	
	
	void
	verify_int_stack(
		const char *	file,
		int		line,
		const int_stack	stack
		)
	{
	  verify( file, line, len_int_stack(stack) >= 0 );
	  verify( file, line, len_int_stack(stack) <= max_int_stack(stack) );
	  verify( file, line, max_int_stack(stack) >= INIT_INT_STACK );
	  verify( file, line,
	  	((max_int_stack(stack) - INIT_INT_STACK)%INCR_INT_STACK) == 0 );
	}
	
	int_stack
	make_int_stack(void)
	{
	  int_stack	stack = xalloc(struct int_stack);
	
	  if( stack == 0 ) {
	    PANIC("out of memory");
	  }
	
	  x_max_int_stack(stack) = INIT_INT_STACK;
	  x_len_int_stack(stack) = 0;
	  x_vals_int_stack(stack) = xnalloc(int, INIT_INT_STACK);
	
	  if( vals_int_stack(stack) == 0 ) {
	    PANIC("out of memory");
	  }
	  VERIFY_INT_STACK(stack);
	
	  return stack;
	}
	
	void
	destroy_int_stack(
		int_stack	stack
		)
	{
	  VERIFY_INT_STACK(stack);
	
	  xnfree(int, max_int_stack(stack), vals_int_stack(stack));
	  xfree(struct int_stack, stack);
	}
	
	void
	push_int_stack(
		int_stack	stack,
		int		val
		)
	{
	  int	len = len_int_stack(stack);
	  int	max = max_int_stack(stack);
	  int	nmax;
	  int *	vals;
	  int *	nvals;
	  int	i;
	
	  if( len == max ) {
	    nmax = max + INCR_INT_STACK;
	    nvals = xnalloc(int, nmax);
	    if( nvals == 0 ) {
	      PANIC("out of memory");
	    }
	    vals = vals_int_stack(stack);
	    for( i = 0; i < len; i++ ) {
	      nvals[i] = vals[i];
	    }
	    x_max_int_stack(stack) = nmax;
	    x_vals_int_stack(stack) = nvals;
	    xnfree(int, max, vals);
	  }
	
	  vals_int_stack(stack)[len] = val;
	  x_len_int_stack(stack) = len+1;
	  VERIFY_INT_STACK(stack);
	}
	
	void
	pop_int_stack(
		int_stack	stack
		)
	{
	
	  x_len_int_stack(stack)--;
	
	  VERIFY_INT_STACK(stack);
	}
	
	int
	top_int_stack(
		const int_stack	stack
		)
	{
	  int	top = val_int_stack(stack, len_int_stack(stack)-1);
	
	  VERIFY_INT_STACK(stack);
	  return top;
	}
	
	int
	is_empty_int_stack(
		const int_stack	stack
		)
	{
	  return len_int_stack(stack) == 0;
	}
	


Next Prev Main Top Feedback