source:
doc/generic_types/evaluation/cfastack.c
@
a381b46
Last change on this file since a381b46 was a381b46, checked in by , 5 years ago  



File size: 1.1 KB 
Rev  Line  

[309be81]  1  #include <stdlib> 
2  #include "cfastack.h"  
3  
4  forall(otype T) struct stack_node {  
5  T value;  
6  stack_node(T)* next;  
7  };  
8  
[a381b46]  9  forall(otype T) void ?{}(stack(T)* s) { (&s>head){ 0 }; } 
[d919f47]  10  
[122aecd]  11  forall(otype T) void ?{}(stack(T)* s, stack(T) t) { 
12  stack_node(T)** crnt = &s>head;  
13  stack_node(T)* next = t.head;  
[309be81]  14  while ( next ) { 
[3fb7f5e]  15  *crnt = ((stack_node(T)*)malloc()){ next>value }; /***/ 
[122aecd]  16  stack_node(T)* acrnt = *crnt; 
17  crnt = &acrnt>next;  
18  next = next>next;  
[309be81]  19  } 
[122aecd]  20  *crnt = 0; 
21  }  
22  
23  forall(otype T) stack(T) ?=?(stack(T)* s, stack(T) t) {  
24  if ( s>head == t.head ) return *s;  
25  clear(s);  
26  s{ t };  
27  return *s;  
28  }  
29  
[a381b46]  30  forall(otype T) void ^?{}(stack(T)* s) { clear(s); } 
[309be81]  31  
[a381b46]  32  forall(otype T) _Bool empty(const stack(T)* s) { return s>head == 0; } 
[309be81]  33  
34  forall(otype T) void push(stack(T)* s, T value) {  
[3fb7f5e]  35  s>head = ((stack_node(T)*)malloc()){ value, s>head }; /***/ 
[309be81]  36  } 
37  
38  forall(otype T) T pop(stack(T)* s) {  
39  stack_node(T)* n = s>head;  
40  s>head = n>next;  
41  T x = n>value;  
[65cb413]  42  ^n{}; 
43  free(n);  
[309be81]  44  return x; 
45  }  
[122aecd]  46  
47  forall(otype T) void clear(stack(T)* s) {  
48  stack_node(T)* next = s>head;  
49  while ( next ) {  
50  stack_node(T)* crnt = next;  
51  next = crnt>next;  
52  delete(crnt);  
53  }  
[d919f47]  54  s>head = 0; 
[122aecd]  55  } 
Note: See TracBrowser
for help on using the repository browser.