# amparojd/42_push_swap architecture

> Architecture diagram of the GitHub repository amparojd/42_push_swap, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/amparojd/42_push_swap
- Repository: https://github.com/amparojd/42_push_swap

## Overview

##### 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.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Input Layer
    subgraph "Input Layer"
        A["Input Parser (ft_parse_args.c)"] 
    end

    %% Core Processing Layer
    subgraph "Core Processing Layer"
        B["Stack Manager (ft_fill_stacks.c, ft_set_target_pos.c)"]
        
        subgraph "Sorting Algorithms"
            C1["General Sorting (ft_sort.c)"]
            C2["Sorting Three Numbers (ft_sort_three_numbers.c)"]
            C3["Sorting >3 Numbers (ft_sort_more_than_three.c)"]
        end

        subgraph "Movement Handlers"
            D1["Movement Execution (ft_execute_movements.c)"]
            D2["Movement Operations (ft_movements.c, ft_movements_reverse.c)"]
        end

        E["Heuristics (ft_heuristics.c)"]
        F["Memory Manager (ft_free.c)"]
    end

    %% Utility Layer
    subgraph "Utility Layer"
        G["Libft (Utility Library)"]
    end

    %% Build & Configuration
    subgraph "Build & Configuration"
        H["Project Header (ft_push_swap.h)"]
        I["Makefile (Project Root)"]
        J["Libft Makefile"]
    end

    %% Main Flow Connections
    A -->|"parse_input"| B
    B -->|"init_stack"| C1
    B -->|"init_stack"| C2
    B -->|"init_stack"| C3

    C1 -->|"invoke_moves"| D1
    C2 -->|"invoke_moves"| D1
    C3 -->|"invoke_moves"| D1

    C1 -->|"invoke_moves"| D2
    C2 -->|"invoke_moves"| D2
    C3 -->|"invoke_moves"| D2

    C1 -->|"calc_cost"| E
    C2 -->|"calc_cost"| E
    C3 -->|"calc_cost"| E

    D1 -->|"cleanup"| F
    D2 -->|"cleanup"| F
    E  -->|"cleanup"| F

    %% Utility Integration (dashed lines)
    B ---|"uses"| G
    C1 ---|"uses"| G
    C2 ---|"uses"| G
    C3 ---|"uses"| G
    D1 ---|"uses"| G
    D2 ---|"uses"| G
    E  ---|"uses"| G
    F  ---|"uses"| G

    %% Build & Configuration Integration (dashed lines)
    H ---|"defines"| B
    H ---|"defines"| C1
    H ---|"defines"| E
    I ---|"builds"| B
    J ---|"builds"| G

    %% Click Events
    click A "https://github.com/amparojd/42_push_swap/blob/master/src/ft_parse_args.c"
    click B "https://github.com/amparojd/42_push_swap/blob/master/src/ft_fill_stacks.c"
    click C1 "https://github.com/amparojd/42_push_swap/blob/master/src/ft_sort.c"
    click C2 "https://github.com/amparojd/42_push_swap/blob/master/src/ft_sort_three_numbers.c"
    click C3 "https://github.com/amparojd/42_push_swap/blob/master/src/ft_sort_more_than_three.c"
    click D1 "https://github.com/amparojd/42_push_swap/blob/master/src/ft_execute_movements.c"
    click D2 "https://github.com/amparojd/42_push_swap/blob/master/src/ft_movements.c"
    click E "https://github.com/amparojd/42_push_swap/blob/master/src/ft_heuristics.c"
    click F "https://github.com/amparojd/42_push_swap/blob/master/src/ft_free.c"
    click G "https://github.com/amparojd/42_push_swap/tree/master/libft"
    click H "https://github.com/amparojd/42_push_swap/blob/master/inc/ft_push_swap.h"
    click I "https://github.com/amparojd/42_push_swap/tree/master/Makefile"
    click J "https://github.com/amparojd/42_push_swap/tree/master/libft/Makefile"

    %% Styles
    class A input;
    class B,C1,C2,C3,E,F core;
    class D1,D2 movement;
    class G util;
    class H,I,J config;

    %% Class Definitions
    classDef input fill:#ffadad,stroke:#000,stroke-width:1px;
    classDef core fill:#ffd6a5,stroke:#000,stroke-width:1px;
    classDef movement fill:#fdffb6,stroke:#000,stroke-width:1px;
    classDef util fill:#caffbf,stroke:#000,stroke-width:1px;
    classDef config fill:#9bf6ff,stroke:#000,stroke-width:1px;
```
