# fibonnacc/push_swap architecture

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

- Interactive diagram: https://gitdiagram.com/fibonnacc/push_swap
- Repository: https://github.com/fibonnacc/push_swap

## Overview

##### Project Type and Purpose
- This is a command-line sorting tool (“push_swap”) that takes a list of integers as input and outputs the minimal sequence of stack operations to sort them.
- Core features:
- Input parsing and validation
- Two stacks (A and B) implementation
- A suite of stack operations (push, swap, rotate, reverse rotate)
- Sorting algorithms (special cases for small N, radix sort for larger N)
- Error handling

##### File Structure
- Top-level C files map to logical modules:
- parse_input.c / split_string.c / handle_errors.c → Input & validation
- size_and_rank.c / stack_sort.c → Stack management & high-level sort orchestration
- radix_sort_algorithm.c / push_smallest_to_b.c → Sorting strategies
- operation_for_swap.c / operation_push_rotate.c / operation_r_rotate.c → Primitive stack operations
- libft_part*.c → Utility functions (string, memory, etc.)
- The header push_swap.h declares shared types (stack nodes, ranks, etc.) and function prototypes.

##### README
- Very minimal—project title only. No explicit architecture section. We infer architecture purely from file names and typical 42 push_swap requirements.

##### Main Components and Their Interactions
a. Command-Line Interface (CLI)
- Entry point (Makefile → build push_swap executable)
- push_swap_sources.c: main() reads arguments, invokes parser & sorter
b. Input & Validation Module
- parse_input.c / split_string.c: tokenizes argv, converts to integers, checks duplicates/overflows
- handle_errors.c: prints “Error” and exits on invalid input
c. Data Model: Two Stacks
- Defined in push_swap.h: stack node struct holds value & rank
- size_and_rank.c: assigns normalized ranks for easier radix sort
d. Primitive Operations Library
- operation_for_swap.c, operation_push_rotate.c, operation_r_rotate.c
- Each exports functions like sa(), pb(), rra() that mutate stacks A and B
e. Sorting Orchestrator
- stack_sort.c: decides which algorithm to apply based on N (small / big)
- push_smallest_to_b.c: special routine for small inputs
- radix_sort_algorithm.c: bitwise radix sort for larger inputs

