Guest User

Untitled

a guest
Jan 9th, 2014
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.96 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <memory.h>
  4. #include <stdint.h>
  5. #define MAX 10
  6.  
  7.  
  8. typedef struct {
  9.   int x, y;
  10. } pointworm_t;
  11.  
  12.  
  13. pointworm_t worm[MAX] = {  
  14.   5,5,
  15.   5,4,
  16.   4,4,
  17.   3,4,
  18.   2,4,
  19.   2,3,
  20. }, newhead;
  21.  
  22. typedef struct {
  23.   pointworm_t * begin, * end;
  24. } vec_t;
  25.  
  26.  
  27.  
  28. typedef enum {SET, GET} vec_ctx_state;
  29.  
  30. vec_t vec_ctx(vec_ctx_state state, vec_t set) {
  31.   static vec_t vec = {0};
  32.   return (state == GET) ? vec : (vec = set);
  33. }
  34.  
  35. inline  void set_ctx(vec_t vec) {
  36.   vec_ctx(SET, vec);
  37. }
  38.  
  39. inline  vec_t get_ctx(void) {
  40.   return vec_ctx(GET, (vec_t){0});
  41. }
  42.  
  43. inline  uintmax_t vec_size_noctx(vec_t v) {
  44.   return (uintptr_t)((void *)v.end - (void *)v.begin);
  45. }
  46.  
  47. inline  uintmax_t vec_size(void) {
  48.   vec_t v = get_ctx(); return vec_size_noctx(v);
  49. }
  50.  
  51. inline  uintmax_t vec_elem_size_noctx(vec_t v) { return sizeof(typeof(*v.begin)); }
  52.  
  53. inline uintmax_t vec_elem_size(void) {  
  54.   vec_t v = get_ctx(); return vec_elem_size_noctx(v);
  55. }
  56.  
  57. inline void vec_movesnake(void) {
  58.   vec_t v = get_ctx();
  59.   memmove(v.begin + 1, v.begin, vec_size() - vec_elem_size());
  60. };
  61.  
  62. inline  void vec_set_head(pointworm_t pnewhead) {
  63.   *(get_ctx().begin) = pnewhead;
  64. }
  65.  
  66. inline pointworm_t add(pointworm_t a, pointworm_t b) {
  67.   return (pointworm_t){a.x + b.x, a.y + b.y};
  68. }
  69.  
  70. inline pointworm_t create_point(int32_t x, int32_t y) {
  71.   return (pointworm_t){x, y};
  72. }
  73.  
  74. inline pointworm_t vec_getn(uintmax_t n) {
  75.   return *(get_ctx().begin + n);
  76. }
  77.  
  78. inline pointworm_t * vec_end(void) {
  79.   return get_ctx().end;
  80. }
  81.  
  82. inline pointworm_t * vec_begin(void) {
  83.   return get_ctx().begin;
  84. }
  85.  
  86. inline vec_print(typeof(stdout) out) {
  87.   typeof(vec_begin()) it = vec_begin(), end = vec_end();
  88.   do {
  89.     fprintf(out, "%d, %d\n", it->x, it->y);
  90.   } while(++it != end);
  91. }
  92.  
  93. int32_t main(void) {
  94.   set_ctx((vec_t){worm, worm + 6});
  95.  
  96.   newhead = add(vec_getn(0), create_point(1, 0));
  97.   vec_movesnake();
  98.   vec_set_head(newhead);  
  99.   vec_print(stdout);
  100.  
  101.   return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment