ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change
on this file since e59f0bf was
e59f0bf,
checked in by Aaron Moss <a3moss@…>, 6 years ago
|
Update benchmarks so that C has assign operator like others
|
-
Property mode set to
100644
|
File size:
1.3 KB
|
Line | |
---|
1 | #include <stdlib.h> |
---|
2 | #include "c-stack.h" |
---|
3 | |
---|
4 | struct stack_node { |
---|
5 | void* value; |
---|
6 | struct stack_node* next; |
---|
7 | }; |
---|
8 | |
---|
9 | struct stack new_stack() { return (struct stack){ NULL }; /***/ } |
---|
10 | |
---|
11 | void copy_stack(struct stack* s, const struct stack* t, void* (*copy)(const void*)) { |
---|
12 | struct stack_node** crnt = &s->head; |
---|
13 | for ( struct stack_node* next = t->head; next; next = next->next ) { |
---|
14 | *crnt = malloc(sizeof(struct stack_node)); /***/ |
---|
15 | (*crnt)->value = copy(next->value); |
---|
16 | crnt = &(*crnt)->next; |
---|
17 | } |
---|
18 | *crnt = NULL; |
---|
19 | } |
---|
20 | |
---|
21 | struct stack* assign_stack(struct stack* s, const struct stack* t, |
---|
22 | void* (*copy_el)(const void*), void (*free_el)(void*)) { |
---|
23 | if ( s->head == t->head ) return s; |
---|
24 | clear_stack( s, free_el ); /***/ |
---|
25 | copy_stack( s, t, copy_el ); /***/ |
---|
26 | return s; |
---|
27 | } |
---|
28 | |
---|
29 | void clear_stack(struct stack* s, void (*free_el)(void*)) { |
---|
30 | for ( struct stack_node* next = s->head; next; ) { |
---|
31 | struct stack_node* crnt = next; |
---|
32 | next = crnt->next; |
---|
33 | free_el(crnt->value); |
---|
34 | free(crnt); |
---|
35 | } |
---|
36 | s->head = NULL; |
---|
37 | } |
---|
38 | |
---|
39 | _Bool stack_empty(const struct stack* s) { return s->head == NULL; } |
---|
40 | |
---|
41 | void push_stack(struct stack* s, void* value) { |
---|
42 | struct stack_node* n = malloc(sizeof(struct stack_node)); /***/ |
---|
43 | *n = (struct stack_node){ value, s->head }; /***/ |
---|
44 | s->head = n; |
---|
45 | } |
---|
46 | |
---|
47 | void* pop_stack(struct stack* s) { |
---|
48 | struct stack_node* n = s->head; |
---|
49 | s->head = n->next; |
---|
50 | void* x = n->value; |
---|
51 | free(n); |
---|
52 | return x; |
---|
53 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.