How thuggonaut/42ic_ring02_push_swap works
Project type and purpose
• This is a single‐executable, C‐based CLI tool for sorting a list of integers using two stacks (a and b) and a restricted set of operations (swap, push, rotate, reverse‐rotate).
• It includes a custom utility library (libft) and a suite of sorting routines (commands + algorithm orchestration).
File structure
• Top level:
• Makefile – build orchestration
• push_swap directory – source tree
• libft subdirectory – reusable helper functions (string, memory, I/O, linked-list)
• Under push_swap/srcs:
• commands/ – implementations of the atomic operations
• push_swap/ – orchestration: input handling, error checking, algorithm entry points (two main init routines: A→B, B→A)
• Header files under inc/ declare stacks, nodes, and core APIs.
• Build artifacts flow: source → object files → final push_swap executable.
Pull extra insights from README
• Explains two modes of input: multiple args or single quoted string passed to split()
• Outlines the three sorting strategies: trivial swap (n=2), “sort_three” (n=3), and generic “Turk Algorithm” (>3)
• Emphasizes outputs of command sequences to stdout, later consumed by a visualizer or checker.
Main components
a. External CLI user input (shell)
b. Input parser & validator (split, string→long, duplicate/overflow checks)
c. Stack data structures: Stack A, Stack B (linked lists)
d. Command modules: swap.c, push.c, rotate.c, rev_rotate.c, sort_three.c
e. Algorithm controller (push_swap.c + init routines)
f. libft utility layer (memory, string, I/O, list ops)
h. External services (visualizer, checker) as optional consumers of stdout
Relationships & interactions
• User → CLI → Parser → Stack A
• Algorithm controller reads Stack A state → decides which command module to call
• Command module updates Stack A/B pointers → writes operation name to stdout
• Algorithm loops until Stack A is sorted and Stack B is empty
• libft functions are invoked by both parser (split, atoi), command modules (list node ops), and controller
• Makefile compiles all .c files (libft + push_swap) into the final executable
Architectural patterns/design principles
• Modular separation:
• Parser/validator
• Core algorithm (controller)
• Primitive operations (commands)
• Utility layer (libft)
• Single responsibility per module
• Data encapsulation: stacks as abstract linked lists with defined APIs
• Clear data flow: CLI → parser → model (stacks) → controller → primitives → output
Detailed elements
• Component names exactly as they appear in the code (e.g., handle_errors.c, stack_utils.c, ft_lstadd_back)
• Build process: from source dirs (srcs/commands, srcs/push_swap, libft/src) through Makefiles to the push_swap binary