##### Architectural Patterns & Design Principles
- Modular layering:
- CLI & Orchestrator
- Input/Validation
- Core Data Structures
- Primitive Operations
- Sorting Algorithms
- Single Responsibility: each .c file handles one concern
- Clear API boundary: all modules interact via functions declared in push_swap.h
- No global state: stacks are passed by pointer to each operation

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% CLI/Main Module
    subgraph "CLI / Main"
        CLI_Makefile["Makefile"]
        CLI_Main["push_swap_sources.c"]
    end
    class CLI_Makefile,CLI_Main cli

    %% Input & Validation Module
    subgraph "Input & Validation"
        Parser_Parse["parse_input.c"]
        Parser_Split["split_string.c"]
        Parser_Err["handle_errors.c"]
    end
    class Parser_Parse,Parser_Split parser
    class Parser_Err error

    %% Sorting Orchestrator
    subgraph "Sorting Orchestrator"
        Orchestrator["stack_sort.c"]
        Decision{"N <= 5?"}
    end
    class Orchestrator orchestrator
    class Decision orchestrator

    %% Core Data Model
    subgraph "Core Data Model"
        Model_H["push_swap.h"]
        Model_Rank["size_and_rank.c"]
    end
    class Model_H,Model_Rank data

    %% Primitive Operations Library
    subgraph "Primitive Operations"
        Op_Swap["operation_for_swap.c"]
        Op_PushRotate["operation_push_rotate.c"]
        Op_RRotate["operation_r_rotate.c"]
    end
    class Op_Swap,Op_PushRotate,Op_RRotate operations

    %% Sorting Strategies
    subgraph "Sorting Strategies"
        Strategy_Small["push_smallest_to_b.c"]
        Strategy_Radix["radix_sort_algorithm.c"]
    end
    class Strategy_Small,Strategy_Radix strategy

    %% Utility Functions
    subgraph "Utility Functions"
        Util1["libft_part1.c"]
        Util2["libft_part2.c"]
        Util3["libft_part3.c"]
    end
    class Util1,Util2,Util3 util

    %% Connections
    CLI_Main -->|"main()"| Parser_Parse
    CLI_Main -->|"sort()"| Orchestrator
    Parser_Parse -->|"tokenize & convert"| Util1
    Parser_Parse -->|"tokenize & convert"| Util2
    Parser_Parse -->|"tokenize & convert"| Util3
    Parser_Parse -->|"build initial stack"| Model_H
    Parser_Parse -->|"assign ranks"| Model_Rank
    Parser_Parse -->|"on error"| Parser_Err
    Model_Rank -->|"normalize ranks"| Model_H

    Orchestrator --> Decision
    Decision -->|"yes"| Strategy_Small
    Decision -->|"no"| Strategy_Radix

    Strategy_Small -->|"sa()/pb()"| Op_Swap
    Strategy_Small -->|"rotate ops"| Op_PushRotate
    Strategy_Radix -->|"bitwise loops"| Op_RRotate

    Op_Swap -->|"mutate stacks"| Model_H
    Op_PushRotate -->|"mutate stacks"| Model_H
    Op_RRotate -->|"mutate stacks"| Model_H

    %% Click Events
    click CLI_Makefile "https://github.com/fibonnacc/push_swap/tree/main/Makefile"
    click CLI_Main "https://github.com/fibonnacc/push_swap/blob/main/push_swap_sources.c"
    click Parser_Parse "https://github.com/fibonnacc/push_swap/blob/main/parse_input.c"
    click Parser_Split "https://github.com/fibonnacc/push_swap/blob/main/split_string.c"
    click Parser_Err "https://github.com/fibonnacc/push_swap/blob/main/handle_errors.c"
    click Model_H "https://github.com/fibonnacc/push_swap/blob/main/push_swap.h"
    click Model_Rank "https://github.com/fibonnacc/push_swap/blob/main/size_and_rank.c"
    click Op_Swap "https://github.com/fibonnacc/push_swap/blob/main/operation_for_swap.c"
    click Op_PushRotate "https://github.com/fibonnacc/push_swap/blob/main/operation_push_rotate.c"
    click Op_RRotate "https://github.com/fibonnacc/push_swap/blob/main/operation_r_rotate.c"
    click Orchestrator "https://github.com/fibonnacc/push_swap/blob/main/stack_sort.c"
    click Strategy_Small "https://github.com/fibonnacc/push_swap/blob/main/push_smallest_to_b.c"
    click Strategy_Radix "https://github.com/fibonnacc/push_swap/blob/main/radix_sort_algorithm.c"
    click Util1 "https://github.com/fibonnacc/push_swap/blob/main/libft_part1.c"
    click Util2 "https://github.com/fibonnacc/push_swap/blob/main/libft_part2.c"
    click Util3 "https://github.com/fibonnacc/push_swap/blob/main/libft_part3.c"

    %% Styles
    classDef cli fill:#ffcccc,stroke:#cc0000,color:#000;
    classDef parser fill:#cce5ff,stroke:#004085,color:#000;
    classDef data fill:#ccffcc,stroke:#155724,color:#000;
    classDef operations fill:#ffe5cc,stroke:#7f6000,color:#000;
    classDef orchestrator fill:#d9d9ff,stroke:#2f2f7f,color:#000;
    classDef strategy fill:#e6ccff,stroke:#5f3f7f,color:#000;
    classDef util fill:#f2f2f2,stroke:#555,color:#000;
    classDef error fill:#f8d7da,stroke:#721c24,color:#000;
```
