Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.43 KB | None | 0 0
  1. # include <stdlib.h>
  2. # include <unistd.h>
  3. # include <fcntl.h>
  4.  
  5. typedef unsigned long t_bitset;
  6. unsigned long g_len;
  7.  
  8. t_bitset *init_first_line(int fd);
  9. t_bitset *init_bitset(int fd, int line);
  10. t_bitset *init_bitset_from_str(char *str);
  11.  
  12. char g_f;
  13. char g_o;
  14. char g_e;
  15. int g_n;
  16. int g_x;
  17. int g_y;
  18. int g_numlongs;
  19. int g_search_size;
  20. char *g_str;
  21. char g_first[13];
  22. t_bitset *g_map;
  23.  
  24. void solve(t_bitset **map);
  25. int store_map(t_bitset ***map, char *file);
  26. void check_existence(int existence, t_bitset **map);
  27. void set_globals(char *str, int i);t_bitset *bs_copy(t_bitset *set);
  28. void bs_and(t_bitset *set1, t_bitset *set2);
  29. void bs_shift(unsigned long **bits);
  30. int existence(t_bitset *set, int line);
  31. void ft_putchar(char c, int fd);
  32. int print_error(void);
  33. int check_error(t_bitset **map);
  34. void add_full_chars(void);
  35. void input(t_bitset ***map);
  36. int ft_atoi(char *str);
  37. char *str_realloc_double(char *str, unsigned long buff);
  38. char *str_realloc(char **str, unsigned long buff);
  39.  
  40. int g_i;
  41. int g_l;
  42. int g_k;
  43.  
  44. void bs_and(t_bitset *set1, t_bitset *set2)
  45. {
  46. int i;
  47.  
  48. i = 0;
  49. while (i < g_numlongs)
  50. {
  51. set1[i] &= set2[i];
  52. i++;
  53. }
  54. }
  55.  
  56. t_bitset *bs_copy(t_bitset *set)
  57. {
  58. t_bitset *output;
  59. int i;
  60.  
  61. output = (t_bitset *)malloc(sizeof(unsigned long) * g_numlongs);
  62. i = 0;
  63. while (i < g_numlongs)
  64. {
  65. output[i] = set[i];
  66. i++;
  67. }
  68. return (output);
  69. }
  70.  
  71. void bs_shift(unsigned long **bits)
  72. {
  73. int i;
  74.  
  75. i = 0;
  76. while (i < g_numlongs - 1)
  77. {
  78. (*bits)[i] &= ((*bits)[i] << 1) | (((*bits)[i + 1] >> 63) & 1);
  79. ++i;
  80. }
  81. (*bits)[g_numlongs - 1] &= (*bits)[g_numlongs - 1] << 1;
  82. }
  83.  
  84. int existence(t_bitset *set, int line)
  85. {
  86. int i;
  87. int j;
  88. unsigned long b;
  89.  
  90. i = 0;
  91. b = 0;
  92. while (i < g_numlongs && !b)
  93. b = (set[i++]);
  94. j = 0;
  95. while (b && j < 64)
  96. {
  97. if (b & (((unsigned long)1 << 63) >> j))
  98. {
  99. g_x = (i - 1) * 64 + j;
  100. g_y = line;
  101. return (1);
  102. }
  103. ++j;
  104. }
  105. return (0);
  106. }
  107.  
  108. void solve(t_bitset **map)
  109. {
  110. int s;
  111.  
  112. g_i = 0;
  113. g_l = 0;
  114. g_k = 0;
  115. g_search_size = 1;
  116. while (g_i + g_search_size <= g_n)
  117. {
  118. while (g_l < g_search_size - 1)
  119. bs_shift(&(map[g_i + g_l++]));
  120. while (g_k < g_search_size - 1)
  121. {
  122. bs_shift(&(map[g_i + g_search_size - 1]));
  123. g_k++;
  124. }
  125. s = 1;
  126. while (s < g_search_size)
  127. bs_and(map[g_i], map[g_i + s++]);
  128. check_existence(existence(map[g_i], g_i), map);
  129. }
  130. }
  131.  
  132. void check_existence(int existence, t_bitset **map)
  133. {
  134. int p;
  135.  
  136. if (existence)
  137. {
  138. g_l = 0;
  139. g_k = 0;
  140. ++g_search_size;
  141. }
  142. else
  143. {
  144. ++g_i;
  145. p = 0;
  146. while (g_i + g_search_size <= g_n && p < g_search_size - 1)
  147. {
  148. if (g_i + g_search_size <= g_n)
  149. bs_shift(&(map[g_i + g_search_size - 1]));
  150. p++;
  151. }
  152. }
  153. }
  154.  
  155. t_bitset *init_first_line(int fd)
  156. {
  157. unsigned int i;
  158. unsigned int buff;
  159.  
  160. buff = 16;
  161. g_str = (char *)malloc(sizeof(char) * buff);
  162. i = 0;
  163. while (read(fd, &g_str[i], 1) && g_str[i] ^ '\n')
  164. {
  165. if (i + 1 == buff)
  166. {
  167. buff <<= 1;
  168. g_str = str_realloc_double(g_str, buff);
  169. }
  170. i++;
  171. }
  172. g_numlongs = i / 64 + (i % 64 != 0);
  173. g_len = i;
  174. if (g_len == 0)
  175. return ((void *)0);
  176. return (init_bitset_from_str(g_str));
  177. }
  178.  
  179. t_bitset *init_bitset_from_str(char *str)
  180. {
  181. t_bitset *output;
  182. unsigned int i;
  183.  
  184. output = (t_bitset *)malloc(sizeof(t_bitset) * g_numlongs);
  185. i = -1;
  186. while (++i < (unsigned int)g_numlongs)
  187. output[i] = 0;
  188. i = 0;
  189. while (i < g_len)
  190. {
  191. if (str[i] != g_e && str[i] != g_o)
  192. return ((void *)0);
  193. output[i / 64] |= (((unsigned long)(str[i] == g_e) << 63) >> (i % 64));
  194. i++;
  195. }
  196. return (output);
  197. }
  198.  
  199. t_bitset *init_bitset(int fd, int line)
  200. {
  201. int ret;
  202.  
  203. ret = read(fd, &(g_str[line]), g_len + 1);
  204. if (ret > 0)
  205. {
  206. return (init_bitset_from_str(&(g_str[line])));
  207. }
  208. return ((void *)0);
  209. }
  210.  
  211. void set_globals(char *str, int i)
  212. {
  213. int digits;
  214. int n;
  215.  
  216. if (i > 3)
  217. {
  218. g_f = str[i - 1];
  219. g_o = str[i - 2];
  220. g_e = str[i - 3];
  221. str[i - 3] = '\0';
  222. g_n = ft_atoi(str);
  223. digits = 1;
  224. n = g_n;
  225. if (n < 0)
  226. n *= -1;
  227. while (n >= 10)
  228. {
  229. n /= 10;
  230. ++digits;
  231. }
  232. if (digits + 3 != i || g_o == g_e || g_o == g_f || g_f == g_e)
  233. g_n = 0;
  234. }
  235. }
  236.  
  237. int store_map(t_bitset ***map, char *file)
  238. {
  239. int i;
  240. char c;
  241. int fd;
  242.  
  243. fd = open(file, O_RDONLY);
  244. if (fd < 0)
  245. return (0);
  246. i = 0;
  247. while (read(fd, &c, 1) && c != '\n' && i < 13)
  248. g_first[i++] = c;
  249. set_globals(g_first, i);
  250. (*map) = (t_bitset **)malloc(sizeof(t_bitset *) * g_n);
  251. (*map)[0] = init_first_line(fd);
  252. g_str = str_realloc(&g_str, (g_len + 1) * g_n);
  253. if ((*map)[0] && g_len > 0)
  254. {
  255. i = 0;
  256. while (++i < g_n)
  257. (*map)[i] = init_bitset(fd, (g_len + 1) * i);
  258. }
  259. if (g_str[(g_len + 1) * i - 1] != '\n')
  260. return (0);
  261. close(fd);
  262. return (check_error(*map));
  263. }
  264.  
  265. int print_error(void)
  266. {
  267. write(2, "map error\n", 10);
  268. return (1);
  269. }
  270.  
  271. int check_error(t_bitset **map)
  272. {
  273. int i;
  274.  
  275. i = 0;
  276. while (i < g_n)
  277. {
  278. if (map[i] == NULL)
  279. return (0);
  280. ++i;
  281. }
  282. return (g_n);
  283. }
  284.  
  285. void add_full_chars(void)
  286. {
  287. int i;
  288. int j;
  289.  
  290. i = g_y;
  291. while (i < g_y + g_search_size - 1)
  292. {
  293. j = g_x;
  294. while (j < g_x + g_search_size - 1)
  295. {
  296. g_str[(g_len + 1) * i + j] = g_f;
  297. j++;
  298. }
  299. i++;
  300. }
  301. }
  302.  
  303. void input(t_bitset ***map)
  304. {
  305. int i;
  306.  
  307. i = 0;
  308. while (read(0, &g_first[i], 1) && g_first[i] != '\n')
  309. ++i;
  310. set_globals(g_first, i);
  311. (*map) = (t_bitset **)malloc(sizeof(t_bitset *) * g_n);
  312. (*map)[0] = init_first_line(0);
  313. g_str = str_realloc(&g_str, (g_len + 1) * g_n);
  314. if ((*map)[0] && g_len > 0)
  315. {
  316. i = 0;
  317. while (++i < g_n)
  318. (*map)[i] =
  319. init_bitset(0, (g_len + 1) * i);
  320. }
  321. if (check_error(*map))
  322. {
  323. solve(*map);
  324. add_full_chars();
  325. write(1, g_str, g_n * (g_len + 1));
  326. free(g_str);
  327. }
  328. else
  329. print_error();
  330. }
  331.  
  332. void ft_putchar(char c, int fd)
  333. {
  334. write(fd, &c, 1);
  335. }
  336.  
  337. int ft_atoi(char *str)
  338. {
  339. int value;
  340. int sign;
  341.  
  342. value = 0;
  343. sign = 1;
  344. while (*str == '\n' || *str == ' ' || *str == '\t' || *str == '\r' ||
  345. *str == '\v' || *str == '\f')
  346. {
  347. ++str;
  348. }
  349. if (*str == '+' || *str == '-')
  350. {
  351. if (*str == '-')
  352. return (0);
  353. str++;
  354. }
  355. while ((*str) >= '0' && (*str) <= '9')
  356. {
  357. value *= 10;
  358. value = (sign ? value + (*str - '0') : value - (*str - '0'));
  359. str++;
  360. }
  361. return (value);
  362. }
  363.  
  364. char *str_realloc(char **str, unsigned long buff)
  365. {
  366. char *new_str;
  367. unsigned int i;
  368.  
  369. new_str = (char *)malloc(sizeof(char) * buff);
  370. i = 0;
  371. while (i < g_len + 1)
  372. {
  373. new_str[i] = (*str)[i];
  374. ++i;
  375. }
  376. free((*str));
  377. return (new_str);
  378. }
  379.  
  380. char *str_realloc_double(char *str, unsigned long buff)
  381. {
  382. char *new_str;
  383. unsigned int i;
  384.  
  385. new_str = (char *)malloc(sizeof(char) * buff);
  386. buff >>= 1;
  387. i = 0;
  388. while (i < buff)
  389. {
  390. new_str[i] = (str)[i];
  391. ++i;
  392. }
  393. free((str));
  394. return (new_str);
  395. }
  396.  
  397. int main(int argc, char *argv[])
  398. {
  399. t_bitset **map;
  400. int i;
  401.  
  402. map = (void *)0;
  403. i = 1;
  404. if (argc == 1)
  405. input(&map);
  406. while (i < argc)
  407. {
  408. if (store_map(&map, argv[i]))
  409. {
  410. solve(map);
  411. add_full_chars();
  412. write(1, g_str, g_n * (g_len + 1));
  413. free(g_str);
  414. }
  415. else
  416. print_error();
  417. ++i;
  418. if (argc > 2)
  419. ft_putchar('\n', 1);
  420. }
  421. return (0);
  422. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement