source: doc/papers/general/evaluation/c-stack.c@ 1a3eab8

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum with_gc
Last change on this file since 1a3eab8 was ac4dad2, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

shorten experimental code

  • Property mode set to 100644
File size: 1.2 KB
Line 
1#include <stdlib.h>
2#include "c-stack.h"
3
4typedef struct node {
5 void * value;
6 struct node * next;
7} node;
8
9void 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
19void 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 );
25 }
26 s->head = NULL;
27}
28
29stack new_stack() { return (stack){ NULL }; /***/ }
30
31stack * assign_stack( stack * s, const stack * t,
32 void * (*copy_el)( const void * ), void (*free_el)( void * ) ) {
33 if ( s->head == t->head ) return s;
34 clear_stack( s, free_el ); /***/
35 copy_stack( s, t, copy_el ); /***/
36 return s;
37}
38
39_Bool stack_empty( const stack * s ) { return s->head == NULL; }
40
41void push_stack( stack * s, void * v ) {
42 node * n = malloc( sizeof(node) ); /***/
43 *n = (node){ v, s->head }; /***/
44 s->head = n;
45}
46
47void * pop_stack( stack * s ) {
48 node * n = s->head;
49 s->head = n->next;
50 void * v = n->value;
51 free( n );
52 return v;
53}
Note: See TracBrowser for help on using the repository browser.