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 8a5bdf0 was ac4dad2, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago |
shorten experimental code
|
-
Property mode
set to
100644
|
File size:
1003 bytes
|
Rev | Line | |
---|
[604e76d] | 1 | #include <stdlib>
|
---|
| 2 | #include "cfa-stack.h"
|
---|
| 3 |
|
---|
[ac4dad2] | 4 | forall( otype T ) {
|
---|
| 5 | struct node {
|
---|
| 6 | T value;
|
---|
| 7 | node(T) * next;
|
---|
| 8 | };
|
---|
| 9 |
|
---|
| 10 | void ?{}( stack(T) & s ) { (s.head){ 0 }; }
|
---|
| 11 |
|
---|
| 12 | void ?{}( stack(T) & s, stack(T) t ) {
|
---|
| 13 | node(T) ** cr = &s.head;
|
---|
| 14 | for ( node(T) * nx = t.head; nx; nx = nx->next ) {
|
---|
| 15 | *cr = alloc();
|
---|
| 16 | ((*cr)->value){ nx->value };
|
---|
| 17 | cr = &(*cr)->next;
|
---|
| 18 | }
|
---|
| 19 | *cr = 0;
|
---|
[8b001bd] | 20 | }
|
---|
| 21 |
|
---|
[ac4dad2] | 22 | void ^?{}( stack(T) & s) { clear( s ); }
|
---|
[604e76d] | 23 |
|
---|
[ac4dad2] | 24 | void clear( stack(T) & s ) with( s ) {
|
---|
| 25 | for ( node(T) * nx = head; nx; ) {
|
---|
| 26 | node(T) * cr = nx;
|
---|
| 27 | nx = cr->next;
|
---|
| 28 | ^(*cr){};
|
---|
| 29 | free(cr);
|
---|
| 30 | }
|
---|
| 31 | head = 0;
|
---|
[604e76d] | 32 | }
|
---|
| 33 |
|
---|
[ac4dad2] | 34 | stack(T) ?=?( stack(T) & s, stack(T) t ) {
|
---|
| 35 | if ( s.head == t.head ) return s;
|
---|
| 36 | clear( s );
|
---|
| 37 | s{ t };
|
---|
| 38 | return s;
|
---|
| 39 | }
|
---|
[604e76d] | 40 |
|
---|
[ac4dad2] | 41 | _Bool empty( const stack(T) & s ) { return s.head == 0; }
|
---|
[604e76d] | 42 |
|
---|
[ac4dad2] | 43 | void push( stack(T) & s, T value ) with( s ) {
|
---|
| 44 | node(T) * n = alloc();
|
---|
| 45 | (*n){ value, head };
|
---|
| 46 | head = n;
|
---|
| 47 | }
|
---|
[604e76d] | 48 |
|
---|
[ac4dad2] | 49 | T pop( stack(T) & s ) with( s ) {
|
---|
| 50 | node(T) * n = head;
|
---|
| 51 | head = n->next;
|
---|
| 52 | T v = n->value;
|
---|
| 53 | ^(*n){};
|
---|
| 54 | free( n );
|
---|
| 55 | return v;
|
---|
| 56 | }
|
---|
[604e76d] | 57 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.