Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeSinLiang committed Jul 16, 2023
0 parents commit 8769237
Show file tree
Hide file tree
Showing 128 changed files with 6,008 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
out
test/
Push-Swap-Tester/
checker_linux
checker
.vscode/
101 changes: 101 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: sinlee <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/07/16 04:09:49 by codespace #+# #+# #
# Updated: 2023/07/16 08:34:50 by sinlee ### ########.fr #
# #
# **************************************************************************** #

# Compiler
NAME = push_swap
CC = gcc
RM = rm -f
INCLUDE = -Iincludes -Ilib/ft_printf/includes -Ilib/libft/includes
CFLAGS = -Wall -Wextra -Werror -g -fsanitize=address

# Source files
ALGO_DIR = algorithm
STACK_DIR = stack
MISC_DIR = misc
BONUS = checker
SRCS_DIR = srcs
MAIN_FILES = main.c $(ALGO_DIR)/push_swap.c
SRCS_FILES = $(ALGO_DIR)/sort_utils.c $(ALGO_DIR)/sort.c $(ALGO_DIR)/calculation.c $(STACK_DIR)/stack_utils.c $(STACK_DIR)/operations.c $(STACK_DIR)/misc_utils.c $(MISC_DIR)/check.c
SRC_1 = $(addprefix $(SRCS_DIR)/,$(MAIN_FILES))
SRC_2 = $(addprefix $(SRCS_DIR)/,$(SRCS_FILES))
BONUS_SRC = srcs/checker.c

# Object files
OBJ_DIR = obj/
OBJ_1 = ${SRC_1:.c=.o}
OBJ_2 = ${SRC_2:.c=.o}
BONUS_OBJ =${BONUS_SRC:.c=.o}

# Libraries
LIBFT_DIR = lib/libft
LIBFT = $(LIBFT_DIR)/libft.a

PRINTF_DIR = lib/ft_printf
PRINTF = $(PRINTF_DIR)/libftprintf.a

LIBS = -L$(LIBFT_DIR) -L$(PRINTF_DIR) -lft -lftprintf

