How amparojd/42_push_swap works
Main Components
• Core Sorting Engine: Represented by the source files in the "src" directory. This includes various modules such as:
• Input Parsing (e.g., ft_parse_args.c)
• Stack Initialization and Filling (e.g., ft_fill_stacks.c)
• Core Operations for Movements and Swaps (ft_movements.c, ft_execute_movements.c, ft_movements_reverse.c)
• Sorting Routines for different cases (ft_sort.c, ft_sort_three_numbers.c, ft_sort_more_than_three.c)
• Heuristics and Cost Calculations (ft_heuristics.c, ft_set_target_pos.c)
• Memory Management (ft_free.c)
• Utility Library (Libft): All helper functions (e.g., ft_atoi, memcpy, string handling, and linked list operations) are encapsulated in the "libft" directory. This is the utility backbone supporting many operations in the sorting engine.
• Header Files: The "inc" directory contains the project-specific header (ft_push_swap.h) that ties together the interfaces of the sorting functions and defines shared data structures.
• Build and Project Configuration: Files like Makefile and README provide build instructions and high-level project documentation.
Relationships and Interactions
• Data Flow Initiation:
• The program begins by parsing input arguments using ft_parse_args (Input Parsing module in "src").
• These inputs are used to “fill” the initial stack (Stack A) in ft_fill_stacks.
• Sorting Logic:
• Different sorting functions (ft_sort, ft_sort_three_numbers, etc.) rely on the status of the stacks and execute various allowed operations (like swap, push, rotate) to reorder the numbers.
• The heuristics module (ft_heuristics) calculates the “cost” of moving a number between stacks, which directs the choice of operations.
• Movement Execution:
• Core movement functions (ft_movements, ft_execute_movements and ft_movements_reverse) are central to transferring and reordering the stacks.
• Utility Integration:
• The libft functions serve as helper components for operations like memory allocation, string manipulation, and linked list management.
• Memory Management:
• After sorting, ft_free ensures that dynamic memory is properly released.
Architectural Patterns and Design Principles
• Modular Organization: Separate modules for parsing, sorting, movement operations, and memory management. This compartmentalization follows good design principles – each module has a clear responsibility.
• Layered Approach: The overall functionality is layered. At a lower layer, you have the utility functions (libft). The next layer includes stack operations and movement logic encapsulated in the "src" folder. The layering is orchestrated by the main push_swap function in ft_push_swap.c.
• Reusability: The libft is designed for reuse across projects, and its integration here represents a clean separation of concerns.
Relevant Technologies and Tools
• C Language (obviously the core technology, as shown by C file implementations).
• GNU Make for building the project (Makefile at the root and inside libft).
• Standard C libraries are supplemented by the custom libft.
• Although this is not a networked or multi-tier full-stack application, the principle of layering components still applies.
Details
• Every component should be accurately labeled.
• Avoid overcomplication; ensure that all key modules and their interactions (especially between sorting logic and utility libraries) are visible.