Advertisement
pberejnoy

Bit Matrix

Nov 22nd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.39 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define ABS(x) ((x) < 0 ? (-1 * x) : x)
  4. #define MAX_MATR_SIZE 20000
  5.  
  6. typedef char MatrT[MAX_MATR_SIZE * 2 / 4]; // x2 to store one cell as 2 bits
  7. static MatrT matrixes[4];
  8.  
  9. struct cell {
  10.     char *matr;
  11.     int byteNum;
  12.     int bitNum;
  13. };
  14.  
  15. static char *getMatr(int x, int y) {
  16.     if (x < 0 && y < 0)
  17.         return matrixes[0];
  18.     if (x < 0 && y > 0)
  19.         return matrixes[1];
  20.     if (x > 0 && y < 0)
  21.         return matrixes[2];
  22.     return matrixes[3];
  23. }
  24.  
  25. static struct cell getCell(int x, int y) {
  26.     char *m = getMatr(x, y);
  27.     x = ABS(x);
  28.     y = ABS(y);
  29.  
  30.     // 1 byte contain 4 cells
  31.     // Find cell index
  32.     int cell = x * 4 + y * 2;
  33.  
  34.     // Find a byte and bif for specific cell
  35.     int byteNum = cell / 8;
  36.     int bitNum = cell - byteNum * 8;
  37.  
  38.     struct cell c = {
  39.         .matr = m,
  40.         .byteNum = byteNum,
  41.         .bitNum = bitNum,
  42.     };
  43.  
  44.     return c;
  45. }
  46.  
  47. #define EMPTY 0     // 00
  48. #define FILLED 1    // 01
  49. #define WALL 2      // 10
  50. #define CELL_MASK 3 // 11
  51.  
  52. static void setCell(int x, int y, int value) {
  53.     struct cell c = getCell(x, y);
  54.     c.matr[c.byteNum] |= value << c.bitNum;
  55. }
  56.  
  57. static void setWall(int x, int y) {
  58.     setCell(x, y, WALL);
  59. }
  60.  
  61. static void setFilled(int x, int y) {
  62.     setCell(x, y, FILLED);
  63. }
  64.  
  65. static int getValue(int x, int y) {
  66.     struct cell c = getCell(x, y);
  67.     int value = c.matr[c.byteNum] & (CELL_MASK << c.bitNum);
  68.     return value >> c.bitNum;
  69. }
  70.  
  71. int main() {
  72.     struct val {
  73.         int x;
  74.         int y;
  75.     };
  76.  
  77.     struct val tests[] = {
  78.         {  0,     0    },
  79.         {  -10,   0    },
  80.         {  0,     -10  },
  81.         {  10,    0    },
  82.         {  0,     10   },
  83.         {  1,     1    },
  84.         {  10,    -1   },
  85.         {  -10,   23   },
  86.         {  -47,   -53  },
  87.         {  1,     1    },
  88.         {  2,     2    },
  89.         {  3,     3    },
  90.         {  4,     4    },
  91.         {  5,     5    },
  92.         {  6,     6    },
  93.         {  7,     7    },
  94.         {  8,     8    },
  95.         {  9,     9    },
  96.         {  10,    10   },
  97.         {  11,    11   },
  98.         {  12,    12   },
  99.         {  13,    13   },
  100.         {  14,    14   },
  101.         {  15,    15   },
  102.         {  16,    16   },
  103.         {  17,    17   },
  104.         {  18,    18   },
  105.         {  19,    19   },
  106.         {  20,    20   },
  107.         {  21,    21   },
  108.         {  22,    22   },
  109.         {  23,    23   },
  110.         {  24,    24   },
  111.         {  25,    25   },
  112.         {  26,    26   },
  113.         {  27,    27   },
  114.         {  28,    28   },
  115.         {  29,    29   },
  116.         {  30,    30   },
  117.         {  31,    31   },
  118.         {  32,    32   },
  119.         {  33,    33   },
  120.         {  34,    34   },
  121.         {  35,    35   },
  122.         {  36,    36   },
  123.         {  37,    37   },
  124.         {  38,    38   },
  125.         {  39,    39   },
  126.         {  40,    40   },
  127.         {  -41,   41   },
  128.         {  -42,   42   },
  129.         {  -43,   43   },
  130.         {  -44,   44   },
  131.         {  -45,   45   },
  132.         {  -46,   46   },
  133.         {  -47,   47   },
  134.         {  -48,   48   },
  135.         {  -49,   49   },
  136.         {  -50,   50   },
  137.         {  -51,   51   },
  138.         {  -52,   52   },
  139.         {  -53,   53   },
  140.         {  -54,   54   },
  141.         {  -55,   55   },
  142.         {  -56,   56   },
  143.         {  -57,   57   },
  144.         {  -58,   58   },
  145.         {  -59,   59   },
  146.         {  -60,   60   },
  147.         {  -61,   61   },
  148.         {  -62,   62   },
  149.         {  -63,   63   },
  150.         {  -64,   64   },
  151.         {  -65,   65   },
  152.         {  -66,   66   },
  153.         {  -67,   67   },
  154.         {  -68,   68   },
  155.         {  -69,   69   },
  156.         {  -70,   70   },
  157.         {  -71,   71   },
  158.         {  -72,   72   },
  159.         {  -73,   73   },
  160.         {  -74,   74   },
  161.         {  -75,   75   },
  162.         {  -76,   76   },
  163.         {  -77,   77   },
  164.         {  -78,   78   },
  165.         {  -79,   79   },
  166.         {  -80,   80   },
  167.         {  -81,   81   },
  168.         {  -82,   82   },
  169.         {  -83,   83   },
  170.         {  -84,   84   },
  171.         {  -85,   85   },
  172.         {  -86,   86   },
  173.         {  -87,   87   },
  174.         {  -88,   88   },
  175.         {  -89,   89   },
  176.         {  -90,   90   },
  177.         {  -91,   91   },
  178.         {  -92,   92   },
  179.         {  -93,   93   },
  180.         {  -94,   94   },
  181.         {  -95,   95   },
  182.         {  -96,   96   },
  183.         {  -97,   97   },
  184.         {  -98,   98   },
  185.         {  -99,   99   },
  186.         {  -100,  100  },
  187.     };
  188.  
  189.     printf("\n\nFILLING\n\n");
  190.  
  191.     for (int i = 0; i < sizeof(tests) / sizeof(*tests); ++i) {
  192.         if (i % 2) {
  193.             printf("% 4d x % 4d is filled\n", tests[i].x, tests[i].y);
  194.             setFilled(tests[i].x, tests[i].y);
  195.         } else {
  196.             printf("% 4d x % 4d is a wall\n", tests[i].x, tests[i].y);
  197.             setWall(tests[i].x, tests[i].y);
  198.         }
  199.     }
  200.  
  201.     printf("\n\nTESTING\n\n");
  202.  
  203.     for (int i = 0; i < sizeof(tests) / sizeof(*tests); ++i) {
  204.         int value = getValue(tests[i].x, tests[i].y);
  205.  
  206.         const char *type = "UNKNOWN";
  207.         if (value == WALL)
  208.             type = "a wall";
  209.         else if (value == FILLED)
  210.             type = "filled";
  211.         else if (value == EMPTY)
  212.             type = "empty";
  213.  
  214.         printf("% 4d x % 4d is %s\n", tests[i].x, tests[i].y, type);
  215.     }
  216.  
  217.     return 0;
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement