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 87c5f40 was
87c5f40,
checked in by Aaron Moss <a3moss@…>, 8 years ago
|
Update benchmarks to include stack of pairs
|
-
Property mode set to
100644
|
File size:
1.1 KB
|
Rev | Line | |
---|
[309be81] | 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() { |
---|
| 10 | return (struct stack){ NULL }; |
---|
| 11 | } |
---|
| 12 | |
---|
[87c5f40] | 13 | void copy_stack(struct stack* s, const struct stack* t, void* (*copy)(const void*)) { |
---|
[122aecd] | 14 | struct stack_node** crnt = &s->head; |
---|
| 15 | struct stack_node* next = t->head; |
---|
| 16 | while ( next ) { |
---|
| 17 | *crnt = malloc(sizeof(struct stack_node)); |
---|
| 18 | **crnt = (struct stack_node){ copy(next->value) }; |
---|
| 19 | crnt = &(*crnt)->next; |
---|
| 20 | next = next->next; |
---|
| 21 | } |
---|
| 22 | *crnt = 0; |
---|
| 23 | } |
---|
| 24 | |
---|
[87c5f40] | 25 | void clear_stack(struct stack* s, void (*free_el)(void*)) { |
---|
[309be81] | 26 | struct stack_node* next = s->head; |
---|
| 27 | while ( next ) { |
---|
| 28 | struct stack_node* crnt = next; |
---|
| 29 | next = crnt->next; |
---|
[87c5f40] | 30 | free_el(crnt->value); |
---|
[309be81] | 31 | free(crnt); |
---|
| 32 | } |
---|
[d919f47] | 33 | s->head = NULL; |
---|
[309be81] | 34 | } |
---|
| 35 | |
---|
| 36 | _Bool stack_empty(const struct stack* s) { |
---|
| 37 | return s->head == NULL; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | void push_stack(struct stack* s, void* value) { |
---|
| 41 | struct stack_node* n = malloc(sizeof(struct stack_node)); |
---|
| 42 | *n = (struct stack_node){ value, s->head }; |
---|
| 43 | s->head = n; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | void* pop_stack(struct stack* s) { |
---|
| 47 | struct stack_node* n = s->head; |
---|
| 48 | s->head = n->next; |
---|
| 49 | void* x = n->value; |
---|
| 50 | free(n); |
---|
| 51 | return x; |
---|
| 52 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.