This repository has been archived by the owner on Mar 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader.h
307 lines (242 loc) · 6.5 KB
/
header.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* checker.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cpireyre <cpireyre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/08/31 12:32:30 by cpireyre #+# #+# */
/* Updated: 2018/09/14 10:36:42 by cpireyre ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef HEADER_H
# define HEADER_H
# include "libft.h"
# include <ncurses.h>
# define VISUAL_MODE "-v"
# define MSG_ERROR "Error\n"
# define MSG_INVALID "Error: invalid argument.\n"
# define MSG_OK "OK\n"
# define MSG_NOT_OK "KO\n"
# define MSG_DUPES "Error: duplicate entries.\n"
# define MSG_BAD_ACTION "Error: invalid instruction.\n"
# define A (ps->a)
# define B (ps->b)
# define PA pa(ps, will_print)
# define PB pb(ps, will_print)
# define SA sa(ps, will_print)
# define SB sb(ps, will_print)
# define SS ss(ps, will_print)
# define RA ra(ps, will_print)
# define RB rb(ps, will_print)
# define RR rr(ps, will_print)
# define RRA rra(ps, will_print)
# define RRB rrb(ps, will_print)
# define RRR rrr(ps, will_print)
typedef struct s_ps
{
int *a;
int *b;
} t_ps;
typedef struct s_ops
{
char *op;
struct s_ops *prev;
struct s_ops *next;
} t_ops;
/*
** list.c
*/
t_ops *add_tail(t_ops **oldtail, char *input);
void free_op(t_ops **to_free);
void free_all_ops(t_ops **head);
t_ops *make_list(int fd);
/*
** parse.c
*/
t_ps *parse(int argc, char **argv);
int *create_sorted_copy(int *tab, int size);
t_bool is_valid_arg(const char *arg);
t_bool has_dupes(int *sorted, int size);
int *create_tab(int argc, char **argv);
/*
** normalize.c
*/
int *calculate_splits(int *sorted, int size, int *nbr_splits);
void normalize_tab(int **tab, int *sorted, int size);
/*
** actions.c
*/
void do_action(t_ps *ps, char *line, t_bool will_print);
/*
** visu.c
*/
t_ps *parse_visu(int argc, char **argv, int **sorted);
/*
** draw.c
*/
void visu_wrapper(t_ps *ps, char *line, int *sorted);
/*
** mem.c
*/
int tablen(int *tab);
void free_ps(t_ps **ps);
void ft_exit(const char *errmsg);
void quit_push_swap(t_ps **ps, const char *errmsg);
/*
** tab.c
*/
void tab_erase_first(int **tab);
void tab_add_first(int **tab, int value);
void tab_rotate_down(int **tab);
void tab_rotate_up(int **tab);
/*
** push.c
*/
void pb(t_ps *ps, t_bool will_print);
void pa(t_ps *ps, t_bool will_print);
/*
** swap.c
*/
void sa(t_ps *ps, t_bool will_print);
void sb(t_ps *ps, t_bool will_print);
void ss(t_ps *ps, t_bool will_print);
/*
** rotate.c
*/
void ra(t_ps *ps, t_bool will_print);
void rb(t_ps *ps, t_bool will_print);
void rr(t_ps *ps, t_bool will_print);
/*
** reverse.c
*/
void rra(t_ps *ps, t_bool will_print);
void rrb(t_ps *ps, t_bool will_print);
void rrr(t_ps *ps, t_bool will_print);
/*
** math.c
*/
int tab_get_min(int *tab);
int tab_get_max(int *tab);
void tab_get_median(int *tab, int size, int *median);
/*
** --- A Pattern Language:
*/
# define PRINT 1
# define NO_PRINT 0
typedef void (*t_pattern)(t_ps *ps, t_bool will_print);
typedef t_bool (*t_checker)(t_ps *ps);
typedef struct s_action
{
char *instruction;
t_pattern operation;
} t_action;
/*
** patterns.c
*/
t_pattern find_pattern(t_ps *ps, t_checker to_pass);
t_bool dry_run(t_ps *ps, int size, t_pattern to_try, t_checker check);
t_pattern deep_run(t_ps *ps, int size, t_pattern to_try, t_checker check);
void print_pattern(t_ps *ps, char *instruction);
void leader(t_ps *ps, t_bool will_print);
/*
** ./patterns/doubles.c
*/
void dra(t_ps *ps, t_bool will_print);
void drra(t_ps *ps, t_bool will_print);
void drb(t_ps *ps, t_bool will_print);
void drrb(t_ps *ps, t_bool will_print);
/*
** ./patterns/swaps.c
*/
void swap_second_third(t_ps *ps, t_bool will_print);
void swap_first_last(t_ps *ps, t_bool will_print);
void b_swap_second_third(t_ps *ps, t_bool will_print);
void b_swap_first_last(t_ps *ps, t_bool will_print);
void swap_and_down(t_ps *ps, t_bool will_print);
void b_swap_and_down(t_ps *ps, t_bool will_print);
/*
** spin.c
*/
int distance(int *tab, int to_find);
t_pattern spin_til(t_ps *ps, int to_find);
t_pattern b_spin_til(t_ps *ps, int to_find);
void b_actually_spin(t_ps *ps, t_bool will_print, int to_find);
/*
** crapsort.c
*/
void place_min_first(t_ps *ps, t_bool will_print);
void crapsort(t_ps *ps, t_bool will_print);
/*
** splitsort.c
*/
void push_all_below_cutoff(t_ps *ps, t_bool will_print, int cutoff);
void splitsort(t_ps *ps, t_bool will_print);
void optisort(t_ps *ps, t_bool will_print);
void push_half(t_ps *ps, t_bool willprint);
void dilettantesort(t_ps *ps, t_bool willprint);
/*
** a few checkers to start with.
** todo: move them to their own files
*/
t_bool is_done(t_ps *ps);
/*
** is_ordered.c
*/
t_bool first_three_ok(t_ps *ps);
t_bool a_is_ordered(t_ps *ps);
t_bool b_is_ordered(t_ps *ps);
t_bool b_is_decreasing(t_ps *ps);
t_bool a_is_increasing(t_ps *ps);
static const t_action g_actions[11] = {
(t_action){"pa", &pa},
(t_action){"pb", &pb},
(t_action){"sa", &sa},
(t_action){"sb", &sb},
(t_action){"ss", &ss},
(t_action){"ra", &ra},
(t_action){"rb", &rb},
(t_action){"rr", &rr},
(t_action){"rra", &rra},
(t_action){"rrb", &rrb},
(t_action){"rrr", &rrr}
};
# define NBR_PATTERNS 18
/*
** this array should be split up.
** all the a patterns, all the cheap a patterns,
** all the b patterns, etc.
** so as to prevent useless checks.
*/
static const t_pattern g_all_patterns[NBR_PATTERNS] = {
&ra,
&rra,
&sa,
&dra,
&drra,
&rb,
&rrb,
&sb,
&drb,
&drrb,
&swap_and_down,
&b_swap_and_down,
&swap_second_third,
&swap_first_last,
&b_swap_and_down,
&b_swap_second_third,
&b_swap_first_last,
&leader
};
/*
** eldritch knowledge
** sorcery.c
*/
t_pattern *composition(t_pattern f, t_pattern g);
void run_sequence(t_ps **ps, t_pattern *composite, t_bool print);
t_bool autosolve(t_ps **ps, t_bool will_print, t_checker check);
/*
** quicksort.c
*/
void two_tapes_quicksort(t_ps *ps, int cutoff, int size, t_byte sort);
#endif