Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <memory.h>
- #include <stdint.h>
- #define MAX 10
- typedef struct {
- int x, y;
- } pointworm_t;
- pointworm_t worm[MAX] = {
- 5,5,
- 5,4,
- 4,4,
- 3,4,
- 2,4,
- 2,3,
- }, newhead;
- typedef struct {
- pointworm_t * begin, * end;
- } vec_t;
- typedef enum {SET, GET} vec_ctx_state;
- vec_t vec_ctx(vec_ctx_state state, vec_t set) {
- static vec_t vec = {0};
- return (state == GET) ? vec : (vec = set);
- }
- inline void set_ctx(vec_t vec) {
- vec_ctx(SET, vec);
- }
- inline vec_t get_ctx(void) {
- return vec_ctx(GET, (vec_t){0});
- }
- inline uintmax_t vec_size_noctx(vec_t v) {
- return (uintptr_t)((void *)v.end - (void *)v.begin);
- }
- inline uintmax_t vec_size(void) {
- vec_t v = get_ctx(); return vec_size_noctx(v);
- }
- inline uintmax_t vec_elem_size_noctx(vec_t v) { return sizeof(typeof(*v.begin)); }
- inline uintmax_t vec_elem_size(void) {
- vec_t v = get_ctx(); return vec_elem_size_noctx(v);
- }
- inline void vec_movesnake(void) {
- vec_t v = get_ctx();
- memmove(v.begin + 1, v.begin, vec_size() - vec_elem_size());
- };
- inline void vec_set_head(pointworm_t pnewhead) {
- *(get_ctx().begin) = pnewhead;
- }
- inline pointworm_t add(pointworm_t a, pointworm_t b) {
- return (pointworm_t){a.x + b.x, a.y + b.y};
- }
- inline pointworm_t create_point(int32_t x, int32_t y) {
- return (pointworm_t){x, y};
- }
- inline pointworm_t vec_getn(uintmax_t n) {
- return *(get_ctx().begin + n);
- }
- inline pointworm_t * vec_end(void) {
- return get_ctx().end;
- }
- inline pointworm_t * vec_begin(void) {
- return get_ctx().begin;
- }
- inline vec_print(typeof(stdout) out) {
- typeof(vec_begin()) it = vec_begin(), end = vec_end();
- do {
- fprintf(out, "%d, %d\n", it->x, it->y);
- } while(++it != end);
- }
- int32_t main(void) {
- set_ctx((vec_t){worm, worm + 6});
- newhead = add(vec_getn(0), create_point(1, 0));
- vec_movesnake();
- vec_set_head(newhead);
- vec_print(stdout);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment