diff options
| -rw-r--r-- | stack.c | 23 | ||||
| -rw-r--r-- | stack.h | 18 |
2 files changed, 40 insertions, 1 deletions
@@ -0,0 +1,23 @@ +#include "stack.h" +#include "structs.h" + +PositionStack ps_new(void) { + PositionStack ps; + ps.top = 0; + return ps; +} + +int ps_push(PositionStack ps, Position pos) { + ps.arr[ps.top] = pos; + ps.top += 1; +} + +Position ps_pop(PositionStack ps) { + ps.top -= 1; + return ps.arr[ps.top]; +} + +Position ps_peek(PositionStack ps) { + return ps.arr[ps.top - 1]; +} + @@ -1,6 +1,22 @@ #ifndef STACK_H_ #define STACK_H_ -// TODO: implement a stack +#include "structs.h" + +/* So, the implementation of the stack is array-based, for hyperspeed. + * For bigger maps we have to increase the STACK_SIZE, but it should + * work fine most of the time. I guess */ +#define STACK_SIZE 4096 + +struct PositionStack_s { + Position arr[STACK_SIZE]; /* The array with all the values */ + size_t top; /* Shows where the top of the stack is */ +}; +typedef struct PositionStack_s PositionStack; + +PositionStack ps_new(void); /* Returns an empty position stack */ +int ps_push(PositionStack ps, Position pos); /* Returns -1 if overflow */ +Position ps_pop(PositionStack ps); +Position ps_peek(PositionStack ps); #endif /* STACK_H_ */ |
