├── README.md ├── clean.sh ├── en.subject.pdf ├── files ├── Makefile ├── algo.cpp ├── gen.cpp ├── includes │ └── utils.h ├── libft │ ├── Makefile │ ├── binary_search.c │ ├── deque.c │ ├── deque_utils.c │ ├── ft_atoi.c │ ├── ft_bzero.c │ ├── ft_calloc.c │ ├── ft_isalnum.c │ ├── ft_isalpha.c │ ├── ft_isascii.c │ ├── ft_isdigit.c │ ├── ft_isprint.c │ ├── ft_isspace.c │ ├── ft_itoa.c │ ├── ft_lstadd_back.c │ ├── ft_lstadd_front.c │ ├── ft_lstclear.c │ ├── ft_lstdelone.c │ ├── ft_lstfind.c │ ├── ft_lstiter.c │ ├── ft_lstlast.c │ ├── ft_lstmap.c │ ├── ft_lstnew.c │ ├── ft_lstsize.c │ ├── ft_memccpy.c │ ├── ft_memchr.c │ ├── ft_memcmp.c │ ├── ft_memcpy.c │ ├── ft_memmove.c │ ├── ft_memory.c │ ├── ft_memory_utils.c │ ├── ft_memset.c │ ├── ft_minmax.c │ ├── ft_putchar_fd.c │ ├── ft_putendl_fd.c │ ├── ft_putnbr_fd.c │ ├── ft_putstr_fd.c │ ├── ft_sort.c │ ├── ft_split.c │ ├── ft_sqrt.c │ ├── ft_strchr.c │ ├── ft_strdup.c │ ├── ft_strjoin.c │ ├── ft_strlcat.c │ ├── ft_strlcpy.c │ ├── ft_strlen.c │ ├── ft_strmapi.c │ ├── ft_strncmp.c │ ├── ft_strnstr.c │ ├── ft_strrchr.c │ ├── ft_strtrim.c │ ├── ft_substr.c │ ├── ft_tolower.c │ ├── ft_toupper.c │ ├── get_next_line.c │ ├── get_next_line.h │ ├── get_next_line_sub.c │ ├── get_next_line_utils.c │ └── libft.h ├── main.cpp └── srcs │ ├── bonus │ ├── bonus.c │ └── main.c │ ├── checker │ ├── check.c │ └── main.c │ ├── radix │ ├── main.c │ └── radix.c │ └── share │ ├── operation1.c │ ├── operation2.c │ ├── operation3.c │ ├── operation4.c │ ├── parse.c │ └── utils.c ├── fr.subject.pdf ├── radix_sort_solution.sh ├── radix_sort_test.sh └── trace └── clean.sh /README.md: -------------------------------------------------------------------------------- 1 | # push_swap_tutorial 2 | 3 | ### Visualize Radix Sort 4 | 5 | First, read the algorithm in the following article : 6 | 7 | Chinese version : 8 | 9 | https://medium.com/台灣人-ecole42/cursus-push-swap-582d5108c537 10 | 11 | English version : 12 | 13 | https://leofu890806.medium.com/push-swap-tutorial-fa746e6aba1e 14 | 15 | Then you can run the following command to see how this algorithm works 16 | 17 | ```bash radix_sort_solution.sh ``` 18 | 19 | (Reminder : duplicate numbers will cause an error.) 20 | 21 | For example 22 | 23 | ```bash radix_sort_solution.sh 9 4 8 7 ``` 24 | 25 | will show you how we sort 9 4 8 7 with radix sort 26 | 27 | ![step1](https://user-images.githubusercontent.com/70040774/119062284-aa864c00-b9d6-11eb-9ed8-ba24efeb457e.png) 28 | 29 | ![step2](https://user-images.githubusercontent.com/70040774/119062295-aeb26980-b9d6-11eb-8fd0-5aa7567f20d7.png) 30 | 31 | ![step3](https://user-images.githubusercontent.com/70040774/119062298-b114c380-b9d6-11eb-87fa-9b7d08c5dcfd.png) 32 | 33 | 34 | ### Test Radix Sort 35 | 36 | Now you finish your radix sort algorithm and you want to test it ? 37 | 38 | First, ```vim radix_sort_test.sh``` and set the variable ```ROOT``` as the path of the root of your repository 39 | 40 | Then 41 | 42 | ```bash radix_sort_test.sh ``` 43 | 44 | (Reminder : duplicate numbers will cause an error.) 45 | 46 | For example 47 | 48 | ```bash radix_sort_test.sh 9 4 8 7 1 2 3``` 49 | 50 | will show you how your push_swap sort these numbers (those numbers are simplified and in base 2) 51 | 52 | ![base2](https://user-images.githubusercontent.com/70040774/119062455-2a141b00-b9d7-11eb-9c0e-778e50afba2c.png) 53 | 54 | ### Clean 55 | 56 | ```bash clean.sh``` 57 | 58 | will clean everything 59 | 60 | ### Contact : 61 | 62 | yfu@student.42lyon.fr 63 | 64 | Or you can also DM me on the slack 65 | -------------------------------------------------------------------------------- /clean.sh: -------------------------------------------------------------------------------- 1 | make fclean -C ./files/ 2 | cd trace/ 3 | bash clean.sh 4 | cd .. 5 | 6 | -------------------------------------------------------------------------------- /en.subject.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeoFu9487/push_swap_tutorial/379ed3edefaf0e778252de9309d75112df94c7f1/en.subject.pdf -------------------------------------------------------------------------------- /files/Makefile: -------------------------------------------------------------------------------- 1 | CHECKER = checker 2 | 3 | CHECKER_BONUS = checker_bonus 4 | 5 | CHECKER_RADIX = checker_radix 6 | 7 | CC = cc 8 | 9 | CFLAGS = -Wall -Werror -Wextra 10 | 11 | INCLUDES_PATH = includes/ 12 | 13 | INCLUDES = $(INCLUDES_PATH)utils.h 14 | 15 | CHECKER_PATH = srcs/checker/ 16 | 17 | CHECKER_FILES = check.c 18 | 19 | CHECKER_MAIN = srcs/checker/main.c 20 | 21 | CHECKER_SRCS = $(addprefix $(CHECKER_PATH), $(CHECKER_FILES)) 22 | 23 | SHARE_PATH = srcs/share/ 24 | 25 | SHARE_FILES = parse.c utils.c operation1.c operation2.c operation3.c\ 26 | operation4.c 27 | 28 | SHARE_SRCS = $(addprefix $(SHARE_PATH), $(SHARE_FILES)) 29 | 30 | BONUS_PATH = srcs/bonus/ 31 | 32 | BONUS_FILES = bonus.c 33 | 34 | BONUS_CHECKER_MAIN = srcs/bonus/main.c 35 | 36 | BONUS_SRCS = $(addprefix $(BONUS_PATH), $(BONUS_FILES)) 37 | 38 | RADIX_PATH = srcs/radix/ 39 | 40 | RADIX_FILES = radix.c 41 | 42 | RADIX_CHECKER_MAIN = $(RADIX_PATH)main.c 43 | 44 | RADIX_SRCS = $(addprefix $(RADIX_PATH), $(RADIX_FILES)) 45 | 46 | LIB_PATH = libft/ 47 | 48 | LIB = $(LIB_PATH)libft.a 49 | 50 | CHECKER_OBJS = $(CHECKER_SRCS:.c=.o) 51 | 52 | CHECKER_MAIN_OBJS = $(CHECKER_MAIN:.c=.o) 53 | 54 | BONUS_CHECKER_MAIN_OBJS = $(BONUS_CHECKER_MAIN:.c=.o) 55 | 56 | SHARE_OBJS = $(SHARE_SRCS:.c=.o) 57 | 58 | BONUS_OBJS = $(BONUS_SRCS:.c=.o) 59 | 60 | RADIX_CHECKER_MAIN_OBJS = $(RADIX_CHECKER_MAIN:.c=.o) 61 | 62 | RADIX_OBJS = $(RADIX_SRCS:.c=.o) 63 | 64 | all : $(CHECKER) 65 | @echo "compile OK" 66 | 67 | bonus : CFLAGS+=-D\ BONUS=1 68 | bonus : $(CHECKER_BONUS) 69 | @echo "compile bonus OK" 70 | 71 | radix : $(CHECKER_RADIX) 72 | @echo "compile radix OK" 73 | 74 | %.o : %.c $(INCLUDES) $(LIB) 75 | @$(CC) $(CFLAGS) -I $(INCLUDES_PATH) -c $< -o $@ 76 | 77 | $(LIB) : 78 | @$(MAKE) -C $(LIB_PATH) 79 | 80 | $(CHECKER) : $(INCLUDES) $(CHECKER_OBJS) $(SHARE_OBJS) $(CHECKER_MAIN_OBJS) 81 | @$(CC) $(CFLAGS) $(CHECKER_OBJS) $(SHARE_OBJS) $(CHECKER_MAIN_OBJS) $(LIB) -o $(CHECKER) 82 | 83 | $(CHECKER_BONUS) : $(INCLUDES) $(CHECKER_OBJS) $(SHARE_OBJS) $(BONUS_OBJS) $(BONUS_CHECKER_MAIN_OBJS) 84 | @$(CC) $(CFLAGS) $(CHECKER_OBJS) $(SHARE_OBJS) $(BONUS_OBJS) $(BONUS_CHECKER_MAIN_OBJS) $(LIB) -o $(CHECKER_BONUS) 85 | 86 | $(CHECKER_RADIX) : $(INCLUDES) $(CHECKER_OBJS) $(SHARE_OBJS) $(RADIX_OBJS) $(RADIX_CHECKER_MAIN_OBJS) 87 | @$(CC) $(CFLAGS) $(CHECKER_OBJS) $(SHARE_OBJS) $(RADIX_OBJS) $(RADIX_CHECKER_MAIN_OBJS) $(LIB) -o $(CHECKER_RADIX) 88 | 89 | 90 | clean : 91 | @rm -rf $(CHECKER_OBJS) $(SHARE_OBJS) $(CHECKER_MAIN_OBJS) $(BONUS_CHECKER_MAIN_OBJS) $(BONUS_OBJS) $(RADIX_CHECKER_MAIN_OBJS) $(RADIX_OBJS) 92 | @$(MAKE) clean -C $(LIB_PATH) 93 | @echo "clean done" 94 | 95 | fclean : clean 96 | @rm -rf $(CHECKER) $(CHECKER_BONUS) $(CHECKER_RADIX) 97 | @$(MAKE) fclean -C $(LIB_PATH) 98 | @echo "fclean done" 99 | 100 | re : fclean all 101 | 102 | norm : 103 | @norminette-old $(SRCS) $(INCLUDES) libft/*.c libft/*.h 104 | 105 | .PHONY : all clean fclean re norm debug bonus radix 106 | -------------------------------------------------------------------------------- /files/algo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define top back 10 | #define push_top push_back 11 | #define pop_top pop_back 12 | #define bottom front 13 | #define push_bottom push_front 14 | #define pop_bottom pop_front 15 | 16 | using namespace std; 17 | 18 | deque a,b; 19 | int max_bits = 4; 20 | 21 | string convert_base_2(int n) 22 | { 23 | string ans; 24 | for(int i = max_bits - 1 ; i >= 0 ; --i){ 25 | if ((n>>i)&1) ans += "1"; 26 | else ans += "0"; 27 | } 28 | return ans; 29 | } 30 | 31 | void print_stacks_base2() 32 | { 33 | int x = a.size(), y = b.size(); 34 | deque::reverse_iterator i = a.rbegin(), j = b.rbegin(); 35 | while (x > y) 36 | { 37 | printf("%10s%15s\n", convert_base_2(*i).c_str(), ""); 38 | --x; 39 | ++i; 40 | } 41 | while (y > x) 42 | { 43 | printf("%15s%10s\n", "", convert_base_2(*j).c_str()); 44 | --y; 45 | ++j; 46 | } 47 | while (x--) 48 | { 49 | printf("%10s%5s%10s\n", convert_base_2(*i).c_str(), "", convert_base_2(*j).c_str()); 50 | ++i; 51 | ++j; 52 | } 53 | for(int i = 0 ; i < 10 ; ++i) printf("-"); 54 | printf("%5s",""); 55 | for(int i = 0 ; i < 10 ; ++i) printf("-"); 56 | printf("\n"); 57 | printf("%10s%5s%10s\n\n", "a", "", "b"); 58 | } 59 | 60 | void print_stacks() 61 | { 62 | int x = a.size(), y = b.size(); 63 | deque::reverse_iterator i = a.rbegin(), j = b.rbegin(); 64 | while (x > y) 65 | { 66 | printf("%10d%15s\n", *i, ""); 67 | --x; 68 | ++i; 69 | } 70 | while (y > x) 71 | { 72 | printf("%15s%10d\n", "", *j); 73 | --y; 74 | ++j; 75 | } 76 | while (x--) 77 | { 78 | printf("%10d%5s%10d", *i, "", *j); 79 | ++i; 80 | ++j; 81 | } 82 | for(int i = 0 ; i < 10 ; ++i) printf("-"); 83 | printf("%5s",""); 84 | for(int i = 0 ; i < 10 ; ++i) printf("-"); 85 | printf("\n"); 86 | printf("%10s%5s%10s\n\n", "a", "", "b"); 87 | } 88 | 89 | void pa() 90 | { 91 | #ifdef REAL 92 | printf("pa\n"); 93 | if (b.empty()) return ; 94 | a.push_top(b.top()); 95 | b.pop_top(); 96 | return ; 97 | #endif 98 | //printf("\npa\n"); 99 | if (b.empty()) return ; 100 | a.push_top(b.top()); 101 | b.pop_top(); 102 | //print_stacks_base2(); 103 | } 104 | 105 | void pb() 106 | { 107 | #ifdef REAL 108 | printf("pb\n"); 109 | if (a.empty()) return ; 110 | b.push_top(a.top()); 111 | a.pop_top(); 112 | return ; 113 | #endif 114 | //printf("\npb\n"); 115 | if (a.empty()) return ; 116 | b.push_top(a.top()); 117 | a.pop_top(); 118 | //print_stacks_base2(); 119 | } 120 | 121 | void ra() 122 | { 123 | #ifdef REAL 124 | printf("ra\n"); 125 | if (a.empty()) return ; 126 | a.push_bottom(a.top()); 127 | a.pop_top(); 128 | return ; 129 | #endif 130 | //printf("\nra\n"); 131 | if (a.empty()) return ; 132 | a.push_bottom(a.top()); 133 | a.pop_top(); 134 | //print_stacks_base2(); 135 | } 136 | 137 | void clearscreen() 138 | { 139 | printf("\e[1;1H\e[2J"); 140 | } 141 | 142 | 143 | int main(int ac, char **av) 144 | { 145 | vector array; 146 | for (int i = 1 ; i < ac ; ++i) 147 | { 148 | array.push_back(atoi(av[i])); 149 | } 150 | if (ac < 2) return 0; 151 | 152 | //step 1 : simplify numbers 153 | 154 | 155 | vector copy_array(array), copy(array); 156 | sort(copy_array.begin(), copy_array.end()); 157 | 158 | { 159 | unordered_map hash; 160 | for(int i = 0 ; i < copy_array.size() ; ++i) 161 | { 162 | hash[copy_array[i]] = i; 163 | } 164 | for(int i = 0 ; i < array.size() ; ++i) 165 | { 166 | array[i] = hash[array[i]]; 167 | } 168 | } 169 | //put the simplified numbers into a 170 | for(int i = 0 ; i < array.size() ; ++i) 171 | { 172 | a.push_bottom(array[i]); 173 | } 174 | //step 1 done 175 | 176 | clearscreen(); 177 | printf("\nstep 1 : simplify numbers\n\n"); 178 | 179 | for(int i = 0 ; i < array.size() ; ++i) 180 | { 181 | printf("%d\t->\t%d\n", copy[i], array[i]); 182 | } 183 | 184 | printf("\n\nOriginal input : "); 185 | for(int i = 0 ; i < array.size() ; ++i) printf("%d ", copy[i]); 186 | printf("\n\nSimplified arr : "); 187 | for(int i = 0 ; i < array.size() ; ++i) printf("%d ", array[i]); 188 | printf("\n\npress enter to continue ..."); 189 | while (getchar() != '\n') 190 | { 191 | 192 | } 193 | 194 | //step 2 : algorithms 195 | 196 | max_bits = 0; 197 | while ((array.size()-1)>>max_bits) ++max_bits; 198 | clearscreen(); 199 | printf("\nstep 2 : put numbers in stack and visualize it in base 2\n\n"); 200 | printf("arr : "); 201 | for(int i = 0 ; i < array.size() ; ++i) printf("%d ", array[i]); 202 | printf("\n\n"); 203 | for(int i = 0; i < array.size() ; ++i) 204 | { 205 | printf("%d\t->\t%s\n", array[i], convert_base_2(array[i]).c_str()); 206 | } 207 | printf("\n"); 208 | print_stacks_base2(); 209 | printf("\npress enter to continue ..."); 210 | while (getchar() != '\n') 211 | { 212 | 213 | } 214 | clearscreen(); 215 | printf("\nALGO TIME !\n\n1) normal mode\n2) step by step mode\n\nPlease enter 1 or 2\n"); 216 | char n; 217 | while ( cin >> n ) 218 | { 219 | if (n == '1') 220 | { 221 | printf("\nnormal mode !\n"); 222 | break ; 223 | } 224 | if (n == '2') 225 | { 226 | printf("\nstep by step mode !\n"); 227 | break ; 228 | } 229 | printf("invalid input ! please type 1 or 2\n"); 230 | } 231 | printf("\npress enter to continue ..."); 232 | while (getchar() != '\n') 233 | { 234 | 235 | } 236 | 237 | if (n == '1') 238 | { 239 | 240 | for (int i = 0 ; i < max_bits ; ++i) 241 | { 242 | clearscreen(); 243 | printf("original status :\n\n"); 244 | print_stacks_base2(); 245 | for(int j = 0 ; j < array.size() ; ++j) 246 | { 247 | if ((a.top()>>i)&1) ra(); 248 | else pb(); 249 | } 250 | printf("seperate into two boxes for the %d-th time ( check the %d-th digit from the right )\n\n", i + 1, i + 1); 251 | print_stacks_base2(); 252 | printf("\npress enter to continue ..."); 253 | while (getchar() != '\n'){} 254 | clearscreen(); 255 | printf("original status :\n\n"); 256 | print_stacks_base2(); 257 | while (b.size()) pa(); 258 | printf("put all back in stack a\n\n"); 259 | print_stacks_base2(); 260 | if (i == max_bits - 1) printf("sorted !\n"); 261 | printf("\npress enter to continue ..."); 262 | while (getchar() != '\n'){} 263 | } 264 | } 265 | 266 | else 267 | { 268 | for(int i = 0 ; i < max_bits ; ++i) 269 | { 270 | for(int j = 0 ; j < array.size() ; ++j) 271 | { 272 | clearscreen(); 273 | printf("original status :\n\n"); 274 | print_stacks_base2(); 275 | if ((a.top()>>i)&1) 276 | { 277 | ra(); 278 | printf("ra\n\n"); 279 | print_stacks_base2(); 280 | } 281 | else 282 | { 283 | pb(); 284 | printf("pb\n\n"); 285 | print_stacks_base2(); 286 | } 287 | printf("\npress enter to continue ..."); 288 | while (getchar() != '\n'){} 289 | } 290 | while (b.size()) 291 | { 292 | clearscreen(); 293 | printf("original status :\n\n"); 294 | print_stacks_base2(); 295 | pa(); 296 | printf("pa\n\n"); 297 | print_stacks_base2(); 298 | printf("\npress enter to continue ..."); 299 | while (getchar() != '\n'){} 300 | } 301 | } 302 | } 303 | //step 2 done 304 | } -------------------------------------------------------------------------------- /files/gen.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define FAST ios_base::sync_with_stdio(false);cin.tie(NULL); 10 | 11 | using namespace std; 12 | 13 | int main(int ac, char **av) 14 | { 15 | FAST 16 | std::random_device rd; 17 | std::mt19937::result_type seed = rd() ^ ( 18 | (std::mt19937::result_type) 19 | std::chrono::duration_cast( 20 | std::chrono::system_clock::now().time_since_epoch() 21 | ).count() + 22 | (std::mt19937::result_type) 23 | std::chrono::duration_cast( 24 | std::chrono::high_resolution_clock::now().time_since_epoch() 25 | ).count() ); 26 | 27 | vector arr; 28 | std::mt19937 gen(seed); 29 | int stack_size = 100; 30 | if (ac > 1) stack_size = atoi(av[1]); 31 | unordered_set used; 32 | for( int j = 0; j < stack_size; ++j ) 33 | { 34 | int n = static_cast(gen()); 35 | while (used.find(n) != used.end()) n = static_cast(gen()); 36 | used.insert(n); 37 | arr.push_back(n); 38 | } 39 | 40 | vector copy = arr; 41 | sort(copy.begin(),copy.end()); 42 | unordered_map mp; 43 | for(int i = 0 ; i < copy.size() ; ++i) mp[copy[i]] = i; 44 | for(int i = 0 ; i < arr.size() ; ++i) cout << mp[arr[i]] <<"\n"; 45 | return 0; 46 | } -------------------------------------------------------------------------------- /files/includes/utils.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* utils.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/03/12 14:23:05 by yfu #+# #+# */ 9 | /* Updated: 2021/05/21 00:26:39 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef UTILS_H 14 | # define UTILS_H 15 | # ifndef DEBUG 16 | # define DEBUG 0 17 | # endif 18 | # ifndef BONUS 19 | # define BONUS 0 20 | # endif 21 | # ifndef RADIX 22 | # define RADIX 0 23 | # endif 24 | # include 25 | # include 26 | # include "../libft/libft.h" 27 | 28 | int g_size; 29 | void normal_exit(void); 30 | void error_exit(void); 31 | int get_nbr_in_line(int **arr, int argc, char **argv); 32 | int ft_atoi_with_range(char **str); 33 | int has_duplicate(int *arr, int arrsize); 34 | void check_instructions(char *str); 35 | int check(int *arr, int arr_size, t_list *instructions); 36 | void check_bonus(int *arr, int arr_size, t_list *instructions); 37 | void sa(t_deque *stack[2]); 38 | void sb(t_deque *stack[2]); 39 | void ss(t_deque *stack[2]); 40 | void pa(t_deque *stack[2]); 41 | void pb(t_deque *stack[2]); 42 | void ra(t_deque *stack[2]); 43 | void rb(t_deque *stack[2]); 44 | void rr(t_deque *stack[2]); 45 | void rra(t_deque *stack[2]); 46 | void rrb(t_deque *stack[2]); 47 | void rrr(t_deque *stack[2]); 48 | void display_stacks(t_deque *stack[2], char *message); 49 | void init_stack(t_deque *stack, int *arr, int arr_size); 50 | void assigned_operation(void *ptr, t_deque *stack[2]); 51 | t_deque *push_swap(t_deque *stack[2]); 52 | void display_instructions(t_deque *ins, int *arr, int size, int fd); 53 | void descretize_stack(t_deque *stack, int *arr, int arr_size); 54 | int check_order(t_deque *stack[2]); 55 | void generate_instructions(t_deque *st[2], char *str, t_deque *ins); 56 | void easy_solve(t_deque *stack[2], t_deque *instructions); 57 | void medium_solve(t_deque *stack[2], t_deque *instructions); 58 | void hard_solve(t_deque *stack[2], t_deque *instructions); 59 | void check_radix(int *arr, int arr_size, t_list *instructions); 60 | void display_stack_radix(t_deque *stack[2], char *message); 61 | 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /files/libft/Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: yfu +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2020/12/12 12:17:32 by yfu #+# #+# # 9 | # Updated: 2021/03/12 15:58:31 by yfu ### ########lyon.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | NAME = libft.a 14 | 15 | CC = gcc 16 | 17 | FLAGS = -Wall -Wextra -Werror 18 | 19 | SRCS = ft_split.c ft_strchr.c \ 20 | ft_atoi.c ft_strdup.c \ 21 | ft_bzero.c ft_strjoin.c \ 22 | ft_calloc.c ft_memccpy.c ft_strlcat.c \ 23 | ft_isalnum.c ft_memchr.c ft_strlcpy.c \ 24 | ft_isalpha.c ft_memcmp.c ft_strlen.c \ 25 | ft_isascii.c ft_memcpy.c ft_strmapi.c \ 26 | ft_isdigit.c ft_memmove.c ft_strncmp.c \ 27 | ft_isprint.c ft_memset.c ft_strnstr.c \ 28 | ft_itoa.c ft_strrchr.c \ 29 | ft_putchar_fd.c ft_strtrim.c \ 30 | ft_putendl_fd.c ft_substr.c \ 31 | ft_putnbr_fd.c ft_tolower.c \ 32 | ft_putstr_fd.c ft_toupper.c 33 | 34 | BONUS_SRCS = ft_lstadd_back.c ft_lstadd_front.c\ 35 | ft_lstclear.c ft_lstdelone.c ft_lstiter.c \ 36 | ft_lstlast.c ft_lstmap.c ft_lstnew.c\ 37 | ft_lstsize.c ft_memory.c ft_memory_utils.c ft_lstfind.c\ 38 | get_next_line.c get_next_line_utils.c get_next_line_sub.c ft_minmax.c ft_isspace.c\ 39 | deque.c deque_utils.c ft_sort.c binary_search.c ft_sqrt.c 40 | 41 | HEADER = libft.h get_next_line.h 42 | 43 | OBJECTS = $(SRCS:.c=.o) 44 | 45 | BONUS_OBJECTS = $(BONUS_SRCS:.c=.o) 46 | 47 | all : $(NAME) 48 | 49 | bonus : $(OBJECTS) $(BONUS_OBJECTS) 50 | @ar rc $(NAME) $(OBJECTS) $(BONUS_OBJECTS) 51 | @ranlib $(NAME) 52 | @echo "libft.a created / updated" 53 | 54 | $(NAME): $(HEADER) $(OBJECTS) $(BONUS_OBJECTS) 55 | @ar rc $(NAME) $(OBJECTS) $(BONUS_OBJECTS) 56 | @ranlib $(NAME) 57 | @echo "libft.a created / updated" 58 | 59 | %.o : %.c $(HEADER) 60 | @$(CC) $(FLAGS) -c $< -o $@ 61 | 62 | clean: 63 | @rm -rf $(OBJECTS) $(BONUS_OBJECTS) 64 | @echo "libft.a clean done" 65 | 66 | fclean: 67 | @rm -rf $(OBJECTS) $(BONUS_OBJECTS) 68 | @rm -rf $(NAME) 69 | @echo "libft.a fclean done" 70 | 71 | re: fclean all 72 | 73 | norm : 74 | norminette $(shell find *.c *.h) 75 | 76 | .PHONY : all bonus clean fclean re 77 | -------------------------------------------------------------------------------- /files/libft/binary_search.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* binary_search.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/03 21:01:53 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 19:46:41 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int binary_search(int *arr, int arr_size, int target) 16 | { 17 | int lb; 18 | int ub; 19 | int mid; 20 | 21 | lb = -1; 22 | ub = arr_size; 23 | while (ub - lb > 1) 24 | { 25 | mid = (ub + lb) / 2; 26 | if (arr[mid] < target) 27 | lb = mid; 28 | else if (arr[mid] > target) 29 | ub = mid; 30 | else 31 | return (mid); 32 | } 33 | return (-1); 34 | } 35 | -------------------------------------------------------------------------------- /files/libft/deque.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* deque.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/03/13 21:46:06 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 23:08:13 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_deque *deque_init(void) 16 | { 17 | t_deque *deque; 18 | 19 | deque = ft_malloc(1, sizeof(t_deque)); 20 | if (!deque) 21 | return (deque); 22 | deque->head = NULL; 23 | deque->tail = NULL; 24 | deque->size = 0; 25 | return (deque); 26 | } 27 | 28 | void deque_push_back(t_deque *deque, void *ptr) 29 | { 30 | t_double_list *temp; 31 | 32 | (deque->size)++; 33 | temp = double_list_init(ptr); 34 | if (!temp) 35 | return ; 36 | if (deque->size == 1) 37 | { 38 | deque->head = temp; 39 | deque->tail = temp; 40 | return ; 41 | } 42 | temp->last = deque->tail; 43 | deque->tail->next = temp; 44 | deque->tail = deque->tail->next; 45 | } 46 | 47 | void deque_push_front(t_deque *deque, void *ptr) 48 | { 49 | t_double_list *temp; 50 | 51 | (deque->size)++; 52 | temp = double_list_init(ptr); 53 | if (!temp) 54 | return ; 55 | if (deque->size == 1) 56 | { 57 | deque->head = temp; 58 | deque->tail = temp; 59 | return ; 60 | } 61 | temp->next = deque->head; 62 | deque->head->last = temp; 63 | deque->head = deque->head->last; 64 | } 65 | 66 | void deque_pop_back(t_deque *deque, void (*f)(void *)) 67 | { 68 | t_double_list *temp; 69 | 70 | if (deque->size < 1 || !deque->tail) 71 | return ; 72 | (deque->size)--; 73 | temp = deque->tail; 74 | deque->tail = deque->tail->last; 75 | if (deque->tail) 76 | deque->tail->next = NULL; 77 | if (f) 78 | f(temp->content); 79 | ft_free(temp); 80 | } 81 | 82 | void deque_pop_front(t_deque *deque, void (*f)(void*)) 83 | { 84 | t_double_list *temp; 85 | 86 | if (deque->size < 1 || !deque->head) 87 | return ; 88 | (deque->size)--; 89 | temp = deque->head; 90 | deque->head = deque->head->next; 91 | if (deque->head) 92 | deque->head->last = NULL; 93 | if (f) 94 | f(temp->content); 95 | ft_free(temp); 96 | } 97 | -------------------------------------------------------------------------------- /files/libft/deque_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* deque_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/03/13 21:58:29 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 19:49:04 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_double_list *double_list_init(void *ptr) 16 | { 17 | t_double_list *double_list; 18 | 19 | double_list = ft_malloc(1, sizeof(t_double_list)); 20 | if (!double_list) 21 | return (NULL); 22 | double_list->content = ptr; 23 | double_list->next = NULL; 24 | double_list->last = NULL; 25 | return (double_list); 26 | } 27 | 28 | void deque_clear(t_deque *deque, void (*f)(void*)) 29 | { 30 | while (deque->size) 31 | deque_pop_back(deque, f); 32 | } 33 | -------------------------------------------------------------------------------- /files/libft/ft_atoi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_atoi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/11/23 13:24:57 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 19:50:52 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_atoi(char *str) 16 | { 17 | long long int ct[3]; 18 | 19 | if (!str || str[0] == 0) 20 | return (0); 21 | if (ft_isspace(*str)) 22 | return (ft_atoi(str + 1)); 23 | if (str[0] == '+' || str[0] == '-') 24 | ct[0] = 0; 25 | else 26 | ct[0] = -1; 27 | if (str[0] == '-') 28 | ct[2] = -1; 29 | else 30 | ct[2] = 1; 31 | ct[1] = 0; 32 | while (str[++ct[0]]) 33 | { 34 | if (str[ct[0]] < '0' || str[ct[0]] > '9') 35 | return (ct[1] * ct[2]); 36 | ct[1] = 10 * ct[1] + str[ct[0]] - '0'; 37 | } 38 | return (ct[1] * ct[2]); 39 | } 40 | -------------------------------------------------------------------------------- /files/libft/ft_bzero.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_bzero.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/11/23 15:02:39 by yfu #+# #+# */ 9 | /* Updated: 2020/12/12 13:21:27 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_bzero(void *s, size_t n) 16 | { 17 | ft_memset(s, 0, n); 18 | } 19 | -------------------------------------------------------------------------------- /files/libft/ft_calloc.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_calloc.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/22 11:42:36 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 23:06:05 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_calloc(size_t elementcount, size_t elementsize) 16 | { 17 | void *ans; 18 | 19 | ans = ft_malloc(elementcount, elementsize); 20 | if (!ans) 21 | { 22 | ft_putstr_fd("ERROR_IN_FT_CALLOC\n", 1); 23 | return (NULL); 24 | } 25 | ft_memset(ans, 0, elementcount * elementsize); 26 | return (ans); 27 | } 28 | -------------------------------------------------------------------------------- /files/libft/ft_isalnum.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalnum.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/11/23 12:57:10 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 23:36:55 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isalnum(int c) 16 | { 17 | if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') 18 | || (c >= 'A' && c <= 'Z')) 19 | return (1); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /files/libft/ft_isalpha.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalpha.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/11/23 12:40:24 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 22:01:28 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isalpha(int c) 16 | { 17 | if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) 18 | return (1); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /files/libft/ft_isascii.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isascii.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/11/23 12:34:51 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 19:44:50 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isascii(int c) 16 | { 17 | if (c >= 0 && c <= 127) 18 | return (1); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /files/libft/ft_isdigit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isdigit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/11/23 12:52:43 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 21:57:21 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isdigit(int c) 16 | { 17 | if (c >= '0' && c <= '9') 18 | return (1); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /files/libft/ft_isprint.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isprint.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/11/23 13:02:54 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 19:42:46 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isprint(int c) 16 | { 17 | if (c >= ' ' && c <= '~') 18 | return (1); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /files/libft/ft_isspace.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isspace.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/03/12 15:59:59 by yfu #+# #+# */ 9 | /* Updated: 2021/03/12 16:00:18 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isspace(char c) 16 | { 17 | if (c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r' 18 | || c == ' ') 19 | return (1); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /files/libft/ft_itoa.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_itoa.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/22 11:42:56 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 23:03:29 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static int ft_cnt(int n) 16 | { 17 | int ans; 18 | 19 | ans = 0; 20 | if (n < 0) 21 | ans = 1; 22 | while (n != 0) 23 | { 24 | ans++; 25 | n /= 10; 26 | } 27 | return (ans); 28 | } 29 | 30 | char *ft_itoa(int n) 31 | { 32 | char *ans; 33 | int ct; 34 | 35 | ans = NULL; 36 | if (n == -2147483648) 37 | return (ft_strdup("-2147483648")); 38 | if (n == 0) 39 | return (ft_strdup("0")); 40 | ct = ft_cnt(n); 41 | ans = ft_malloc(ct + 1, sizeof(char)); 42 | if (!ans) 43 | return (ans); 44 | if (n < 0) 45 | ans[0] = '-'; 46 | ans[ct] = '\0'; 47 | if (n < 0) 48 | n *= -1; 49 | while (n > 0) 50 | { 51 | ans[--ct] = n % 10 + '0'; 52 | n /= 10; 53 | } 54 | return (ans); 55 | } 56 | -------------------------------------------------------------------------------- /files/libft/ft_lstadd_back.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstadd_back.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/22 11:43:07 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 23:00:17 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_lstadd_back(t_list **lst, t_list *new) 16 | { 17 | t_list *temp; 18 | 19 | if (!new || !lst) 20 | { 21 | ft_putstr_fd("ERROR_IN_FT_LSTADD_BACK\n", 1); 22 | return (0); 23 | } 24 | temp = ft_lstlast(*lst); 25 | if (!temp) 26 | { 27 | *lst = new; 28 | return (1); 29 | } 30 | temp->next = new; 31 | return (1); 32 | } 33 | -------------------------------------------------------------------------------- /files/libft/ft_lstadd_front.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstadd_front.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/12 11:34:30 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 23:00:21 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_lstadd_front(t_list **lst, t_list *new) 16 | { 17 | if (!new || !lst) 18 | { 19 | ft_putstr_fd("ERROR_IN_FT_LSTADD_FRONT\n", 1); 20 | return (0); 21 | } 22 | if (!*lst) 23 | { 24 | *lst = new; 25 | return (1); 26 | } 27 | new->next = *lst; 28 | *lst = new; 29 | return (1); 30 | } 31 | -------------------------------------------------------------------------------- /files/libft/ft_lstclear.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstclear.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/22 11:43:16 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 23:00:31 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstclear(t_list **lst, void (*del)(void *)) 16 | { 17 | t_list *temp; 18 | t_list *dd; 19 | 20 | if (!(lst)) 21 | { 22 | ft_putstr_fd("ERROR_IN_FT_LSTCLEAR\n", 1); 23 | return ; 24 | } 25 | temp = *lst; 26 | *lst = NULL; 27 | while (temp) 28 | { 29 | if (del) 30 | del(temp->content); 31 | dd = temp; 32 | temp = temp->next; 33 | ft_free(dd); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /files/libft/ft_lstdelone.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstdelone.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/22 11:43:22 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 23:00:06 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstdelone(t_list **head, t_list *lst, void (*del)(void *)) 16 | { 17 | t_list *temp; 18 | 19 | if (!lst || !head) 20 | return ; 21 | temp = *head; 22 | if (temp == lst) 23 | *head = temp->next; 24 | else 25 | { 26 | while (temp && temp->next != lst) 27 | temp = temp->next; 28 | if (!temp) 29 | { 30 | ft_putstr_fd("ERROR_IN_FT_LSTDELONE\n", 1); 31 | return ; 32 | } 33 | temp->next = lst->next; 34 | } 35 | if (del) 36 | del(lst->content); 37 | ft_free(lst); 38 | } 39 | -------------------------------------------------------------------------------- /files/libft/ft_lstfind.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstfind.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/01/29 08:19:59 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 22:00:43 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lstfind(t_list *head, void *target) 16 | { 17 | while (head) 18 | { 19 | if (head->content == target) 20 | return (head); 21 | head = head->next; 22 | } 23 | return (NULL); 24 | } 25 | -------------------------------------------------------------------------------- /files/libft/ft_lstiter.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstiter.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/22 11:43:27 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 19:45:14 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstiter(t_list *lst, void (*f)(void *)) 16 | { 17 | if (!f) 18 | return ; 19 | while (lst) 20 | { 21 | f(lst->content); 22 | lst = lst->next; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /files/libft/ft_lstlast.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstlast.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/22 11:43:33 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 19:42:02 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lstlast(t_list *lst) 16 | { 17 | t_list *temp; 18 | 19 | temp = lst; 20 | if (!temp) 21 | return (NULL); 22 | while (temp->next) 23 | temp = temp->next; 24 | return (temp); 25 | } 26 | -------------------------------------------------------------------------------- /files/libft/ft_lstmap.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstmap.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/22 11:43:38 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 23:36:00 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) 16 | { 17 | t_list *new; 18 | t_list *ans; 19 | 20 | if (!f || !del) 21 | return (NULL); 22 | ans = NULL; 23 | while (lst) 24 | { 25 | new = ft_lstnew(f(lst->content)); 26 | if (!new) 27 | { 28 | ft_lstclear(&ans, del); 29 | return (NULL); 30 | } 31 | ft_lstadd_back(&ans, new); 32 | lst = lst->next; 33 | } 34 | return (ans); 35 | } 36 | -------------------------------------------------------------------------------- /files/libft/ft_lstnew.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstnew.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/22 11:43:47 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 19:46:15 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lstnew(void *content) 16 | { 17 | t_list *nw; 18 | 19 | nw = (t_list *)ft_malloc(1, sizeof(t_list)); 20 | if (!nw) 21 | { 22 | ft_putstr_fd("ERROR_IN_FT_LSTNEW\n", 1); 23 | return (NULL); 24 | } 25 | nw->content = content; 26 | nw->next = NULL; 27 | return (nw); 28 | } 29 | -------------------------------------------------------------------------------- /files/libft/ft_lstsize.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstsize.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/22 11:43:52 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 19:46:53 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_lstsize(t_list *lst) 16 | { 17 | int ans; 18 | t_list *temp; 19 | 20 | ans = 0; 21 | temp = lst; 22 | while (temp) 23 | { 24 | ans++; 25 | temp = temp->next; 26 | } 27 | return (ans); 28 | } 29 | -------------------------------------------------------------------------------- /files/libft/ft_memccpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memccpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/22 11:43:58 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 22:03:42 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memccpy(void *dest, const void *src, int c, size_t n) 16 | { 17 | size_t i; 18 | 19 | i = -1; 20 | while (++i < n) 21 | { 22 | *(unsigned char *)(dest + i) = *(unsigned char *)(src + i); 23 | if (*(unsigned char *)(src + i) == (unsigned char)c) 24 | return (dest + i + 1); 25 | } 26 | return (NULL); 27 | } 28 | -------------------------------------------------------------------------------- /files/libft/ft_memchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/22 11:44:03 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 23:37:24 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memchr(const void *s, int c, size_t n) 16 | { 17 | unsigned char *str; 18 | 19 | str = (unsigned char *)s; 20 | while (n--) 21 | { 22 | if (*str == (unsigned char)c) 23 | return (str); 24 | str++; 25 | } 26 | return (NULL); 27 | } 28 | -------------------------------------------------------------------------------- /files/libft/ft_memcmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/22 11:44:09 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 23:08:34 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_memcmp(const void *s1, const void *s2, size_t n) 16 | { 17 | unsigned char *str1; 18 | unsigned char *str2; 19 | 20 | str1 = (unsigned char *)s1; 21 | str2 = (unsigned char *)s2; 22 | while (n--) 23 | { 24 | if (*str1 != *str2) 25 | return (*str1 - *str2); 26 | str1++; 27 | str2++; 28 | } 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /files/libft/ft_memcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/22 11:44:15 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 22:02:47 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memcpy(void *destination, const void *source, size_t size) 16 | { 17 | size_t i; 18 | 19 | if (!destination && !source) 20 | return (destination); 21 | i = -1; 22 | while (++i < size) 23 | *(char *)(destination + i) = *(char *)(source + i); 24 | return (destination); 25 | } 26 | -------------------------------------------------------------------------------- /files/libft/ft_memmove.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memmove.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/22 11:44:20 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 23:35:29 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memmove(void *dest, const void *src, size_t n) 16 | { 17 | unsigned char *ucs; 18 | unsigned char *udst; 19 | size_t i; 20 | 21 | if (src == dest) 22 | return (dest); 23 | ucs = (unsigned char *)src; 24 | udst = (unsigned char *)dest; 25 | if (src < dest) 26 | { 27 | i = n; 28 | while (i--) 29 | udst[i] = ucs[i]; 30 | } 31 | else 32 | { 33 | i = -1; 34 | while (++i < n) 35 | udst[i] = ucs[i]; 36 | } 37 | return (dest); 38 | } 39 | -------------------------------------------------------------------------------- /files/libft/ft_memory.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memory.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/01/29 08:19:43 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 19:38:51 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | ** example : 17 | ** int *ptr = ft_memory(5, sizeof(int), NULL, push); 18 | ** ft_memory(0, 0, ptr, pop); 19 | ** ft_memory(0, 0, NULL, clear); 20 | */ 21 | 22 | static t_list *ft_lstnew_2(void *content) 23 | { 24 | t_list *nw; 25 | 26 | nw = (t_list *)malloc(sizeof(t_list)); 27 | if (!nw) 28 | { 29 | ft_putstr_fd("ERROR_IN_FT_LSTNEW_2\n", 1); 30 | return (NULL); 31 | } 32 | nw->content = content; 33 | nw->next = NULL; 34 | return (nw); 35 | } 36 | 37 | static void ft_lstdelone_2(t_list **head, t_list *lst, void (*del)(void *)) 38 | { 39 | t_list *temp; 40 | 41 | if (!lst || !head) 42 | return ; 43 | temp = *head; 44 | if (temp == lst) 45 | *head = temp->next; 46 | else 47 | { 48 | while (temp && temp->next != lst) 49 | temp = temp->next; 50 | if (!temp) 51 | { 52 | ft_putstr_fd("ERROR_IN_FT_LSTDELONE_2\n", 1); 53 | return ; 54 | } 55 | temp->next = lst->next; 56 | } 57 | if (del) 58 | del(lst->content); 59 | free(lst); 60 | } 61 | 62 | static void *ft_calloc_2(size_t elementcount, size_t elementsize) 63 | { 64 | void *ans; 65 | 66 | ans = malloc(elementcount * elementsize); 67 | if (!ans) 68 | { 69 | ft_putstr_fd("ERROR_IN_FT_CALLOC_2\n", 1); 70 | return (NULL); 71 | } 72 | ft_memset(ans, 0, elementcount * elementsize); 73 | return (ans); 74 | } 75 | 76 | static void ft_lstclear_2(t_list **lst, void (*del)(void *)) 77 | { 78 | t_list *temp; 79 | t_list *dd; 80 | 81 | if (!(lst)) 82 | { 83 | ft_putstr_fd("ERROR_IN_FT_LSTCLEAR_2\n", 1); 84 | return ; 85 | } 86 | temp = *lst; 87 | *lst = NULL; 88 | while (temp) 89 | { 90 | if (del) 91 | del(temp->content); 92 | dd = temp; 93 | temp = temp->next; 94 | free(dd); 95 | } 96 | } 97 | 98 | void *ft_memory(size_t elem_cnt, 99 | size_t elem_size, void *del, t_memory type) 100 | { 101 | static t_list *head; 102 | void *new_mem; 103 | 104 | if (type == push) 105 | { 106 | new_mem = ft_calloc_2(elem_cnt, elem_size); 107 | if (!new_mem) 108 | ft_putstr_fd("ERROR_IN_FT_MEMORY_0\n", 1); 109 | else if (!(ft_lstadd_front(&head, ft_lstnew_2(new_mem)))) 110 | ft_putstr_fd("ERROR_IN_FT_MEMORY_1\n", 1); 111 | return (new_mem); 112 | } 113 | if (type == pop) 114 | ft_lstdelone_2(&head, ft_lstfind(head, del), free); 115 | else 116 | ft_lstclear_2(&head, free); 117 | return (NULL); 118 | } 119 | -------------------------------------------------------------------------------- /files/libft/ft_memory_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memory_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/03/09 00:04:33 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 23:37:35 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_malloc(size_t elem_cnt, size_t elem_size) 16 | { 17 | return (ft_memory(elem_cnt, elem_size, NULL, push)); 18 | } 19 | 20 | void ft_free(void *del) 21 | { 22 | ft_memory(0U, 0U, del, pop); 23 | } 24 | 25 | void ft_free_all(void) 26 | { 27 | ft_memory(0U, 0U, NULL, clear); 28 | } 29 | -------------------------------------------------------------------------------- /files/libft/ft_memset.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memset.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/11/23 11:10:22 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 23:08:45 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memset(void *b, int c, size_t len) 16 | { 17 | unsigned char *s; 18 | 19 | s = (unsigned char *)b; 20 | while (len--) 21 | *(s++) = (unsigned char)c; 22 | return (b); 23 | } 24 | -------------------------------------------------------------------------------- /files/libft/ft_minmax.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_minmax.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/03/09 00:04:57 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 22:02:15 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_min(int a, int b) 16 | { 17 | if (a < b) 18 | return (a); 19 | return (b); 20 | } 21 | 22 | int ft_max(int a, int b) 23 | { 24 | if (a > b) 25 | return (a); 26 | return (b); 27 | } 28 | -------------------------------------------------------------------------------- /files/libft/ft_putchar_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putchar_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/22 12:44:23 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 18:56:07 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putchar_fd(char c, int fd) 16 | { 17 | if (fd > 0) 18 | write(fd, &c, 1); 19 | } 20 | -------------------------------------------------------------------------------- /files/libft/ft_putendl_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putendl_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/22 11:45:16 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 23:34:04 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putendl_fd(char *s, int fd) 16 | { 17 | if (!s || fd < 0) 18 | return ; 19 | ft_putstr_fd(s, fd); 20 | ft_putchar_fd('\n', fd); 21 | } 22 | -------------------------------------------------------------------------------- /files/libft/ft_putnbr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putnbr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/22 12:44:33 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 21:10:28 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putnbr_fd(int n, int fd) 16 | { 17 | if (fd < 0) 18 | return ; 19 | if (n == 0) 20 | { 21 | ft_putstr_fd("0", fd); 22 | return ; 23 | } 24 | else if (n == -2147483648) 25 | { 26 | ft_putstr_fd("-2147483648", fd); 27 | return ; 28 | } 29 | else if (n < 0) 30 | ft_putchar_fd('-', fd); 31 | if (n < 0) 32 | n /= -1; 33 | if (n > 9) 34 | ft_putnbr_fd(n / 10, fd); 35 | ft_putchar_fd('0' + n % 10, fd); 36 | } 37 | -------------------------------------------------------------------------------- /files/libft/ft_putstr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putstr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/22 12:44:43 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 22:00:54 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putstr_fd(char *s, int fd) 16 | { 17 | if (!s || *s == '\0' || fd < 0) 18 | return ; 19 | write(fd, s, ft_strlen(s)); 20 | } 21 | -------------------------------------------------------------------------------- /files/libft/ft_sort.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_sort.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/05 16:01:35 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 22:00:06 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_ascending(int a, int b) 16 | { 17 | return (a < b); 18 | } 19 | 20 | int ft_descending(int a, int b) 21 | { 22 | return (a > b); 23 | } 24 | 25 | static void ft_swap(int *a, int *b) 26 | { 27 | int temp; 28 | 29 | temp = *a; 30 | *a = *b; 31 | *b = temp; 32 | } 33 | 34 | static void ft_deep(int *arr, int index, int nbr, int (*cmp)(int, int)) 35 | { 36 | int temp_index; 37 | 38 | if (index * 2 + 1 >= nbr) 39 | return ; 40 | if (index * 2 + 2 >= nbr) 41 | temp_index = index * 2 + 1; 42 | else 43 | { 44 | if (cmp(arr[index * 2 + 1], arr[index * 2 + 2])) 45 | temp_index = index * 2 + 2; 46 | else 47 | temp_index = index * 2 + 1; 48 | } 49 | if (cmp(arr[temp_index], arr[index])) 50 | return ; 51 | ft_swap(arr + index, arr + temp_index); 52 | ft_deep(arr, temp_index, nbr, cmp); 53 | } 54 | 55 | void ft_sort(int *head, int *tail, int (*cmp)(int, int)) 56 | { 57 | int *arr; 58 | int nbr; 59 | int ct; 60 | 61 | if (!cmp) 62 | cmp = ft_ascending; 63 | arr = head; 64 | nbr = (int)(tail - head); 65 | ct = nbr; 66 | while (--ct >= 0) 67 | ft_deep(arr, ct, nbr, cmp); 68 | while (--nbr >= 1) 69 | { 70 | ft_swap(arr + nbr, arr); 71 | ft_deep(arr, 0, nbr, cmp); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /files/libft/ft_split.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_split.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/22 12:38:31 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 21:56:00 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static int ft_cnt(char const *s, char c) 16 | { 17 | int ct[2]; 18 | 19 | if (!s) 20 | return (-1); 21 | ct[0] = 0; 22 | ct[1] = 0; 23 | while (s[ct[0]]) 24 | { 25 | while (s[ct[0]] && s[ct[0]] == c) 26 | ct[0]++; 27 | if (!s[ct[0]]) 28 | break ; 29 | while (s[ct[0]] && s[ct[0]] != c) 30 | ct[0]++; 31 | ct[1]++; 32 | } 33 | return (ct[1]); 34 | } 35 | 36 | static int ft_sub(char **ans, const char *s, int *ct) 37 | { 38 | ans[ct[1]++] = ft_substr(s, ct[0] - ct[2], ct[2]); 39 | if (!ans[ct[1] - 1]) 40 | return (0); 41 | return (1); 42 | } 43 | 44 | static char **ft_free_sub(char **ans) 45 | { 46 | int ct; 47 | 48 | ct = -1; 49 | while (ans[++ct]) 50 | ft_memory(0, 0, ans[ct], pop); 51 | ft_memory(0, 0, ans, pop); 52 | return (NULL); 53 | } 54 | 55 | static void norm(int *ct) 56 | { 57 | ct[0] = 0; 58 | ct[1] = 0; 59 | } 60 | 61 | char **ft_split(char const *s, char c) 62 | { 63 | char **ans; 64 | int sz; 65 | int ct[3]; 66 | 67 | if (!s) 68 | return (NULL); 69 | sz = ft_cnt(s, c); 70 | ans = ft_calloc(sizeof(char *), sz + 1); 71 | if (!ans) 72 | return (NULL); 73 | norm(ct); 74 | while (s[ct[0]]) 75 | { 76 | ct[2] = 0; 77 | while (s[ct[0]] && s[ct[0]] == c) 78 | ct[0]++; 79 | while (s[ct[0]] && s[ct[0]] != c) 80 | { 81 | ct[0]++; 82 | ct[2]++; 83 | } 84 | if (ct[2] > 0 && !ft_sub(ans, s, (int *)ct)) 85 | return (ft_free_sub(ans)); 86 | } 87 | return (ans); 88 | } 89 | -------------------------------------------------------------------------------- /files/libft/ft_sqrt.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_sqrt.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/04 11:58:49 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 18:55:57 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_sqrt(int n) 16 | { 17 | long long lb; 18 | long long ub; 19 | long long mid; 20 | 21 | if (n <= 1LL) 22 | return (n); 23 | lb = 0LL; 24 | ub = (long long)n; 25 | while (ub - lb > 1LL) 26 | { 27 | mid = (lb + ub) / 2LL; 28 | if (mid * mid <= (long long)n) 29 | lb = mid; 30 | else 31 | ub = mid; 32 | } 33 | return ((int)lb); 34 | } 35 | -------------------------------------------------------------------------------- /files/libft/ft_strchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/11/23 14:47:31 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 19:48:28 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strchr(const char *s, int c) 16 | { 17 | size_t ct; 18 | 19 | if (!s) 20 | return (NULL); 21 | ct = 0; 22 | while (s[ct]) 23 | { 24 | if (s[ct] == (char)c) 25 | return ((char *)s + ct); 26 | ct++; 27 | } 28 | if (s[ct] == (char)c) 29 | return ((char *)s + ct); 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /files/libft/ft_strdup.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strdup.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/22 12:37:51 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 23:01:35 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strdup(const char *source) 16 | { 17 | return (ft_substr(source, 0, ft_strlen(source))); 18 | } 19 | -------------------------------------------------------------------------------- /files/libft/ft_strjoin.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strjoin.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/11/23 16:07:51 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 23:34:51 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strjoin(char const *s1, char const *s2) 16 | { 17 | int l; 18 | int l2; 19 | int ct; 20 | char *str; 21 | 22 | l2 = ft_strlen(s1); 23 | l = ft_strlen(s1) + ft_strlen(s2); 24 | if (!s1 || !s2 || l < 0) 25 | return (NULL); 26 | str = ft_malloc(l + 1, sizeof(char)); 27 | if (!str) 28 | return (NULL); 29 | ct = -1; 30 | while (++ct < l) 31 | { 32 | if (ct < l2) 33 | str[ct] = s1[ct]; 34 | else 35 | str[ct] = s2[ct - l2]; 36 | } 37 | str[l] = '\0'; 38 | return (str); 39 | } 40 | -------------------------------------------------------------------------------- /files/libft/ft_strlcat.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlcat.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/22 12:36:12 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 19:44:01 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t ft_strlcat(char *s1, const char *s2, size_t n) 16 | { 17 | size_t i; 18 | size_t j; 19 | 20 | if (!s1 || !s2) 21 | return (0); 22 | if (n == 0) 23 | return (ft_strlen(s2)); 24 | i = 0; 25 | while (s1[i] != '\0' && i < n) 26 | i++; 27 | j = i; 28 | while (i < n - 1 && s2[i - j] != '\0') 29 | { 30 | s1[i] = s2[i - j]; 31 | i++; 32 | } 33 | if (j < n) 34 | s1[i] = '\0'; 35 | return (j + ft_strlen(s2)); 36 | } 37 | 38 | void ft_strcat(char *s1, char *s2) 39 | { 40 | int n; 41 | int i; 42 | 43 | n = ft_strlen(s1); 44 | i = 0; 45 | while (s2[i]) 46 | { 47 | s1[n + i] = s2[i]; 48 | i++; 49 | } 50 | s1[n + i] = '\0'; 51 | } 52 | -------------------------------------------------------------------------------- /files/libft/ft_strlcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/22 12:36:19 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 19:48:03 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t ft_strlcpy(char *dst, const char *src, size_t size) 16 | { 17 | size_t i; 18 | 19 | i = -1; 20 | if (!dst || !src) 21 | return (0); 22 | if (size == 0) 23 | return (ft_strlen(src)); 24 | while (src[++i] && --size) 25 | dst[i] = src[i]; 26 | dst[i] = '\0'; 27 | while (src[i]) 28 | i++; 29 | return (i); 30 | } 31 | -------------------------------------------------------------------------------- /files/libft/ft_strlen.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlen.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/22 12:34:25 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 22:05:11 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t ft_strlen(const char *s) 16 | { 17 | size_t ans; 18 | 19 | ans = 0; 20 | if (!s) 21 | return (0); 22 | while (s[ans]) 23 | ++ans; 24 | return (ans); 25 | } 26 | -------------------------------------------------------------------------------- /files/libft/ft_strmapi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strmapi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/22 12:44:11 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 21:03:25 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) 16 | { 17 | char *ans; 18 | int len; 19 | int ct; 20 | 21 | if (!s || !f) 22 | return (NULL); 23 | len = ft_strlen(s); 24 | ans = ft_memory(len + 1, sizeof(char), 0, push); 25 | if (!ans) 26 | return (NULL); 27 | ct = -1; 28 | while (++ct < len) 29 | ans[ct] = f(ct, s[ct]); 30 | ans[ct] = '\0'; 31 | return (ans); 32 | } 33 | -------------------------------------------------------------------------------- /files/libft/ft_strncmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strncmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/11/23 13:46:54 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 23:39:09 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_strncmp(const char *ss1, const char *ss2, size_t n) 16 | { 17 | unsigned char *s1; 18 | unsigned char *s2; 19 | 20 | s1 = (unsigned char *)ss1; 21 | s2 = (unsigned char *)ss2; 22 | if (!s1 || !s2) 23 | return (0); 24 | if (n == 0) 25 | return (0); 26 | if (*s1 == 0 || *s2 == 0 || *s1 != *s2) 27 | return (*s1 - *s2); 28 | s1++; 29 | s2++; 30 | return (ft_strncmp((char *)(s1), (char *)(s2), n - 1)); 31 | } 32 | -------------------------------------------------------------------------------- /files/libft/ft_strnstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strnstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/11/23 13:59:45 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 19:41:42 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strnstr(const char *h, const char *needle, size_t len) 16 | { 17 | char *haystack; 18 | size_t ct[2]; 19 | 20 | haystack = (char *)h; 21 | if (!h) 22 | return (NULL); 23 | if (!(*needle)) 24 | return (haystack); 25 | ct[0] = 0; 26 | while (ct[0] < len && haystack[ct[0]]) 27 | { 28 | ct[1] = 0; 29 | while (needle[ct[1]] && haystack[ct[0] + ct[1]] 30 | && needle[ct[1]] == haystack[ct[0] + ct[1]] && ct[0] + ct[1] < len) 31 | ct[1]++; 32 | if (needle[ct[1]] == 0) 33 | return (haystack + ct[0]); 34 | ct[0]++; 35 | } 36 | return (NULL); 37 | } 38 | -------------------------------------------------------------------------------- /files/libft/ft_strrchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strrchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/11/23 14:32:04 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 22:04:20 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strrchr(const char *s, int c) 16 | { 17 | char *add; 18 | 19 | if (!s) 20 | return (NULL); 21 | add = (char *)s; 22 | add += ft_strlen(s); 23 | while (add >= (char *)s) 24 | { 25 | if (*add == (char)c) 26 | return (add); 27 | else 28 | add--; 29 | } 30 | return (NULL); 31 | } 32 | -------------------------------------------------------------------------------- /files/libft/ft_strtrim.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strtrim.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/22 12:38:07 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 21:08:55 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strtrim(char const *s1, char const *set) 16 | { 17 | int ct[2]; 18 | int ascii[256]; 19 | 20 | if (!s1) 21 | return (NULL); 22 | if (!set) 23 | return (ft_substr(s1, 0, ft_strlen(s1))); 24 | ct[0] = -1; 25 | while (++ct[0] < 256) 26 | ascii[ct[0]] = 0; 27 | ct[0] = -1; 28 | while (set[++ct[0]]) 29 | ascii[(int)set[ct[0]]]++; 30 | ct[0] = -1; 31 | while (s1[++ct[0]]) 32 | if (ascii[(int)s1[ct[0]]] == 0) 33 | break ; 34 | ct[1] = ft_strlen(s1); 35 | while (--ct[1] >= 0) 36 | if (ascii[(int)s1[ct[1]]] == 0) 37 | break ; 38 | return (ft_substr(s1, ct[0], ct[1] - ct[0] + 1)); 39 | } 40 | -------------------------------------------------------------------------------- /files/libft/ft_substr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_substr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/22 13:00:51 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 21:59:26 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_substr(char const *s, size_t start, size_t len) 16 | { 17 | char *ans; 18 | size_t l; 19 | size_t ct; 20 | 21 | if (start >= ft_strlen(s)) 22 | { 23 | ans = ft_calloc(1, sizeof(char)); 24 | return (ans); 25 | } 26 | l = (size_t)ft_min((int)(ft_strlen(s) - start), (int)len); 27 | if (!s) 28 | return (NULL); 29 | ans = ft_calloc(l + 1, sizeof(char)); 30 | if (!ans) 31 | return (NULL); 32 | ct = -1; 33 | while (++ct < l) 34 | ans[ct] = s[start + ct]; 35 | ans[ct] = '\0'; 36 | return (ans); 37 | } 38 | -------------------------------------------------------------------------------- /files/libft/ft_tolower.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_tolower.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/11/23 13:16:38 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 19:39:39 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_tolower(int c) 16 | { 17 | if (c >= 'A' && c <= 'Z') 18 | return (c + 'a' - 'A'); 19 | return (c); 20 | } 21 | -------------------------------------------------------------------------------- /files/libft/ft_toupper.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_toupper.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/11/23 13:11:08 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 19:47:52 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_toupper(int c) 16 | { 17 | if (c >= 'a' && c <= 'z') 18 | return (c - 'a' + 'A'); 19 | return (c); 20 | } 21 | -------------------------------------------------------------------------------- /files/libft/get_next_line.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/01/15 17:08:32 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 23:16:42 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static int ft_fill(char **line, t_lst *temp) 16 | { 17 | int idx[2]; 18 | t_str *str; 19 | 20 | *line = ft_malloc(((temp->len) - (temp->idx) + 1), sizeof(char)); 21 | if (!(*line)) 22 | return (-1); 23 | idx[0] = temp->idx; 24 | idx[1] = 0; 25 | str = temp->str; 26 | while (idx[1] < (temp->len) - (temp->idx)) 27 | { 28 | if (str->s[idx[0]] == '\0') 29 | { 30 | str = str->next; 31 | idx[0] = 0; 32 | } 33 | line[0][idx[1]++] = str->s[idx[0]++]; 34 | } 35 | line[0][idx[1]] = '\0'; 36 | return (ft_clean_lst(temp)); 37 | } 38 | 39 | static int ft_end(char **line, t_lst *temp, int res, char *buff) 40 | { 41 | int idx[3]; 42 | 43 | if (res == 0 && temp->len == 0) 44 | return (1); 45 | idx[0] = 0; 46 | if (res && *buff == '\n' && ft_fill(line, temp) == -1) 47 | return (-1); 48 | else if (res && *buff == '\n') 49 | return (ft_str_add(temp, buff + 1, res - 1)); 50 | while (idx[0] < res && buff[idx[0]] != '\n') 51 | idx[0]++; 52 | if (ft_str_add(temp, buff, idx[0]) == -1) 53 | return (-1); 54 | if (res != BUFFER_SIZE || idx[0] < res) 55 | { 56 | if (ft_fill(line, temp) == -1) 57 | return (-1); 58 | if (res == idx[0]) 59 | return (2); 60 | return (ft_str_add(temp, buff + idx[0] + 1, res - idx[0] - 1)); 61 | } 62 | return (0); 63 | } 64 | 65 | static int ft_check(t_lst *temp, char **line, t_lst *save) 66 | { 67 | int ct[3]; 68 | 69 | if (temp->len > temp->idx && temp->str->s[temp->idx] == '\n') 70 | { 71 | (temp->idx)++; 72 | *line = ft_substr("", 0, 0); 73 | return (1); 74 | } 75 | ct[0] = -1; 76 | ct[1] = 0; 77 | while (++ct[0] + (temp->idx) < (temp->len)) 78 | if (temp->str->s[ct[0] + temp->idx] == '\n') 79 | break ; 80 | if (ct[0] == 0) 81 | return (ft_clean_lst(temp)); 82 | if (ct[0] + temp->idx < temp->len) 83 | { 84 | line[0] = ft_substr(temp->str->s, temp->idx, ct[0]); 85 | (temp->idx) += ct[0] + 1; 86 | if (temp->len == temp->idx) 87 | ft_del_lst(temp, &save, (int *)ct); 88 | return (1); 89 | } 90 | return (0); 91 | } 92 | 93 | static void ft_gnl(int *res, char *buff, char **line, t_lst *temp) 94 | { 95 | res[0] = read(res[2], buff, BUFFER_SIZE); 96 | if (res[0] == -1) 97 | { 98 | res[1] = -1; 99 | return ; 100 | } 101 | buff[res[0]] = '\0'; 102 | res[1] = ft_end(line, temp, res[0], (char *)buff); 103 | if (res[1] == 1 || res[1] == 2 || res[0] == 0) 104 | if (*line == 0) 105 | *line = ft_substr("", 0, 0); 106 | } 107 | 108 | int get_next_line(int fd, char **line) 109 | { 110 | char buff[BUFFER_SIZE + 1]; 111 | static t_lst *save; 112 | t_lst *temp; 113 | int res[3]; 114 | 115 | temp = save; 116 | while (temp && temp->fd != fd) 117 | temp = temp->next; 118 | if (!temp) 119 | temp = ft_lst_add(&save, fd); 120 | if (!temp || fd < 0 || BUFFER_SIZE < 1 || !line) 121 | return (-1); 122 | *line = 0; 123 | if (ft_check(temp, line, save)) 124 | return (1); 125 | res[2] = fd; 126 | while (1) 127 | { 128 | ft_gnl((int *)res, buff, line, temp); 129 | if (res[0] == -1 || res[1] == -1) 130 | return (ft_del_lst(temp, &save, (int *)res)); 131 | if (res[1] == 1 || res[1] == 2 || res[0] == 0) 132 | break ; 133 | } 134 | return (ft_del_lst(temp, &save, (int *)res)); 135 | } 136 | -------------------------------------------------------------------------------- /files/libft/get_next_line.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/01/15 17:09:14 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 23:16:49 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef GET_NEXT_LINE_H 14 | 15 | # define GET_NEXT_LINE_H 16 | # include 17 | # include 18 | # ifndef BUFFER_SIZE 19 | # define BUFFER_SIZE 42 20 | # endif 21 | 22 | typedef struct s_str 23 | { 24 | char *s; 25 | struct s_str *next; 26 | } t_str; 27 | 28 | typedef struct s_lst 29 | { 30 | t_str *str; 31 | int len; 32 | int fd; 33 | int idx; 34 | struct s_lst *next; 35 | } t_lst; 36 | 37 | int get_next_line(int fd, char **line); 38 | t_lst *ft_lst_add(t_lst **dst, int fd); 39 | int ft_str_add(t_lst *dst, char *s, int len); 40 | int ft_clean_lst(t_lst *lst); 41 | int ft_del_lst(t_lst *target, t_lst **head, int *res); 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /files/libft/get_next_line_sub.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line_sub.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/05/04 23:31:09 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 23:32:52 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static int sub(t_lst *target, t_lst **head, int *res) 16 | { 17 | if (target->str) 18 | ft_clean_lst(target); 19 | if (target->next) 20 | *head = target->next; 21 | else 22 | *head = 0; 23 | ft_free(target); 24 | if (res[1] == -1) 25 | return (-1); 26 | return (res[1] && res[0]); 27 | } 28 | 29 | int ft_del_lst(t_lst *target, t_lst **head, int *res) 30 | { 31 | t_lst *temp; 32 | 33 | if (res[1] != -1 && target->len) 34 | return (1); 35 | if (target == *head) 36 | return (sub(target, head, res)); 37 | temp = *head; 38 | while (temp->next != target) 39 | temp = temp->next; 40 | temp->next = target->next; 41 | if (target->str) 42 | ft_clean_lst(target); 43 | ft_free(target); 44 | if (res[1] == -1) 45 | return (-1); 46 | return (res[1] == res[0]); 47 | } 48 | -------------------------------------------------------------------------------- /files/libft/get_next_line_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/01/15 17:08:54 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 23:31:31 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static int ft_sub(t_lst *dst, t_str *str, int len) 16 | { 17 | t_str *temp; 18 | 19 | temp = dst->str; 20 | if (!temp) 21 | { 22 | dst->str = str; 23 | dst->len = len; 24 | return (1); 25 | } 26 | while (temp->next) 27 | temp = temp->next; 28 | temp->next = str; 29 | dst->len += len; 30 | return (1); 31 | } 32 | 33 | int ft_str_add(t_lst *dst, char *s, int len) 34 | { 35 | t_str *str; 36 | int idx; 37 | 38 | if (len < 0 || !s) 39 | return (-1); 40 | str = ft_malloc(1, sizeof(t_str)); 41 | if (!str) 42 | return (-1); 43 | idx = -1; 44 | str->next = 0; 45 | str->s = ft_calloc(len + 1, sizeof(char)); 46 | if (!(str->s)) 47 | return (-1); 48 | while (++idx < len) 49 | str->s[idx] = s[idx]; 50 | return (ft_sub(dst, str, len)); 51 | } 52 | 53 | t_lst *ft_lst_add(t_lst **dst, int fd) 54 | { 55 | t_lst *temp; 56 | 57 | temp = ft_malloc(1, sizeof(t_lst)); 58 | if (!temp) 59 | return (NULL); 60 | temp->str = 0; 61 | temp->len = 0; 62 | temp->fd = fd; 63 | temp->idx = 0; 64 | temp->next = 0; 65 | if (*dst == 0) 66 | *dst = temp; 67 | else 68 | { 69 | temp->next = *dst; 70 | *dst = temp; 71 | } 72 | return (temp); 73 | } 74 | 75 | int ft_clean_lst(t_lst *lst) 76 | { 77 | t_str *temp; 78 | 79 | lst->idx = 0; 80 | lst->len = 0; 81 | while (lst->str) 82 | { 83 | temp = lst->str; 84 | lst->str = lst->str->next; 85 | ft_free(temp->s); 86 | ft_free(temp); 87 | } 88 | lst->str = 0; 89 | return (0); 90 | } 91 | -------------------------------------------------------------------------------- /files/libft/libft.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* libft.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/12 11:22:42 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 19:49:39 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef LIBFT_H 14 | 15 | # define LIBFT_H 16 | # include 17 | # include 18 | # include "get_next_line.h" 19 | 20 | typedef struct s_list 21 | { 22 | void *content; 23 | struct s_list *next; 24 | } t_list; 25 | 26 | typedef struct s_double_list 27 | { 28 | void *content; 29 | struct s_double_list *next; 30 | struct s_double_list *last; 31 | } t_double_list; 32 | 33 | typedef struct s_deque 34 | { 35 | struct s_double_list *head; 36 | struct s_double_list *tail; 37 | int size; 38 | } t_deque; 39 | 40 | typedef enum e_memory 41 | { 42 | push, 43 | pop, 44 | clear 45 | } t_memory; 46 | 47 | void ft_sort(int *head, int *tail, int (*cmp)(int, int)); 48 | void deque_clear(t_deque *deque, void (*f)(void*)); 49 | t_double_list *double_list_init(void *ptr); 50 | void deque_push_back(t_deque *deque, void *ptr); 51 | void deque_push_front(t_deque *deque, void *ptr); 52 | void deque_pop_back(t_deque *deque, void (*f)(void*)); 53 | void deque_pop_front(t_deque *deque, void (*f)(void*)); 54 | t_deque *deque_init(void); 55 | void *ft_malloc(size_t elem_cnt, size_t elem_size); 56 | void ft_free(void *del); 57 | void ft_free_all(void); 58 | void *ft_memory(size_t ec, size_t es, void *add, t_memory type); 59 | void *ft_memset(void *pointer, int value, size_t count); 60 | void ft_bzero(void *s, size_t n); 61 | void *ft_memcpy(void *destination, const void *source, size_t size); 62 | void *ft_memccpy(void *dest, const void *src, int c, size_t n); 63 | void *ft_memmove(void *dest, const void *src, size_t n); 64 | void *ft_memchr(const void *s, int c, size_t n); 65 | int ft_memcmp(const void *s1, const void *s2, size_t n); 66 | size_t ft_strlcpy(char *dst, const char *src, size_t size); 67 | char *ft_strchr(const char *string, int searchedchar); 68 | char *ft_strrchr(const char *string, int searchedchar); 69 | char *ft_strnstr(const char *s1, const char *s2, size_t len); 70 | int ft_strncmp(const char *f, const char *s, size_t len); 71 | int ft_atoi(char *str); 72 | int ft_isalpha(int character); 73 | int ft_isdigit(int character); 74 | int ft_isalnum(int character); 75 | int ft_isascii(int character); 76 | int ft_isprint(int character); 77 | int ft_toupper(int character); 78 | int ft_tolower(int character); 79 | void *ft_calloc(size_t elementcount, size_t elementsize); 80 | char *ft_strdup(const char *source); 81 | size_t ft_strlcat(char *s1, const char *s2, size_t n); 82 | void ft_strcat(char *s1, char *s2); 83 | size_t ft_strlen(const char *s); 84 | char *ft_substr(char const *s, size_t start, size_t len); 85 | char *ft_strjoin(char const *s1, char const *s2); 86 | char *ft_strtrim(char const *s1, char const *set); 87 | char **ft_split(char const *s, char c); 88 | char *ft_itoa(int n); 89 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); 90 | void ft_putchar_fd(char c, int fd); 91 | void ft_putstr_fd(char *s, int fd); 92 | void ft_putendl_fd(char *s, int fd); 93 | void ft_putnbr_fd(int n, int fd); 94 | t_list *ft_lstnew(void *content); 95 | int ft_lstadd_front(t_list **lst, t_list *new); 96 | int ft_lstsize(t_list *lst); 97 | t_list *ft_lstlast(t_list *lst); 98 | int ft_lstadd_back(t_list **lst, t_list *new); 99 | void ft_lstdelone(t_list **head, t_list *lst, void (*del)(void *)); 100 | void ft_lstclear(t_list **lst, void (*del)(void *)); 101 | void ft_lstiter(t_list *lst, void (*f)(void *)); 102 | t_list *ft_lstfind(t_list *head, void *target); 103 | t_list *ft_lstmap(t_list *l, void *(*f)(void *), void (*d)(void *)); 104 | int ft_min(int a, int b); 105 | int ft_max(int a, int b); 106 | int ft_isspace(char c); 107 | int ft_ascending(int a, int b); 108 | int ft_descending(int a, int b); 109 | int binary_search(int *arr, int arr_size, int target); 110 | int ft_sqrt(int n); 111 | 112 | #endif 113 | -------------------------------------------------------------------------------- /files/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #define FAST ios_base::sync_with_stdio(false);cin.tie(NULL); 9 | 10 | using namespace std; 11 | 12 | int main(int ac, char **av) 13 | { 14 | FAST 15 | std::random_device rd; 16 | std::mt19937::result_type seed = rd() ^ ( 17 | (std::mt19937::result_type) 18 | std::chrono::duration_cast( 19 | std::chrono::system_clock::now().time_since_epoch() 20 | ).count() + 21 | (std::mt19937::result_type) 22 | std::chrono::duration_cast( 23 | std::chrono::high_resolution_clock::now().time_since_epoch() 24 | ).count() ); 25 | 26 | std::mt19937 gen(seed); 27 | int stack_size = 100; 28 | if (ac > 1) stack_size = atoi(av[1]); 29 | unordered_set used; 30 | for( int j = 0; j < stack_size; ++j ) 31 | { 32 | int n = static_cast(gen()); 33 | while (used.find(n) != used.end()) n = static_cast(gen()); 34 | used.insert(n); 35 | std::cout << n << '\n'; 36 | } 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /files/srcs/bonus/bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/04 19:57:16 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 23:43:14 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "utils.h" 14 | #include 15 | 16 | static void clearscreen(void) 17 | { 18 | write(1, "\e[1;1H\e[2J", 11); 19 | } 20 | 21 | static void ko(void) 22 | { 23 | ft_putendl_fd("\033[0;31mKO\033[0m", 1); 24 | exit(EXIT_SUCCESS); 25 | } 26 | 27 | static void ok(t_list *instructions) 28 | { 29 | ft_putstr_fd("\033[1;32mOK with ", 1); 30 | ft_putnbr_fd(ft_lstsize(instructions), 1); 31 | ft_putendl_fd(" operations\033[0m", 1); 32 | exit(EXIT_SUCCESS); 33 | } 34 | 35 | static void sub(t_list *cur_instruction, t_deque *stack[2], 36 | void *last_operation) 37 | { 38 | char c; 39 | t_double_list *cur; 40 | 41 | while (cur_instruction) 42 | { 43 | read(0, &c, 1); 44 | if (c == '\n') 45 | { 46 | clearscreen(); 47 | display_stacks(stack, last_operation); 48 | assigned_operation(cur_instruction->content, stack); 49 | display_stacks(stack, (char *)(cur_instruction->content)); 50 | last_operation = cur_instruction->content; 51 | cur_instruction = cur_instruction->next; 52 | } 53 | } 54 | if (stack[1]->size) 55 | ko(); 56 | cur = stack[0]->head; 57 | while (cur->next) 58 | { 59 | if (*((int *)cur->content) < *((int *)cur->next->content)) 60 | ko(); 61 | cur = cur->next; 62 | } 63 | } 64 | 65 | void check_bonus(int *arr, int arr_size, t_list *instructions) 66 | { 67 | t_list *cur_instruction; 68 | t_deque *stack[2]; 69 | void *last_operation; 70 | 71 | stack[0] = deque_init(); 72 | stack[1] = deque_init(); 73 | if (!stack[0] || !stack[1]) 74 | error_exit(); 75 | init_stack(stack[0], arr, arr_size); 76 | cur_instruction = instructions; 77 | last_operation = "Init a and b"; 78 | clearscreen(); 79 | display_stacks(stack, last_operation); 80 | ft_putendl_fd("press enter to continue", 1); 81 | sub(cur_instruction, stack, last_operation); 82 | ok(instructions); 83 | } 84 | -------------------------------------------------------------------------------- /files/srcs/bonus/main.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/04 21:22:47 by yfu #+# #+# */ 9 | /* Updated: 2021/05/12 17:00:41 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "utils.h" 14 | #include 15 | 16 | int main(int argc, char **argv) 17 | { 18 | int *arr; 19 | int arr_size; 20 | t_list *instructions; 21 | char *line; 22 | int fd; 23 | 24 | arr_size = get_nbr_in_line(&arr, argc, argv); 25 | instructions = NULL; 26 | fd = open("instructions", O_RDONLY); 27 | if (fd < 0) 28 | error_exit(); 29 | while (get_next_line(fd, &line) == 1) 30 | { 31 | check_instructions(line); 32 | ft_lstadd_back(&instructions, ft_lstnew(line)); 33 | } 34 | if (arr_size == 0) 35 | normal_exit(); 36 | close(fd); 37 | check_bonus(arr, arr_size, instructions); 38 | return (0); 39 | } 40 | -------------------------------------------------------------------------------- /files/srcs/checker/check.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* check.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/03/12 18:16:15 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 23:46:21 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "utils.h" 14 | 15 | /* 16 | ** arr_size >= 2 17 | ** tail means top 18 | ** head means bottom 19 | */ 20 | 21 | static void sub(t_list *cur_instruction, t_deque *stack[2]) 22 | { 23 | while (cur_instruction) 24 | { 25 | assigned_operation(cur_instruction->content, stack); 26 | if (DEBUG == 1) 27 | display_stacks(stack, (char *)(cur_instruction->content)); 28 | cur_instruction = cur_instruction->next; 29 | } 30 | } 31 | 32 | int check(int *arr, int arr_size, t_list *instructions) 33 | { 34 | t_deque *stack[2]; 35 | t_double_list *cur; 36 | 37 | stack[0] = deque_init(); 38 | stack[1] = deque_init(); 39 | if (!stack[0] || !stack[1]) 40 | error_exit(); 41 | init_stack(stack[0], arr, arr_size); 42 | if (DEBUG == 1) 43 | display_stacks(stack, "Init a and b"); 44 | sub(instructions, stack); 45 | if (stack[1]->size) 46 | return (0); 47 | cur = stack[0]->head; 48 | while (cur->next) 49 | { 50 | if (*((int *)cur->content) < *((int *)cur->next->content)) 51 | return (0); 52 | cur = cur->next; 53 | } 54 | return (1); 55 | } 56 | -------------------------------------------------------------------------------- /files/srcs/checker/main.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/03/12 14:16:31 by yfu #+# #+# */ 9 | /* Updated: 2021/05/04 18:45:36 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "utils.h" 14 | 15 | int main(int argc, char **argv) 16 | { 17 | int *arr; 18 | int arr_size; 19 | t_list *instructions; 20 | char *line; 21 | 22 | arr_size = get_nbr_in_line(&arr, argc, argv); 23 | if (arr_size == 0) 24 | normal_exit(); 25 | instructions = NULL; 26 | while (get_next_line(0, &line) == 1) 27 | { 28 | check_instructions(line); 29 | ft_lstadd_back(&instructions, ft_lstnew(line)); 30 | } 31 | if (arr_size == 0) 32 | normal_exit(); 33 | else if (check(arr, arr_size, instructions)) 34 | ft_putendl_fd("OK", 1); 35 | else 36 | ft_putendl_fd("KO", 1); 37 | ft_free_all(); 38 | return (0); 39 | } 40 | -------------------------------------------------------------------------------- /files/srcs/radix/main.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/04 21:22:47 by yfu #+# #+# */ 9 | /* Updated: 2021/05/21 01:16:03 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "utils.h" 14 | #include 15 | 16 | int main(int argc, char **argv) 17 | { 18 | int *arr; 19 | int arr_size; 20 | t_list *instructions; 21 | char *line; 22 | int fd; 23 | int *copy; 24 | 25 | arr_size = get_nbr_in_line(&arr, argc, argv); 26 | copy = ft_malloc(arr_size, sizeof(int)); 27 | for(int i = 0 ; i < arr_size ; ++i) copy[i] = arr[i]; 28 | ft_sort(copy, copy + arr_size, ft_ascending); 29 | for(int i = 0 ; i < arr_size ; ++i) arr[i] = binary_search(copy, arr_size, arr[i]); 30 | instructions = NULL; 31 | 32 | fd = open("radix_instructions", O_RDONLY); 33 | if (fd < 0) 34 | error_exit(); 35 | while (get_next_line(fd, &line) == 1) 36 | { 37 | check_instructions(line); 38 | ft_lstadd_back(&instructions, ft_lstnew(line)); 39 | } 40 | if (arr_size == 0) 41 | normal_exit(); 42 | close(fd); 43 | check_radix(arr, arr_size, instructions); 44 | return (0); 45 | } 46 | -------------------------------------------------------------------------------- /files/srcs/radix/radix.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* radix.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/04 19:57:16 by yfu #+# #+# */ 9 | /* Updated: 2021/05/21 00:47:26 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "utils.h" 14 | #include 15 | 16 | static void clearscreen(void) 17 | { 18 | write(1, "\e[1;1H\e[2J", 11); 19 | } 20 | 21 | static void ko(void) 22 | { 23 | ft_putendl_fd("\033[0;31mKO\033[0m", 1); 24 | exit(EXIT_SUCCESS); 25 | } 26 | 27 | static void ok(t_list *instructions) 28 | { 29 | ft_putstr_fd("\033[1;32mOK with ", 1); 30 | ft_putnbr_fd(ft_lstsize(instructions), 1); 31 | ft_putendl_fd(" operations\033[0m", 1); 32 | exit(EXIT_SUCCESS); 33 | } 34 | 35 | static void sub(t_list *cur_instruction, t_deque *stack[2], 36 | void *last_operation) 37 | { 38 | char c; 39 | t_double_list *cur; 40 | 41 | while (cur_instruction) 42 | { 43 | read(0, &c, 1); 44 | if (c == '\n') 45 | { 46 | clearscreen(); 47 | display_stack_radix(stack, last_operation); 48 | assigned_operation(cur_instruction->content, stack); 49 | display_stack_radix(stack, (char *)(cur_instruction->content)); 50 | last_operation = cur_instruction->content; 51 | cur_instruction = cur_instruction->next; 52 | } 53 | } 54 | if (stack[1]->size) 55 | ko(); 56 | cur = stack[0]->head; 57 | while (cur->next) 58 | { 59 | if (*((int *)cur->content) < *((int *)cur->next->content)) 60 | ko(); 61 | cur = cur->next; 62 | } 63 | } 64 | 65 | void check_radix(int *arr, int arr_size, t_list *instructions) 66 | { 67 | t_list *cur_instruction; 68 | t_deque *stack[2]; 69 | void *last_operation; 70 | extern int g_size; 71 | 72 | stack[0] = deque_init(); 73 | stack[1] = deque_init(); 74 | if (!stack[0] || !stack[1]) 75 | error_exit(); 76 | init_stack(stack[0], arr, arr_size); 77 | cur_instruction = instructions; 78 | last_operation = "Init a and b"; 79 | clearscreen(); 80 | g_size = 0; 81 | while (((arr_size - 1)>>g_size) > 0) 82 | ++g_size; 83 | display_stack_radix(stack, last_operation); 84 | ft_putendl_fd("press enter to continue", 1); 85 | sub(cur_instruction, stack, last_operation); 86 | ok(instructions); 87 | } 88 | 89 | static void display_number_width11_base2(int num) 90 | { 91 | extern int g_size; 92 | 93 | for(int i = 0 ; i + g_size < 11 ; ++i) ft_putchar_fd(' ', 1); 94 | for (int i = g_size - 1 ; i >= 0 ; --i) 95 | { 96 | ft_putchar_fd(((num>>i)&1) + '0', 1); 97 | } 98 | } 99 | 100 | static void sub2(int ct[3], t_double_list *cur[2], 101 | char *message, t_deque *stack[2]) 102 | { 103 | ct[0] = ft_max(stack[0]->size, stack[1]->size); 104 | ct[2] = stack[0]->size + stack[1]->size - ct[0]; 105 | ft_putendl_fd(message, 1); 106 | while (ct[2]-- > 0) 107 | ft_putendl_fd("", 1); 108 | cur[0] = stack[0]->tail; 109 | cur[1] = stack[1]->tail; 110 | } 111 | 112 | void display_stack_radix(t_deque *stack[2], char *message) 113 | { 114 | int ct[3]; 115 | t_double_list *cur[2]; 116 | 117 | sub2(ct, cur, message, stack); 118 | while (--ct[0] >= 0) 119 | { 120 | if (ct[0] < stack[0]->size) 121 | { 122 | display_number_width11_base2(*((int *)cur[0]->content)); 123 | cur[0] = cur[0]->last; 124 | } 125 | else 126 | ft_putstr_fd(" ", 1); 127 | ft_putstr_fd(" ", 1); 128 | if (ct[0] < stack[1]->size) 129 | { 130 | display_number_width11_base2(*((int *)cur[1]->content)); 131 | cur[1] = cur[1]->last; 132 | } 133 | ft_putendl_fd("", 1); 134 | } 135 | ft_putendl_fd("__________a __________b", 1); 136 | ft_putendl_fd("---------------------------", 1); 137 | } 138 | -------------------------------------------------------------------------------- /files/srcs/share/operation1.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* operation1.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/03/12 18:21:42 by yfu #+# #+# */ 9 | /* Updated: 2021/04/04 00:23:19 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "utils.h" 14 | 15 | void sa(t_deque *stack[2]) 16 | { 17 | void *ptr[2]; 18 | 19 | if (stack[0]->size > 1) 20 | { 21 | ptr[0] = stack[0]->tail->content; 22 | deque_pop_back(stack[0], NULL); 23 | ptr[1] = stack[0]->tail->content; 24 | deque_pop_back(stack[0], NULL); 25 | deque_push_back(stack[0], ptr[0]); 26 | deque_push_back(stack[0], ptr[1]); 27 | } 28 | } 29 | 30 | void sb(t_deque *stack[2]) 31 | { 32 | void *ptr[2]; 33 | 34 | if (stack[1]->size > 1) 35 | { 36 | ptr[0] = stack[1]->tail->content; 37 | deque_pop_back(stack[1], NULL); 38 | ptr[1] = stack[1]->tail->content; 39 | deque_pop_back(stack[1], NULL); 40 | deque_push_back(stack[1], ptr[0]); 41 | deque_push_back(stack[1], ptr[1]); 42 | } 43 | } 44 | 45 | void ss(t_deque *stack[2]) 46 | { 47 | sa(stack); 48 | sb(stack); 49 | } 50 | 51 | void pa(t_deque *stack[2]) 52 | { 53 | void *ptr; 54 | 55 | if (stack[1]->size > 0) 56 | { 57 | ptr = stack[1]->tail->content; 58 | deque_pop_back(stack[1], NULL); 59 | deque_push_back(stack[0], ptr); 60 | } 61 | } 62 | 63 | void pb(t_deque *stack[2]) 64 | { 65 | void *ptr; 66 | 67 | if (stack[0]->size > 0) 68 | { 69 | ptr = stack[0]->tail->content; 70 | deque_pop_back(stack[0], NULL); 71 | deque_push_back(stack[1], ptr); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /files/srcs/share/operation2.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* operation2.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/03/12 18:21:42 by yfu #+# #+# */ 9 | /* Updated: 2021/03/13 23:42:59 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "utils.h" 14 | 15 | void ra(t_deque *stack[2]) 16 | { 17 | void *ptr; 18 | 19 | if (stack[0]->size == 0) 20 | return ; 21 | ptr = stack[0]->tail->content; 22 | deque_pop_back(stack[0], NULL); 23 | deque_push_front(stack[0], ptr); 24 | } 25 | 26 | void rb(t_deque *stack[2]) 27 | { 28 | void *ptr; 29 | 30 | if (stack[1]->size == 0) 31 | return ; 32 | ptr = stack[1]->tail->content; 33 | deque_pop_back(stack[1], NULL); 34 | deque_push_front(stack[1], ptr); 35 | } 36 | 37 | void rr(t_deque *stack[2]) 38 | { 39 | ra(stack); 40 | rb(stack); 41 | } 42 | 43 | void rra(t_deque *stack[2]) 44 | { 45 | void *ptr; 46 | 47 | if (stack[0]->size == 0) 48 | return ; 49 | ptr = stack[0]->head->content; 50 | deque_pop_front(stack[0], NULL); 51 | deque_push_back(stack[0], ptr); 52 | } 53 | 54 | void rrb(t_deque *stack[2]) 55 | { 56 | void *ptr; 57 | 58 | if (stack[1]->size == 0) 59 | return ; 60 | ptr = stack[1]->head->content; 61 | deque_pop_front(stack[1], NULL); 62 | deque_push_back(stack[1], ptr); 63 | } 64 | -------------------------------------------------------------------------------- /files/srcs/share/operation3.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* operation3.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/03/12 18:21:42 by yfu #+# #+# */ 9 | /* Updated: 2021/05/05 00:00:02 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "utils.h" 14 | 15 | void rrr(t_deque *stack[2]) 16 | { 17 | rra(stack); 18 | rrb(stack); 19 | } 20 | 21 | void init_stack(t_deque *stack, int *arr, int arr_size) 22 | { 23 | int ct; 24 | 25 | ct = -1; 26 | while (++ct < arr_size) 27 | deque_push_front(stack, arr + ct); 28 | } 29 | 30 | void assigned_operation(void *ptr, t_deque *stack[2]) 31 | { 32 | char *str; 33 | 34 | str = ptr; 35 | if (!ft_strncmp(str, "sa", 3)) 36 | sa(stack); 37 | if (!ft_strncmp(str, "sb", 3)) 38 | sb(stack); 39 | if (!ft_strncmp(str, "ss", 3)) 40 | ss(stack); 41 | if (!ft_strncmp(str, "pa", 3)) 42 | pa(stack); 43 | if (!ft_strncmp(str, "pb", 3)) 44 | pb(stack); 45 | if (!ft_strncmp(str, "ra", 3)) 46 | ra(stack); 47 | if (!ft_strncmp(str, "rb", 3)) 48 | rb(stack); 49 | if (!ft_strncmp(str, "rr", 3)) 50 | rr(stack); 51 | if (!ft_strncmp(str, "rra", 4)) 52 | rra(stack); 53 | if (!ft_strncmp(str, "rrb", 4)) 54 | rrb(stack); 55 | if (!ft_strncmp(str, "rrr", 4)) 56 | rrr(stack); 57 | } 58 | -------------------------------------------------------------------------------- /files/srcs/share/operation4.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* operation4.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/05 16:31:57 by yfu #+# #+# */ 9 | /* Updated: 2021/05/05 00:00:27 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "utils.h" 14 | 15 | static void display_number_width11(int num) 16 | { 17 | int ct[2]; 18 | 19 | ct[0] = 1; 20 | if (num < 0) 21 | ct[0]++; 22 | ct[1] = num; 23 | while (ct[1] > 9 || ct[1] < -9) 24 | { 25 | ct[0]++; 26 | ct[1] /= 10; 27 | } 28 | ct[0] = 11 - ct[0]; 29 | while (ct[0]--) 30 | ft_putchar_fd(' ', 1); 31 | ft_putnbr_fd(num, 1); 32 | } 33 | 34 | static void sub(int ct[3], t_double_list *cur[2], 35 | char *message, t_deque *stack[2]) 36 | { 37 | ct[0] = ft_max(stack[0]->size, stack[1]->size); 38 | ct[2] = stack[0]->size + stack[1]->size - ct[0]; 39 | ft_putendl_fd(message, 1); 40 | while (ct[2]-- > 0) 41 | ft_putendl_fd("", 1); 42 | cur[0] = stack[0]->tail; 43 | cur[1] = stack[1]->tail; 44 | } 45 | 46 | void display_stacks(t_deque *stack[2], char *message) 47 | { 48 | int ct[3]; 49 | t_double_list *cur[2]; 50 | 51 | sub(ct, cur, message, stack); 52 | while (--ct[0] >= 0) 53 | { 54 | if (ct[0] < stack[0]->size) 55 | { 56 | display_number_width11(*((int *)cur[0]->content)); 57 | cur[0] = cur[0]->last; 58 | } 59 | else 60 | ft_putstr_fd(" ", 1); 61 | ft_putstr_fd(" ", 1); 62 | if (ct[0] < stack[1]->size) 63 | { 64 | display_number_width11(*((int *)cur[1]->content)); 65 | cur[1] = cur[1]->last; 66 | } 67 | ft_putendl_fd("", 1); 68 | } 69 | ft_putendl_fd("__________a __________b", 1); 70 | ft_putendl_fd("---------------------------", 1); 71 | } 72 | -------------------------------------------------------------------------------- /files/srcs/share/parse.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* parse.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/03/12 14:20:00 by yfu #+# #+# */ 9 | /* Updated: 2021/05/05 00:02:17 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "utils.h" 14 | 15 | static void put_in_list(t_list **lst, char *str) 16 | { 17 | int *temp; 18 | 19 | while (*str) 20 | { 21 | if (ft_isspace(*str)) 22 | str++; 23 | else if (ft_isdigit(*str) || *str == '-' || *str == '+') 24 | { 25 | temp = ft_malloc(1, sizeof(int *)); 26 | if (!temp) 27 | error_exit(); 28 | *temp = ft_atoi_with_range(&str); 29 | ft_lstadd_back(lst, ft_lstnew((void *)temp)); 30 | } 31 | else 32 | error_exit(); 33 | } 34 | } 35 | 36 | int has_duplicate(int *arr, int arrsize) 37 | { 38 | int ct[2]; 39 | 40 | ct[0] = -1; 41 | while (++ct[0] < arrsize - 1) 42 | { 43 | ct[1] = ct[0]; 44 | while (++ct[1] < arrsize) 45 | { 46 | if (arr[ct[0]] == arr[ct[1]]) 47 | return (1); 48 | } 49 | } 50 | return (0); 51 | } 52 | 53 | int get_nbr_in_line(int **arr, int argc, char **argv) 54 | { 55 | t_list *lst; 56 | int ct; 57 | 58 | lst = NULL; 59 | if (argc == 1) 60 | return (0); 61 | ct = 0; 62 | while (++ct < argc) 63 | put_in_list(&lst, argv[ct]); 64 | *arr = ft_malloc(ft_lstsize(lst), sizeof(int)); 65 | if (!(*arr)) 66 | error_exit(); 67 | ct = 0; 68 | while (lst) 69 | { 70 | arr[0][ct++] = *((int *)(lst->content)); 71 | lst = lst->next; 72 | } 73 | if (has_duplicate(arr[0], ct)) 74 | error_exit(); 75 | return (ct); 76 | } 77 | 78 | void check_instructions(char *str) 79 | { 80 | if (!ft_strncmp(str, "sa", 3)) 81 | return ; 82 | if (!ft_strncmp(str, "sb", 3)) 83 | return ; 84 | if (!ft_strncmp(str, "ss", 3)) 85 | return ; 86 | if (!ft_strncmp(str, "pa", 3)) 87 | return ; 88 | if (!ft_strncmp(str, "pb", 3)) 89 | return ; 90 | if (!ft_strncmp(str, "ra", 3)) 91 | return ; 92 | if (!ft_strncmp(str, "rb", 3)) 93 | return ; 94 | if (!ft_strncmp(str, "rr", 3)) 95 | return ; 96 | if (!ft_strncmp(str, "rra", 4)) 97 | return ; 98 | if (!ft_strncmp(str, "rrb", 4)) 99 | return ; 100 | if (!ft_strncmp(str, "rrr", 4)) 101 | return ; 102 | error_exit(); 103 | } 104 | -------------------------------------------------------------------------------- /files/srcs/share/utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: yfu +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/03/12 16:16:29 by yfu #+# #+# */ 9 | /* Updated: 2021/05/05 00:10:28 by yfu ### ########lyon.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "utils.h" 14 | 15 | static void sub(long long ct[3], char **str) 16 | { 17 | ct[0] = -1LL; 18 | if (str[0][0] == '+' || str[0][0] == '-') 19 | ct[0] = 0LL; 20 | ct[2] = 1LL; 21 | if (str[0][0] == '-') 22 | ct[2] = -1LL; 23 | ct[1] = 0LL; 24 | if ((str[0][0] == '-' || str[0][0] == '+') && !ft_isdigit(str[0][1])) 25 | error_exit(); 26 | } 27 | 28 | int ft_atoi_with_range(char **str) 29 | { 30 | long long int ct[3]; 31 | 32 | sub(ct, str); 33 | while (str[0][++ct[0]]) 34 | { 35 | if (!ft_isdigit(str[0][ct[0]])) 36 | { 37 | *str += ct[0]; 38 | if (ft_isspace(str[0][0])) 39 | return (ct[1] * ct[2]); 40 | error_exit(); 41 | } 42 | ct[1] = 10LL * ct[1] + str[0][ct[0]] - '0'; 43 | if ((ct[2] == 1LL && ct[1] > 2147483647LL) || (ct[2] == -1LL && ct[1] 44 | > 2147483648LL) || (ct[1] == 0LL 45 | && ft_isdigit(str[0][ct[0] + 1]))) 46 | error_exit(); 47 | } 48 | *str += ct[0]; 49 | return (ct[1] * ct[2]); 50 | } 51 | 52 | void error_exit(void) 53 | { 54 | ft_putendl_fd("Error", 2); 55 | ft_free_all(); 56 | exit(EXIT_FAILURE); 57 | } 58 | 59 | void normal_exit(void) 60 | { 61 | ft_free_all(); 62 | exit(EXIT_SUCCESS); 63 | } 64 | -------------------------------------------------------------------------------- /fr.subject.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeoFu9487/push_swap_tutorial/379ed3edefaf0e778252de9309d75112df94c7f1/fr.subject.pdf -------------------------------------------------------------------------------- /radix_sort_solution.sh: -------------------------------------------------------------------------------- 1 | RED="\e[31m" 2 | GREEN="\e[32m" 3 | NOCOLOR="\e[0m" 4 | 5 | if [ $# -lt 2 ] 6 | then 7 | printf "${RED}Error : not enough arguments${NOCOLOR}\n" 8 | printf "Usage : bash radix_sort_solution.sh \n" 9 | printf "Example : bash radix_sort_solution.sh \"9 4 8 7\"\n" 10 | exit 1 11 | fi 12 | 13 | g++ ./files/algo.cpp -o ./files/algo 14 | ./files/algo $@ 15 | rm -rf ./files/algo -------------------------------------------------------------------------------- /radix_sort_test.sh: -------------------------------------------------------------------------------- 1 | ROOT=.. 2 | 3 | RED="\e[31m" 4 | GREEN="\e[32m" 5 | NOCOLOR="\e[0m" 6 | TIME_LIMIT=2.0 7 | 8 | make radix -C ./files/ 9 | 10 | make -C $ROOT/ 11 | 12 | clear 13 | 14 | if [[ -f "$ROOT/push_swap" ]] 15 | then 16 | printf "${GREEN}push_swap compilation ok${NOCOLOR}\n\n" 17 | else 18 | printf "${RED}push_swap compilation ko${NOCOLOR}\n\n" 19 | exit 1 20 | fi 21 | 22 | if [ $# -lt 1 ] 23 | then 24 | printf "${RED}Error : not enough arguments${NOCOLOR}\n" 25 | printf "Usage : bash radix_sort_test.sh \n" 26 | printf "Example : bash radix_sort_test.sh \"9 4 8 7\"\n" 27 | exit 1 28 | fi 29 | 30 | echo "" > ./trace/test_case.txt 31 | 32 | for i in $@ 33 | do 34 | echo $i >> ./trace/test_case.txt 35 | done 36 | 37 | ($ROOT/push_swap $(cat ./trace/test_case.txt) > ./trace/output.txt) & pid=$! 38 | (sleep $TIME_LIMIT && kill -HUP $pid) 2>/dev/null & watcher=$! 39 | if wait $pid 2>/dev/null; then 40 | TLEFLAG=0 41 | else 42 | printf "${RED}TLE${NOCOLOR}\n" 43 | exit 1 44 | fi 45 | 46 | 47 | cat ./trace/output.txt > radix_instructions 48 | ./files/checker_radix $(cat ./trace/test_case.txt) 49 | rm -rf radix_instructions 50 | echo "test_case and output are in trace" 51 | -------------------------------------------------------------------------------- /trace/clean.sh: -------------------------------------------------------------------------------- 1 | rm -rf test_case.txt 2 | rm -rf output.txt --------------------------------------------------------------------------------