Changeset ac4dad2 for doc/papers/general/evaluation/c-stack.c
- Timestamp:
- Apr 28, 2018, 9:20:28 AM (5 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, with_gc
- Children:
- 6926a6d
- Parents:
- aa5fdac
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/papers/general/evaluation/c-stack.c
raa5fdac rac4dad2 2 2 #include "c-stack.h" 3 3 4 struct stack_node {4 typedef struct node { 5 5 void * value; 6 struct stack_node * next;7 } ;6 struct node * next; 7 } node; 8 8 9 void clear_stack( struct stack * s, void (*free_el)( void * ) ) { 10 for ( struct stack_node * next = s->head; next; ) { 11 struct stack_node * crnt = next; 12 next = crnt->next; 13 free_el( crnt->value ); 14 free( crnt ); 9 void copy_stack( stack * s, const stack * t, void * (*copy)( const void * ) ) { 10 node ** cr = &s->head; 11 for ( node * nx = t->head; nx; nx = nx->next ) { 12 *cr = malloc( sizeof(node) ); /***/ 13 (*cr)->value = copy( nx->value ); 14 cr = &(*cr)->next; 15 } 16 *cr = NULL; 17 } 18 19 void clear_stack( stack * s, void (* free_el)( void * ) ) { 20 for ( node * nx = s->head; nx; ) { 21 node * cr = nx; 22 nx = cr->next; 23 free_el( cr->value ); 24 free( cr ); 15 25 } 16 26 s->head = NULL; 17 27 } 18 28 19 st ruct stack new_stack() { return (structstack){ NULL }; /***/ }29 stack new_stack() { return (stack){ NULL }; /***/ } 20 30 21 void copy_stack( struct stack * s, const struct stack * t, void * (*copy)( const void * ) ) { 22 struct stack_node ** crnt = &s->head; 23 for ( struct stack_node * next = t->head; next; next = next->next ) { 24 *crnt = malloc( sizeof(struct stack_node) ); /***/ 25 (*crnt)->value = copy( next->value ); 26 crnt = &(*crnt)->next; 27 } 28 *crnt = NULL; 29 } 30 struct stack * assign_stack( struct stack * s, const struct stack * t, 31 stack * assign_stack( stack * s, const stack * t, 31 32 void * (*copy_el)( const void * ), void (*free_el)( void * ) ) { 32 33 if ( s->head == t->head ) return s; … … 36 37 } 37 38 38 _Bool stack_empty( const st ruct stack * s ) { return s->head == NULL; }39 _Bool stack_empty( const stack * s ) { return s->head == NULL; } 39 40 40 void push_stack( st ruct stack * s, void * v ) {41 struct stack_node * n = malloc( sizeof(struct stack_node) ); /***/42 *n = ( struct stack_node){ v, s->head }; /***/41 void push_stack( stack * s, void * v ) { 42 node * n = malloc( sizeof(node) ); /***/ 43 *n = (node){ v, s->head }; /***/ 43 44 s->head = n; 44 45 } 45 46 46 void * pop_stack( st ruct stack * s ) {47 struct stack_node * n = s->head;47 void * pop_stack( stack * s ) { 48 node * n = s->head; 48 49 s->head = n->next; 49 50 void * v = n->value;
Note: See TracChangeset
for help on using the changeset viewer.