# Colors and text formatting
RESET = \033[0m
BOLD = \033[1m
DIM = \033[2m
UNDERLINE = \033[4m
BLINK = \033[5m
INVERT = \033[7m
LIGHT_BLUE = \033[94m
YELLOW = \033[93m

# Makefile rules
# @${CC} -c $(CFLAGS) $(INCLUDE) $< -o ${<:.c=.o}
.c.o:
@echo "$(BOLD)$(YELLOW)Compiling $<...$(RESET)"
@$(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@

${NAME}: ${OBJ_1} ${OBJ_2} $(LIBFT) $(PRINTF)
@echo "$(BOLD)$(LIGHT_BLUE)Linking objects...$(RESET)"
@$(CC) $(CFLAGS) $(INCLUDES) ${OBJ_1} ${OBJ_2} $(LIBS) -o $(NAME)
@echo "$(BOLD)$(LIGHT_BLUE)$(NAME) created successfully!$(RESET)"
@echo "$(BOLD)Copyright Reserved. Lee Sin Liang."

${BONUS}: ${OBJ_2} ${BONUS_OBJ} $(LIBFT) $(PRINTF)
@echo "$(BOLD)$(LIGHT_BLUE)Linking objects...$(RESET)"
@${CC} ${CFLAGS} ${BONUS_OBJ} ${OBJ_2} $(LIBS) -o ${BONUS} ${INCLUDE}
@echo "$(BOLD)$(LIGHT_BLUE)$(NAME) created successfully!$(RESET)"
@echo "$(BOLD)Copyright Reserved. Lee Sin Liang."

$(LIBFT):
@echo "$(BOLD)$(LIGHT_BLUE)Building libft...$(RESET)"
@make -C $(LIBFT_DIR) -s

$(PRINTF):
@echo "$(BOLD)$(LIGHT_BLUE)Building ft_printf...$(RESET)"
@make -C $(PRINTF_DIR) -s

all: ${NAME}

bonus: ${BONUS}

clean:
@echo "$(BOLD)$(LIGHT_BLUE)Cleaning objects...$(RESET)"
@${RM} ${OBJ_1} ${OBJ_2} ${BONUS_OBJ} ${NAME} ${BONUS}
@make -C $(LIBFT_DIR) clean -s
@make -C $(PRINTF_DIR) clean -s

fclean: clean
@echo "$(BOLD)$(LIGHT_BLUE)Cleaning $(NAME)...$(RESET)"
@${RM} ${OBJ_1} ${OBJ_2} ${BONUS_OBJ} ${NAME} ${BONUS}
@make -C $(LIBFT_DIR) fclean -s
@make -C $(PRINTF_DIR) fclean -s

re: clean all

.PHONY: all bonus clean fclean re bonus
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
```
push_swap/
├── includes/
│ ├── push_swap.h # Header file for push_swap
├── srcs/
│ ├── main.c # Main entry point of the program
│ ├── push_swap.c # Implementation of push_swap algorithm
│ ├── operations.c # Implementation of stack operations
│ ├── utils.c # Utility functions
├── lib/
│ ├── ft_printf/
│ │ ├── includes/
│ │ │ ├── ft_printf.h # Header file for ft_printf
│ │ ├── srcs/
│ │ │ ├── ft_printf.c # Implementation of ft_printf function
│ │ │ ├── ... # Other source code files for ft_printf
│ ├── libft/
│ │ ├── includes/
│ │ │ ├── libft.h # Header file for libft
│ │ ├── srcs/
│ │ │ ├── ft_*.c # Implementation files for libft functions
│ │ │ ├── ... # Other source code files for libft
├── Makefile # Makefile for compiling the project
├── README.md # Project documentation and instructions
```
40 changes: 40 additions & 0 deletions includes/checker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* checker.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sinlee <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/15 11:31:24 by codespace #+# #+# */
/* Updated: 2023/07/16 04:11:28 by sinlee ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef CHECKER_H
# define CHECKER_H

# include "ft_printf.h"
# include "libft.h"
# include <stdbool.h>

typedef struct s_node
{
int content;
struct s_node *prev;
struct s_node *next;
} t_node;

bool is_sorted(t_node *stack, bool reverse);
char *get_next_line(int fd);
bool execute(t_node **stack_a, t_node **stack_b, char *line,
bool s_print);
void print_stack(t_node *stack, char *str, bool advanced,
bool to_first);
t_node *add_node(t_node *node, int content);
void clear_lst_node(t_node *node);
void ft_error(t_node *stack_a);
int ft_atoi_ps(char *str, t_node *stack_a);
int ft_strcmp(const char *s1, const char *s2);
bool is_valid(t_node *stack_a, char *str);

#endif
79 changes: 79 additions & 0 deletions includes/push_swap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push_swap.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sinlee <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/04 02:39:31 by sinlee #+# #+# */
/* Updated: 2023/07/16 04:11:27 by sinlee ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef PUSH_SWAP_H
# define PUSH_SWAP_H

# include "ft_printf.h"
# include "libft.h"
# include <stdbool.h>

typedef struct s_node
{
int content;
struct s_node *prev;
struct s_node *next;
} t_node;

// Stack Utilties
t_node *last_first_node(t_node *node, bool is_last);
t_node *add_node(t_node *node, int content);
t_node *create_node(int content);
void clear_lst_node(t_node *node);
void print_stack(t_node *stack, char *str, bool advanced,
bool to_first);
int stack_len(t_node *stack);
int min(int a, int b);
int max(int a, int b);

// push swap operations
bool swap(t_node **stack);
bool push(t_node **stack_from, t_node **stack_to);
bool rotate(t_node **stack, bool reverse);
bool execute(t_node **stack_a, t_node **stack_b, char *line,
bool s_print);
int execute_calc(t_node *stack_a, t_node *stack_b, int len,
bool return_pos_b);
int reverse_calc(t_node *stack_a, t_node *stack_b, int len,
bool return_pos_b);
bool multi_execute(t_node **stack_a, t_node **stack_b,
char *line, int n);
void check_exit_else_exec(t_node **stack_a, t_node **stack_b,
char *line);
void exec_smt(t_node **stack_a, t_node **stack_b, int pos[2],
int mode);
void execute_ps(t_node **stack_a, t_node **stack_b, int pos[2],
int mode);
void reverse_pos(t_node **stack_a, t_node **stack_b, int pos[2]);

// sorting Utilities
void push_swap(t_node *stack_a, t_node *stack_b, int len);
bool is_sorted(t_node *stack, bool reverse);
int min_max_pos(t_node *stack, bool max, bool pos);
int find_target(t_node *stack_from, t_node *stack_to);
void target_push(t_node *stack, int pos);
void min_max_push(t_node *stack, bool max);
int calc(t_node *stack_a, t_node *stack_b, int len,
bool return_pos_b);
int lcm(int pos[2], int len_a, int len_b, bool return_move);
int node_index(t_node *stack, int target);
int find_min_index(t_node *stack_a, t_node *stack_b, int len);
void sort_three(t_node *stack_a);
void reverse_pos(t_node **stack_a, t_node **stack_b, int pos[2]);

// generic Utilities
int ft_strcmp(const char *s1, const char *s2);
void ft_error(t_node *stack_a);
int ft_atoi_ps(char *str, t_node *stack_a);
bool is_valid(t_node *stack_a, char *str);

#endif
17 changes: 17 additions & 0 deletions lib/ft_printf/.github/workflows/c-cpp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: C/C++ CI

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: make all
run: make all
52 changes: 52 additions & 0 deletions lib/ft_printf/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Prerequisites
*.d

# Object files
*.o
*.ko
*.obj
*.elf

# Linker output
*.ilk
*.map
*.exp

# Precompiled Headers
*.gch
*.pch

# Libraries
*.lib
*.a
*.la
*.lo

# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib

# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex

# Debug files
*.dSYM/
*.su
*.idb
*.pdb

# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf
21 changes: 21 additions & 0 deletions lib/ft_printf/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Architect

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
39 changes: 39 additions & 0 deletions lib/ft_printf/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: sinlee <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/05/19 10:16:35 by sinlee #+# #+# #
# Updated: 2023/07/04 02:43:31 by sinlee ### ########.fr #
# #
# **************************************************************************** #

NAME = libftprintf.a
CC = gcc
CFLAGS = -Wall -Wextra -Werror
CPPFLAGS = -I includes -I libft/includes
SOURCES = ft_printf.c ft_string.c ft_nbr.c
SRC = $(addprefix src/, $(SOURCES))
OBJS = ${SRC:.c=.o}
RM = rm -rf

${NAME} : ${OBJS}
cd libft && $(MAKE)
cp libft/libft.a $(NAME)
ar -rcs $(NAME) $(OBJS)

all: $(NAME)

clean:
cd libft && $(MAKE) clean
$(RM) $(OBJS)

fclean: clean
cd libft && $(MAKE) fclean
$(RM) $(NAME)

re: fclean all

.PHONY: all clean fclean re
Loading

0 comments on commit 8769237

Please sign in to comment.