How fibonnacc/push_swap works
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