From 2776ee1ebcb7d52ccfac8f8174db7de3abdba5d8 Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Thu, 11 Jan 2024 21:28:36 +0100 Subject: [PATCH 01/29] trying to work --- .clang-format | 9 + .clangd | 16 ++ .gitignore | 57 ++++ .gitmodules | 3 + Makefile | 135 +++++++++ flake.nix | 39 +++ gen.list | 6 + include/app/dup_state.h | 26 ++ include/app/element.h | 25 ++ include/app/lis.h | 22 ++ include/app/moves.h | 37 +++ include/app/state.h | 28 ++ input.toml | 40 +++ mecstd | 1 + push_swap | Bin 0 -> 98944 bytes src.list | 8 + src/app/lis/lis.c | 231 +++++++++++++++ src/app/lis/lower_bound.c | 11 + src/app/main.c | 180 ++++++++++++ src/app/moves.c | 44 +++ src/app/moves/push.c | 37 +++ src/app/moves/rev_rotate.c | 35 +++ src/app/moves/rotate.c | 36 +++ src/app/moves/swap.c | 39 +++ subject.txt | 556 +++++++++++++++++++++++++++++++++++++ 25 files changed, 1621 insertions(+) create mode 100644 .clang-format create mode 100644 .clangd create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 Makefile create mode 100644 flake.nix create mode 100644 gen.list create mode 100644 include/app/dup_state.h create mode 100644 include/app/element.h create mode 100644 include/app/lis.h create mode 100644 include/app/moves.h create mode 100644 include/app/state.h create mode 100644 input.toml create mode 160000 mecstd create mode 100755 push_swap create mode 100644 src.list create mode 100644 src/app/lis/lis.c create mode 100644 src/app/lis/lower_bound.c create mode 100644 src/app/main.c create mode 100644 src/app/moves.c create mode 100644 src/app/moves/push.c create mode 100644 src/app/moves/rev_rotate.c create mode 100644 src/app/moves/rotate.c create mode 100644 src/app/moves/swap.c create mode 100644 subject.txt diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..6b13f20 --- /dev/null +++ b/.clang-format @@ -0,0 +1,9 @@ +BasedOnStyle: Microsoft +IndentWidth: 4 +ColumnLimit: 80 +UseTab: Always +SortIncludes: CaseInsensitive +IndentPPDirectives: AfterHash +PPIndentWidth: 1 +AllowShortIfStatementsOnASingleLine: false +AlignConsecutiveDeclarations: true diff --git a/.clangd b/.clangd new file mode 100644 index 0000000..1c222e6 --- /dev/null +++ b/.clangd @@ -0,0 +1,16 @@ +CompileFlags: # Tweak the parse settings + Add: + - "-I/usr/include/" + - "-I/home/maix/school/push_swap/mecstd/include/" + - "-I/home/maix/school/push_swap/mecstd/output/include/" + - "-I/home/maix/school/push_swap/mecstd/generic_sources/header/" + - "-I/home/maix/school/push_swap/include/" + - "-I/home/maix/school/push_swap/output/include/" + - "-I/home/maix/school/push_swap/generic_sources/header/" + - "-I/home/maiboyer/Documents/ring-1/push_swap/mecstd/include/" + - "-I/home/maiboyer/Documents/ring-1/push_swap/mecstd/output/include/" + - "-I/home/maiboyer/Documents/ring-1/push_swap/mecstd/generic_sources/header/" + - "-I/home/maiboyer/Documents/ring-1/push_swap/include/" + - "-I/home/maiboyer/Documents/ring-1/push_swap/output/include/" + - "-I/home/maiboyer/Documents/ring-1/push_swap/generic_sources/header/" + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ce2fd6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,57 @@ +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf + +./generic +.direnv +test +output/ diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..92ca1ba --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "mecstd"] + path = mecstd + url = git@github.com:Maix0/mecstd.git diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..780fe88 --- /dev/null +++ b/Makefile @@ -0,0 +1,135 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: maiboyer +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/11/03 13:20:01 by maiboyer #+# #+# # +# Updated: 2024/01/11 14:14:03 by maiboyer ### ########.fr # +# # +# **************************************************************************** # + +define_module = $(addprefix $(1)/, $(2)) + +BUILD_DIR = build +SRC_DIR = src +INCLUDE_DIR = include +LIBS_DIR = . +GENERIC_DIR = output/src +GENERIC_INCLUDE = output/include + +ifeq ($(MAKECMDGOALS), bonus) + CFLAGS += -DBONUS + BUILD_DIR := $(BUILD_DIR)/bonus/ +endif + +NAME = push_swap +LIB_NAME ?= +TARGET = $(NAME) +CC = clang +CFLAGS += -Wall -Wextra -Werror -g2 -lme -L$(BUILD_DIR) -Wno-unused-command-line-argument -MMD +BONUS_FILES = +LIBS_NAME = mecstd +SUBJECT_URL = 'https://cdn.intra.42.fr/pdf/pdf/118610/en.subject.pdf' + +GENERIC_FILES = $(shell cat ./gen.list) +SRC_FILES = $(shell cat ./src.list) +BONUS = $(addsuffix .c,$(addprefix $(SRC_DIR)/,$(BONUS_FILES))) +SRC = $(addsuffix .c,$(addprefix $(SRC_DIR)/,$(SRC_FILES))) \ + $(addsuffix .c,$(addprefix $(GENERIC_DIR)/,$(GENERIC_FILES))) +BONUS_OBJ = $(addsuffix .o,$(addprefix $(BUILD_DIR)/,$(BONUS_FILES))) +OBJ = $(addsuffix .o,$(addprefix $(BUILD_DIR)/,$(SRC_FILES))) \ + $(addsuffix .o,$(addprefix $(BUILD_DIR)/,$(GENERIC_FILES))) +DEPS = $(addsuffix .d,$(addprefix $(BUILD_DIR)/,$(SRC_FILES))) \ + $(addsuffix .d,$(addprefix $(BUILD_DIR)/,$(GENERIC_FILES))) +LIBS = $(addprefix $(LIBS_DIR)/,$(LIBS_NAME)) +INCLUDES = $(addprefix -I,$(INCLUDE_DIR) $(GENERIC_INCLUDE) $(LIBS) $(addsuffix /include,$(LIBS))) +COL_GRAY = \\e[90m +COL_WHITE = \\e[37m +COL_GREEN = \\e[32m +COL_BOLD = \\e[1m +COL_RESET = \\e[0m + + +.PHONY: remove_name clean all fclean bonus format re generate_filelist subject get_lib + +all: $(NAME) + +bonus: all + @printf \\n$(COL_GRAY)Building\ Output\ \(with\ bonus\)\ $(COL_WHITE)$(COL_BOLD)%-28s$(COL_RESET)\ \ + $(NAME) + @printf $(COL_RESET)$(COL_GREEN)done$(COL_RESET)\\n + +get_lib: + @printf $(LIB_NAME)/$(NAME) + +$(NAME): $(OBJ) libs_build + @printf \\n$(COL_GRAY)Building\ Output\ $(COL_WHITE)$(COL_BOLD)%-28s$(COL_RESET)\ \ + $(NAME) + @$(CC) $(INCLUDES) $(OBJ) $(CFLAGS) -o $(NAME) + @#ar rcs $(BUILD_DIR)/$(NAME) $(OBJ) + @printf $(COL_GREEN)done$(COL_RESET)\\n + +libs_build: + @- $(foreach LIB,$(LIBS),\ + printf \\n; \ + printf $(COL_GRAY)Building\ library\ $(COL_RESET); \ + printf $(COL_WHITE)$(COL_BOLD)%-25s$(COL_RESET)\\n $(LIB); \ + make LIB_NAME=$(LIB)/ BUILD_DIR=$(realpath $(BUILD_DIR)) -C $(LIB) --no-print-directory all; \ + printf \\n; \ + ) + +$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c + @mkdir -p $(dir $@) + @printf $(COL_GRAY)Building\ $(COL_BOLD)$(COL_WHITE)%-50s\ $(LIB_NAME)$< + @$(CC) $(CFLAGS) $(WERROR) $(INCLUDES) -c $< -o $@ + @printf $(COL_RESET)$(COL_GREEN)done$(COL_RESET)\\n + +$(BUILD_DIR)/%.o: $(GENERIC_DIR)/%.c + @mkdir -p $(dir $@) + @printf $(COL_GRAY)Building\ $(COL_BOLD)$(COL_WHITE)%-50s\ $(LIB_NAME)$< + @$(CC) $(CFLAGS) $(WERROR) $(INCLUDES) -c $< -o $@ + @printf $(COL_RESET)$(COL_GREEN)done$(COL_RESET)\\n + +clean: + @- $(foreach LIB,$(LIBS), \ + make clean LIB_NAME=$(LIB)/ BUILD_DIR=$(realpath $(BUILD_DIR)) -C $(LIB) --no-print-directory || true;\ + ) + @- $(if $(LIB_NAME),,\ + printf $(COL_WHITE)Clearing\ Artefacts\ ; \ + printf $(COL_GRAY)\%-25s$(COL_RESET)\ \($(BUILD_DIR)\); \ + rm -rf $(BUILD_DIR); \ + printf $(COL_GREEN)done$(COL_RESET)\\n; \ + ) + @echo >/dev/null + +fclean: clean + @- $(foreach LIB,$(LIBS), \ + make fclean LIB_NAME=$(LIB)/ BUILD_DIR=$(realpath $(BUILD_DIR)) -C $(LIB) --no-print-directory || true;\ + ) + @printf $(COL_WHITE)Clearing\ Output\ $(COL_GRAY)%-28s$(COL_RESET)\ \ + \($(LIB_NAME)$(NAME)\) + @rm -f $(BUILD_DIR)$(NAME) + @printf $(COL_GREEN)done$(COL_RESET)\\n + +re: fclean all + +generate_filelist: + @/usr/bin/env zsh -c "tree -iFf --noreport output | rg '^output/src/(.*)\.c\$$' --replace '\$$1' | sort -u" > ./gen.list + @/usr/bin/env zsh -c "tree -iFf --noreport src | rg '^src/(.*)\.c\$$' --replace '\$$1' | sort -u" > ./src.list + +format: + @zsh -c "c_formatter_42 **/*.c **/*.h" + +subject: subject.txt + @bat --plain subject.txt + +subject.txt: + @curl $(SUBJECT_URL) | pdftotext -layout -nopgbrk - subject.txt + +fuck_raphael: + @echo "Oh que oui~~~\net jte nioc" + +-include $(DEPS) + diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..24419ec --- /dev/null +++ b/flake.nix @@ -0,0 +1,39 @@ +{ + description = "Flake utils demo"; + + inputs = { + flake-utils.url = "github:numtide/flake-utils"; + generic_c.url = "github:maix0/generic_c"; + c_formatter_42.url = "github:maix0/c_formatter_42-flake"; + }; + outputs = { + self, + nixpkgs, + flake-utils, + generic_c, + c_formatter_42, + }: + flake-utils.lib.eachDefaultSystem ( + system: let + pkgs = nixpkgs.legacyPackages.${system}; + in { + devShell = pkgs.mkShell { + packages = [ + pkgs.xorg.libXext + pkgs.xorg.libX11 + pkgs.clang + pkgs.clang-tools + pkgs.norminette + generic_c.packages.${system}.default + c_formatter_42.packages.${system}.default + pkgs.poppler_utils + pkgs.minilibx + pkgs.valgrind + pkgs.libbsd + pkgs.tree + pkgs.fastmod + ]; + }; + } + ); +} diff --git a/gen.list b/gen.list new file mode 100644 index 0000000..7728ddd --- /dev/null +++ b/gen.list @@ -0,0 +1,6 @@ +vec/vec_element +vec/vec_element_functions2 +vec/vec_element_functions3 +vec/vec_i64 +vec/vec_i64_functions2 +vec/vec_i64_functions3 diff --git a/include/app/dup_state.h b/include/app/dup_state.h new file mode 100644 index 0000000..69abfeb --- /dev/null +++ b/include/app/dup_state.h @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* dup_state.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/11 15:58:29 by maiboyer #+# #+# */ +/* Updated: 2024/01/11 15:59:29 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef DUP_STATE_H +#define DUP_STATE_H + +#include "app/element.h" +#include "me/types.h" + +typedef struct s_dup_state +{ + t_element *lhs; + t_usize index_to_skip; + bool found_dup; +} t_dup_state; + +#endif /* DUP_STATE_H */ diff --git a/include/app/element.h b/include/app/element.h new file mode 100644 index 0000000..74d625f --- /dev/null +++ b/include/app/element.h @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* element.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/11 14:28:41 by maiboyer #+# #+# */ +/* Updated: 2024/01/11 14:34:27 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ELEMENT_H +#define ELEMENT_H + +#include "me/types.h" + +typedef struct s_element +{ + t_i64 value; +} t_element; + +void free_element(t_element elem); + +#endif /* ELEMENT_H */ diff --git a/include/app/lis.h b/include/app/lis.h new file mode 100644 index 0000000..c1f757b --- /dev/null +++ b/include/app/lis.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lis.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/11 19:03:30 by maiboyer #+# #+# */ +/* Updated: 2024/01/11 20:50:43 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIS_H +#define LIS_H + +#include "me/types.h" +#include "me/vec/vec_element.h" +#include "me/vec/vec_i64.h" + +t_vec_i64 lis(t_vec_element *elements); + +#endif /* LIS_H */ diff --git a/include/app/moves.h b/include/app/moves.h new file mode 100644 index 0000000..677abe7 --- /dev/null +++ b/include/app/moves.h @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* moves.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/11 16:13:08 by maiboyer #+# #+# */ +/* Updated: 2024/01/11 16:18:49 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MOVES_H +#define MOVES_H + +#include "app/element.h" +#include "app/state.h" +#include "me/types.h" + +typedef enum e_move +{ + SWAP_A = 1 << 0, + SWAP_B = 1 << 1, + SWAP_BOTH = SWAP_A | SWAP_B, + PUSH_A = 1 << 2, + PUSH_B = 1 << 3, + ROTATE_A = 1 << 4, + ROTATE_B = 1 << 5, + ROTATE_BOTH = ROTATE_A | ROTATE_B, + REVERSE_ROTATE_A = 1 << 6, + REVERSE_ROTATE_B = 1 << 7, + REVERSE_ROTATE_BOTH = REVERSE_ROTATE_A | REVERSE_ROTATE_B, +} t_move; + +void do_move(t_move m, t_state *s); + +#endif /* MOVES_H */ diff --git a/include/app/state.h b/include/app/state.h new file mode 100644 index 0000000..15d0529 --- /dev/null +++ b/include/app/state.h @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* state.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/11 14:27:25 by maiboyer #+# #+# */ +/* Updated: 2024/01/11 14:36:28 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STATE_H +#define STATE_H + +#include "me/types.h" +#include "me/vec/vec_element.h" + +typedef struct s_state +{ + t_vec_element stack_a; + t_vec_element stack_b; +} t_state; + +t_state parses_arguments(t_usize count, t_str nums[]); +void free_state(t_state state); + +#endif /* STATE_H */ diff --git a/input.toml b/input.toml new file mode 100644 index 0000000..9371862 --- /dev/null +++ b/input.toml @@ -0,0 +1,40 @@ +[definition.vec] +headers = ["mecstd/generic_sources/header/vec_C__PREFIX__.h__TEMPLATE__"] +sources = [ + "mecstd/generic_sources/src/vec_C__PREFIX__.c__TEMPLATE__", + "mecstd/generic_sources/src/vec_C__PREFIX___functions2.c__TEMPLATE__", + "mecstd/generic_sources/src/vec_C__PREFIX___functions3.c__TEMPLATE__", +] +replace.C__TYPENAME__ = "type" +replace.C__TYPEHEADER__ = "header_include" +replace.C__PREFIX__ = "prefix" +replace.C__PREFIXUP__ = "prefix" + + +[definition.hashmap] +headers = ["mecstd/generic_sources/header/hashmap_C__PREFIX__.h__TEMPLATE__"] +sources = [ + "mecstd/generic_sources/src/hashmap_C__PREFIX__.c__TEMPLATE__", + "mecstd/generic_sources/src/hashmap_C__PREFIX___utils.c__TEMPLATE__", +] +replace.C__VALTYPE__ = "type" +replace.C__KEYTYPE__ = "type" +replace.C__TYPEHEADER__ = "header_include" +replace.C__PREFIX__ = "prefix" +replace.C__PREFIXUP__ = "prefix" + +[[create.vec]] +sources_output = "src/vec/" +headers_output = "include/me/vec/" +replace.C__TYPENAME__ = "t_element" +replace.C__TYPEHEADER__ = '#include "app/element.h"' +replace.C__PREFIX__ = "element" +replace.C__PREFIXUP__ = "ELEMENT" + +[[create.vec]] +sources_output = "src/vec/" +headers_output = "include/me/vec/" +replace.C__TYPENAME__ = "t_i64" +replace.C__TYPEHEADER__ = '' +replace.C__PREFIX__ = "i64" +replace.C__PREFIXUP__ = "I64" diff --git a/mecstd b/mecstd new file mode 160000 index 0000000..f9c81ca --- /dev/null +++ b/mecstd @@ -0,0 +1 @@ +Subproject commit f9c81caf38361c927fa81f72a1e832bbb619e161 diff --git a/push_swap b/push_swap new file mode 100755 index 0000000000000000000000000000000000000000..865eaa529bcb0321329fe083a7e918e83e7d7755 GIT binary patch literal 98944 zcmb<-^>JfjWMqH=CI&kO5buD116T+`GB7k$f<(bW3=Rwy47>~u3@QxD3~USx3@i)` z3@~*LP!^27!35C*qd6d424<)}2@r#UfkA>9VgihofvSViFxx>i$Udkvip?M~0d^3{ zzyPBe1R#8nK2{JD%4dN34@MitfFv0hU^KEmuzdv(d4>XrJ7KhrFhm?iBkKc&4MQJ9 zo?#wTAC&$9Hk5&Z0qiIS2ADojaDns*K=lbg)x+oqAdfIGFu-V7c!G=rVGC$@T0p}S zMk_#_0i)6NML_jMK=r|B7pOiM4YC6y6#TR#1;i%Rz5u8`nL>~$3=A@bkZ^&~OQ7z6 z(I7iODg&REq=3QlyAqB#g^AFg;{00lV6Kqw6jUIzV~%p@}t{hSotoXot^3f&3| zGhH(iz2ba5Bamuvng`hdiavM0PzI(3h65nC!t{gKA`A@RbPkf&O1$&jf&Ch9*Y@wd zcb9CwZfP1L1X2Ss1EdF}24pbE`%tHVxM2M}3=9lh3=9n13=9mMU{MGG(g$L})PwYb z!U&WFK<)y?F~|;(8jxBT2Jwl-It&a9LRb{@;S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&kU9&Vx5$OP4IyhxfFw! zix!ojEzkw!ad0q07KK2PNIY}`7CIjWOPeX6000$D`QSwyph6TJ1IW<`Ug(evT7?Lk zkwvWK0Tps6-i6u*p78|*5;%pF7H2S|R%D_kDX>bguTvqp4^*x}*R90Ihxj@}3tx~a zFz28z0|cc4)It^N@%+40Fblj~1H8ByRAqt=i38aJ(gv!zzyS@NjRjc)axBOSaBBt@ z^B^xG&rU*@0D}yLl!h?JAs4YwV$drtDT2@$Fji(>Nor96gI->K zNvd9YUa4L|QGP*cQAs6|5NI`1if(2KRLIfEQx~+%39LLLu{eW4FQqcCxH1<)mlQF8 zWuVK$Q8@8M40=VWIf)<*P*y=s34;l*+us+{|PKz4ZJNFrf#w0Ag5DaWR7)X!<`juY^G_B{iuuJw7up zEg!~COiC(BEranuYXD&EVvw&O8glZJp$%#ZDQWP0CTQ#(Vvq!MURDA+t|S8yhLh0l z51g;ez`y|7k_8=JV6cGdw}9%0&F{iAfaZT;Y!GdRrauCzKLV;>1*8ZGL%U^g5zu%O zvi-1m8;hPP%1)xqkuF{smC|u=z8P9Ux0#?uVHZ&cML%?f?ILnEn?~ z{V$;UVd00aA7mB?Co(WFaD(SqK|BTq4i-p1hXZPX0yMxu_JWuo8l+MJL@+Qgfck|X zt6=tPutM}}KnE^h^D$86py^o{6HFH}FfjZ_a{oH0{&i6OFneI`huH(8L0eS*A?ZH> z)qeu2AL1GY29Udv-4C{$fq|hHJdcb>e;jNO9UP$T#taM$22h1C`$6V_gkbKP4%H7E z53qphw}9%u0a6T2V<0`~`WHgeKU6uQe+wPp1mzE?5nu`$Enqf?0H2=0zyLmm5X4u2 z_Ky{y`eEZZAUP0*3NygyIeASIwY02YJIv-N>_;B*IJK}eW>7=53C z0W^96IwT6L0JaW@K*BXL191z7cKRzUOzK`6KuG%f=G`?YSD literal 0 HcmV?d00001 diff --git a/src.list b/src.list new file mode 100644 index 0000000..1fef610 --- /dev/null +++ b/src.list @@ -0,0 +1,8 @@ +app/lis/lis +app/lis/lower_bound +app/main +app/moves +app/moves/push +app/moves/rev_rotate +app/moves/rotate +app/moves/swap diff --git a/src/app/lis/lis.c b/src/app/lis/lis.c new file mode 100644 index 0000000..b8e85b3 --- /dev/null +++ b/src/app/lis/lis.c @@ -0,0 +1,231 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lis.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/11 18:50:35 by maiboyer #+# #+# */ +/* Updated: 2024/01/11 21:27:36 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/element.h" +#include "me/mem/mem_alloc_array.h" +#include "me/printf/printf.h" +#include "me/vec/vec_element.h" +#include "me/vec/vec_i64.h" +#include + +/* +P = array of length N +M = array of length N + 1 +M[0] = -1 // undefined so can be set to any value + +L = 0 +for i in range 0 to N-1: //N-1 included + // Binary search for the smallest positive l ≤ L + // such that X[M[l]] > X[i] + lo = 1 + hi = L + 1 + while lo < hi: + mid = lo + floor((hi-lo)/2) // lo <= mid < hi + if X[M[mid]] >= X[i] + hi = mid + else: // if X[M[mid]] < X[i] + lo = mid + 1 + + // After searching, lo == hi is 1 greater than the + // length of the longest prefix of X[i] + newL = lo + + // The predecessor of X[i] is the last index of + // the subsequence of length newL-1 + P[i] = M[newL-1] + M[newL] = i + + if newL > L: + // If we found a subsequence longer than any we've + // found yet, update L + L = newL + +// Reconstruct the longest increasing subsequence +// It consists of the values of X at the L indices: +// ..., P[P[M[L]]], P[M[L]], M[L] +S = array of length L +k = M[L] +for j in range L-1 to 0: //0 included + S[j] = X[k] + k = P[k] + +return S +*/ + +// static t_i64 binary_search(t_i64 L, t_i64 i, t_i64 m[], t_vec_element +// *elements) +//{ +// return (low); +//} + +// static t_vec_i64 lis_inner(t_vec_element *elements, t_i64 L, t_i64 m[], +// t_i64 p[]) +//{ +// } + +/* +/// Finds one of the [longest increasing subsequences][1] +/// from the subsequence created by applying `selector` on each item in `items`. +/// The result is a vector of indices within `items` +/// corresponding to one of the longest increasing subsequences. +/// +/// [1]: https://en.wikipedia.org/wiki/Longest_increasing_subsequence +pub fn lis I>(items: &[T], selector: F) -> Vec { + // This algorithm is adapted from + // +http://www.algorithmist.com/index.php?title=Longest_Increasing_Subsequence.cpp&oldid=13595 + + let mut result = Vec::new(); + + // If `items` is empty, then the result is also empty. + if items.is_empty() { + return result; + } + + // This vector stores, for each item, + // the index of the largest item prior to itself that is smaller than +itself. + // We'll use this vector at the end to build the final result. + let mut previous_chain = vec![0; items.len()]; + + // Initially, we assume that the first item is part of the result. + // We will replace this index later if that's not the case. + result.push(0); + + for i in 1..items.len() { + // If the next item is greater than the last item of the current longest +subsequence, + // push its index at the end of the result and continue. + if selector(&items[*result.last().unwrap()]) < selector(&items[i]) { + previous_chain[i] = *result.last().unwrap(); + result.push(i); + continue; + } + + // Perform a binary search to find the index of an item in `result` to +overwrite. + // We want to overwrite an index that refers to the smallest item that +is larger than `items[i]`. + // If there is no such item, then we do nothing. + let comparator = |&result_index| { + use std::cmp::Ordering; + + // We don't return Ordering::Equal when we find an equal value, + // because we want to find the index of the first equal value. + if selector(&items[result_index]) < selector(&items[i]) { + Ordering::Less + } else { + Ordering::Greater + } + }; + + let next_element_index = match result.binary_search_by(comparator) { + Ok(index) | Err(index) => index, + }; + + if selector(&items[i]) < selector(&items[result[next_element_index]]) { + if next_element_index > 0 { + previous_chain[i] = result[next_element_index - 1]; + } + + result[next_element_index] = i; + } + } + + // The last item in `result` is correct, + // but we might have started overwriting earlier items + // with what could have been a longer subsequence. + // Walk back `previous_chain` to restore the proper subsequence. + let mut u = result.len(); + let mut v = *result.last().unwrap(); + while u != 0 { + u -= 1; + result[u] = v; + v = previous_chain[v]; + } + + result +} +*/ + +t_i64 binary_search(t_vec_element *items, t_i64 i) +{ + t_i64 lo; + t_i64 hi; + t_i64 mid; + + lo = 0; + hi = items->len; + while (lo < hi) + { + mid = lo + (hi - lo) / 2; + if (items->buffer[mid].value < items->buffer[i].value) + lo = mid + 1; + else + hi = mid; + } + return (lo); +} + +void lis_inner(t_vec_element *elements, t_vec_i64 *result, + t_vec_i64 *previous_chain) +{ + t_i64 i; + t_i64 next_element_index; + + i = 1; + while (i < (t_i64)elements->len) + { + if (elements->buffer[result->buffer[result->len - 1]].value < + elements->buffer[i].value) + { + previous_chain->buffer[i] = result->buffer[result->len - 1]; + vec_i64_push(result, i++); + continue; + } + next_element_index = binary_search(elements, i); + if (elements->buffer[i].value > + elements->buffer[result->buffer[next_element_index]].value) + { + if (next_element_index > 0) + previous_chain->buffer[i] = + result->buffer[next_element_index - 1]; + result->buffer[next_element_index] = i; + } + i++; + } +} + +t_vec_i64 lis(t_vec_element *elements) +{ + t_vec_i64 result; + t_i64 u; + t_i64 v; + t_vec_i64 previous_chain; + + result = vec_i64_new(elements->len, NULL); + if (elements->len == 0) + return (result); + previous_chain = vec_i64_new(elements->len, NULL); + previous_chain.len = elements->len; + vec_i64_push(&result, 0); + lis_inner(elements, &result, &previous_chain); + u = result.len; + v = result.buffer[result.len - 1]; + while (u != 0) + { + u--; + result.buffer[u] = v; + v = previous_chain.buffer[v]; + } + return (result); +} diff --git a/src/app/lis/lower_bound.c b/src/app/lis/lower_bound.c new file mode 100644 index 0000000..79bceeb --- /dev/null +++ b/src/app/lis/lower_bound.c @@ -0,0 +1,11 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lower_bound.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/11 18:17:23 by maiboyer #+# #+# */ +/* Updated: 2024/01/11 19:17:19 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ diff --git a/src/app/main.c b/src/app/main.c new file mode 100644 index 0000000..755a321 --- /dev/null +++ b/src/app/main.c @@ -0,0 +1,180 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/11 14:14:18 by maiboyer #+# #+# */ +/* Updated: 2024/01/11 21:03:29 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/dup_state.h" +#include "app/element.h" +#include "app/lis.h" +#include "app/moves.h" +#include "app/state.h" +#include "me/convert/atoi.h" +#include "me/convert/itoa.h" +#include "me/printf/printf.h" +#include "me/string/str_clone.h" +#include "me/types.h" +#include "me/vec/vec_element.h" +#include +#include +#include +#include +#include + +void free_element(t_element elem) +{ + (void)(elem); +} + +void free_state(t_state state) +{ + vec_element_free(state.stack_a); + vec_element_free(state.stack_b); +} + +t_error parse_element(t_str s, t_element *e) +{ + t_usize i; + t_i64 value; + t_element out; + + if (s == NULL || e == NULL) + return (ERROR); + i = 0; + if (s[i] == '-' || s[i] == '+') + i++; + while (s[i] && s[i] >= '0' && s[i] <= '9') + i++; + if (s[i] != '\0') + return (ERROR); + value = me_atoi_64(s); + if (value > INT_MAX || value < INT_MIN) + return (ERROR); + out.value = value; + *e = out; + return (NO_ERROR); +} +void check_eq(t_usize i, t_element *rhs, t_dup_state *dup_state) +{ + dup_state->found_dup = + dup_state->found_dup || + (i != dup_state->index_to_skip && rhs->value == dup_state->lhs->value); +} + +void check_no_duplicate(t_vec_element *stack, t_vec_element *to_free_in_need) +{ + t_usize i; + t_dup_state s; + + i = 0; + while (i < stack->len) + { + s = (t_dup_state){ + .lhs = &stack->buffer[i], + .index_to_skip = i, + .found_dup = false, + }; + vec_element_iter(stack, (void (*)())check_eq, &s); + if (s.found_dup) + return (me_eprintf("Error:\nDuplicate numbers (%i)\n", + stack->buffer[i]), + vec_element_free(*stack), + vec_element_free(*to_free_in_need), exit(1)); + i++; + } +} + +t_state parses_arguments(t_usize count, t_str nums[]) +{ + t_state out; + t_usize i; + t_element e; + + i = 0; + out.stack_a = vec_element_new(count * 2, free_element); + out.stack_b = vec_element_new(count * 2, free_element); + while (i < count) + { + if (parse_element(nums[i], &e)) + (me_eprintf("Error:\nArgument '%s' isn't a valid number for this " + "program!\n", + nums[i]), + vec_element_free(out.stack_a), vec_element_free(out.stack_b), + exit(1)); + vec_element_push(&out.stack_b, e); + i++; + } + while (!vec_element_pop(&out.stack_b, &e)) + vec_element_push(&out.stack_a, e); + check_no_duplicate(&out.stack_a, &out.stack_b); + return out; +} + +#define LEFT(str, w) \ + ({ \ + int m = w + strlen(str); \ + m % 2 ? (m + 1) / 2 : m / 2; \ + }) + +#define RIGHT(str, w) \ + ({ \ + int m = w - strlen(str); \ + m % 2 ? (m - 1) / 2 : m / 2; \ + }) + +#define STR_CENTER(str, width) LEFT(str, width), str, RIGHT(str, width), "" + +#define STACK_WIDTH 12 +void print_state(t_state *s) +{ + t_usize max_len; + + if (s->stack_a.len > s->stack_b.len) + max_len = s->stack_a.len; + else + max_len = s->stack_b.len; + printf("|%*s%*s|%*s%*s|\n", STR_CENTER("Stack A", STACK_WIDTH), + STR_CENTER("Stack B", STACK_WIDTH)); + for (t_usize i = 0; i < max_len; i++) + { + t_str s_a; + t_str s_b; + + if (i < s->stack_a.len) + s_a = me_itoa(s->stack_a.buffer[s->stack_a.len - i - 1].value); + else + s_a = str_clone(""); + if (i < s->stack_b.len) + s_b = me_itoa(s->stack_b.buffer[s->stack_b.len - i - 1].value); + else + s_b = str_clone(""); + printf("|%*s%*s|%*s%*s|\n", STR_CENTER(s_a, STACK_WIDTH), + STR_CENTER(s_b, STACK_WIDTH)); + free(s_a); + free(s_b); + } + printf("\n"); +} + +int main(t_i32 argc, t_str argv[]) +{ + t_state state; + t_vec_i64 o; + + state = parses_arguments(--argc, ++argv); + print_state(&state); + o = lis(&state.stack_a); + printf("len = %zu", o.len); + for (t_usize i = 0; i < o.len; i++) + printf("%zi\n", (t_isize)o.buffer[i]); + printf("\n"); + free_state(state); + vec_i64_free(o); + return (0); +} diff --git a/src/app/moves.c b/src/app/moves.c new file mode 100644 index 0000000..a24428c --- /dev/null +++ b/src/app/moves.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* moves.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/11 16:17:26 by maiboyer #+# #+# */ +/* Updated: 2024/01/11 16:31:56 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/moves.h" +#include "app/element.h" +#include "app/state.h" + +void push_a(t_state *s); +void push_b(t_state *s); +void swap_a(t_state *s); +void swap_b(t_state *s); +void rotate_a(t_state *s); +void rotate_b(t_state *s); +void rev_rotate_a(t_state *s); +void rev_rotate_b(t_state *s); + +void do_move(t_move m, t_state *s) +{ + if (m & PUSH_A) + push_a(s); + if (m & PUSH_B) + push_b(s); + if (m & SWAP_A) + swap_a(s); + if (m & SWAP_B) + swap_b(s); + if (m & ROTATE_A) + rotate_a(s); + if (m & ROTATE_B) + rotate_b(s); + if (m & REVERSE_ROTATE_A) + rev_rotate_a(s); + if (m & REVERSE_ROTATE_B) + rev_rotate_b(s); +} diff --git a/src/app/moves/push.c b/src/app/moves/push.c new file mode 100644 index 0000000..26379bf --- /dev/null +++ b/src/app/moves/push.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* push.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/11 16:22:54 by maiboyer #+# #+# */ +/* Updated: 2024/01/11 17:17:45 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/element.h" +#include "app/state.h" +#include "me/vec/vec_element.h" + +static inline void push_inner(t_vec_element *to, t_vec_element *from, + t_const_str tag) +{ + t_element e; + + (void)(tag); + if (from->len == 0) + return; + vec_element_pop(from, &e); + vec_element_push(to, e); +} + +void push_a(t_state *s) +{ + push_inner(&s->stack_a, &s->stack_b, "Push A"); +} + +void push_b(t_state *s) +{ + push_inner(&s->stack_b, &s->stack_a, "Push B"); +} diff --git a/src/app/moves/rev_rotate.c b/src/app/moves/rev_rotate.c new file mode 100644 index 0000000..7344e6c --- /dev/null +++ b/src/app/moves/rev_rotate.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rev_rotate.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/11 16:26:04 by maiboyer #+# #+# */ +/* Updated: 2024/01/11 17:31:44 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/element.h" +#include "app/state.h" + +static inline void rev_rotate_inner(t_vec_element *stack, t_const_str tag) +{ + t_element e; + + (void)(tag); + if (stack->len <= 1) + return; + vec_element_pop(stack, &e); + vec_element_push_front(stack, e); +} + +void rev_rotate_a(t_state *s) +{ + rev_rotate_inner(&s->stack_a, "RevRotate A"); +} + +void rev_rotate_b(t_state *s) +{ + rev_rotate_inner(&s->stack_b, "RevRotate B"); +} diff --git a/src/app/moves/rotate.c b/src/app/moves/rotate.c new file mode 100644 index 0000000..6a78f8f --- /dev/null +++ b/src/app/moves/rotate.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rotate.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/11 16:26:04 by maiboyer #+# #+# */ +/* Updated: 2024/01/11 17:31:20 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/element.h" +#include "app/state.h" + + +static inline void rotate_inner(t_vec_element *stack, t_const_str tag) +{ + t_element e; + + (void)(tag); + if (stack->len <= 1) + return; + vec_element_pop(stack, &e); + vec_element_push_front(stack, e); +} + +void rotate_a(t_state *s) +{ + rotate_inner(&s->stack_a, "Rotate A"); +} + +void rotate_b(t_state *s) +{ + rotate_inner(&s->stack_b, "Rotate B"); +} diff --git a/src/app/moves/swap.c b/src/app/moves/swap.c new file mode 100644 index 0000000..62a316d --- /dev/null +++ b/src/app/moves/swap.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* swap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/11 16:26:04 by maiboyer #+# #+# */ +/* Updated: 2024/01/11 17:56:12 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/element.h" +#include "app/state.h" +#include "me/vec/vec_element.h" + +static inline void swap_inner(t_vec_element *stack, t_const_str tag) +{ + t_element first; + t_element second; + + (void)(tag); + if (stack->len <= 1) + return; + vec_element_pop(stack, &first); + vec_element_pop(stack, &second); + vec_element_push(stack, first); + vec_element_push(stack, second); +} + +void swap_a(t_state *s) +{ + swap_inner(&s->stack_a, "Swap A"); +} + +void swap_b(t_state *s) +{ + swap_inner(&s->stack_b, "Swap B"); +} diff --git a/subject.txt b/subject.txt new file mode 100644 index 0000000..83b2e90 --- /dev/null +++ b/subject.txt @@ -0,0 +1,556 @@ + Push_swap + Because Swap_push isn’t as natural + + + + + Summary: +This project will make you sort data on a stack, with a limited set of instructions, using + the lowest possible number of actions. To succeed you’ll have to manipulate various + types of algorithms and choose the most appropriate solution (out of many) for an + optimized data sorting. + + Version: 7 +Contents +I Foreword 2 + +II Introduction 4 + +III Objectives 5 + +IV Common Instructions 6 + +V Mandatory part 8 + V.1 The rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 + V.2 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 + V.3 The "push_swap" program . . . . . . . . . . . . . . . . . . . . . . . . 10 + +VI Bonus part 12 + VI.1 The "checker" program . . . . . . . . . . . . . . . . . . . . . . . . . . 12 + +VII Submission and peer-evaluation 14 + + + + + 1 +Chapter I + +Foreword + + • C + #include + + int main(void) + { + printf("hello, world\n"); + return 0; + } + + + + • ASM + cseg segment + assume cs:cseg, ds:cseg + org 100h + main proc + jmp debut + mess db 'Hello world!$' + debut: + mov dx, offset mess + mov ah, 9 + int 21h + ret + main endp + cseg ends + end main + + + + • LOLCODE + HAI + CAN HAS STDIO? + VISIBLE "HELLO WORLD!" + KTHXBYE + + + + • PHP + + + + + • BrainFuck + ++++++++++[>+++++++>++++++++++>+++>+<<<<-] + >++.>+.+++++++..+++.>++. + <<+++++++++++++++.>.+++.------.--------.>+.>. + + + + + 2 +Push_swap Because Swap_push isn’t as natural + + + • C# + using System; + + public class HelloWorld { + public static void Main () { + Console.WriteLine("Hello world!"); + } + } + + + + • HTML5 + + + + + Hello world ! + + +

Hello World !

+ + + + + + • YASL + "Hello world!" + print + + + + • OCaml + let main () = + print_endline "Hello world !" + + let _ = main () + + + + + 3 +Chapter II + +Introduction + +The Push swap project is a very simple and a highly straightforward algorithm project: +data must be sorted. + + You have at your disposal a set of integer values, 2 stacks, and a set of instructions +to manipulate both stacks. + + Your goal? Write a program in C called push_swap which calculates and displays +on the standard output the smallest program, made of Push swap language instructions, +that sorts the integers received as arguments. + + Easy? + + We’ll see... + + + + + 4 +Chapter III + +Objectives + +Writing a sorting algorithm is always a very important step in a developer’s journey. It +is often the first encounter with the concept of complexity. + + Sorting algorithms and their complexity are part of the classic questions discussed +during job interviews. It’s probably a good time to look at these concepts since you’ll +have to face them at some point. + + The learning objectives of this project are rigor, use of C, and use of basic algorithms. +Especially focusing on their complexity. + + Sorting values is simple. To sort them the fastest way possible is less simple. Especially +because from one integers configuration to another, the most efficient sorting solution can +differ. + + + + + 5 +Chapter IV + +Common Instructions + + • Your project must be written in C. + + • Your project must be written in accordance with the Norm. If you have bonus + files/functions, they are included in the norm check and you will receive a 0 if there + is a norm error inside. + + • Your functions should not quit unexpectedly (segmentation fault, bus error, double + free, etc) apart from undefined behaviors. If this happens, your project will be + considered non functional and will receive a 0 during the evaluation. + + • All heap allocated memory space must be properly freed when necessary. No leaks + will be tolerated. + + • If the subject requires it, you must submit a Makefile which will compile your + source files to the required output with the flags -Wall, -Wextra and -Werror, use + cc, and your Makefile must not relink. + + • Your Makefile must at least contain the rules $(NAME), all, clean, fclean and + re. + + • To turn in bonuses to your project, you must include a rule bonus to your Makefile, + which will add all the various headers, librairies or functions that are forbidden on + the main part of the project. Bonuses must be in a different file _bonus.{c/h} if + the subject does not specify anything else. Mandatory and bonus part evaluation + is done separately. + + • If your project allows you to use your libft, you must copy its sources and its + associated Makefile in a libft folder with its associated Makefile. Your project’s + Makefile must compile the library by using its Makefile, then compile the project. + + • We encourage you to create test programs for your project even though this work + won’t have to be submitted and won’t be graded. It will give you a chance + to easily test your work and your peers’ work. You will find those tests especially + useful during your defence. Indeed, during defence, you are free to use your tests + and/or the tests of the peer you are evaluating. + + • Submit your work to your assigned git repository. Only the work in the git reposi- + tory will be graded. If Deepthought is assigned to grade your work, it will be done + + + 6 +Push_swap Because Swap_push isn’t as natural + + + after your peer-evaluations. If an error happens in any section of your work during + Deepthought’s grading, the evaluation will stop. + + + + + 7 +Chapter V + +Mandatory part + +V.1 The rules + • You have 2 stacks named a and b. + • At the beginning: + + ◦ The stack a contains a random amount of negative and/or positive numbers + which cannot be duplicated. + ◦ The stack b is empty. + + • The goal is to sort in ascending order numbers into stack a. To do so you have the + following operations at your disposal: + + sa (swap a): Swap the first 2 elements at the top of stack a. + Do nothing if there is only one or no elements. + sb (swap b): Swap the first 2 elements at the top of stack b. + Do nothing if there is only one or no elements. + ss : sa and sb at the same time. + pa (push a): Take the first element at the top of b and put it at the top of a. + Do nothing if b is empty. + pb (push b): Take the first element at the top of a and put it at the top of b. + Do nothing if a is empty. + ra (rotate a): Shift up all elements of stack a by 1. + The first element becomes the last one. + rb (rotate b): Shift up all elements of stack b by 1. + The first element becomes the last one. + rr : ra and rb at the same time. + rra (reverse rotate a): Shift down all elements of stack a by 1. + The last element becomes the first one. + rrb (reverse rotate b): Shift down all elements of stack b by 1. + The last element becomes the first one. + rrr : rra and rrb at the same time. + + + 8 +Push_swap Because Swap_push isn’t as natural + + +V.2 Example +To illustrate the effect of some of these instructions, let’s sort a random list of integers. +In this example, we’ll consider that both stacks grow from the right. + +---------------------------------------------------------------------------------------------------------- +Init a and b: +2 +1 +3 +6 +5 +8 +_ _ +a b +---------------------------------------------------------------------------------------------------------- +Exec sa: +1 +2 +3 +6 +5 +8 +_ _ +a b +---------------------------------------------------------------------------------------------------------- +Exec pb pb pb: +6 3 +5 2 +8 1 +_ _ +a b +---------------------------------------------------------------------------------------------------------- +Exec ra rb (equiv. to rr): +5 2 +8 1 +6 3 +_ _ +a b +---------------------------------------------------------------------------------------------------------- +Exec rra rrb (equiv. to rrr): +6 3 +5 2 +8 1 +_ _ +a b +---------------------------------------------------------------------------------------------------------- +Exec sa: +5 3 +6 2 +8 1 +_ _ +a b +---------------------------------------------------------------------------------------------------------- +Exec pa pa pa: +1 +2 +3 +5 +6 +8 +_ _ +a b +---------------------------------------------------------------------------------------------------------- + + Integers from a get sorted in 12 instructions. Can you do better? + + + + + 9 +Push_swap Because Swap_push isn’t as natural + + +V.3 The "push_swap" program + + Program name push_swap + Turn in files Makefile, *.h, *.c + Makefile NAME, all, clean, fclean, re + Arguments stack a: A list of integers + External functs. + + • read, write, malloc, free, + exit + • ft_printf and any equivalent + YOU coded + + Libft authorized Yes + Description Sort stacks + + + + Your project must comply with the following rules: + + • You have to turn in a Makefile which will compile your source files. It must not + relink. + + • Global variables are forbidden. + + • You have to write a program named push_swap that takes as an argument the stack + a formatted as a list of integers. The first argument should be at the top of the + stack (be careful about the order). + + • The program must display the smallest list of instructions possible to sort the stack + a, the smallest number being at the top. + + • Instructions must be separated by a ’\n’ and nothing else. + + • The goal is to sort the stack with the lowest possible number of operations. During + the evaluation process, the number of instructions found by your program will be + compared against a limit: the maximum number of operations tolerated. If your + program either displays a longer list or if the numbers aren’t sorted properly, your + grade will be 0. + + • If no parameters are specified, the program must not display anything and give the + prompt back. + + • In case of error, it must display "Error" followed by a ’\n’ on the standard error. + Errors include for example: some arguments aren’t integers, some arguments are + bigger than an integer and/or there are duplicates. + + + + + 10 +Push_swap Because Swap_push isn’t as natural + + +$>./push_swap 2 1 3 6 5 8 +sa +pb +pb +pb +sa +pa +pa +pa +$>./push_swap 0 one 2 3 +Error +$> + + + + During the evaluation process, a binary will be provided in order to properly check +your program. + + It will work as follows: + +$>ARG="4 67 3 87 23"; ./push_swap $ARG | wc -l + 6 +$>ARG="4 67 3 87 23"; ./push_swap $ARG | ./checker_OS $ARG +OK +$> + + + + If the program checker_OS displays "KO", it means that your push_swap came up +with a list of instructions that doesn’t sort the numbers. + + + The checker_OS program is available in the resources of the project + in the intranet. + You can find a description of how it works in the Bonus Part of this + document. + + + + + 11 +Chapter VI + +Bonus part + +This project leaves little room for adding extra features due to its simplicity. However, +how about creating your own checker? + + + Thanks to the checker program, you will be able to check whether + the list of instructions generated by the push_swap program actually + sorts the stack properly. + + + + +VI.1 The "checker" program + + Program name checker + Turn in files *.h, *.c + Makefile bonus + Arguments stack a: A list of integers + External functs. + + • read, write, malloc, free, + exit + • ft_printf and any equivalent + YOU coded + + Libft authorized Yes + Description Execute the sorting instructions + + • Write a program named checker that takes as an argument the stack a formatted + as a list of integers. The first argument should be at the top of the stack (be careful + about the order). If no argument is given, it stops and displays nothing. + • It will then wait and read instructions on the standard input, each instruction will + be followed by ’\n’. Once all the instructions have been read, the program has to + execute them on the stack received as an argument. + + 12 +Push_swap Because Swap_push isn’t as natural + + + • If after executing those instructions, the stack a is actually sorted and the stack b + is empty, then the program must display "OK" followed by a ’\n’ on the standard + output. + + • In every other case, it must display "KO" followed by a ’\n’ on the standard output. + + • In case of error, you must display "Error" followed by a ’\n’ on the standard er- + ror. Errors include for example: some arguments are not integers, some arguments + are bigger than an integer, there are duplicates, an instruction doesn’t exist and/or + is incorrectly formatted. + + +$>./checker 3 2 1 0 +rra +pb +sa +rra +pa +OK +$>./checker 3 2 1 0 +sa +rra +pb +KO +$>./checker 3 2 one 0 +Error +$>./checker "" 1 +Error +$> + + + + + You DO NOT have to reproduce the exact same behavior as the provided + binary. It is mandatory to manage errors but it is up to you to + decide how you want to parse the arguments. + + + + + The bonus part will only be assessed if the mandatory part is + PERFECT. Perfect means the mandatory part has been integrally done + and works without malfunctioning. If you have not passed ALL the + mandatory requirements, your bonus part will not be evaluated at all. + + + + + 13 +Chapter VII + +Submission and peer-evaluation + +Turn in your assignment in your Git repository as usual. Only the work inside your repos- +itory will be evaluated during the defense. Don’t hesitate to double check the names of +your files to ensure they are correct. + + As these assignments are not verified by a program, feel free to organize your files as +you wish, as long as you turn in the mandatory files and comply with the requirements. + + + + + file.bfe:VABB7yO9xm7xWXROeASsmsgnY0o0sDMJev7zFHhwQS8mvM8V5xQQp + Lc6cDCFXDWTiFzZ2H9skYkiJ/DpQtnM/uZ0 + + + + + 14 From 3462169a6fba5d01a90868177c9aff4ceae4f11e Mon Sep 17 00:00:00 2001 From: Maix0 Date: Fri, 12 Jan 2024 18:55:14 +0100 Subject: [PATCH 02/29] removed executable --- .gitignore | 1 + push_swap | Bin 98944 -> 0 bytes 2 files changed, 1 insertion(+) delete mode 100755 push_swap diff --git a/.gitignore b/.gitignore index 5ce2fd6..6bd3e47 100644 --- a/.gitignore +++ b/.gitignore @@ -55,3 +55,4 @@ dkms.conf .direnv test output/ +push_swap diff --git a/push_swap b/push_swap deleted file mode 100755 index 865eaa529bcb0321329fe083a7e918e83e7d7755..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 98944 zcmb<-^>JfjWMqH=CI&kO5buD116T+`GB7k$f<(bW3=Rwy47>~u3@QxD3~USx3@i)` z3@~*LP!^27!35C*qd6d424<)}2@r#UfkA>9VgihofvSViFxx>i$Udkvip?M~0d^3{ zzyPBe1R#8nK2{JD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|BTq4i-p1hXZPX0yMxu_JWuo8l+MJL@+Qgfck|X zt6=tPutM}}KnE^h^D$86py^o{6HFH}FfjZ_a{oH0{&i6OFneI`huH(8L0eS*A?ZH> z)qeu2AL1GY29Udv-4C{$fq|hHJdcb>e;jNO9UP$T#taM$22h1C`$6V_gkbKP4%H7E z53qphw}9%u0a6T2V<0`~`WHgeKU6uQe+wPp1mzE?5nu`$Enqf?0H2=0zyLmm5X4u2 z_Ky{y`eEZZAUP0*3NygyIeASIwY02YJIv-N>_;B*IJK}eW>7=53C z0W^96IwT6L0JaW@K*BXL191z7cKRzUOzK`6KuG%f=G`?YSD From 54a5b658d4f8395e5eb5043cee92e124ae6f3b83 Mon Sep 17 00:00:00 2001 From: Maix0 Date: Fri, 12 Jan 2024 19:42:25 +0100 Subject: [PATCH 03/29] I have an LDS instead of a LIS... --- .envrc | 1 + flake.lock | 314 ++++++++++++++++++++++++++++++++++++++++++++++ src/app/lis/lis.c | 5 +- src/app/main.c | 7 +- 4 files changed, 321 insertions(+), 6 deletions(-) create mode 100644 .envrc create mode 100644 flake.lock diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..5b430eb --- /dev/null +++ b/flake.lock @@ -0,0 +1,314 @@ +{ + "nodes": { + "c_formatter_42": { + "inputs": { + "c_formatter_42_src": "c_formatter_42_src", + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + }, + "locked": { + "lastModified": 1700259489, + "narHash": "sha256-Ye2zwphDUY/eDjyFCPFt0r9RyqMr4ZQprCE4bydNdpw=", + "owner": "maix0", + "repo": "c_formatter_42-flake", + "rev": "3d4f6a40bd1edf9fdb4959051edc172473d9544d", + "type": "github" + }, + "original": { + "owner": "maix0", + "repo": "c_formatter_42-flake", + "type": "github" + } + }, + "c_formatter_42_src": { + "flake": false, + "locked": { + "lastModified": 1696506114, + "narHash": "sha256-jUScF2lAHhjTWOWZsIAocE6FN8+HG+kLdpbYsEA1SZs=", + "owner": "dawnbeen", + "repo": "c_formatter_42", + "rev": "ef91ff383966885374695c327fa6015f9cfbc364", + "type": "github" + }, + "original": { + "owner": "dawnbeen", + "repo": "c_formatter_42", + "type": "github" + } + }, + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1694529238, + "narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "ff7b65b44d01cf9ba6a71320833626af21126384", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "flake-utils_2": { + "inputs": { + "systems": "systems_2" + }, + "locked": { + "lastModified": 1701680307, + "narHash": "sha256-kAuep2h5ajznlPMD9rnQyffWG8EM/C73lejGofXvdM8=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "4022d587cbbfd70fe950c1e2083a02621806a725", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "flake-utils_3": { + "inputs": { + "systems": "systems_3" + }, + "locked": { + "lastModified": 1701680307, + "narHash": "sha256-kAuep2h5ajznlPMD9rnQyffWG8EM/C73lejGofXvdM8=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "4022d587cbbfd70fe950c1e2083a02621806a725", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "flake-utils_4": { + "inputs": { + "systems": "systems_4" + }, + "locked": { + "lastModified": 1681202837, + "narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "cfacdce06f30d2b68473a46042957675eebb3401", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "generic_c": { + "inputs": { + "flake-utils": "flake-utils_3", + "naersk": "naersk", + "nixpkgs": "nixpkgs_3", + "rust-overlay": "rust-overlay" + }, + "locked": { + "lastModified": 1702388541, + "narHash": "sha256-ExzIrROCDRXVnvbOs81aL9DKHm1OxlzVXQPpUTrB75c=", + "owner": "maix0", + "repo": "generic_c", + "rev": "52b5de885c015d3b84ca26c02c3684ca0e9b426d", + "type": "github" + }, + "original": { + "owner": "maix0", + "repo": "generic_c", + "type": "github" + } + }, + "naersk": { + "inputs": { + "nixpkgs": "nixpkgs_2" + }, + "locked": { + "lastModified": 1698420672, + "narHash": "sha256-/TdeHMPRjjdJub7p7+w55vyABrsJlt5QkznPYy55vKA=", + "owner": "nix-community", + "repo": "naersk", + "rev": "aeb58d5e8faead8980a807c840232697982d47b9", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "naersk", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1700108881, + "narHash": "sha256-+Lqybl8kj0+nD/IlAWPPG/RDTa47gff9nbei0u7BntE=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "7414e9ee0b3e9903c24d3379f577a417f0aae5f1", + "type": "github" + }, + "original": { + "id": "nixpkgs", + "type": "indirect" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1704842529, + "narHash": "sha256-OTeQA+F8d/Evad33JMfuXC89VMetQbsU4qcaePchGr4=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "eabe8d3eface69f5bb16c18f8662a702f50c20d5", + "type": "github" + }, + "original": { + "id": "nixpkgs", + "type": "indirect" + } + }, + "nixpkgs_3": { + "locked": { + "lastModified": 1702272962, + "narHash": "sha256-D+zHwkwPc6oYQ4G3A1HuadopqRwUY/JkMwHz1YF7j4Q=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "e97b3e4186bcadf0ef1b6be22b8558eab1cdeb5d", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_4": { + "locked": { + "lastModified": 1681358109, + "narHash": "sha256-eKyxW4OohHQx9Urxi7TQlFBTDWII+F+x2hklDOQPB50=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "96ba1c52e54e74c3197f4d43026b3f3d92e83ff9", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_5": { + "locked": { + "lastModified": 1704842529, + "narHash": "sha256-OTeQA+F8d/Evad33JMfuXC89VMetQbsU4qcaePchGr4=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "eabe8d3eface69f5bb16c18f8662a702f50c20d5", + "type": "github" + }, + "original": { + "id": "nixpkgs", + "type": "indirect" + } + }, + "root": { + "inputs": { + "c_formatter_42": "c_formatter_42", + "flake-utils": "flake-utils_2", + "generic_c": "generic_c", + "nixpkgs": "nixpkgs_5" + } + }, + "rust-overlay": { + "inputs": { + "flake-utils": "flake-utils_4", + "nixpkgs": "nixpkgs_4" + }, + "locked": { + "lastModified": 1702347444, + "narHash": "sha256-ueDw7aQf4Xyk69XnDD0YNWDlFdlOgJGPeWFa7uu/cfw=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "bc13176f27cf3be724d18924b4f6aa47686ca2e3", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "systems_2": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "systems_3": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "systems_4": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/src/app/lis/lis.c b/src/app/lis/lis.c index b8e85b3..2b3a409 100644 --- a/src/app/lis/lis.c +++ b/src/app/lis/lis.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 18:50:35 by maiboyer #+# #+# */ -/* Updated: 2024/01/11 21:27:36 by maiboyer ### ########.fr */ +/* Updated: 2024/01/12 19:41:23 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -164,7 +164,7 @@ t_i64 binary_search(t_vec_element *items, t_i64 i) t_i64 mid; lo = 0; - hi = items->len; + hi = items->len - 1; while (lo < hi) { mid = lo + (hi - lo) / 2; @@ -227,5 +227,6 @@ t_vec_i64 lis(t_vec_element *elements) result.buffer[u] = v; v = previous_chain.buffer[v]; } + vec_i64_free(previous_chain); return (result); } diff --git a/src/app/main.c b/src/app/main.c index 755a321..5daaf2f 100644 --- a/src/app/main.c +++ b/src/app/main.c @@ -6,14 +6,13 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 14:14:18 by maiboyer #+# #+# */ -/* Updated: 2024/01/11 21:03:29 by maiboyer ### ########.fr */ +/* Updated: 2024/01/12 19:28:15 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ #include "app/dup_state.h" #include "app/element.h" #include "app/lis.h" -#include "app/moves.h" #include "app/state.h" #include "me/convert/atoi.h" #include "me/convert/itoa.h" @@ -170,9 +169,9 @@ int main(t_i32 argc, t_str argv[]) state = parses_arguments(--argc, ++argv); print_state(&state); o = lis(&state.stack_a); - printf("len = %zu", o.len); + printf("len = %zu\n", o.len); for (t_usize i = 0; i < o.len; i++) - printf("%zi\n", (t_isize)o.buffer[i]); + printf("%lli\n", state.stack_a.buffer[o.buffer[i]].value); printf("\n"); free_state(state); vec_i64_free(o); From 33db892d3b45f78533440e0c0894855e2e89eee2 Mon Sep 17 00:00:00 2001 From: Maix0 Date: Sat, 13 Jan 2024 18:50:17 +0100 Subject: [PATCH 04/29] yes --- gen.list | 6 +++ include/app/cost.h | 32 +++++++++++++++ include/app/moves.h | 19 ++------- include/app/state.h | 4 +- include/app/types/type_cost.h | 26 ++++++++++++ include/app/types/type_move.h | 31 ++++++++++++++ input.toml | 16 ++++++++ src.list | 1 + src/app/cost.c | 76 +++++++++++++++++++++++++++++++++++ src/app/lis/lis.c | 11 ++--- src/app/main.c | 49 ++++++++++++++-------- 11 files changed, 233 insertions(+), 38 deletions(-) create mode 100644 include/app/cost.h create mode 100644 include/app/types/type_cost.h create mode 100644 include/app/types/type_move.h create mode 100644 src/app/cost.c diff --git a/gen.list b/gen.list index 7728ddd..7b3d9f6 100644 --- a/gen.list +++ b/gen.list @@ -1,6 +1,12 @@ +vec/vec_cost +vec/vec_cost_functions2 +vec/vec_cost_functions3 vec/vec_element vec/vec_element_functions2 vec/vec_element_functions3 vec/vec_i64 vec/vec_i64_functions2 vec/vec_i64_functions3 +vec/vec_moves +vec/vec_moves_functions2 +vec/vec_moves_functions3 diff --git a/include/app/cost.h b/include/app/cost.h new file mode 100644 index 0000000..407edc8 --- /dev/null +++ b/include/app/cost.h @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cost.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/12 21:13:51 by maiboyer #+# #+# */ +/* Updated: 2024/01/12 23:37:23 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef COST_H +#define COST_H + +#include "app/element.h" +#include "app/state.h" +#include "app/types/type_cost.h" +#include "me/types.h" + +t_cost alloc_cost(t_state *s); +void free_cost(t_cost self); +void calculate_cost(t_state *s); +void cost_for_index(t_state *s, t_usize index); + +typedef struct s_cost_iter_state +{ + t_cost *current_minimum; + t_usize max_index; +} t_cost_iter_state; + +#endif /* COST_H */ diff --git a/include/app/moves.h b/include/app/moves.h index 677abe7..4adfee6 100644 --- a/include/app/moves.h +++ b/include/app/moves.h @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 16:13:08 by maiboyer #+# #+# */ -/* Updated: 2024/01/11 16:18:49 by maiboyer ### ########.fr */ +/* Updated: 2024/01/12 21:27:27 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,22 +15,9 @@ #include "app/element.h" #include "app/state.h" -#include "me/types.h" +#include "app/types/type_move.h" -typedef enum e_move -{ - SWAP_A = 1 << 0, - SWAP_B = 1 << 1, - SWAP_BOTH = SWAP_A | SWAP_B, - PUSH_A = 1 << 2, - PUSH_B = 1 << 3, - ROTATE_A = 1 << 4, - ROTATE_B = 1 << 5, - ROTATE_BOTH = ROTATE_A | ROTATE_B, - REVERSE_ROTATE_A = 1 << 6, - REVERSE_ROTATE_B = 1 << 7, - REVERSE_ROTATE_BOTH = REVERSE_ROTATE_A | REVERSE_ROTATE_B, -} t_move; +#include "me/types.h" void do_move(t_move m, t_state *s); diff --git a/include/app/state.h b/include/app/state.h index 15d0529..94b7bf3 100644 --- a/include/app/state.h +++ b/include/app/state.h @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 14:27:25 by maiboyer #+# #+# */ -/* Updated: 2024/01/11 14:36:28 by maiboyer ### ########.fr */ +/* Updated: 2024/01/12 21:16:59 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,11 +15,13 @@ #include "me/types.h" #include "me/vec/vec_element.h" +#include "me/vec/vec_cost.h" typedef struct s_state { t_vec_element stack_a; t_vec_element stack_b; + t_vec_cost costs; } t_state; t_state parses_arguments(t_usize count, t_str nums[]); diff --git a/include/app/types/type_cost.h b/include/app/types/type_cost.h new file mode 100644 index 0000000..87de305 --- /dev/null +++ b/include/app/types/type_cost.h @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* type_cost.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/12 21:20:00 by maiboyer #+# #+# */ +/* Updated: 2024/01/12 23:16:24 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef TYPE_COST_H +#define TYPE_COST_H + +#include "me/types.h" +#include "me/vec/vec_moves.h" + +typedef struct s_cost +{ + t_usize index; + t_vec_moves moves; + bool active; +} t_cost; + +#endif /* TYPE_COST_H */ diff --git a/include/app/types/type_move.h b/include/app/types/type_move.h new file mode 100644 index 0000000..9ab4650 --- /dev/null +++ b/include/app/types/type_move.h @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* type_move.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/12 21:26:56 by maiboyer #+# #+# */ +/* Updated: 2024/01/12 21:27:08 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef TYPE_MOVE_H +#define TYPE_MOVE_H + +typedef enum e_move +{ + SWAP_A = 1 << 0, + SWAP_B = 1 << 1, + SWAP_BOTH = SWAP_A | SWAP_B, + PUSH_A = 1 << 2, + PUSH_B = 1 << 3, + ROTATE_A = 1 << 4, + ROTATE_B = 1 << 5, + ROTATE_BOTH = ROTATE_A | ROTATE_B, + REVERSE_ROTATE_A = 1 << 6, + REVERSE_ROTATE_B = 1 << 7, + REVERSE_ROTATE_BOTH = REVERSE_ROTATE_A | REVERSE_ROTATE_B, +} t_move; + +#endif /* TYPE_MOVE_H */ diff --git a/input.toml b/input.toml index 9371862..dfae69b 100644 --- a/input.toml +++ b/input.toml @@ -38,3 +38,19 @@ replace.C__TYPENAME__ = "t_i64" replace.C__TYPEHEADER__ = '' replace.C__PREFIX__ = "i64" replace.C__PREFIXUP__ = "I64" + +[[create.vec]] +sources_output = "src/vec/" +headers_output = "include/me/vec/" +replace.C__TYPENAME__ = "t_cost" +replace.C__TYPEHEADER__ = '#include "app/types/type_cost.h"' +replace.C__PREFIX__ = "cost" +replace.C__PREFIXUP__ = "COST" + +[[create.vec]] +sources_output = "src/vec/" +headers_output = "include/me/vec/" +replace.C__TYPENAME__ = "t_move" +replace.C__TYPEHEADER__ = '#include "app/types/type_move.h"' +replace.C__PREFIX__ = "moves" +replace.C__PREFIXUP__ = "MOVES" diff --git a/src.list b/src.list index 1fef610..a865e49 100644 --- a/src.list +++ b/src.list @@ -1,3 +1,4 @@ +app/cost app/lis/lis app/lis/lower_bound app/main diff --git a/src/app/cost.c b/src/app/cost.c new file mode 100644 index 0000000..c70a865 --- /dev/null +++ b/src/app/cost.c @@ -0,0 +1,76 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cost.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/12 21:22:44 by maiboyer #+# #+# */ +/* Updated: 2024/01/13 00:58:47 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/cost.h" +#include "app/state.h" +#include "me/types.h" +#include "me/vec/vec_cost.h" +#include "me/vec/vec_moves.h" + +void reset_cost(t_usize _index, t_cost *c, void *_state) +{ + (void)(_index + (t_usize)_state); + c->active = false; + c->moves.len = 0; +} + +void find_minimum(t_usize index, t_cost *c, t_cost_iter_state *s) +{ + if (index >= s->max_index || !c->active) + return; + if (c->moves.len < s->current_minimum->moves.len) + s->current_minimum = c; +} + +void cost_for_index(t_state *s, t_usize index) +{ + +} + +void free_cost(t_cost self) +{ + vec_moves_free(self.moves); +} + +t_cost alloc_cost(t_state *s) +{ + t_cost out; + + out = (t_cost){}; + out.active = false; + out.index = s->costs.len; + out.moves = vec_moves_new(s->stack_a.len, NULL); + return (out); +} + +void calculate_cost(t_state *s) +{ + t_vec_cost *costs; + t_cost *cur; + t_usize index; + t_cost_iter_state iter_state; + + costs = &s->costs; + vec_cost_iter(costs, &reset_cost, NULL); + index = 0; + while (index > costs->len) + { + cur = &costs->buffer[index]; + cost_for_index(s, index); + if (cur->moves.len == 1) + break; + index++; + } + iter_state = (t_cost_iter_state){.current_minimum = costs->buffer, + .max_index = index}; + vec_cost_iter(costs, (void (*)())find_minimum, &iter_state); +} diff --git a/src/app/lis/lis.c b/src/app/lis/lis.c index 2b3a409..f3e72cd 100644 --- a/src/app/lis/lis.c +++ b/src/app/lis/lis.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 18:50:35 by maiboyer #+# #+# */ -/* Updated: 2024/01/12 19:41:23 by maiboyer ### ########.fr */ +/* Updated: 2024/01/12 20:42:48 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -185,16 +185,16 @@ void lis_inner(t_vec_element *elements, t_vec_i64 *result, i = 1; while (i < (t_i64)elements->len) { - if (elements->buffer[result->buffer[result->len - 1]].value < - elements->buffer[i].value) + if (-elements->buffer[result->buffer[result->len - 1]].value < + -elements->buffer[i].value) { previous_chain->buffer[i] = result->buffer[result->len - 1]; vec_i64_push(result, i++); continue; } next_element_index = binary_search(elements, i); - if (elements->buffer[i].value > - elements->buffer[result->buffer[next_element_index]].value) + if (-elements->buffer[i].value > + -elements->buffer[result->buffer[next_element_index]].value) { if (next_element_index > 0) previous_chain->buffer[i] = @@ -228,5 +228,6 @@ t_vec_i64 lis(t_vec_element *elements) v = previous_chain.buffer[v]; } vec_i64_free(previous_chain); + vec_i64_reverse(&result); return (result); } diff --git a/src/app/main.c b/src/app/main.c index 5daaf2f..8ebb919 100644 --- a/src/app/main.c +++ b/src/app/main.c @@ -6,10 +6,11 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 14:14:18 by maiboyer #+# #+# */ -/* Updated: 2024/01/12 19:28:15 by maiboyer ### ########.fr */ +/* Updated: 2024/01/12 23:41:30 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ +#include "app/cost.h" #include "app/dup_state.h" #include "app/element.h" #include "app/lis.h" @@ -19,12 +20,12 @@ #include "me/printf/printf.h" #include "me/string/str_clone.h" #include "me/types.h" +#include "me/vec/vec_cost.h" #include "me/vec/vec_element.h" #include #include #include #include -#include void free_element(t_element elem) { @@ -35,6 +36,7 @@ void free_state(t_state state) { vec_element_free(state.stack_a); vec_element_free(state.stack_b); + vec_cost_free(state.costs); } t_error parse_element(t_str s, t_element *e) @@ -59,7 +61,7 @@ t_error parse_element(t_str s, t_element *e) *e = out; return (NO_ERROR); } -void check_eq(t_usize i, t_element *rhs, t_dup_state *dup_state) +void check_eq(t_usize i, const t_element *rhs, t_dup_state *dup_state) { dup_state->found_dup = dup_state->found_dup || @@ -96,8 +98,8 @@ t_state parses_arguments(t_usize count, t_str nums[]) t_element e; i = 0; - out.stack_a = vec_element_new(count * 2, free_element); - out.stack_b = vec_element_new(count * 2, free_element); + out.stack_a = vec_element_new(count + 1, free_element); + out.stack_b = vec_element_new(count + 1, free_element); while (i < count) { if (parse_element(nums[i], &e)) @@ -106,11 +108,10 @@ t_state parses_arguments(t_usize count, t_str nums[]) nums[i]), vec_element_free(out.stack_a), vec_element_free(out.stack_b), exit(1)); - vec_element_push(&out.stack_b, e); + vec_element_push(&out.stack_a, e); i++; } - while (!vec_element_pop(&out.stack_b, &e)) - vec_element_push(&out.stack_a, e); + vec_element_reverse(&out.stack_a); check_no_duplicate(&out.stack_a, &out.stack_b); return out; } @@ -130,6 +131,23 @@ t_state parses_arguments(t_usize count, t_str nums[]) #define STR_CENTER(str, width) LEFT(str, width), str, RIGHT(str, width), "" #define STACK_WIDTH 12 + +bool check_sorted(t_state *s) +{ + t_usize not_in_order; + t_usize i; + + not_in_order = 0; + i = 0; + while (i + 1 < s->stack_a.len) + { + if (s->stack_a.buffer[i].value > s->stack_a.buffer[i].value) + not_in_order++; + i++; + } + return (not_in_order == 0); +} + void print_state(t_state *s) { t_usize max_len; @@ -163,17 +181,16 @@ void print_state(t_state *s) int main(t_i32 argc, t_str argv[]) { - t_state state; - t_vec_i64 o; + t_state state; state = parses_arguments(--argc, ++argv); + state.costs = vec_cost_new(state.stack_a.len, &free_cost); + while (state.costs.len < state.stack_a.len) + vec_cost_push(&state.costs, alloc_cost(&state)); print_state(&state); - o = lis(&state.stack_a); - printf("len = %zu\n", o.len); - for (t_usize i = 0; i < o.len; i++) - printf("%lli\n", state.stack_a.buffer[o.buffer[i]].value); - printf("\n"); + while (!check_sorted(&state)) + { + } free_state(state); - vec_i64_free(o); return (0); } From b16030f661151631a3f1578f04f62c4305a892d4 Mon Sep 17 00:00:00 2001 From: Maix0 Date: Sat, 13 Jan 2024 18:51:47 +0100 Subject: [PATCH 05/29] update --- mecstd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mecstd b/mecstd index f9c81ca..b032e9d 160000 --- a/mecstd +++ b/mecstd @@ -1 +1 @@ -Subproject commit f9c81caf38361c927fa81f72a1e832bbb619e161 +Subproject commit b032e9dcc8c869b4759958e50597cef690353ccc From 63d62aec4d96a29ccdcc7f6b22e2e9b6a022747c Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Tue, 23 Jan 2024 13:19:18 +0100 Subject: [PATCH 06/29] update --- include/app/cost.h | 26 +++++------ include/app/dup_state.h | 14 +++--- include/app/element.h | 10 ++--- include/app/lis.h | 10 ++--- include/app/moves.h | 13 +++--- include/app/state.h | 20 ++++----- include/app/types/type_cost.h | 10 ++--- include/app/types/type_move.h | 4 +- src/app/cost.c | 63 +++++++++++++++++++------- src/app/lis/lis.c | 38 ++++++++-------- src/app/main.c | 85 +++++++++++++++++------------------ src/app/moves.c | 20 ++++----- src/app/moves/push.c | 12 ++--- src/app/moves/rev_rotate.c | 10 ++--- src/app/moves/rotate.c | 11 +++-- src/app/moves/swap.c | 12 ++--- 16 files changed, 193 insertions(+), 165 deletions(-) diff --git a/include/app/cost.h b/include/app/cost.h index 407edc8..073a603 100644 --- a/include/app/cost.h +++ b/include/app/cost.h @@ -6,27 +6,27 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/12 21:13:51 by maiboyer #+# #+# */ -/* Updated: 2024/01/12 23:37:23 by maiboyer ### ########.fr */ +/* Updated: 2024/01/17 15:32:25 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef COST_H -#define COST_H +# define COST_H -#include "app/element.h" -#include "app/state.h" -#include "app/types/type_cost.h" -#include "me/types.h" +# include "app/element.h" +# include "app/state.h" +# include "app/types/type_cost.h" +# include "me/types.h" -t_cost alloc_cost(t_state *s); -void free_cost(t_cost self); -void calculate_cost(t_state *s); -void cost_for_index(t_state *s, t_usize index); +t_cost alloc_cost(t_state *s); +void free_cost(t_cost self); +t_cost *calculate_cost(t_state *s); +void cost_for_index(t_state *s, t_usize index); typedef struct s_cost_iter_state { - t_cost *current_minimum; - t_usize max_index; -} t_cost_iter_state; + t_cost *current_minimum; + t_usize max_index; +} t_cost_iter_state; #endif /* COST_H */ diff --git a/include/app/dup_state.h b/include/app/dup_state.h index 69abfeb..f163dfd 100644 --- a/include/app/dup_state.h +++ b/include/app/dup_state.h @@ -11,16 +11,16 @@ /* ************************************************************************** */ #ifndef DUP_STATE_H -#define DUP_STATE_H +# define DUP_STATE_H -#include "app/element.h" -#include "me/types.h" +# include "app/element.h" +# include "me/types.h" typedef struct s_dup_state { - t_element *lhs; - t_usize index_to_skip; - bool found_dup; -} t_dup_state; + t_element *lhs; + t_usize index_to_skip; + bool found_dup; +} t_dup_state; #endif /* DUP_STATE_H */ diff --git a/include/app/element.h b/include/app/element.h index 74d625f..cd8855f 100644 --- a/include/app/element.h +++ b/include/app/element.h @@ -11,15 +11,15 @@ /* ************************************************************************** */ #ifndef ELEMENT_H -#define ELEMENT_H +# define ELEMENT_H -#include "me/types.h" +# include "me/types.h" typedef struct s_element { - t_i64 value; -} t_element; + t_i64 value; +} t_element; -void free_element(t_element elem); +void free_element(t_element elem); #endif /* ELEMENT_H */ diff --git a/include/app/lis.h b/include/app/lis.h index c1f757b..c44310b 100644 --- a/include/app/lis.h +++ b/include/app/lis.h @@ -11,12 +11,12 @@ /* ************************************************************************** */ #ifndef LIS_H -#define LIS_H +# define LIS_H -#include "me/types.h" -#include "me/vec/vec_element.h" -#include "me/vec/vec_i64.h" +# include "me/types.h" +# include "me/vec/vec_element.h" +# include "me/vec/vec_i64.h" -t_vec_i64 lis(t_vec_element *elements); +t_vec_i64 lis(t_vec_element *elements); #endif /* LIS_H */ diff --git a/include/app/moves.h b/include/app/moves.h index 4adfee6..4688577 100644 --- a/include/app/moves.h +++ b/include/app/moves.h @@ -11,14 +11,13 @@ /* ************************************************************************** */ #ifndef MOVES_H -#define MOVES_H +# define MOVES_H -#include "app/element.h" -#include "app/state.h" -#include "app/types/type_move.h" +# include "app/element.h" +# include "app/state.h" +# include "app/types/type_move.h" +# include "me/types.h" -#include "me/types.h" - -void do_move(t_move m, t_state *s); +void do_move(t_move m, t_state *s); #endif /* MOVES_H */ diff --git a/include/app/state.h b/include/app/state.h index 94b7bf3..65bc31e 100644 --- a/include/app/state.h +++ b/include/app/state.h @@ -11,20 +11,20 @@ /* ************************************************************************** */ #ifndef STATE_H -#define STATE_H +# define STATE_H -#include "me/types.h" -#include "me/vec/vec_element.h" -#include "me/vec/vec_cost.h" +# include "me/types.h" +# include "me/vec/vec_cost.h" +# include "me/vec/vec_element.h" typedef struct s_state { - t_vec_element stack_a; - t_vec_element stack_b; - t_vec_cost costs; -} t_state; + t_vec_element stack_a; + t_vec_element stack_b; + t_vec_cost costs; +} t_state; -t_state parses_arguments(t_usize count, t_str nums[]); -void free_state(t_state state); +t_state parses_arguments(t_usize count, t_str nums[]); +void free_state(t_state state); #endif /* STATE_H */ diff --git a/include/app/types/type_cost.h b/include/app/types/type_cost.h index 87de305..2efbb54 100644 --- a/include/app/types/type_cost.h +++ b/include/app/types/type_cost.h @@ -11,16 +11,16 @@ /* ************************************************************************** */ #ifndef TYPE_COST_H -#define TYPE_COST_H +# define TYPE_COST_H -#include "me/types.h" -#include "me/vec/vec_moves.h" +# include "me/types.h" +# include "me/vec/vec_moves.h" typedef struct s_cost { t_usize index; - t_vec_moves moves; + t_vec_moves moves; bool active; -} t_cost; +} t_cost; #endif /* TYPE_COST_H */ diff --git a/include/app/types/type_move.h b/include/app/types/type_move.h index 9ab4650..761a82a 100644 --- a/include/app/types/type_move.h +++ b/include/app/types/type_move.h @@ -11,7 +11,7 @@ /* ************************************************************************** */ #ifndef TYPE_MOVE_H -#define TYPE_MOVE_H +# define TYPE_MOVE_H typedef enum e_move { @@ -26,6 +26,6 @@ typedef enum e_move REVERSE_ROTATE_A = 1 << 6, REVERSE_ROTATE_B = 1 << 7, REVERSE_ROTATE_BOTH = REVERSE_ROTATE_A | REVERSE_ROTATE_B, -} t_move; +} t_move; #endif /* TYPE_MOVE_H */ diff --git a/src/app/cost.c b/src/app/cost.c index c70a865..ec71f30 100644 --- a/src/app/cost.c +++ b/src/app/cost.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/12 21:22:44 by maiboyer #+# #+# */ -/* Updated: 2024/01/13 00:58:47 by maiboyer ### ########.fr */ +/* Updated: 2024/01/18 17:01:34 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,35 +16,65 @@ #include "me/vec/vec_cost.h" #include "me/vec/vec_moves.h" -void reset_cost(t_usize _index, t_cost *c, void *_state) +void reset_cost(t_usize _index, t_cost *c, void *_state) { (void)(_index + (t_usize)_state); c->active = false; c->moves.len = 0; } -void find_minimum(t_usize index, t_cost *c, t_cost_iter_state *s) +void find_minimum(t_usize index, t_cost *c, t_cost_iter_state *s) { if (index >= s->max_index || !c->active) - return; + return ; if (c->moves.len < s->current_minimum->moves.len) s->current_minimum = c; } - -void cost_for_index(t_state *s, t_usize index) +typedef struct s_max_iter_state { + t_usize current_index; + t_vec_element *v; +} t_max_iter_state; +void find_maximum_iter(t_usize index, t_element *c, t_max_iter_state *s) +{ + if (s->v->buffer[s->current_index].value < c->value) + s->current_index = index; } -void free_cost(t_cost self) +static t_usize find_max(t_vec_element *v) +{ + t_max_iter_state state; + + state.v = v; + state.current_index = 0; + vec_element_iter(v, (void (*)())find_maximum_iter, &state); + return (state.current_index); +} + +void cost_for_index(t_state *s, t_usize index) +{ + t_usize rotate_a; + t_usize rotate_b; + t_element *elem; + t_usize max_index; + + rotate_a = 0; + rotate_b = 0; + elem = &s->stack_a.buffer[index]; + max_index = find_max(&s->stack_b); +} + +void free_cost(t_cost self) { vec_moves_free(self.moves); } -t_cost alloc_cost(t_state *s) +t_cost alloc_cost(t_state *s) { - t_cost out; + t_cost out; + (void)(s); out = (t_cost){}; out.active = false; out.index = s->costs.len; @@ -52,12 +82,12 @@ t_cost alloc_cost(t_state *s) return (out); } -void calculate_cost(t_state *s) +t_cost *calculate_cost(t_state *s) { - t_vec_cost *costs; - t_cost *cur; - t_usize index; - t_cost_iter_state iter_state; + t_vec_cost *costs; + t_cost *cur; + t_usize index; + t_cost_iter_state iter_state; costs = &s->costs; vec_cost_iter(costs, &reset_cost, NULL); @@ -67,10 +97,11 @@ void calculate_cost(t_state *s) cur = &costs->buffer[index]; cost_for_index(s, index); if (cur->moves.len == 1) - break; + break ; index++; } iter_state = (t_cost_iter_state){.current_minimum = costs->buffer, - .max_index = index}; + .max_index = index}; vec_cost_iter(costs, (void (*)())find_minimum, &iter_state); + return (iter_state.current_minimum); } diff --git a/src/app/lis/lis.c b/src/app/lis/lis.c index f3e72cd..53b7418 100644 --- a/src/app/lis/lis.c +++ b/src/app/lis/lis.c @@ -68,7 +68,7 @@ return S //} // static t_vec_i64 lis_inner(t_vec_element *elements, t_i64 L, t_i64 m[], -// t_i64 p[]) +// t_i64 p[]) //{ // } @@ -88,7 +88,7 @@ http://www.algorithmist.com/index.php?title=Longest_Increasing_Subsequence.cpp&o // If `items` is empty, then the result is also empty. if items.is_empty() { - return result; + return (result); } // This vector stores, for each item, @@ -108,7 +108,7 @@ subsequence, if selector(&items[*result.last().unwrap()]) < selector(&items[i]) { previous_chain[i] = *result.last().unwrap(); result.push(i); - continue; + continue ; } // Perform a binary search to find the index of an item in `result` to @@ -157,11 +157,11 @@ is larger than `items[i]`. } */ -t_i64 binary_search(t_vec_element *items, t_i64 i) +t_i64 binary_search(t_vec_element *items, t_i64 i) { - t_i64 lo; - t_i64 hi; - t_i64 mid; + t_i64 lo; + t_i64 hi; + t_i64 mid; lo = 0; hi = items->len - 1; @@ -176,11 +176,11 @@ t_i64 binary_search(t_vec_element *items, t_i64 i) return (lo); } -void lis_inner(t_vec_element *elements, t_vec_i64 *result, - t_vec_i64 *previous_chain) +void lis_inner(t_vec_element *elements, t_vec_i64 *result, + t_vec_i64 *previous_chain) { - t_i64 i; - t_i64 next_element_index; + t_i64 i; + t_i64 next_element_index; i = 1; while (i < (t_i64)elements->len) @@ -190,27 +190,27 @@ void lis_inner(t_vec_element *elements, t_vec_i64 *result, { previous_chain->buffer[i] = result->buffer[result->len - 1]; vec_i64_push(result, i++); - continue; + continue ; } next_element_index = binary_search(elements, i); if (-elements->buffer[i].value > -elements->buffer[result->buffer[next_element_index]].value) { if (next_element_index > 0) - previous_chain->buffer[i] = - result->buffer[next_element_index - 1]; + previous_chain->buffer[i] = result->buffer[next_element_index + - 1]; result->buffer[next_element_index] = i; } i++; } } -t_vec_i64 lis(t_vec_element *elements) +t_vec_i64 lis(t_vec_element *elements) { - t_vec_i64 result; - t_i64 u; - t_i64 v; - t_vec_i64 previous_chain; + t_vec_i64 result; + t_i64 u; + t_i64 v; + t_vec_i64 previous_chain; result = vec_i64_new(elements->len, NULL); if (elements->len == 0) diff --git a/src/app/main.c b/src/app/main.c index 8ebb919..9f9a5d1 100644 --- a/src/app/main.c +++ b/src/app/main.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 14:14:18 by maiboyer #+# #+# */ -/* Updated: 2024/01/12 23:41:30 by maiboyer ### ########.fr */ +/* Updated: 2024/01/18 13:45:26 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,23 +27,23 @@ #include #include -void free_element(t_element elem) +void free_element(t_element elem) { (void)(elem); } -void free_state(t_state state) +void free_state(t_state state) { vec_element_free(state.stack_a); vec_element_free(state.stack_b); vec_cost_free(state.costs); } -t_error parse_element(t_str s, t_element *e) +t_error parse_element(t_str s, t_element *e) { - t_usize i; - t_i64 value; - t_element out; + t_usize i; + t_i64 value; + t_element out; if (s == NULL || e == NULL) return (ERROR); @@ -61,17 +61,17 @@ t_error parse_element(t_str s, t_element *e) *e = out; return (NO_ERROR); } -void check_eq(t_usize i, const t_element *rhs, t_dup_state *dup_state) +void check_eq(t_usize i, const t_element *rhs, t_dup_state *dup_state) { - dup_state->found_dup = - dup_state->found_dup || - (i != dup_state->index_to_skip && rhs->value == dup_state->lhs->value); + dup_state->found_dup = dup_state->found_dup + || (i != dup_state->index_to_skip + && rhs->value == dup_state->lhs->value); } -void check_no_duplicate(t_vec_element *stack, t_vec_element *to_free_in_need) +void check_no_duplicate(t_vec_element *stack, t_vec_element *to_free_in_need) { t_usize i; - t_dup_state s; + t_dup_state s; i = 0; while (i < stack->len) @@ -84,18 +84,17 @@ void check_no_duplicate(t_vec_element *stack, t_vec_element *to_free_in_need) vec_element_iter(stack, (void (*)())check_eq, &s); if (s.found_dup) return (me_eprintf("Error:\nDuplicate numbers (%i)\n", - stack->buffer[i]), - vec_element_free(*stack), - vec_element_free(*to_free_in_need), exit(1)); + stack->buffer[i]), vec_element_free(*stack), + vec_element_free(*to_free_in_need), exit(1)); i++; } } -t_state parses_arguments(t_usize count, t_str nums[]) +t_state parses_arguments(t_usize count, t_str nums[]) { - t_state out; - t_usize i; - t_element e; + t_state out; + t_usize i; + t_element e; i = 0; out.stack_a = vec_element_new(count + 1, free_element); @@ -106,36 +105,37 @@ t_state parses_arguments(t_usize count, t_str nums[]) (me_eprintf("Error:\nArgument '%s' isn't a valid number for this " "program!\n", nums[i]), - vec_element_free(out.stack_a), vec_element_free(out.stack_b), - exit(1)); + vec_element_free(out.stack_a), + vec_element_free(out.stack_b), + exit(1)); vec_element_push(&out.stack_a, e); i++; } vec_element_reverse(&out.stack_a); check_no_duplicate(&out.stack_a, &out.stack_b); - return out; + return (out); } -#define LEFT(str, w) \ - ({ \ - int m = w + strlen(str); \ - m % 2 ? (m + 1) / 2 : m / 2; \ +#define LEFT(str, w) \ + ({ \ + int m = w + strlen(str); \ + m % 2 ? (m + 1) / 2 : m / 2; \ }) -#define RIGHT(str, w) \ - ({ \ - int m = w - strlen(str); \ - m % 2 ? (m - 1) / 2 : m / 2; \ +#define RIGHT(str, w) \ + ({ \ + int m = w - strlen(str); \ + m % 2 ? (m - 1) / 2 : m / 2; \ }) #define STR_CENTER(str, width) LEFT(str, width), str, RIGHT(str, width), "" #define STACK_WIDTH 12 -bool check_sorted(t_state *s) +bool check_sorted(t_state *s) { - t_usize not_in_order; - t_usize i; + t_usize not_in_order; + t_usize i; not_in_order = 0; i = 0; @@ -148,21 +148,20 @@ bool check_sorted(t_state *s) return (not_in_order == 0); } -void print_state(t_state *s) +void print_state(t_state *s) { - t_usize max_len; + t_usize max_len; + t_str s_a; + t_str s_b; if (s->stack_a.len > s->stack_b.len) max_len = s->stack_a.len; else max_len = s->stack_b.len; printf("|%*s%*s|%*s%*s|\n", STR_CENTER("Stack A", STACK_WIDTH), - STR_CENTER("Stack B", STACK_WIDTH)); + STR_CENTER("Stack B", STACK_WIDTH)); for (t_usize i = 0; i < max_len; i++) { - t_str s_a; - t_str s_b; - if (i < s->stack_a.len) s_a = me_itoa(s->stack_a.buffer[s->stack_a.len - i - 1].value); else @@ -172,16 +171,16 @@ void print_state(t_state *s) else s_b = str_clone(""); printf("|%*s%*s|%*s%*s|\n", STR_CENTER(s_a, STACK_WIDTH), - STR_CENTER(s_b, STACK_WIDTH)); + STR_CENTER(s_b, STACK_WIDTH)); free(s_a); free(s_b); } printf("\n"); } -int main(t_i32 argc, t_str argv[]) +int main(t_i32 argc, t_str argv[]) { - t_state state; + t_state state; state = parses_arguments(--argc, ++argv); state.costs = vec_cost_new(state.stack_a.len, &free_cost); diff --git a/src/app/moves.c b/src/app/moves.c index a24428c..d314a7c 100644 --- a/src/app/moves.c +++ b/src/app/moves.c @@ -10,20 +10,20 @@ /* */ /* ************************************************************************** */ -#include "app/moves.h" #include "app/element.h" +#include "app/moves.h" #include "app/state.h" -void push_a(t_state *s); -void push_b(t_state *s); -void swap_a(t_state *s); -void swap_b(t_state *s); -void rotate_a(t_state *s); -void rotate_b(t_state *s); -void rev_rotate_a(t_state *s); -void rev_rotate_b(t_state *s); +void push_a(t_state *s); +void push_b(t_state *s); +void swap_a(t_state *s); +void swap_b(t_state *s); +void rotate_a(t_state *s); +void rotate_b(t_state *s); +void rev_rotate_a(t_state *s); +void rev_rotate_b(t_state *s); -void do_move(t_move m, t_state *s) +void do_move(t_move m, t_state *s) { if (m & PUSH_A) push_a(s); diff --git a/src/app/moves/push.c b/src/app/moves/push.c index 26379bf..85453be 100644 --- a/src/app/moves/push.c +++ b/src/app/moves/push.c @@ -14,24 +14,24 @@ #include "app/state.h" #include "me/vec/vec_element.h" -static inline void push_inner(t_vec_element *to, t_vec_element *from, - t_const_str tag) +static inline void push_inner(t_vec_element *to, t_vec_element *from, + t_const_str tag) { - t_element e; + t_element e; (void)(tag); if (from->len == 0) - return; + return ; vec_element_pop(from, &e); vec_element_push(to, e); } -void push_a(t_state *s) +void push_a(t_state *s) { push_inner(&s->stack_a, &s->stack_b, "Push A"); } -void push_b(t_state *s) +void push_b(t_state *s) { push_inner(&s->stack_b, &s->stack_a, "Push B"); } diff --git a/src/app/moves/rev_rotate.c b/src/app/moves/rev_rotate.c index 7344e6c..1e1649d 100644 --- a/src/app/moves/rev_rotate.c +++ b/src/app/moves/rev_rotate.c @@ -13,23 +13,23 @@ #include "app/element.h" #include "app/state.h" -static inline void rev_rotate_inner(t_vec_element *stack, t_const_str tag) +static inline void rev_rotate_inner(t_vec_element *stack, t_const_str tag) { - t_element e; + t_element e; (void)(tag); if (stack->len <= 1) - return; + return ; vec_element_pop(stack, &e); vec_element_push_front(stack, e); } -void rev_rotate_a(t_state *s) +void rev_rotate_a(t_state *s) { rev_rotate_inner(&s->stack_a, "RevRotate A"); } -void rev_rotate_b(t_state *s) +void rev_rotate_b(t_state *s) { rev_rotate_inner(&s->stack_b, "RevRotate B"); } diff --git a/src/app/moves/rotate.c b/src/app/moves/rotate.c index 6a78f8f..fbbeb2c 100644 --- a/src/app/moves/rotate.c +++ b/src/app/moves/rotate.c @@ -13,24 +13,23 @@ #include "app/element.h" #include "app/state.h" - -static inline void rotate_inner(t_vec_element *stack, t_const_str tag) +static inline void rotate_inner(t_vec_element *stack, t_const_str tag) { - t_element e; + t_element e; (void)(tag); if (stack->len <= 1) - return; + return ; vec_element_pop(stack, &e); vec_element_push_front(stack, e); } -void rotate_a(t_state *s) +void rotate_a(t_state *s) { rotate_inner(&s->stack_a, "Rotate A"); } -void rotate_b(t_state *s) +void rotate_b(t_state *s) { rotate_inner(&s->stack_b, "Rotate B"); } diff --git a/src/app/moves/swap.c b/src/app/moves/swap.c index 62a316d..76a01d2 100644 --- a/src/app/moves/swap.c +++ b/src/app/moves/swap.c @@ -14,26 +14,26 @@ #include "app/state.h" #include "me/vec/vec_element.h" -static inline void swap_inner(t_vec_element *stack, t_const_str tag) +static inline void swap_inner(t_vec_element *stack, t_const_str tag) { - t_element first; - t_element second; + t_element first; + t_element second; (void)(tag); if (stack->len <= 1) - return; + return ; vec_element_pop(stack, &first); vec_element_pop(stack, &second); vec_element_push(stack, first); vec_element_push(stack, second); } -void swap_a(t_state *s) +void swap_a(t_state *s) { swap_inner(&s->stack_a, "Swap A"); } -void swap_b(t_state *s) +void swap_b(t_state *s) { swap_inner(&s->stack_b, "Swap B"); } From 4d7255f105ae159e68841f8fef2d8908f2f5fd3d Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Mon, 29 Jan 2024 22:45:54 +0100 Subject: [PATCH 07/29] update... --- gen.list | 12 +- include/app/best_move.h | 45 ++++ include/app/best_move_inner.h | 69 ++++++ include/app/cost.h | 2 +- include/app/dup_state.h | 4 +- include/app/find_place.h | 29 +++ include/app/lis.h | 4 +- include/app/moves.h | 7 +- include/app/rotate.h | 55 +++++ include/app/state.h | 22 +- include/app/target.h | 21 ++ .../app/{element.h => types/type_i64_bool.h} | 23 +- input.toml | 24 +- src.list | 8 +- src/app/best_move.c | 108 ++++++++ src/app/cost.c | 107 -------- src/app/find_place.c | 51 ++++ src/app/lis/lis.c | 233 ------------------ src/app/main.c | 195 --------------- src/app/moves.c | 50 +++- src/app/moves/push.c | 14 +- src/app/moves/rev_rotate.c | 12 +- src/app/moves/rotate.c | 10 +- src/app/moves/swap.c | 20 +- src/app/{lis/lower_bound.c => rotate.c} | 10 +- src/app/target.c | 24 ++ 26 files changed, 518 insertions(+), 641 deletions(-) create mode 100644 include/app/best_move.h create mode 100644 include/app/best_move_inner.h create mode 100644 include/app/find_place.h create mode 100644 include/app/rotate.h create mode 100644 include/app/target.h rename include/app/{element.h => types/type_i64_bool.h} (65%) create mode 100644 src/app/best_move.c delete mode 100644 src/app/cost.c create mode 100644 src/app/find_place.c delete mode 100644 src/app/lis/lis.c delete mode 100644 src/app/main.c rename src/app/{lis/lower_bound.c => rotate.c} (74%) create mode 100644 src/app/target.c diff --git a/gen.list b/gen.list index 7b3d9f6..4fb083b 100644 --- a/gen.list +++ b/gen.list @@ -1,12 +1,6 @@ -vec/vec_cost -vec/vec_cost_functions2 -vec/vec_cost_functions3 -vec/vec_element -vec/vec_element_functions2 -vec/vec_element_functions3 vec/vec_i64 +vec/vec_i64_bool +vec/vec_i64_bool_functions2 +vec/vec_i64_bool_functions3 vec/vec_i64_functions2 vec/vec_i64_functions3 -vec/vec_moves -vec/vec_moves_functions2 -vec/vec_moves_functions3 diff --git a/include/app/best_move.h b/include/app/best_move.h new file mode 100644 index 0000000..4ac62e9 --- /dev/null +++ b/include/app/best_move.h @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* best_move.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/29 20:10:21 by maiboyer #+# #+# */ +/* Updated: 2024/01/29 20:28:35 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef BEST_MOVE_H +#define BEST_MOVE_H + +#include "app/state.h" + +enum e_stack_selector { + STACK_A, + STACK_B, +}; + +enum e_zero_position { + MIN_ZERO_POS, + MAX_ZERO_POS, +}; + +struct s_functions { + void (*forward)(void *); + void (*reverse)(void *); +}; + +typedef struct s_best_move_args { + enum e_stack_selector main_stack; + enum e_zero_position zero_pos; + void *function_arguments; + struct s_functions main; + struct s_functions other; + struct s_functions both; +} t_best_move_args; + +void run_func_with_best_rotate_for_item(t_state *state, t_usize index, + t_best_move_args data); + +#endif /* BEST_MOVE_H */ diff --git a/include/app/best_move_inner.h b/include/app/best_move_inner.h new file mode 100644 index 0000000..4913278 --- /dev/null +++ b/include/app/best_move_inner.h @@ -0,0 +1,69 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* best_move_inner.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/29 21:31:42 by maiboyer #+# #+# */ +/* Updated: 2024/01/29 21:50:15 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef BEST_MOVE_INNER_H +#define BEST_MOVE_INNER_H + +#include "app/best_move.h" +#include "app/rotate.h" +#include "app/state.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" + +typedef void (*t_banana_func)(void *); +typedef t_usize (*t_iter_pos_func)(t_vec_i64 *); + +enum e_best_move_stack_selector +{ + MAIN, + OTHER, + BOTH, +}; + +typedef struct s_best_move_args_but_better +{ + t_best_move_args data; + t_vec_i64 *main_stack; + t_vec_i64 *other_stack; + t_iter_pos_func iter_func; + +} t_best_move_args_but_better; + +static inline t_banana_func choose_rot(enum e_best_move_stack_selector ty, + t_best_move_args_but_better *data, + t_rotation *rot) +{ + if (ty == MAIN) + { + if (rot->direction == FORWARD) + return (data->data.main.forward); + else + return (data->data.main.reverse); + } + else if (ty == OTHER) + { + if (rot->direction == FORWARD) + return (data->data.other.forward); + else + return (data->data.other.reverse); + } + else if (ty == BOTH) + { + if (rot->direction == FORWARD) + return (data->data.both.forward); + else + return (data->data.both.reverse); + } + return (NULL); +} + +#endif /* BEST_MOVE_INNER_H */ diff --git a/include/app/cost.h b/include/app/cost.h index 073a603..624d1ed 100644 --- a/include/app/cost.h +++ b/include/app/cost.h @@ -13,7 +13,7 @@ #ifndef COST_H # define COST_H -# include "app/element.h" + # include "app/state.h" # include "app/types/type_cost.h" # include "me/types.h" diff --git a/include/app/dup_state.h b/include/app/dup_state.h index f163dfd..bf4f05f 100644 --- a/include/app/dup_state.h +++ b/include/app/dup_state.h @@ -13,12 +13,12 @@ #ifndef DUP_STATE_H # define DUP_STATE_H -# include "app/element.h" + # include "me/types.h" typedef struct s_dup_state { - t_element *lhs; + t_i64 *lhs; t_usize index_to_skip; bool found_dup; } t_dup_state; diff --git a/include/app/find_place.h b/include/app/find_place.h new file mode 100644 index 0000000..00484f6 --- /dev/null +++ b/include/app/find_place.h @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* find_place.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/29 22:00:27 by maiboyer #+# #+# */ +/* Updated: 2024/01/29 22:15:08 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FIND_PLACE_H +#define FIND_PLACE_H + +#include "app/state.h" +#include "me/types.h" + +typedef struct s_find_place_iter_state +{ + t_usize current_index; + t_usize found_index; + t_i64 to_find_elem; + +} t_find_place_iter_state; + +t_usize find_place(t_i64 elem, t_state *state); + +#endif /* FIND_PLACE_H */ diff --git a/include/app/lis.h b/include/app/lis.h index c44310b..aa6aba9 100644 --- a/include/app/lis.h +++ b/include/app/lis.h @@ -14,9 +14,9 @@ # define LIS_H # include "me/types.h" -# include "me/vec/vec_element.h" +# include "me/vec/vec_i64.h" # include "me/vec/vec_i64.h" -t_vec_i64 lis(t_vec_element *elements); +t_vec_i64 lis(t_vec_i64 *elements); #endif /* LIS_H */ diff --git a/include/app/moves.h b/include/app/moves.h index 4688577..b2f6efc 100644 --- a/include/app/moves.h +++ b/include/app/moves.h @@ -6,18 +6,19 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 16:13:08 by maiboyer #+# #+# */ -/* Updated: 2024/01/12 21:27:27 by maiboyer ### ########.fr */ +/* Updated: 2024/01/29 19:07:15 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef MOVES_H # define MOVES_H -# include "app/element.h" + # include "app/state.h" # include "app/types/type_move.h" # include "me/types.h" -void do_move(t_move m, t_state *s); +void do_move(t_move m, t_state *s); +t_const_str get_str_for_move(t_move m); #endif /* MOVES_H */ diff --git a/include/app/rotate.h b/include/app/rotate.h new file mode 100644 index 0000000..fa930ed --- /dev/null +++ b/include/app/rotate.h @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rotate.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/29 19:00:18 by maiboyer #+# #+# */ +/* Updated: 2024/01/29 19:27:19 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ROTATE_H +#define ROTATE_H + +#include "me/types.h" + +enum e_rotation_direction +{ + FORWARD, + REVERSE, +}; +typedef struct s_rotation +{ + t_usize value; + t_usize ring_size; + enum e_rotation_direction direction; +} t_rotation; + +static inline t_rotation forward(t_usize by, t_usize ring_size) +{ + return ((t_rotation){ + .value = by % ring_size, .ring_size = ring_size, .direction = FORWARD}); +} + +static inline t_rotation reverse(t_usize by, t_usize ring_size) +{ + return ((t_rotation){ + .value = by % ring_size, .ring_size = ring_size, .direction = REVERSE}); +} + +static inline t_rotation flip(t_rotation rot) +{ + enum e_rotation_direction flipped; + + flipped = FORWARD; + if (rot.direction == FORWARD) + flipped = REVERSE; + + return ((t_rotation){.value = rot.ring_size - rot.value, + .ring_size = rot.ring_size, + .direction = flipped}); +} + +#endif /* ROTATE_H */ diff --git a/include/app/state.h b/include/app/state.h index 65bc31e..60c10eb 100644 --- a/include/app/state.h +++ b/include/app/state.h @@ -6,25 +6,25 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 14:27:25 by maiboyer #+# #+# */ -/* Updated: 2024/01/12 21:16:59 by maiboyer ### ########.fr */ +/* Updated: 2024/01/29 22:15:52 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef STATE_H -# define STATE_H +#define STATE_H -# include "me/types.h" -# include "me/vec/vec_cost.h" -# include "me/vec/vec_element.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" +#include "me/vec/vec_i64_bool.h" typedef struct s_state { - t_vec_element stack_a; - t_vec_element stack_b; - t_vec_cost costs; -} t_state; + t_vec_i64_bool sorted; + t_vec_i64 stack_a; + t_vec_i64 stack_b; +} t_state; -t_state parses_arguments(t_usize count, t_str nums[]); -void free_state(t_state state); +t_state parses_arguments(t_usize count, t_str nums[]); +void free_state(t_state state); #endif /* STATE_H */ diff --git a/include/app/target.h b/include/app/target.h new file mode 100644 index 0000000..6cee43b --- /dev/null +++ b/include/app/target.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* target.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/29 20:30:43 by maiboyer #+# #+# */ +/* Updated: 2024/01/29 20:31:28 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef TARGET_H +#define TARGET_H + +#include "app/rotate.h" +#include "me/types.h" + +t_rotation target(t_usize from, t_usize to, t_usize ring_size); + +#endif /* TARGET_H */ diff --git a/include/app/element.h b/include/app/types/type_i64_bool.h similarity index 65% rename from include/app/element.h rename to include/app/types/type_i64_bool.h index cd8855f..58ffce1 100644 --- a/include/app/element.h +++ b/include/app/types/type_i64_bool.h @@ -1,25 +1,24 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* element.h :+: :+: :+: */ +/* type_i64_bool.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2024/01/11 14:28:41 by maiboyer #+# #+# */ -/* Updated: 2024/01/11 14:34:27 by maiboyer ### ########.fr */ +/* Created: 2024/01/29 19:14:52 by maiboyer #+# #+# */ +/* Updated: 2024/01/29 19:15:56 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ -#ifndef ELEMENT_H -# define ELEMENT_H +#ifndef TYPE_I64_BOOL_H +#define TYPE_I64_BOOL_H -# include "me/types.h" +#include "me/types.h" -typedef struct s_element +typedef struct s_i64_bool { - t_i64 value; -} t_element; + t_i64 value; + bool active; +} t_i64_bool; -void free_element(t_element elem); - -#endif /* ELEMENT_H */ +#endif /* TYPE_I64_BOOL_H */ diff --git a/input.toml b/input.toml index dfae69b..c18a4a1 100644 --- a/input.toml +++ b/input.toml @@ -23,14 +23,6 @@ replace.C__TYPEHEADER__ = "header_include" replace.C__PREFIX__ = "prefix" replace.C__PREFIXUP__ = "prefix" -[[create.vec]] -sources_output = "src/vec/" -headers_output = "include/me/vec/" -replace.C__TYPENAME__ = "t_element" -replace.C__TYPEHEADER__ = '#include "app/element.h"' -replace.C__PREFIX__ = "element" -replace.C__PREFIXUP__ = "ELEMENT" - [[create.vec]] sources_output = "src/vec/" headers_output = "include/me/vec/" @@ -42,15 +34,7 @@ replace.C__PREFIXUP__ = "I64" [[create.vec]] sources_output = "src/vec/" headers_output = "include/me/vec/" -replace.C__TYPENAME__ = "t_cost" -replace.C__TYPEHEADER__ = '#include "app/types/type_cost.h"' -replace.C__PREFIX__ = "cost" -replace.C__PREFIXUP__ = "COST" - -[[create.vec]] -sources_output = "src/vec/" -headers_output = "include/me/vec/" -replace.C__TYPENAME__ = "t_move" -replace.C__TYPEHEADER__ = '#include "app/types/type_move.h"' -replace.C__PREFIX__ = "moves" -replace.C__PREFIXUP__ = "MOVES" +replace.C__TYPENAME__ = "t_i64_bool" +replace.C__TYPEHEADER__ = '#include "app/types/type_i64_bool.h"' +replace.C__PREFIX__ = "i64_bool" +replace.C__PREFIXUP__ = "I64_BOOL" diff --git a/src.list b/src.list index a865e49..2572cb1 100644 --- a/src.list +++ b/src.list @@ -1,9 +1,9 @@ -app/cost -app/lis/lis -app/lis/lower_bound -app/main +app/best_move +app/find_best_move_for app/moves app/moves/push app/moves/rev_rotate app/moves/rotate app/moves/swap +app/rotate +app/target diff --git a/src/app/best_move.c b/src/app/best_move.c new file mode 100644 index 0000000..4fce44a --- /dev/null +++ b/src/app/best_move.c @@ -0,0 +1,108 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* best_move.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/29 20:04:33 by maiboyer #+# #+# */ +/* Updated: 2024/01/29 22:41:28 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/best_move.h" +#include "app/best_move_inner.h" +#include "app/find_place.h" +#include "app/rotate.h" +#include "app/state.h" +#include "app/target.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" + +t_usize min_iter_zero_pos(t_vec_i64 *vec); +t_usize max_iter_zero_pos(t_vec_i64 *vec); + +static inline t_usize abs_diff(t_usize lhs, t_usize rhs) +{ + if (lhs > rhs) + return (lhs - rhs); + else + return (rhs - lhs); +} + +static inline t_usize min(t_usize lhs, t_usize rhs) +{ + if (lhs > rhs) + return (rhs); + else + return (lhs); +} + +static void find_least_move(t_rotation *main, t_rotation *other) +{ + t_usize main_fliped; + t_usize other_fliped; + t_usize none_fliped; + t_rotation tmp; + t_usize minimum; + + none_fliped = main->value + other->value; + tmp = flip(*main); + main_fliped = abs_diff(tmp.value, other->value); + tmp = flip(*other); + other_fliped = abs_diff(tmp.value, main->value); + minimum = min(none_fliped, min(main_fliped, other_fliped)); + if (minimum == none_fliped) + return; + else if (minimum == main_fliped) + *main = flip(*main); + else if (minimum == other_fliped) + *other = flip(*other); +} + +static void run_func_with_best_rotate_for_item_inner( + t_state *state, t_usize index, t_best_move_args_but_better data) +{ + t_usize other_size_len_min_1; + t_usize target_index; + t_rotation rotate_main; + t_rotation rotate_other; + + other_size_len_min_1 = data.other_stack->len; + if (other_size_len_min_1 == 0) + other_size_len_min_1++; + target_index = + (find_place(data.main_stack->buffer[index], state) + + (data.other_stack->len - data.iter_func(data.other_stack))) % + other_size_len_min_1; + rotate_main = target(0, index, data.main_stack->len); + rotate_other = target(target_index, 0, data.other_stack->len); +} +void run_func_with_best_rotate_for_item(t_state *state, t_usize index, + t_best_move_args data) +{ + t_vec_i64 *main_stack; + t_vec_i64 *other_stack; + t_best_move_args_but_better better_args; + + if (data.zero_pos == MIN_ZERO_POS) + better_args.iter_func = min_iter_zero_pos; + else if (data.zero_pos == MAX_ZERO_POS) + better_args.iter_func = max_iter_zero_pos; + else + return; + if (data.main_stack == STACK_A) + { + better_args.main_stack = &state->stack_a; + better_args.other_stack = &state->stack_b; + } + else if (data.main_stack == STACK_B) + { + better_args.main_stack = &state->stack_b; + better_args.other_stack = &state->stack_a; + } + else + return; + better_args.data = data; + run_func_with_best_rotate_for_item_inner(state, index, better_args); +} diff --git a/src/app/cost.c b/src/app/cost.c deleted file mode 100644 index ec71f30..0000000 --- a/src/app/cost.c +++ /dev/null @@ -1,107 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* cost.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: maiboyer +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/01/12 21:22:44 by maiboyer #+# #+# */ -/* Updated: 2024/01/18 17:01:34 by maiboyer ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "app/cost.h" -#include "app/state.h" -#include "me/types.h" -#include "me/vec/vec_cost.h" -#include "me/vec/vec_moves.h" - -void reset_cost(t_usize _index, t_cost *c, void *_state) -{ - (void)(_index + (t_usize)_state); - c->active = false; - c->moves.len = 0; -} - -void find_minimum(t_usize index, t_cost *c, t_cost_iter_state *s) -{ - if (index >= s->max_index || !c->active) - return ; - if (c->moves.len < s->current_minimum->moves.len) - s->current_minimum = c; -} -typedef struct s_max_iter_state -{ - t_usize current_index; - t_vec_element *v; -} t_max_iter_state; - -void find_maximum_iter(t_usize index, t_element *c, t_max_iter_state *s) -{ - if (s->v->buffer[s->current_index].value < c->value) - s->current_index = index; -} - -static t_usize find_max(t_vec_element *v) -{ - t_max_iter_state state; - - state.v = v; - state.current_index = 0; - vec_element_iter(v, (void (*)())find_maximum_iter, &state); - return (state.current_index); -} - -void cost_for_index(t_state *s, t_usize index) -{ - t_usize rotate_a; - t_usize rotate_b; - t_element *elem; - t_usize max_index; - - rotate_a = 0; - rotate_b = 0; - elem = &s->stack_a.buffer[index]; - max_index = find_max(&s->stack_b); -} - -void free_cost(t_cost self) -{ - vec_moves_free(self.moves); -} - -t_cost alloc_cost(t_state *s) -{ - t_cost out; - - (void)(s); - out = (t_cost){}; - out.active = false; - out.index = s->costs.len; - out.moves = vec_moves_new(s->stack_a.len, NULL); - return (out); -} - -t_cost *calculate_cost(t_state *s) -{ - t_vec_cost *costs; - t_cost *cur; - t_usize index; - t_cost_iter_state iter_state; - - costs = &s->costs; - vec_cost_iter(costs, &reset_cost, NULL); - index = 0; - while (index > costs->len) - { - cur = &costs->buffer[index]; - cost_for_index(s, index); - if (cur->moves.len == 1) - break ; - index++; - } - iter_state = (t_cost_iter_state){.current_minimum = costs->buffer, - .max_index = index}; - vec_cost_iter(costs, (void (*)())find_minimum, &iter_state); - return (iter_state.current_minimum); -} diff --git a/src/app/find_place.c b/src/app/find_place.c new file mode 100644 index 0000000..8df0be9 --- /dev/null +++ b/src/app/find_place.c @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* find_place.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/29 22:01:12 by maiboyer #+# #+# */ +/* Updated: 2024/01/29 22:17:33 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/find_place.h" +#include "app/types/type_i64_bool.h" +#include "me/vec/vec_i64_bool.h" + +static void find_place_iter(t_usize index, t_i64_bool *elem, + t_find_place_iter_state *state) +{ + if (!elem->active) + return; + if (elem->value == state->to_find_elem) + state->found_index = state->current_index; + state->current_index++; +} + +t_usize find_place(t_i64 elem, t_state *state) +{ + t_find_place_iter_state iter_state; + + iter_state.current_index = 0; + iter_state.found_index = 0; + iter_state.to_find_elem = elem; + vec_i64_bool_iter(&state->sorted, (void(*))find_place_iter, &iter_state); + return (iter_state.found_index); +} + +/* +state + .sorted + .iter() + .copied() + .filter(|&(e, active)| active || elem == e) + .enumerate() + .find(|(_, (e, _))| *e == elem) + .map(|(i, _)| i) + .unwrap_or_else(|| { + println!("why..."); + 0 + }) +*/ diff --git a/src/app/lis/lis.c b/src/app/lis/lis.c deleted file mode 100644 index 53b7418..0000000 --- a/src/app/lis/lis.c +++ /dev/null @@ -1,233 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* lis.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: maiboyer +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/01/11 18:50:35 by maiboyer #+# #+# */ -/* Updated: 2024/01/12 20:42:48 by maiboyer ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "app/element.h" -#include "me/mem/mem_alloc_array.h" -#include "me/printf/printf.h" -#include "me/vec/vec_element.h" -#include "me/vec/vec_i64.h" -#include - -/* -P = array of length N -M = array of length N + 1 -M[0] = -1 // undefined so can be set to any value - -L = 0 -for i in range 0 to N-1: //N-1 included - // Binary search for the smallest positive l ≤ L - // such that X[M[l]] > X[i] - lo = 1 - hi = L + 1 - while lo < hi: - mid = lo + floor((hi-lo)/2) // lo <= mid < hi - if X[M[mid]] >= X[i] - hi = mid - else: // if X[M[mid]] < X[i] - lo = mid + 1 - - // After searching, lo == hi is 1 greater than the - // length of the longest prefix of X[i] - newL = lo - - // The predecessor of X[i] is the last index of - // the subsequence of length newL-1 - P[i] = M[newL-1] - M[newL] = i - - if newL > L: - // If we found a subsequence longer than any we've - // found yet, update L - L = newL - -// Reconstruct the longest increasing subsequence -// It consists of the values of X at the L indices: -// ..., P[P[M[L]]], P[M[L]], M[L] -S = array of length L -k = M[L] -for j in range L-1 to 0: //0 included - S[j] = X[k] - k = P[k] - -return S -*/ - -// static t_i64 binary_search(t_i64 L, t_i64 i, t_i64 m[], t_vec_element -// *elements) -//{ -// return (low); -//} - -// static t_vec_i64 lis_inner(t_vec_element *elements, t_i64 L, t_i64 m[], -// t_i64 p[]) -//{ -// } - -/* -/// Finds one of the [longest increasing subsequences][1] -/// from the subsequence created by applying `selector` on each item in `items`. -/// The result is a vector of indices within `items` -/// corresponding to one of the longest increasing subsequences. -/// -/// [1]: https://en.wikipedia.org/wiki/Longest_increasing_subsequence -pub fn lis I>(items: &[T], selector: F) -> Vec { - // This algorithm is adapted from - // -http://www.algorithmist.com/index.php?title=Longest_Increasing_Subsequence.cpp&oldid=13595 - - let mut result = Vec::new(); - - // If `items` is empty, then the result is also empty. - if items.is_empty() { - return (result); - } - - // This vector stores, for each item, - // the index of the largest item prior to itself that is smaller than -itself. - // We'll use this vector at the end to build the final result. - let mut previous_chain = vec![0; items.len()]; - - // Initially, we assume that the first item is part of the result. - // We will replace this index later if that's not the case. - result.push(0); - - for i in 1..items.len() { - // If the next item is greater than the last item of the current longest -subsequence, - // push its index at the end of the result and continue. - if selector(&items[*result.last().unwrap()]) < selector(&items[i]) { - previous_chain[i] = *result.last().unwrap(); - result.push(i); - continue ; - } - - // Perform a binary search to find the index of an item in `result` to -overwrite. - // We want to overwrite an index that refers to the smallest item that -is larger than `items[i]`. - // If there is no such item, then we do nothing. - let comparator = |&result_index| { - use std::cmp::Ordering; - - // We don't return Ordering::Equal when we find an equal value, - // because we want to find the index of the first equal value. - if selector(&items[result_index]) < selector(&items[i]) { - Ordering::Less - } else { - Ordering::Greater - } - }; - - let next_element_index = match result.binary_search_by(comparator) { - Ok(index) | Err(index) => index, - }; - - if selector(&items[i]) < selector(&items[result[next_element_index]]) { - if next_element_index > 0 { - previous_chain[i] = result[next_element_index - 1]; - } - - result[next_element_index] = i; - } - } - - // The last item in `result` is correct, - // but we might have started overwriting earlier items - // with what could have been a longer subsequence. - // Walk back `previous_chain` to restore the proper subsequence. - let mut u = result.len(); - let mut v = *result.last().unwrap(); - while u != 0 { - u -= 1; - result[u] = v; - v = previous_chain[v]; - } - - result -} -*/ - -t_i64 binary_search(t_vec_element *items, t_i64 i) -{ - t_i64 lo; - t_i64 hi; - t_i64 mid; - - lo = 0; - hi = items->len - 1; - while (lo < hi) - { - mid = lo + (hi - lo) / 2; - if (items->buffer[mid].value < items->buffer[i].value) - lo = mid + 1; - else - hi = mid; - } - return (lo); -} - -void lis_inner(t_vec_element *elements, t_vec_i64 *result, - t_vec_i64 *previous_chain) -{ - t_i64 i; - t_i64 next_element_index; - - i = 1; - while (i < (t_i64)elements->len) - { - if (-elements->buffer[result->buffer[result->len - 1]].value < - -elements->buffer[i].value) - { - previous_chain->buffer[i] = result->buffer[result->len - 1]; - vec_i64_push(result, i++); - continue ; - } - next_element_index = binary_search(elements, i); - if (-elements->buffer[i].value > - -elements->buffer[result->buffer[next_element_index]].value) - { - if (next_element_index > 0) - previous_chain->buffer[i] = result->buffer[next_element_index - - 1]; - result->buffer[next_element_index] = i; - } - i++; - } -} - -t_vec_i64 lis(t_vec_element *elements) -{ - t_vec_i64 result; - t_i64 u; - t_i64 v; - t_vec_i64 previous_chain; - - result = vec_i64_new(elements->len, NULL); - if (elements->len == 0) - return (result); - previous_chain = vec_i64_new(elements->len, NULL); - previous_chain.len = elements->len; - vec_i64_push(&result, 0); - lis_inner(elements, &result, &previous_chain); - u = result.len; - v = result.buffer[result.len - 1]; - while (u != 0) - { - u--; - result.buffer[u] = v; - v = previous_chain.buffer[v]; - } - vec_i64_free(previous_chain); - vec_i64_reverse(&result); - return (result); -} diff --git a/src/app/main.c b/src/app/main.c deleted file mode 100644 index 9f9a5d1..0000000 --- a/src/app/main.c +++ /dev/null @@ -1,195 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* main.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: maiboyer +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/01/11 14:14:18 by maiboyer #+# #+# */ -/* Updated: 2024/01/18 13:45:26 by maiboyer ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "app/cost.h" -#include "app/dup_state.h" -#include "app/element.h" -#include "app/lis.h" -#include "app/state.h" -#include "me/convert/atoi.h" -#include "me/convert/itoa.h" -#include "me/printf/printf.h" -#include "me/string/str_clone.h" -#include "me/types.h" -#include "me/vec/vec_cost.h" -#include "me/vec/vec_element.h" -#include -#include -#include -#include - -void free_element(t_element elem) -{ - (void)(elem); -} - -void free_state(t_state state) -{ - vec_element_free(state.stack_a); - vec_element_free(state.stack_b); - vec_cost_free(state.costs); -} - -t_error parse_element(t_str s, t_element *e) -{ - t_usize i; - t_i64 value; - t_element out; - - if (s == NULL || e == NULL) - return (ERROR); - i = 0; - if (s[i] == '-' || s[i] == '+') - i++; - while (s[i] && s[i] >= '0' && s[i] <= '9') - i++; - if (s[i] != '\0') - return (ERROR); - value = me_atoi_64(s); - if (value > INT_MAX || value < INT_MIN) - return (ERROR); - out.value = value; - *e = out; - return (NO_ERROR); -} -void check_eq(t_usize i, const t_element *rhs, t_dup_state *dup_state) -{ - dup_state->found_dup = dup_state->found_dup - || (i != dup_state->index_to_skip - && rhs->value == dup_state->lhs->value); -} - -void check_no_duplicate(t_vec_element *stack, t_vec_element *to_free_in_need) -{ - t_usize i; - t_dup_state s; - - i = 0; - while (i < stack->len) - { - s = (t_dup_state){ - .lhs = &stack->buffer[i], - .index_to_skip = i, - .found_dup = false, - }; - vec_element_iter(stack, (void (*)())check_eq, &s); - if (s.found_dup) - return (me_eprintf("Error:\nDuplicate numbers (%i)\n", - stack->buffer[i]), vec_element_free(*stack), - vec_element_free(*to_free_in_need), exit(1)); - i++; - } -} - -t_state parses_arguments(t_usize count, t_str nums[]) -{ - t_state out; - t_usize i; - t_element e; - - i = 0; - out.stack_a = vec_element_new(count + 1, free_element); - out.stack_b = vec_element_new(count + 1, free_element); - while (i < count) - { - if (parse_element(nums[i], &e)) - (me_eprintf("Error:\nArgument '%s' isn't a valid number for this " - "program!\n", - nums[i]), - vec_element_free(out.stack_a), - vec_element_free(out.stack_b), - exit(1)); - vec_element_push(&out.stack_a, e); - i++; - } - vec_element_reverse(&out.stack_a); - check_no_duplicate(&out.stack_a, &out.stack_b); - return (out); -} - -#define LEFT(str, w) \ - ({ \ - int m = w + strlen(str); \ - m % 2 ? (m + 1) / 2 : m / 2; \ - }) - -#define RIGHT(str, w) \ - ({ \ - int m = w - strlen(str); \ - m % 2 ? (m - 1) / 2 : m / 2; \ - }) - -#define STR_CENTER(str, width) LEFT(str, width), str, RIGHT(str, width), "" - -#define STACK_WIDTH 12 - -bool check_sorted(t_state *s) -{ - t_usize not_in_order; - t_usize i; - - not_in_order = 0; - i = 0; - while (i + 1 < s->stack_a.len) - { - if (s->stack_a.buffer[i].value > s->stack_a.buffer[i].value) - not_in_order++; - i++; - } - return (not_in_order == 0); -} - -void print_state(t_state *s) -{ - t_usize max_len; - t_str s_a; - t_str s_b; - - if (s->stack_a.len > s->stack_b.len) - max_len = s->stack_a.len; - else - max_len = s->stack_b.len; - printf("|%*s%*s|%*s%*s|\n", STR_CENTER("Stack A", STACK_WIDTH), - STR_CENTER("Stack B", STACK_WIDTH)); - for (t_usize i = 0; i < max_len; i++) - { - if (i < s->stack_a.len) - s_a = me_itoa(s->stack_a.buffer[s->stack_a.len - i - 1].value); - else - s_a = str_clone(""); - if (i < s->stack_b.len) - s_b = me_itoa(s->stack_b.buffer[s->stack_b.len - i - 1].value); - else - s_b = str_clone(""); - printf("|%*s%*s|%*s%*s|\n", STR_CENTER(s_a, STACK_WIDTH), - STR_CENTER(s_b, STACK_WIDTH)); - free(s_a); - free(s_b); - } - printf("\n"); -} - -int main(t_i32 argc, t_str argv[]) -{ - t_state state; - - state = parses_arguments(--argc, ++argv); - state.costs = vec_cost_new(state.stack_a.len, &free_cost); - while (state.costs.len < state.stack_a.len) - vec_cost_push(&state.costs, alloc_cost(&state)); - print_state(&state); - while (!check_sorted(&state)) - { - } - free_state(state); - return (0); -} diff --git a/src/app/moves.c b/src/app/moves.c index d314a7c..d1bc31c 100644 --- a/src/app/moves.c +++ b/src/app/moves.c @@ -6,24 +6,25 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 16:17:26 by maiboyer #+# #+# */ -/* Updated: 2024/01/11 16:31:56 by maiboyer ### ########.fr */ +/* Updated: 2024/01/29 18:58:48 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ -#include "app/element.h" #include "app/moves.h" + #include "app/state.h" +#include "app/types/type_move.h" -void push_a(t_state *s); -void push_b(t_state *s); -void swap_a(t_state *s); -void swap_b(t_state *s); -void rotate_a(t_state *s); -void rotate_b(t_state *s); -void rev_rotate_a(t_state *s); -void rev_rotate_b(t_state *s); +void push_a(t_state *s); +void push_b(t_state *s); +void swap_a(t_state *s); +void swap_b(t_state *s); +void rotate_a(t_state *s); +void rotate_b(t_state *s); +void rev_rotate_a(t_state *s); +void rev_rotate_b(t_state *s); -void do_move(t_move m, t_state *s) +void do_move(t_move m, t_state *s) { if (m & PUSH_A) push_a(s); @@ -42,3 +43,30 @@ void do_move(t_move m, t_state *s) if (m & REVERSE_ROTATE_B) rev_rotate_b(s); } + +t_const_str get_str_for_move(t_move m) +{ + if (m & PUSH_A) + return ("pa"); + if (m & PUSH_B) + return ("pb"); + if (m & SWAP_A && m & SWAP_B) + return ("ss"); + if (m & SWAP_A) + return ("sa"); + if (m & SWAP_B) + return ("sb"); + if (m & ROTATE_A && m & ROTATE_B) + return ("rr"); + if (m & ROTATE_A) + return ("ra"); + if (m & ROTATE_B) + return ("rb"); + if (m & REVERSE_ROTATE_A && m & REVERSE_ROTATE_B) + return ("rrr"); + if (m & REVERSE_ROTATE_A) + return ("rra"); + if (m & REVERSE_ROTATE_B) + return ("rrb"); + return (""); +} diff --git a/src/app/moves/push.c b/src/app/moves/push.c index 85453be..cd6c296 100644 --- a/src/app/moves/push.c +++ b/src/app/moves/push.c @@ -10,20 +10,20 @@ /* */ /* ************************************************************************** */ -#include "app/element.h" -#include "app/state.h" -#include "me/vec/vec_element.h" -static inline void push_inner(t_vec_element *to, t_vec_element *from, +#include "app/state.h" +#include "me/vec/vec_i64.h" + +static inline void push_inner(t_vec_i64 *to, t_vec_i64 *from, t_const_str tag) { - t_element e; + t_i64 e; (void)(tag); if (from->len == 0) return ; - vec_element_pop(from, &e); - vec_element_push(to, e); + vec_i64_pop(from, &e); + vec_i64_push(to, e); } void push_a(t_state *s) diff --git a/src/app/moves/rev_rotate.c b/src/app/moves/rev_rotate.c index 1e1649d..e83cba7 100644 --- a/src/app/moves/rev_rotate.c +++ b/src/app/moves/rev_rotate.c @@ -6,22 +6,22 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 16:26:04 by maiboyer #+# #+# */ -/* Updated: 2024/01/11 17:31:44 by maiboyer ### ########.fr */ +/* Updated: 2024/01/29 18:56:05 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ -#include "app/element.h" + #include "app/state.h" -static inline void rev_rotate_inner(t_vec_element *stack, t_const_str tag) +static inline void rev_rotate_inner(t_vec_i64 *stack, t_const_str tag) { - t_element e; + t_i64 e; (void)(tag); if (stack->len <= 1) return ; - vec_element_pop(stack, &e); - vec_element_push_front(stack, e); + vec_i64_pop(stack, &e); + vec_i64_push_front(stack, e); } void rev_rotate_a(t_state *s) diff --git a/src/app/moves/rotate.c b/src/app/moves/rotate.c index fbbeb2c..83a569b 100644 --- a/src/app/moves/rotate.c +++ b/src/app/moves/rotate.c @@ -10,18 +10,18 @@ /* */ /* ************************************************************************** */ -#include "app/element.h" + #include "app/state.h" -static inline void rotate_inner(t_vec_element *stack, t_const_str tag) +static inline void rotate_inner(t_vec_i64 *stack, t_const_str tag) { - t_element e; + t_i64 e; (void)(tag); if (stack->len <= 1) return ; - vec_element_pop(stack, &e); - vec_element_push_front(stack, e); + vec_i64_pop(stack, &e); + vec_i64_push_front(stack, e); } void rotate_a(t_state *s) diff --git a/src/app/moves/swap.c b/src/app/moves/swap.c index 76a01d2..f9029c1 100644 --- a/src/app/moves/swap.c +++ b/src/app/moves/swap.c @@ -10,22 +10,22 @@ /* */ /* ************************************************************************** */ -#include "app/element.h" -#include "app/state.h" -#include "me/vec/vec_element.h" -static inline void swap_inner(t_vec_element *stack, t_const_str tag) +#include "app/state.h" +#include "me/vec/vec_i64.h" + +static inline void swap_inner(t_vec_i64 *stack, t_const_str tag) { - t_element first; - t_element second; + t_i64 first; + t_i64 second; (void)(tag); if (stack->len <= 1) return ; - vec_element_pop(stack, &first); - vec_element_pop(stack, &second); - vec_element_push(stack, first); - vec_element_push(stack, second); + vec_i64_pop(stack, &first); + vec_i64_pop(stack, &second); + vec_i64_push(stack, first); + vec_i64_push(stack, second); } void swap_a(t_state *s) diff --git a/src/app/lis/lower_bound.c b/src/app/rotate.c similarity index 74% rename from src/app/lis/lower_bound.c rename to src/app/rotate.c index 79bceeb..e2f2ed3 100644 --- a/src/app/lis/lower_bound.c +++ b/src/app/rotate.c @@ -1,11 +1,15 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* lower_bound.c :+: :+: :+: */ +/* rotate.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2024/01/11 18:17:23 by maiboyer #+# #+# */ -/* Updated: 2024/01/11 19:17:19 by maiboyer ### ########.fr */ +/* Created: 2024/01/29 18:59:56 by maiboyer #+# #+# */ +/* Updated: 2024/01/29 20:04:18 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ + +#include "app/rotate.h" +#include "me/types.h" + diff --git a/src/app/target.c b/src/app/target.c new file mode 100644 index 0000000..26c62e7 --- /dev/null +++ b/src/app/target.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* target.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/29 20:30:27 by maiboyer #+# #+# */ +/* Updated: 2024/01/29 22:29:50 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/target.h" +#include "app/rotate.h" + +t_rotation target(t_usize from, t_usize to, t_usize ring_size) +{ + if (ring_size == 0) + ring_size++; + if (from < to) + return (forward(to - from, ring_size)); + else + return (reverse(from - to, ring_size)); +} From e0e790c230e80e4f27a115ef59dfa27e16b62c95 Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Wed, 31 Jan 2024 16:15:47 +0100 Subject: [PATCH 08/29] yes --- include/app/best_move.h | 22 ++++--- include/app/best_move_inner.h | 32 ++-------- include/app/find_iter.h | 22 +++++++ include/app/iter_state.h | 24 +++++++ include/app/state.h | 41 +++++++++++- src.list | 4 +- src/app/best_move.c | 40 +++++++++--- src/app/do_move.c | 70 ++++++++++++++++++++ src/app/find_place.c | 3 +- src/app/iter_find.c | 57 +++++++++++++++++ src/app/moves/rev_rotate.c | 8 ++- src/app/moves/rotate.c | 19 ++++-- src/app/moves/swap.c | 21 +++--- src/app/run_with_items.c | 116 ++++++++++++++++++++++++++++++++++ 14 files changed, 416 insertions(+), 63 deletions(-) create mode 100644 include/app/find_iter.h create mode 100644 include/app/iter_state.h create mode 100644 src/app/do_move.c create mode 100644 src/app/iter_find.c create mode 100644 src/app/run_with_items.c diff --git a/include/app/best_move.h b/include/app/best_move.h index 4ac62e9..d0e075b 100644 --- a/include/app/best_move.h +++ b/include/app/best_move.h @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/29 20:10:21 by maiboyer #+# #+# */ -/* Updated: 2024/01/29 20:28:35 by maiboyer ### ########.fr */ +/* Updated: 2024/01/31 14:10:08 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,28 +15,32 @@ #include "app/state.h" -enum e_stack_selector { +enum e_stack_selector +{ STACK_A, STACK_B, }; -enum e_zero_position { +enum e_zero_position +{ MIN_ZERO_POS, MAX_ZERO_POS, }; -struct s_functions { +struct s_functions +{ void (*forward)(void *); void (*reverse)(void *); }; -typedef struct s_best_move_args { +typedef struct s_best_move_args +{ enum e_stack_selector main_stack; enum e_zero_position zero_pos; - void *function_arguments; - struct s_functions main; - struct s_functions other; - struct s_functions both; + void *args; + struct s_functions main; + struct s_functions other; + struct s_functions both; } t_best_move_args; void run_func_with_best_rotate_for_item(t_state *state, t_usize index, diff --git a/include/app/best_move_inner.h b/include/app/best_move_inner.h index 4913278..3c419dc 100644 --- a/include/app/best_move_inner.h +++ b/include/app/best_move_inner.h @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/29 21:31:42 by maiboyer #+# #+# */ -/* Updated: 2024/01/29 21:50:15 by maiboyer ### ########.fr */ +/* Updated: 2024/01/31 14:12:25 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,31 +38,13 @@ typedef struct s_best_move_args_but_better } t_best_move_args_but_better; -static inline t_banana_func choose_rot(enum e_best_move_stack_selector ty, - t_best_move_args_but_better *data, - t_rotation *rot) +static inline t_banana_func choose_func(struct s_functions *funcs, + t_rotation rotate) { - if (ty == MAIN) - { - if (rot->direction == FORWARD) - return (data->data.main.forward); - else - return (data->data.main.reverse); - } - else if (ty == OTHER) - { - if (rot->direction == FORWARD) - return (data->data.other.forward); - else - return (data->data.other.reverse); - } - else if (ty == BOTH) - { - if (rot->direction == FORWARD) - return (data->data.both.forward); - else - return (data->data.both.reverse); - } + if (rotate.direction == FORWARD) + return (funcs->forward); + else if (rotate.direction == REVERSE) + return (funcs->reverse); return (NULL); } diff --git a/include/app/find_iter.h b/include/app/find_iter.h new file mode 100644 index 0000000..0f445c6 --- /dev/null +++ b/include/app/find_iter.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* find_iter.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/31 14:24:10 by maiboyer #+# #+# */ +/* Updated: 2024/01/31 14:24:47 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FIND_ITER_H +#define FIND_ITER_H + +#include "me/types.h" +#include "me/vec/vec_i64.h" + +t_usize min_iter_zero_pos(t_vec_i64 *vec); +t_usize max_iter_zero_pos(t_vec_i64 *vec); + +#endif /* FIND_ITER_H */ diff --git a/include/app/iter_state.h b/include/app/iter_state.h new file mode 100644 index 0000000..8eb554d --- /dev/null +++ b/include/app/iter_state.h @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* iter_state.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/31 14:19:06 by maiboyer #+# #+# */ +/* Updated: 2024/01/31 14:19:50 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ITER_STATE_H +#define ITER_STATE_H + +#include "me/types.h" + +typedef struct s_iter_state +{ + t_usize pos; + t_i64 elem; +} t_iter_state; + +#endif /* ITER_STATE_H */ diff --git a/include/app/state.h b/include/app/state.h index 60c10eb..63c630a 100644 --- a/include/app/state.h +++ b/include/app/state.h @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 14:27:25 by maiboyer #+# #+# */ -/* Updated: 2024/01/29 22:15:52 by maiboyer ### ########.fr */ +/* Updated: 2024/01/31 15:09:44 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,4 +27,43 @@ typedef struct s_state t_state parses_arguments(t_usize count, t_str nums[]); void free_state(t_state state); +static inline void make_sorted_true_for_elem(t_state *s, t_i64 elem) +{ + t_usize i; + + i = 0; + while (i < s->sorted.len) + { + if (s->sorted.buffer[i].value == elem) + { + s->sorted.buffer[i].active = true; + return; + } + i++; + } +} + +static inline void make_sorted_true_from_stack(t_state *s, t_vec_i64 *stack) +{ + t_usize i; + + i = 0; + while (i < stack->len) + { + make_sorted_true_for_elem(s, stack->buffer[i++]); + } +} + +static inline void make_sorted_all_false(t_state *s) +{ + t_usize i; + + i = 0; + while (i < s->sorted.len) + { + s->sorted.buffer[i].active = false; + i++; + } +} + #endif /* STATE_H */ diff --git a/src.list b/src.list index 2572cb1..e6591e3 100644 --- a/src.list +++ b/src.list @@ -1,5 +1,7 @@ app/best_move -app/find_best_move_for +app/do_move +app/find_place +app/iter_find app/moves app/moves/push app/moves/rev_rotate diff --git a/src/app/best_move.c b/src/app/best_move.c index 4fce44a..c23fabc 100644 --- a/src/app/best_move.c +++ b/src/app/best_move.c @@ -6,12 +6,13 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/29 20:04:33 by maiboyer #+# #+# */ -/* Updated: 2024/01/29 22:41:28 by maiboyer ### ########.fr */ +/* Updated: 2024/01/31 14:25:00 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ #include "app/best_move.h" #include "app/best_move_inner.h" +#include "app/find_iter.h" #include "app/find_place.h" #include "app/rotate.h" #include "app/state.h" @@ -19,9 +20,6 @@ #include "me/types.h" #include "me/vec/vec_i64.h" -t_usize min_iter_zero_pos(t_vec_i64 *vec); -t_usize max_iter_zero_pos(t_vec_i64 *vec); - static inline t_usize abs_diff(t_usize lhs, t_usize rhs) { if (lhs > rhs) @@ -65,8 +63,9 @@ static void run_func_with_best_rotate_for_item_inner( { t_usize other_size_len_min_1; t_usize target_index; - t_rotation rotate_main; - t_rotation rotate_other; + t_rotation main; + t_rotation other; + t_usize i; other_size_len_min_1 = data.other_stack->len; if (other_size_len_min_1 == 0) @@ -75,14 +74,35 @@ static void run_func_with_best_rotate_for_item_inner( (find_place(data.main_stack->buffer[index], state) + (data.other_stack->len - data.iter_func(data.other_stack))) % other_size_len_min_1; - rotate_main = target(0, index, data.main_stack->len); - rotate_other = target(target_index, 0, data.other_stack->len); + main = target(0, index, data.main_stack->len); + other = target(target_index, 0, data.other_stack->len); + find_least_move(&main, &other); + if (main.direction == other.direction) + { + i = 0; + while (i++ < min(main.value, other.value)) + choose_func(&data.data.both, main)(data.data.args); + i = 0; + if (main.value > other.value) + while (i++ < main.value - other.value) + choose_func(&data.data.main, main)(data.data.args); + else + while (i++ < other.value - main.value) + choose_func(&data.data.other, other)(data.data.args); + } + else + { + i = 0; + while (i++ < main.value) + choose_func(&data.data.main, main)(data.data.args); + i = 0; + while (i++ < other.value) + choose_func(&data.data.other, other)(data.data.args); + } } void run_func_with_best_rotate_for_item(t_state *state, t_usize index, t_best_move_args data) { - t_vec_i64 *main_stack; - t_vec_i64 *other_stack; t_best_move_args_but_better better_args; if (data.zero_pos == MIN_ZERO_POS) diff --git a/src/app/do_move.c b/src/app/do_move.c new file mode 100644 index 0000000..756123a --- /dev/null +++ b/src/app/do_move.c @@ -0,0 +1,70 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* do_move.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/31 14:25:57 by maiboyer #+# #+# */ +/* Updated: 2024/01/31 15:11:24 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/best_move.h" +#include "app/moves.h" +#include "app/state.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" + +void push_a(void *s); +void push_b(void *s); +void swap_a(void *s); +void swap_b(void *s); +void swap_both(void *s); +void rotate_a(void *s); +void rotate_b(void *s); +void rotate_both(void *s); +void rev_rotate_a(void *s); +void rev_rotate_b(void *s); +void rev_rotate_both(void *s); + +void do_sort_insert(t_state *state, t_usize index, + enum e_stack_selector move_from, + enum e_zero_position zero_pos) +{ + void (*pop_move)(void *); + t_vec_i64 *pushed_stack; + struct s_functions main; + struct s_functions other; + + if (move_from == STACK_A) + { + pushed_stack = &state->stack_b; + pop_move = push_b; + main.forward = rev_rotate_a; + main.reverse = rotate_a; + other.forward = rev_rotate_b; + other.reverse = rotate_b; + } + else + { + pushed_stack = &state->stack_a; + pop_move = push_a; + main.forward = rev_rotate_b; + main.reverse = rotate_b; + other.forward = rev_rotate_a; + other.reverse = rotate_a; + } + run_func_with_best_rotate_for_item( + state, index, + (t_best_move_args){ + .zero_pos = zero_pos, + .main_stack = move_from, + .args = state, + .both = {.forward = rev_rotate_both, .reverse = rotate_both}, + .main = main, + .other = other, + }); + (pop_move)(state); + make_sorted_true_for_elem(state, pushed_stack->buffer[0]); +} diff --git a/src/app/find_place.c b/src/app/find_place.c index 8df0be9..7636ac4 100644 --- a/src/app/find_place.c +++ b/src/app/find_place.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/29 22:01:12 by maiboyer #+# #+# */ -/* Updated: 2024/01/29 22:17:33 by maiboyer ### ########.fr */ +/* Updated: 2024/01/31 14:13:34 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,7 @@ static void find_place_iter(t_usize index, t_i64_bool *elem, t_find_place_iter_state *state) { + (void)(index); if (!elem->active) return; if (elem->value == state->to_find_elem) diff --git a/src/app/iter_find.c b/src/app/iter_find.c new file mode 100644 index 0000000..474e9cc --- /dev/null +++ b/src/app/iter_find.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* iter_find.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/31 14:17:53 by maiboyer #+# #+# */ +/* Updated: 2024/01/31 14:23:26 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/iter_state.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" + +static void iter_min(t_usize index, t_i64 *elem, t_iter_state *state) +{ + if (*elem < state->elem) + { + state->elem = *elem; + state->pos = index; + } +} + +static void iter_max(t_usize index, t_i64 *elem, t_iter_state *state) +{ + if (*elem > state->elem) + { + state->elem = *elem; + state->pos = index; + } +} + +t_usize min_iter_zero_pos(t_vec_i64 *vec) +{ + t_iter_state state; + + if (vec->len == 0) + return (0); + state.pos = 0; + state.elem = vec->buffer[0]; + vec_i64_iter(vec, (void (*)())iter_min, &state); + return (state.pos); +} + +t_usize max_iter_zero_pos(t_vec_i64 *vec) +{ + t_iter_state state; + + if (vec->len == 0) + return (0); + state.pos = 0; + state.elem = vec->buffer[0]; + vec_i64_iter(vec, (void (*)())iter_max, &state); + return (state.pos); +} diff --git a/src/app/moves/rev_rotate.c b/src/app/moves/rev_rotate.c index e83cba7..29ae711 100644 --- a/src/app/moves/rev_rotate.c +++ b/src/app/moves/rev_rotate.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 16:26:04 by maiboyer #+# #+# */ -/* Updated: 2024/01/29 18:56:05 by maiboyer ### ########.fr */ +/* Updated: 2024/01/31 14:57:21 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,3 +33,9 @@ void rev_rotate_b(t_state *s) { rev_rotate_inner(&s->stack_b, "RevRotate B"); } + +void rev_rotate_both(t_state *s) +{ + rev_rotate_inner(&s->stack_a, "RevRotate Both"); + rev_rotate_inner(&s->stack_b, "RevRotate Both"); +} diff --git a/src/app/moves/rotate.c b/src/app/moves/rotate.c index 83a569b..23cb016 100644 --- a/src/app/moves/rotate.c +++ b/src/app/moves/rotate.c @@ -6,30 +6,35 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 16:26:04 by maiboyer #+# #+# */ -/* Updated: 2024/01/11 17:31:20 by maiboyer ### ########.fr */ +/* Updated: 2024/01/31 14:58:10 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ - #include "app/state.h" -static inline void rotate_inner(t_vec_i64 *stack, t_const_str tag) +static inline void rotate_inner(t_vec_i64 *stack, t_const_str tag) { - t_i64 e; + t_i64 e; (void)(tag); if (stack->len <= 1) - return ; + return; vec_i64_pop(stack, &e); vec_i64_push_front(stack, e); } -void rotate_a(t_state *s) +void rotate_a(t_state *s) { rotate_inner(&s->stack_a, "Rotate A"); } -void rotate_b(t_state *s) +void rotate_b(t_state *s) { rotate_inner(&s->stack_b, "Rotate B"); } + +void rotate_both(t_state *s) +{ + rotate_inner(&s->stack_a, "Rotate Both"); + rotate_inner(&s->stack_b, "Rotate Both"); +} diff --git a/src/app/moves/swap.c b/src/app/moves/swap.c index f9029c1..416abd7 100644 --- a/src/app/moves/swap.c +++ b/src/app/moves/swap.c @@ -6,34 +6,39 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 16:26:04 by maiboyer #+# #+# */ -/* Updated: 2024/01/11 17:56:12 by maiboyer ### ########.fr */ +/* Updated: 2024/01/31 14:58:32 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ - #include "app/state.h" #include "me/vec/vec_i64.h" -static inline void swap_inner(t_vec_i64 *stack, t_const_str tag) +static inline void swap_inner(t_vec_i64 *stack, t_const_str tag) { - t_i64 first; - t_i64 second; + t_i64 first; + t_i64 second; (void)(tag); if (stack->len <= 1) - return ; + return; vec_i64_pop(stack, &first); vec_i64_pop(stack, &second); vec_i64_push(stack, first); vec_i64_push(stack, second); } -void swap_a(t_state *s) +void swap_a(t_state *s) { swap_inner(&s->stack_a, "Swap A"); } -void swap_b(t_state *s) +void swap_b(t_state *s) { swap_inner(&s->stack_b, "Swap B"); } + +void swap_both(t_state *s) +{ + swap_inner(&s->stack_a, "Swap Both"); + swap_inner(&s->stack_b, "Swap Both"); +} diff --git a/src/app/run_with_items.c b/src/app/run_with_items.c new file mode 100644 index 0000000..aff138b --- /dev/null +++ b/src/app/run_with_items.c @@ -0,0 +1,116 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* run_with_items.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/31 15:12:47 by maiboyer #+# #+# */ +/* Updated: 2024/01/31 15:41:41 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/best_move.h" +#include "app/state.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" + +void push_a(void *s); +void push_b(void *s); +void swap_a(void *s); +void swap_b(void *s); +void swap_both(void *s); +void rotate_a(void *s); +void rotate_b(void *s); +void rotate_both(void *s); +void rev_rotate_a(void *s); +void rev_rotate_b(void *s); +void rev_rotate_both(void *s); + +void do_sort_insert(t_state *state, t_usize index, + enum e_stack_selector move_from, + enum e_zero_position zero_pos); + +void inc(void *s) +{ + t_usize *p; + + p = (t_usize *)s; + *p += 1; +} + +t_usize best_index_to_move(t_state *state, enum e_stack_selector move_from, + enum e_zero_position zero_pos) +{ + t_vec_i64 *stack; + t_usize min_pos; + t_usize min_val; + t_usize i; + t_usize tmp; + + if (move_from == STACK_A) + stack = &state->stack_a; + else + stack = &state->stack_b; + if (stack->len == 0) + return (0); + i = 1; + min_pos = 0; + run_func_with_best_rotate_for_item(state, 0, + (t_best_move_args){ + .args = &tmp, + .zero_pos = zero_pos, + .main_stack = move_from, + .other = {inc, inc}, + .main = {inc, inc}, + .both = {inc, inc}, + }); + min_val = tmp; + while (i < stack->len) + { + run_func_with_best_rotate_for_item(state, i, + (t_best_move_args){ + .args = &tmp, + .zero_pos = zero_pos, + .main_stack = move_from, + .other = {inc, inc}, + .main = {inc, inc}, + .both = {inc, inc}, + }); + if (tmp < min_val) + { + min_val = tmp; + min_pos = i; + } + i++; + } + return (min_val); +} + +bool is_sorted(t_vec_i64 *v) +{ + t_usize i; + i = 1; + while (i < v->len) + { + if (v->buffer[i - 1] > v->buffer[i]) + return (false); + i++; + } + return (true); +} + +void run_with_items(t_state *state) +{ + if (is_sorted(&state->stack_a)) + return; + while (state->stack_a.len > state->stack_b.len) + { + do_sort_insert(state, best_index_to_move(state, STACK_A, MAX_ZERO_POS), + STACK_A, MAX_ZERO_POS); + } + while (state->stack_a.len != 0) + { + push_b(state); + } +} From a08b85d7cce2cb3e10679e7dd149480c9b3bf41e Mon Sep 17 00:00:00 2001 From: Maix0 Date: Fri, 2 Feb 2024 01:06:44 +0100 Subject: [PATCH 09/29] added stuff --- flake.lock | 54 +++++++-------- gen.list | 18 +++++ include/me/convert/str_to_numbers.h | 30 ++++++++ input.toml | 104 ++++++++++++++++++++++++++++ mecstd | 2 +- src.list | 2 + src/app/main.c | 72 +++++++++++++++++++ src/app/run_with_items.c | 31 +++++++-- 8 files changed, 281 insertions(+), 32 deletions(-) create mode 100644 include/me/convert/str_to_numbers.h create mode 100644 src/app/main.c diff --git a/flake.lock b/flake.lock index 5b430eb..1589801 100644 --- a/flake.lock +++ b/flake.lock @@ -59,11 +59,11 @@ "systems": "systems_2" }, "locked": { - "lastModified": 1701680307, - "narHash": "sha256-kAuep2h5ajznlPMD9rnQyffWG8EM/C73lejGofXvdM8=", + "lastModified": 1705309234, + "narHash": "sha256-uNRRNRKmJyCRC/8y1RqBkqWBLM034y4qN7EprSdmgyA=", "owner": "numtide", "repo": "flake-utils", - "rev": "4022d587cbbfd70fe950c1e2083a02621806a725", + "rev": "1ef2e671c3b0c19053962c07dbda38332dcebf26", "type": "github" }, "original": { @@ -77,11 +77,11 @@ "systems": "systems_3" }, "locked": { - "lastModified": 1701680307, - "narHash": "sha256-kAuep2h5ajznlPMD9rnQyffWG8EM/C73lejGofXvdM8=", + "lastModified": 1705309234, + "narHash": "sha256-uNRRNRKmJyCRC/8y1RqBkqWBLM034y4qN7EprSdmgyA=", "owner": "numtide", "repo": "flake-utils", - "rev": "4022d587cbbfd70fe950c1e2083a02621806a725", + "rev": "1ef2e671c3b0c19053962c07dbda38332dcebf26", "type": "github" }, "original": { @@ -95,11 +95,11 @@ "systems": "systems_4" }, "locked": { - "lastModified": 1681202837, - "narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=", + "lastModified": 1705309234, + "narHash": "sha256-uNRRNRKmJyCRC/8y1RqBkqWBLM034y4qN7EprSdmgyA=", "owner": "numtide", "repo": "flake-utils", - "rev": "cfacdce06f30d2b68473a46042957675eebb3401", + "rev": "1ef2e671c3b0c19053962c07dbda38332dcebf26", "type": "github" }, "original": { @@ -116,11 +116,11 @@ "rust-overlay": "rust-overlay" }, "locked": { - "lastModified": 1702388541, - "narHash": "sha256-ExzIrROCDRXVnvbOs81aL9DKHm1OxlzVXQPpUTrB75c=", + "lastModified": 1706829609, + "narHash": "sha256-A0imQ9AIJafdL1/+j/1b3G7bm2j+N+VhzTsvKikKjz4=", "owner": "maix0", "repo": "generic_c", - "rev": "52b5de885c015d3b84ca26c02c3684ca0e9b426d", + "rev": "a470c2c5a8c8aadc852a7a50d72853f2a3873595", "type": "github" }, "original": { @@ -163,11 +163,11 @@ }, "nixpkgs_2": { "locked": { - "lastModified": 1704842529, - "narHash": "sha256-OTeQA+F8d/Evad33JMfuXC89VMetQbsU4qcaePchGr4=", + "lastModified": 1706683685, + "narHash": "sha256-FtPPshEpxH/ewBOsdKBNhlsL2MLEFv1hEnQ19f/bFsQ=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "eabe8d3eface69f5bb16c18f8662a702f50c20d5", + "rev": "5ad9903c16126a7d949101687af0aa589b1d7d3d", "type": "github" }, "original": { @@ -177,11 +177,11 @@ }, "nixpkgs_3": { "locked": { - "lastModified": 1702272962, - "narHash": "sha256-D+zHwkwPc6oYQ4G3A1HuadopqRwUY/JkMwHz1YF7j4Q=", + "lastModified": 1706683685, + "narHash": "sha256-FtPPshEpxH/ewBOsdKBNhlsL2MLEFv1hEnQ19f/bFsQ=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "e97b3e4186bcadf0ef1b6be22b8558eab1cdeb5d", + "rev": "5ad9903c16126a7d949101687af0aa589b1d7d3d", "type": "github" }, "original": { @@ -193,11 +193,11 @@ }, "nixpkgs_4": { "locked": { - "lastModified": 1681358109, - "narHash": "sha256-eKyxW4OohHQx9Urxi7TQlFBTDWII+F+x2hklDOQPB50=", + "lastModified": 1706487304, + "narHash": "sha256-LE8lVX28MV2jWJsidW13D2qrHU/RUUONendL2Q/WlJg=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "96ba1c52e54e74c3197f4d43026b3f3d92e83ff9", + "rev": "90f456026d284c22b3e3497be980b2e47d0b28ac", "type": "github" }, "original": { @@ -209,11 +209,11 @@ }, "nixpkgs_5": { "locked": { - "lastModified": 1704842529, - "narHash": "sha256-OTeQA+F8d/Evad33JMfuXC89VMetQbsU4qcaePchGr4=", + "lastModified": 1706683685, + "narHash": "sha256-FtPPshEpxH/ewBOsdKBNhlsL2MLEFv1hEnQ19f/bFsQ=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "eabe8d3eface69f5bb16c18f8662a702f50c20d5", + "rev": "5ad9903c16126a7d949101687af0aa589b1d7d3d", "type": "github" }, "original": { @@ -235,11 +235,11 @@ "nixpkgs": "nixpkgs_4" }, "locked": { - "lastModified": 1702347444, - "narHash": "sha256-ueDw7aQf4Xyk69XnDD0YNWDlFdlOgJGPeWFa7uu/cfw=", + "lastModified": 1706753617, + "narHash": "sha256-ZKqTFzhFwSWFEpQTJ0uXnfJBs5Y/po9/8TK4bzssdbs=", "owner": "oxalica", "repo": "rust-overlay", - "rev": "bc13176f27cf3be724d18924b4f6aa47686ca2e3", + "rev": "58be43ae223034217ea1bd58c73210644031b687", "type": "github" }, "original": { diff --git a/gen.list b/gen.list index 4fb083b..a1c896e 100644 --- a/gen.list +++ b/gen.list @@ -1,6 +1,24 @@ +convert/str_to_i16 +convert/str_to_i16_utils +convert/str_to_i32 +convert/str_to_i32_utils +convert/str_to_i64 +convert/str_to_i64_utils +convert/str_to_i8 +convert/str_to_i8_utils +convert/str_to_u16 +convert/str_to_u16_utils +convert/str_to_u32 +convert/str_to_u32_utils +convert/str_to_u64 +convert/str_to_u64_utils +convert/str_to_u8 +convert/str_to_u8_utils vec/vec_i64 vec/vec_i64_bool vec/vec_i64_bool_functions2 vec/vec_i64_bool_functions3 +vec/vec_i64_bool_sort vec/vec_i64_functions2 vec/vec_i64_functions3 +vec/vec_i64_sort diff --git a/include/me/convert/str_to_numbers.h b/include/me/convert/str_to_numbers.h new file mode 100644 index 0000000..180ad5c --- /dev/null +++ b/include/me/convert/str_to_numbers.h @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_numbers.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:08:13 by maiboyer #+# #+# */ +/* Updated: 2024/02/02 00:22:50 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR_TO_NUMBERS_H +#define STR_TO_NUMBERS_H + +#include "me/types.h" + +t_error str_to_isize(t_const_str str, t_u32 radix, t_isize *out); +t_error str_to_i64(t_const_str str, t_u32 radix, t_i64 *out); +t_error str_to_i32(t_const_str str, t_u32 radix, t_i32 *out); +t_error str_to_i16(t_const_str str, t_u32 radix, t_i16 *out); +t_error str_to_i8(t_const_str str, t_u32 radix, t_i8 *out); + +t_error str_to_usize(t_const_str str, t_u32 radix, t_usize *out); +t_error str_to_u64(t_const_str str, t_u32 radix, t_u64 *out); +t_error str_to_u32(t_const_str str, t_u32 radix, t_u32 *out); +t_error str_to_u16(t_const_str str, t_u32 radix, t_u16 *out); +t_error str_to_u8(t_const_str str, t_u32 radix, t_u8 *out); + +#endif /* STR_TO_NUMBERS_H */ diff --git a/input.toml b/input.toml index c18a4a1..c245ded 100644 --- a/input.toml +++ b/input.toml @@ -4,6 +4,7 @@ sources = [ "mecstd/generic_sources/src/vec_C__PREFIX__.c__TEMPLATE__", "mecstd/generic_sources/src/vec_C__PREFIX___functions2.c__TEMPLATE__", "mecstd/generic_sources/src/vec_C__PREFIX___functions3.c__TEMPLATE__", + "mecstd/generic_sources/src/vec_C__PREFIX___sort.c__TEMPLATE__", ] replace.C__TYPENAME__ = "type" replace.C__TYPEHEADER__ = "header_include" @@ -23,6 +24,109 @@ replace.C__TYPEHEADER__ = "header_include" replace.C__PREFIX__ = "prefix" replace.C__PREFIXUP__ = "prefix" +[definition.str_to_num] +headers = [] +sources = [ + "mecstd/generic_sources/src/str_to_C__PREFIX__.c__TEMPLATE__", + "mecstd/generic_sources/src/str_to_C__PREFIX___utils.c__TEMPLATE__", +] +replace.C__TYPE__ = "type" +replace.C__UNSIGNED_TYPE__ = "type" +replace.C__PREFIX__ = "prefix" +replace.C__MAX__ = "value" +replace.C__MIN__ = "value" +replace.C__ZERO__ = "value" +replace.C__SIGNED_TYPE__ = "bool" + +[[create.str_to_num]] +sources_output = "src/convert/" +headers_output = "" +replace.C__TYPE__ = "t_i64" +replace.C__UNSIGNED_TYPE__ = "t_u64" +replace.C__PREFIX__ = "i64" +replace.C__MAX__ = "9223372036854775807ll" +replace.C__MIN__ = "-9223372036854775808ll" +replace.C__ZERO__ = "0ll" +replace.C__SIGNED_TYPE__ = "true" + +[[create.str_to_num]] +sources_output = "src/convert/" +headers_output = "" +replace.C__TYPE__ = "t_i32" +replace.C__UNSIGNED_TYPE__ = "t_u32" +replace.C__PREFIX__ = "i32" +replace.C__MAX__ = "2147483647" +replace.C__MIN__ = "-2147483648" +replace.C__ZERO__ = "0" +replace.C__SIGNED_TYPE__ = "true" + +[[create.str_to_num]] +sources_output = "src/convert/" +headers_output = "" +replace.C__TYPE__ = "t_i16" +replace.C__UNSIGNED_TYPE__ = "t_u16" +replace.C__PREFIX__ = "i16" +replace.C__MAX__ = "32767" +replace.C__MIN__ = "-32768" +replace.C__ZERO__ = "0" +replace.C__SIGNED_TYPE__ = "true" + +[[create.str_to_num]] +sources_output = "src/convert/" +headers_output = "" +replace.C__TYPE__ = "t_i8" +replace.C__UNSIGNED_TYPE__ = "t_u8" +replace.C__PREFIX__ = "i8" +replace.C__MAX__ = "127" +replace.C__MIN__ = "-128" +replace.C__ZERO__ = "0" +replace.C__SIGNED_TYPE__ = "true" + +[[create.str_to_num]] +sources_output = "src/convert/" +headers_output = "" +replace.C__TYPE__ = "t_u64" +replace.C__UNSIGNED_TYPE__ = "t_u64" +replace.C__PREFIX__ = "u64" +replace.C__MAX__ = "18446744073709551615llu" +replace.C__MIN__ = "0llu" +replace.C__ZERO__ = "0llu" +replace.C__SIGNED_TYPE__ = "false" + +[[create.str_to_num]] +sources_output = "src/convert/" +headers_output = "" +replace.C__TYPE__ = "t_u32" +replace.C__UNSIGNED_TYPE__ = "t_u32" +replace.C__PREFIX__ = "u32" +replace.C__MAX__ = "4294967295u" +replace.C__MIN__ = "0u" +replace.C__ZERO__ = "0u" +replace.C__SIGNED_TYPE__ = "false" + +[[create.str_to_num]] +sources_output = "src/convert/" +headers_output = "" +replace.C__TYPE__ = "t_u16" +replace.C__UNSIGNED_TYPE__ = "t_u16" +replace.C__PREFIX__ = "u16" +replace.C__MAX__ = "65535u" +replace.C__MIN__ = "0u" +replace.C__ZERO__ = "0u" +replace.C__SIGNED_TYPE__ = "false" + + +[[create.str_to_num]] +sources_output = "src/convert/" +headers_output = "" +replace.C__TYPE__ = "t_u8" +replace.C__UNSIGNED_TYPE__ = "t_u8" +replace.C__PREFIX__ = "u8" +replace.C__MAX__ = "128u" +replace.C__MIN__ = "0u" +replace.C__ZERO__ = "0u" +replace.C__SIGNED_TYPE__ = "false" + [[create.vec]] sources_output = "src/vec/" headers_output = "include/me/vec/" diff --git a/mecstd b/mecstd index b032e9d..54174ae 160000 --- a/mecstd +++ b/mecstd @@ -1 +1 @@ -Subproject commit b032e9dcc8c869b4759958e50597cef690353ccc +Subproject commit 54174ae7cea63a007dbde26f470b842d78d43a19 diff --git a/src.list b/src.list index e6591e3..8a1324a 100644 --- a/src.list +++ b/src.list @@ -2,10 +2,12 @@ app/best_move app/do_move app/find_place app/iter_find +app/main app/moves app/moves/push app/moves/rev_rotate app/moves/rotate app/moves/swap app/rotate +app/run_with_items app/target diff --git a/src/app/main.c b/src/app/main.c new file mode 100644 index 0000000..a1f14c2 --- /dev/null +++ b/src/app/main.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:00:12 by maiboyer #+# #+# */ +/* Updated: 2024/02/02 01:05:32 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/state.h" +#include "app/types/type_i64_bool.h" +#include "me/convert/str_to_numbers.h" +#include "me/printf/printf.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" +#include "me/vec/vec_i64_bool.h" +#include + +bool sort_i64_bool(t_i64_bool *lhs, t_i64_bool *rhs) +{ + return (lhs->value <= rhs->value); +} +void free_state(t_state state) +{ + vec_i64_free(state.stack_a); + vec_i64_free(state.stack_b); + vec_i64_bool_free(state.sorted); +} + +t_state parses_arguments(t_usize count, t_str nums[]) +{ + + t_state state; + t_i32 atoi; + t_usize i; + + state.stack_a = vec_i64_new(count, NULL); + state.stack_b = vec_i64_new(count, NULL); + state.sorted = vec_i64_bool_new(count, NULL); + i = 0; + while (i < count) + { + if (str_to_i32(nums[i], 10, &atoi)) + (free_state(state), me_eprintf("Error:\nInvalid Number\n"), + exit(1)); + vec_i64_push(&state.stack_a, atoi); + vec_i64_bool_push(&state.sorted, + (t_i64_bool){.value = atoi, .active = false}); + i++; + } + vec_i64_bool_sort(&state.sorted, sort_i64_bool); + return (state); +} + +void run_with_items(t_state *state); + +int main(t_i32 argc, t_str argv[]) +{ + t_state state; + + (argc--, argv++); + state = parses_arguments(argc, argv); + run_with_items(&state); + for (t_usize i = 0; i < state.stack_a.len; i++) + { + ft_printf("%i\n", state.stack_a.buffer[i]); + } + (void)(state); +} diff --git a/src/app/run_with_items.c b/src/app/run_with_items.c index aff138b..abbb661 100644 --- a/src/app/run_with_items.c +++ b/src/app/run_with_items.c @@ -6,14 +6,19 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/31 15:12:47 by maiboyer #+# #+# */ -/* Updated: 2024/01/31 15:41:41 by maiboyer ### ########.fr */ +/* Updated: 2024/02/01 20:59:24 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ #include "app/best_move.h" +#include "app/find_iter.h" +#include "app/rotate.h" #include "app/state.h" +#include "app/target.h" #include "me/types.h" #include "me/vec/vec_i64.h" +#include "me/vec/vec_i64_bool.h" +#include void push_a(void *s); void push_b(void *s); @@ -43,7 +48,6 @@ t_usize best_index_to_move(t_state *state, enum e_stack_selector move_from, enum e_zero_position zero_pos) { t_vec_i64 *stack; - t_usize min_pos; t_usize min_val; t_usize i; t_usize tmp; @@ -55,7 +59,6 @@ t_usize best_index_to_move(t_state *state, enum e_stack_selector move_from, if (stack->len == 0) return (0); i = 1; - min_pos = 0; run_func_with_best_rotate_for_item(state, 0, (t_best_move_args){ .args = &tmp, @@ -80,7 +83,6 @@ t_usize best_index_to_move(t_state *state, enum e_stack_selector move_from, if (tmp < min_val) { min_val = tmp; - min_pos = i; } i++; } @@ -102,6 +104,8 @@ bool is_sorted(t_vec_i64 *v) void run_with_items(t_state *state) { + t_rotation rot; + if (is_sorted(&state->stack_a)) return; while (state->stack_a.len > state->stack_b.len) @@ -113,4 +117,23 @@ void run_with_items(t_state *state) { push_b(state); } + vec_i64_bool_reverse(&state->sorted); + make_sorted_all_false(state); + while (state->stack_b.len != 0) + { + do_sort_insert(state, best_index_to_move(state, STACK_B, MIN_ZERO_POS), + STACK_B, MIN_ZERO_POS); + } + rot = target(min_iter_zero_pos(&state->stack_a), 0, state->stack_a.len); + if (rot.value > flip(rot).value) + rot = flip(rot); + while (rot.value > 0) + { + if (rot.direction == FORWARD) + rotate_a(state); + else + rev_rotate_a(state); + rot.value--; + } + printf("is_sorted: %s\n", is_sorted(&state->stack_a) ? "true" : "false"); } From adb8fc17b7e699e808b0f43facae37768db94da8 Mon Sep 17 00:00:00 2001 From: Maix0 Date: Fri, 2 Feb 2024 01:07:59 +0100 Subject: [PATCH 10/29] removed unused headers --- include/app/cost.h | 32 -------------------------------- include/app/dup_state.h | 26 -------------------------- include/app/lis.h | 22 ---------------------- include/app/types/type_cost.h | 26 -------------------------- 4 files changed, 106 deletions(-) delete mode 100644 include/app/cost.h delete mode 100644 include/app/dup_state.h delete mode 100644 include/app/lis.h delete mode 100644 include/app/types/type_cost.h diff --git a/include/app/cost.h b/include/app/cost.h deleted file mode 100644 index 624d1ed..0000000 --- a/include/app/cost.h +++ /dev/null @@ -1,32 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* cost.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: maiboyer +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/01/12 21:13:51 by maiboyer #+# #+# */ -/* Updated: 2024/01/17 15:32:25 by maiboyer ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef COST_H -# define COST_H - - -# include "app/state.h" -# include "app/types/type_cost.h" -# include "me/types.h" - -t_cost alloc_cost(t_state *s); -void free_cost(t_cost self); -t_cost *calculate_cost(t_state *s); -void cost_for_index(t_state *s, t_usize index); - -typedef struct s_cost_iter_state -{ - t_cost *current_minimum; - t_usize max_index; -} t_cost_iter_state; - -#endif /* COST_H */ diff --git a/include/app/dup_state.h b/include/app/dup_state.h deleted file mode 100644 index bf4f05f..0000000 --- a/include/app/dup_state.h +++ /dev/null @@ -1,26 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* dup_state.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: maiboyer +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/01/11 15:58:29 by maiboyer #+# #+# */ -/* Updated: 2024/01/11 15:59:29 by maiboyer ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef DUP_STATE_H -# define DUP_STATE_H - - -# include "me/types.h" - -typedef struct s_dup_state -{ - t_i64 *lhs; - t_usize index_to_skip; - bool found_dup; -} t_dup_state; - -#endif /* DUP_STATE_H */ diff --git a/include/app/lis.h b/include/app/lis.h deleted file mode 100644 index aa6aba9..0000000 --- a/include/app/lis.h +++ /dev/null @@ -1,22 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* lis.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: maiboyer +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/01/11 19:03:30 by maiboyer #+# #+# */ -/* Updated: 2024/01/11 20:50:43 by maiboyer ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef LIS_H -# define LIS_H - -# include "me/types.h" -# include "me/vec/vec_i64.h" -# include "me/vec/vec_i64.h" - -t_vec_i64 lis(t_vec_i64 *elements); - -#endif /* LIS_H */ diff --git a/include/app/types/type_cost.h b/include/app/types/type_cost.h deleted file mode 100644 index 2efbb54..0000000 --- a/include/app/types/type_cost.h +++ /dev/null @@ -1,26 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* type_cost.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: maiboyer +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/01/12 21:20:00 by maiboyer #+# #+# */ -/* Updated: 2024/01/12 23:16:24 by maiboyer ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef TYPE_COST_H -# define TYPE_COST_H - -# include "me/types.h" -# include "me/vec/vec_moves.h" - -typedef struct s_cost -{ - t_usize index; - t_vec_moves moves; - bool active; -} t_cost; - -#endif /* TYPE_COST_H */ From f98cc2c30e13e22e1ef8257d9a5f18be5efc7026 Mon Sep 17 00:00:00 2001 From: Maix0 Date: Wed, 7 Feb 2024 14:46:49 +0100 Subject: [PATCH 11/29] update --- include/app/best_move.h | 34 ++++------ include/app/best_move_inner.h | 19 +----- include/app/rotate.h | 15 +++-- src/app/best_move.c | 117 +++++++++++++++++++--------------- src/app/do_move.c | 42 +----------- src/app/find_place.c | 4 +- src/app/main.c | 6 +- src/app/moves/push.c | 26 ++++---- src/app/moves/rev_rotate.c | 27 ++++---- src/app/moves/rotate.c | 21 +++--- src/app/moves/swap.c | 16 +++-- src/app/run_with_items.c | 116 +++++++++++++++++++++------------ src/app/target.c | 8 +-- 13 files changed, 224 insertions(+), 227 deletions(-) diff --git a/include/app/best_move.h b/include/app/best_move.h index d0e075b..afb748f 100644 --- a/include/app/best_move.h +++ b/include/app/best_move.h @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/29 20:10:21 by maiboyer #+# #+# */ -/* Updated: 2024/01/31 14:10:08 by maiboyer ### ########.fr */ +/* Updated: 2024/02/02 22:48:02 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,18 +14,7 @@ #define BEST_MOVE_H #include "app/state.h" - -enum e_stack_selector -{ - STACK_A, - STACK_B, -}; - -enum e_zero_position -{ - MIN_ZERO_POS, - MAX_ZERO_POS, -}; +#include "me/vec/vec_i64.h" struct s_functions { @@ -33,17 +22,20 @@ struct s_functions void (*reverse)(void *); }; +typedef t_usize (*t_iter_pos_func)(t_vec_i64 *); + typedef struct s_best_move_args { - enum e_stack_selector main_stack; - enum e_zero_position zero_pos; - void *args; - struct s_functions main; - struct s_functions other; - struct s_functions both; + t_iter_pos_func iter_func; + t_usize index; + t_vec_i64 *from; + t_vec_i64 *to; + void *args; + struct s_functions main; + struct s_functions other; + struct s_functions both; } t_best_move_args; -void run_func_with_best_rotate_for_item(t_state *state, t_usize index, - t_best_move_args data); +void run_func_with_best_rotate_for_item(t_state *state, t_best_move_args data); #endif /* BEST_MOVE_H */ diff --git a/include/app/best_move_inner.h b/include/app/best_move_inner.h index 3c419dc..c767e7a 100644 --- a/include/app/best_move_inner.h +++ b/include/app/best_move_inner.h @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/29 21:31:42 by maiboyer #+# #+# */ -/* Updated: 2024/01/31 14:12:25 by maiboyer ### ########.fr */ +/* Updated: 2024/02/02 22:47:44 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,23 +20,6 @@ #include "me/vec/vec_i64.h" typedef void (*t_banana_func)(void *); -typedef t_usize (*t_iter_pos_func)(t_vec_i64 *); - -enum e_best_move_stack_selector -{ - MAIN, - OTHER, - BOTH, -}; - -typedef struct s_best_move_args_but_better -{ - t_best_move_args data; - t_vec_i64 *main_stack; - t_vec_i64 *other_stack; - t_iter_pos_func iter_func; - -} t_best_move_args_but_better; static inline t_banana_func choose_func(struct s_functions *funcs, t_rotation rotate) diff --git a/include/app/rotate.h b/include/app/rotate.h index fa930ed..1d44033 100644 --- a/include/app/rotate.h +++ b/include/app/rotate.h @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/29 19:00:18 by maiboyer #+# #+# */ -/* Updated: 2024/01/29 19:27:19 by maiboyer ### ########.fr */ +/* Updated: 2024/02/02 22:42:31 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,30 +22,33 @@ enum e_rotation_direction }; typedef struct s_rotation { - t_usize value; - t_usize ring_size; + t_usize value; + t_usize ring_size; enum e_rotation_direction direction; } t_rotation; static inline t_rotation forward(t_usize by, t_usize ring_size) { return ((t_rotation){ - .value = by % ring_size, .ring_size = ring_size, .direction = FORWARD}); + .value = by, .ring_size = ring_size, .direction = FORWARD}); } static inline t_rotation reverse(t_usize by, t_usize ring_size) { return ((t_rotation){ - .value = by % ring_size, .ring_size = ring_size, .direction = REVERSE}); + .value = by, .ring_size = ring_size, .direction = REVERSE}); } static inline t_rotation flip(t_rotation rot) { enum e_rotation_direction flipped; - flipped = FORWARD; if (rot.direction == FORWARD) flipped = REVERSE; + else if (rot.direction == REVERSE) + flipped = FORWARD; + else + return (rot); return ((t_rotation){.value = rot.ring_size - rot.value, .ring_size = rot.ring_size, diff --git a/src/app/best_move.c b/src/app/best_move.c index c23fabc..0343954 100644 --- a/src/app/best_move.c +++ b/src/app/best_move.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/29 20:04:33 by maiboyer #+# #+# */ -/* Updated: 2024/01/31 14:25:00 by maiboyer ### ########.fr */ +/* Updated: 2024/02/02 23:01:46 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,14 +19,16 @@ #include "app/target.h" #include "me/types.h" #include "me/vec/vec_i64.h" +#include -static inline t_usize abs_diff(t_usize lhs, t_usize rhs) +/*static inline t_usize abs_diff(t_usize lhs, t_usize rhs) { if (lhs > rhs) return (lhs - rhs); else return (rhs - lhs); } +*/ static inline t_usize min(t_usize lhs, t_usize rhs) { @@ -35,8 +37,24 @@ static inline t_usize min(t_usize lhs, t_usize rhs) else return (lhs); } +static inline t_usize max(t_usize lhs, t_usize rhs) +{ + if (lhs < rhs) + return (rhs); + else + return (lhs); +} -static void find_least_move(t_rotation *main, t_rotation *other) +static inline t_usize move_count(t_rotation lhs, t_rotation rhs) +{ + if (lhs.direction == rhs.direction) + return (max(lhs.value, rhs.value)); + else + return (lhs.value + rhs.value); +} + +// static +void find_least_move(t_rotation *main, t_rotation *other) { t_usize main_fliped; t_usize other_fliped; @@ -44,85 +62,80 @@ static void find_least_move(t_rotation *main, t_rotation *other) t_rotation tmp; t_usize minimum; - none_fliped = main->value + other->value; - tmp = flip(*main); - main_fliped = abs_diff(tmp.value, other->value); - tmp = flip(*other); - other_fliped = abs_diff(tmp.value, main->value); + // return; + none_fliped = move_count(*main, *other); + main_fliped = move_count(flip(*main), *other); + other_fliped = move_count(*main, flip(*other)); minimum = min(none_fliped, min(main_fliped, other_fliped)); if (minimum == none_fliped) return; else if (minimum == main_fliped) - *main = flip(*main); + { + tmp = flip(*main); + *main = tmp; + } else if (minimum == other_fliped) - *other = flip(*other); + { + tmp = flip(*other); + *other = tmp; + } } +#include "me/printf/printf.h" -static void run_func_with_best_rotate_for_item_inner( - t_state *state, t_usize index, t_best_move_args_but_better data) +static void run_func_with_best_rotate_for_item_inner(t_state *state, + t_best_move_args data) { - t_usize other_size_len_min_1; t_usize target_index; t_rotation main; t_rotation other; t_usize i; - other_size_len_min_1 = data.other_stack->len; - if (other_size_len_min_1 == 0) - other_size_len_min_1++; - target_index = - (find_place(data.main_stack->buffer[index], state) + - (data.other_stack->len - data.iter_func(data.other_stack))) % - other_size_len_min_1; - main = target(0, index, data.main_stack->len); - other = target(target_index, 0, data.other_stack->len); - find_least_move(&main, &other); + target_index = (find_place(data.from->buffer[data.index], state) + + (data.to->len - data.iter_func(data.to))) % + max(data.to->len, 1); + main = target(0, data.index, data.from->len); + other = target(0, target_index, data.to->len); + if (main.direction != other.direction) + find_least_move(&main, &other); if (main.direction == other.direction) { i = 0; while (i++ < min(main.value, other.value)) - choose_func(&data.data.both, main)(data.data.args); + { + // me_eprintf("both\n"); + choose_func(&data.both, main)(data.args); + } i = 0; if (main.value > other.value) while (i++ < main.value - other.value) - choose_func(&data.data.main, main)(data.data.args); + { + // me_eprintf("main\n"); + choose_func(&data.main, main)(data.args); + } else while (i++ < other.value - main.value) - choose_func(&data.data.other, other)(data.data.args); + { + // me_eprintf("other\n"); + choose_func(&data.other, other)(data.args); + } } else { i = 0; while (i++ < main.value) - choose_func(&data.data.main, main)(data.data.args); + { + // me_eprintf("main\n"); + choose_func(&data.main, main)(data.args); + } i = 0; while (i++ < other.value) - choose_func(&data.data.other, other)(data.data.args); + { + // me_eprintf("other\n"); + choose_func(&data.other, other)(data.args); + } } } -void run_func_with_best_rotate_for_item(t_state *state, t_usize index, - t_best_move_args data) +void run_func_with_best_rotate_for_item(t_state *state, t_best_move_args data) { - t_best_move_args_but_better better_args; - - if (data.zero_pos == MIN_ZERO_POS) - better_args.iter_func = min_iter_zero_pos; - else if (data.zero_pos == MAX_ZERO_POS) - better_args.iter_func = max_iter_zero_pos; - else - return; - if (data.main_stack == STACK_A) - { - better_args.main_stack = &state->stack_a; - better_args.other_stack = &state->stack_b; - } - else if (data.main_stack == STACK_B) - { - better_args.main_stack = &state->stack_b; - better_args.other_stack = &state->stack_a; - } - else - return; - better_args.data = data; - run_func_with_best_rotate_for_item_inner(state, index, better_args); + run_func_with_best_rotate_for_item_inner(state, data); } diff --git a/src/app/do_move.c b/src/app/do_move.c index 756123a..23f0e49 100644 --- a/src/app/do_move.c +++ b/src/app/do_move.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/31 14:25:57 by maiboyer #+# #+# */ -/* Updated: 2024/01/31 15:11:24 by maiboyer ### ########.fr */ +/* Updated: 2024/02/02 22:54:12 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,43 +28,3 @@ void rev_rotate_a(void *s); void rev_rotate_b(void *s); void rev_rotate_both(void *s); -void do_sort_insert(t_state *state, t_usize index, - enum e_stack_selector move_from, - enum e_zero_position zero_pos) -{ - void (*pop_move)(void *); - t_vec_i64 *pushed_stack; - struct s_functions main; - struct s_functions other; - - if (move_from == STACK_A) - { - pushed_stack = &state->stack_b; - pop_move = push_b; - main.forward = rev_rotate_a; - main.reverse = rotate_a; - other.forward = rev_rotate_b; - other.reverse = rotate_b; - } - else - { - pushed_stack = &state->stack_a; - pop_move = push_a; - main.forward = rev_rotate_b; - main.reverse = rotate_b; - other.forward = rev_rotate_a; - other.reverse = rotate_a; - } - run_func_with_best_rotate_for_item( - state, index, - (t_best_move_args){ - .zero_pos = zero_pos, - .main_stack = move_from, - .args = state, - .both = {.forward = rev_rotate_both, .reverse = rotate_both}, - .main = main, - .other = other, - }); - (pop_move)(state); - make_sorted_true_for_elem(state, pushed_stack->buffer[0]); -} diff --git a/src/app/find_place.c b/src/app/find_place.c index 7636ac4..11d93ab 100644 --- a/src/app/find_place.c +++ b/src/app/find_place.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/29 22:01:12 by maiboyer #+# #+# */ -/* Updated: 2024/01/31 14:13:34 by maiboyer ### ########.fr */ +/* Updated: 2024/02/02 23:00:09 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ static void find_place_iter(t_usize index, t_i64_bool *elem, t_find_place_iter_state *state) { (void)(index); - if (!elem->active) + if (!(elem->active || elem->value == state->to_find_elem)) return; if (elem->value == state->to_find_elem) state->found_index = state->current_index; diff --git a/src/app/main.c b/src/app/main.c index a1f14c2..7bf85d4 100644 --- a/src/app/main.c +++ b/src/app/main.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/01 21:00:12 by maiboyer #+# #+# */ -/* Updated: 2024/02/02 01:05:32 by maiboyer ### ########.fr */ +/* Updated: 2024/02/02 22:06:51 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -66,7 +66,7 @@ int main(t_i32 argc, t_str argv[]) run_with_items(&state); for (t_usize i = 0; i < state.stack_a.len; i++) { - ft_printf("%i\n", state.stack_a.buffer[i]); + //me_eprintf("%i\n", state.stack_a.buffer[i]); } - (void)(state); + free_state(state); } diff --git a/src/app/moves/push.c b/src/app/moves/push.c index cd6c296..a9ecd7f 100644 --- a/src/app/moves/push.c +++ b/src/app/moves/push.c @@ -6,32 +6,34 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 16:22:54 by maiboyer #+# #+# */ -/* Updated: 2024/01/11 17:17:45 by maiboyer ### ########.fr */ +/* Updated: 2024/02/02 21:40:17 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ - #include "app/state.h" +#include "me/printf/printf.h" #include "me/vec/vec_i64.h" -static inline void push_inner(t_vec_i64 *to, t_vec_i64 *from, - t_const_str tag) +static inline void push_inner(t_vec_i64 *to, t_vec_i64 *from, t_const_str tag, + t_const_str print) { - t_i64 e; + t_i64 e; (void)(tag); if (from->len == 0) - return ; - vec_i64_pop(from, &e); - vec_i64_push(to, e); + return; + vec_i64_pop_front(from, &e); + vec_i64_push_front(to, e); + if (print) + ft_printf("%s\n", print); } -void push_a(t_state *s) +void push_a(t_state *s) { - push_inner(&s->stack_a, &s->stack_b, "Push A"); + push_inner(&s->stack_a, &s->stack_b, "Push A", "pa"); } -void push_b(t_state *s) +void push_b(t_state *s) { - push_inner(&s->stack_b, &s->stack_a, "Push B"); + push_inner(&s->stack_b, &s->stack_a, "Push B", "pb"); } diff --git a/src/app/moves/rev_rotate.c b/src/app/moves/rev_rotate.c index 29ae711..642c5d0 100644 --- a/src/app/moves/rev_rotate.c +++ b/src/app/moves/rev_rotate.c @@ -6,36 +6,39 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 16:26:04 by maiboyer #+# #+# */ -/* Updated: 2024/01/31 14:57:21 by maiboyer ### ########.fr */ +/* Updated: 2024/02/02 14:12:23 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ - #include "app/state.h" +#include "me/printf/printf.h" -static inline void rev_rotate_inner(t_vec_i64 *stack, t_const_str tag) +static inline void rev_rotate_inner(t_vec_i64 *stack, t_const_str tag, + t_const_str print) { - t_i64 e; + t_i64 e; (void)(tag); if (stack->len <= 1) - return ; + return; vec_i64_pop(stack, &e); vec_i64_push_front(stack, e); + if (print) + ft_printf("%s\n", print); } -void rev_rotate_a(t_state *s) +void rev_rotate_a(t_state *s) { - rev_rotate_inner(&s->stack_a, "RevRotate A"); + rev_rotate_inner(&s->stack_a, "RevRotate A", "rra"); } -void rev_rotate_b(t_state *s) +void rev_rotate_b(t_state *s) { - rev_rotate_inner(&s->stack_b, "RevRotate B"); + rev_rotate_inner(&s->stack_b, "RevRotate B", "rrb"); } -void rev_rotate_both(t_state *s) +void rev_rotate_both(t_state *s) { - rev_rotate_inner(&s->stack_a, "RevRotate Both"); - rev_rotate_inner(&s->stack_b, "RevRotate Both"); + rev_rotate_inner(&s->stack_a, "RevRotate Both", NULL); + rev_rotate_inner(&s->stack_b, "RevRotate Both", "rrr"); } diff --git a/src/app/moves/rotate.c b/src/app/moves/rotate.c index 23cb016..51afccc 100644 --- a/src/app/moves/rotate.c +++ b/src/app/moves/rotate.c @@ -6,35 +6,40 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 16:26:04 by maiboyer #+# #+# */ -/* Updated: 2024/01/31 14:58:10 by maiboyer ### ########.fr */ +/* Updated: 2024/02/02 22:34:51 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ #include "app/state.h" +#include "me/printf/printf.h" -static inline void rotate_inner(t_vec_i64 *stack, t_const_str tag) +static inline void rotate_inner(t_vec_i64 *stack, t_const_str tag, + t_const_str print) { t_i64 e; (void)(tag); if (stack->len <= 1) return; - vec_i64_pop(stack, &e); - vec_i64_push_front(stack, e); + vec_i64_pop_front(stack, &e); + vec_i64_push(stack, e); + if (print) + ft_printf("%s\n", print); } void rotate_a(t_state *s) { - rotate_inner(&s->stack_a, "Rotate A"); + rotate_inner(&s->stack_a, "Rotate A", "ra"); } void rotate_b(t_state *s) { - rotate_inner(&s->stack_b, "Rotate B"); + rotate_inner(&s->stack_b, "Rotate B", "rb"); } void rotate_both(t_state *s) { - rotate_inner(&s->stack_a, "Rotate Both"); - rotate_inner(&s->stack_b, "Rotate Both"); + me_eprintf("BOTHHHHHH!!!!"); + rotate_inner(&s->stack_a, "Rotate Both", NULL); + rotate_inner(&s->stack_b, "Rotate Both", "rr"); } diff --git a/src/app/moves/swap.c b/src/app/moves/swap.c index 416abd7..4b90c5d 100644 --- a/src/app/moves/swap.c +++ b/src/app/moves/swap.c @@ -6,14 +6,16 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 16:26:04 by maiboyer #+# #+# */ -/* Updated: 2024/01/31 14:58:32 by maiboyer ### ########.fr */ +/* Updated: 2024/02/02 14:14:12 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ #include "app/state.h" +#include "me/printf/printf.h" #include "me/vec/vec_i64.h" -static inline void swap_inner(t_vec_i64 *stack, t_const_str tag) +static inline void swap_inner(t_vec_i64 *stack, t_const_str tag, + t_const_str print) { t_i64 first; t_i64 second; @@ -25,20 +27,22 @@ static inline void swap_inner(t_vec_i64 *stack, t_const_str tag) vec_i64_pop(stack, &second); vec_i64_push(stack, first); vec_i64_push(stack, second); + if (print) + ft_printf("%s\n", print); } void swap_a(t_state *s) { - swap_inner(&s->stack_a, "Swap A"); + swap_inner(&s->stack_a, "Swap A", "sa"); } void swap_b(t_state *s) { - swap_inner(&s->stack_b, "Swap B"); + swap_inner(&s->stack_b, "Swap B", "sb"); } void swap_both(t_state *s) { - swap_inner(&s->stack_a, "Swap Both"); - swap_inner(&s->stack_b, "Swap Both"); + swap_inner(&s->stack_a, "Swap Both", NULL); + swap_inner(&s->stack_b, "Swap Both", "ss"); } diff --git a/src/app/run_with_items.c b/src/app/run_with_items.c index abbb661..eab39a6 100644 --- a/src/app/run_with_items.c +++ b/src/app/run_with_items.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/31 15:12:47 by maiboyer #+# #+# */ -/* Updated: 2024/02/01 20:59:24 by maiboyer ### ########.fr */ +/* Updated: 2024/02/02 22:55:08 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,7 @@ #include "app/rotate.h" #include "app/state.h" #include "app/target.h" +#include "me/printf/printf.h" #include "me/types.h" #include "me/vec/vec_i64.h" #include "me/vec/vec_i64_bool.h" @@ -32,11 +33,7 @@ void rev_rotate_a(void *s); void rev_rotate_b(void *s); void rev_rotate_both(void *s); -void do_sort_insert(t_state *state, t_usize index, - enum e_stack_selector move_from, - enum e_zero_position zero_pos); - -void inc(void *s) +static void inc(void *s) { t_usize *p; @@ -44,49 +41,40 @@ void inc(void *s) *p += 1; } -t_usize best_index_to_move(t_state *state, enum e_stack_selector move_from, - enum e_zero_position zero_pos) +t_usize best_index_to_move(t_state *state, t_vec_i64 *from, t_vec_i64 *to, + t_iter_pos_func f) { - t_vec_i64 *stack; t_usize min_val; + t_usize min_pos; t_usize i; t_usize tmp; - if (move_from == STACK_A) - stack = &state->stack_a; - else - stack = &state->stack_b; - if (stack->len == 0) + if (from->len == 0) return (0); - i = 1; - run_func_with_best_rotate_for_item(state, 0, - (t_best_move_args){ - .args = &tmp, - .zero_pos = zero_pos, - .main_stack = move_from, - .other = {inc, inc}, - .main = {inc, inc}, - .both = {inc, inc}, - }); - min_val = tmp; - while (i < stack->len) + i = 0; + min_val = 0; + min_pos = 0; + while (i < from->len) { - run_func_with_best_rotate_for_item(state, i, - (t_best_move_args){ - .args = &tmp, - .zero_pos = zero_pos, - .main_stack = move_from, - .other = {inc, inc}, - .main = {inc, inc}, - .both = {inc, inc}, - }); + tmp = 1; + run_func_with_best_rotate_for_item(state, (t_best_move_args){ + .index = i, + .args = &tmp, + .iter_func = f, + .from = from, + .to = to, + .other = {inc, inc}, + .main = {inc, inc}, + .both = {inc, inc}, + }); if (tmp < min_val) { min_val = tmp; + min_pos = i; } i++; } - return (min_val); + return (min_pos); } bool is_sorted(t_vec_i64 *v) @@ -102,16 +90,43 @@ bool is_sorted(t_vec_i64 *v) return (true); } +void print_stack(t_const_str msg, t_vec_i64 *stack) +{ + return; + me_eprintf("[%s] = [\n", msg); + + for (t_usize i = 0; i < stack->len; i++) + me_eprintf("\t%i\n", stack->buffer[i]); + me_eprintf("]\n"); +} + void run_with_items(t_state *state) { t_rotation rot; + t_usize idx; if (is_sorted(&state->stack_a)) return; while (state->stack_a.len > state->stack_b.len) { - do_sort_insert(state, best_index_to_move(state, STACK_A, MAX_ZERO_POS), - STACK_A, MAX_ZERO_POS); + idx = best_index_to_move(state, &state->stack_a, &state->stack_b, + &max_iter_zero_pos); + run_func_with_best_rotate_for_item( + state, + (t_best_move_args){ + .index = idx, + .iter_func = &max_iter_zero_pos, + .from = &state->stack_a, + .to = &state->stack_b, + .args = state, + .both = {.forward = rotate_both, .reverse = rev_rotate_both}, + .main = {.forward = rotate_a, .reverse = rev_rotate_a}, + .other = {.forward = rotate_b, .reverse = rev_rotate_b}, + }); + push_b(state); + make_sorted_true_from_stack(state, &state->stack_b); + print_stack("f_stack_a", &state->stack_a); + print_stack("f_stack_b", &state->stack_b); } while (state->stack_a.len != 0) { @@ -121,10 +136,26 @@ void run_with_items(t_state *state) make_sorted_all_false(state); while (state->stack_b.len != 0) { - do_sort_insert(state, best_index_to_move(state, STACK_B, MIN_ZERO_POS), - STACK_B, MIN_ZERO_POS); + idx = best_index_to_move(state, &state->stack_b, &state->stack_a, + &min_iter_zero_pos); + run_func_with_best_rotate_for_item( + state, + (t_best_move_args){ + .index = idx, + .from = &state->stack_b, + .to = &state->stack_a, + .iter_func = &min_iter_zero_pos, + .args = state, + .both = {.forward = rotate_both, .reverse = rev_rotate_both}, + .main = {.forward = rotate_b, .reverse = rev_rotate_b}, + .other = {.forward = rotate_a, .reverse = rev_rotate_a}, + }); + push_a(state); + make_sorted_true_from_stack(state, &state->stack_a); + print_stack("s_stack_a", &state->stack_a); + print_stack("s_stack_b", &state->stack_b); } - rot = target(min_iter_zero_pos(&state->stack_a), 0, state->stack_a.len); + rot = target(0, min_iter_zero_pos(&state->stack_a), state->stack_a.len); if (rot.value > flip(rot).value) rot = flip(rot); while (rot.value > 0) @@ -135,5 +166,6 @@ void run_with_items(t_state *state) rev_rotate_a(state); rot.value--; } - printf("is_sorted: %s\n", is_sorted(&state->stack_a) ? "true" : "false"); + me_eprintf("is_sorted: %s\n", + is_sorted(&state->stack_a) ? "true" : "false"); } diff --git a/src/app/target.c b/src/app/target.c index 26c62e7..1fc1b55 100644 --- a/src/app/target.c +++ b/src/app/target.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/29 20:30:27 by maiboyer #+# #+# */ -/* Updated: 2024/01/29 22:29:50 by maiboyer ### ########.fr */ +/* Updated: 2024/02/02 22:40:55 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,8 +17,8 @@ t_rotation target(t_usize from, t_usize to, t_usize ring_size) { if (ring_size == 0) ring_size++; - if (from < to) - return (forward(to - from, ring_size)); - else + if (from > to) return (reverse(from - to, ring_size)); + else + return (forward(to - from, ring_size)); } From 3ab3a384c3d1709460e37c7820e343945e0c6dd3 Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Wed, 7 Feb 2024 15:23:24 +0000 Subject: [PATCH 12/29] update to make a new setup easier --- .gitignore | 1 + mecstd | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 6bd3e47..2dab770 100644 --- a/.gitignore +++ b/.gitignore @@ -56,3 +56,4 @@ dkms.conf test output/ push_swap +push_swap_visualizer diff --git a/mecstd b/mecstd index 54174ae..f4c3ed9 160000 --- a/mecstd +++ b/mecstd @@ -1 +1 @@ -Subproject commit 54174ae7cea63a007dbde26f470b842d78d43a19 +Subproject commit f4c3ed9f01ed41b9579b3881fde43fe2a8b5993e From ed0c78d8a299701274a28bf3f3bbcd4b6428b5b0 Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Thu, 8 Feb 2024 14:27:45 +0100 Subject: [PATCH 13/29] everything in src|include should pass the norminette now --- Makefile | 6 +- flake.lock | 43 +++++++- flake.nix | 13 ++- include/app/best_index_to_move.h | 30 ++++++ include/app/best_move.h | 38 +++---- include/app/best_move_inner.h | 44 ++++++-- include/app/find_iter.h | 10 +- include/app/find_place.h | 14 +-- include/app/iter_state.h | 8 +- include/app/moves.h | 1 - include/app/rotate.h | 38 ++++--- include/app/state.h | 34 +++---- include/app/target.h | 8 +- include/app/types/type_i64_bool.h | 10 +- include/me/convert/str_to_numbers.h | 24 ++--- mecstd | 2 +- src.list | 1 + src/app/best_index_to_move.c | 77 ++++++++++++++ src/app/best_move.c | 118 +++++++-------------- src/app/do_move.c | 23 ++--- src/app/find_place.c | 14 +-- src/app/iter_find.c | 12 +-- src/app/main.c | 34 +++---- src/app/moves.c | 21 ++-- src/app/moves/push.c | 12 +-- src/app/moves/rev_rotate.c | 14 +-- src/app/moves/rotate.c | 14 +-- src/app/moves/swap.c | 16 +-- src/app/rotate.c | 1 - src/app/run_with_items.c | 153 +++++++++------------------- src/app/target.c | 4 +- 31 files changed, 453 insertions(+), 384 deletions(-) create mode 100644 include/app/best_index_to_move.h create mode 100644 src/app/best_index_to_move.c diff --git a/Makefile b/Makefile index 780fe88..c8ff40e 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: maiboyer +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/03 13:20:01 by maiboyer #+# #+# # -# Updated: 2024/01/11 14:14:03 by maiboyer ### ########.fr # +# Updated: 2024/02/08 14:25:56 by maiboyer ### ########.fr # # # # **************************************************************************** # @@ -27,8 +27,8 @@ endif NAME = push_swap LIB_NAME ?= TARGET = $(NAME) -CC = clang -CFLAGS += -Wall -Wextra -Werror -g2 -lme -L$(BUILD_DIR) -Wno-unused-command-line-argument -MMD +CC ?= clang +CFLAGS += -Wall -Werror -Wextra -g2 -lme -L$(BUILD_DIR) -Wno-unused-command-line-argument -MMD BONUS_FILES = LIBS_NAME = mecstd SUBJECT_URL = 'https://cdn.intra.42.fr/pdf/pdf/118610/en.subject.pdf' diff --git a/flake.lock b/flake.lock index 1589801..d2e09c7 100644 --- a/flake.lock +++ b/flake.lock @@ -108,6 +108,21 @@ "type": "github" } }, + "flake-utils_5": { + "locked": { + "lastModified": 1659877975, + "narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, "generic_c": { "inputs": { "flake-utils": "flake-utils_3", @@ -147,6 +162,27 @@ "type": "github" } }, + "nixGL": { + "inputs": { + "flake-utils": "flake-utils_5", + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1685908677, + "narHash": "sha256-E4zUPEUFyVWjVm45zICaHRpfGepfkE9Z2OECV9HXfA4=", + "owner": "guibou", + "repo": "nixGL", + "rev": "489d6b095ab9d289fe11af0219a9ff00fe87c7c5", + "type": "github" + }, + "original": { + "owner": "guibou", + "repo": "nixGL", + "type": "github" + } + }, "nixpkgs": { "locked": { "lastModified": 1700108881, @@ -209,11 +245,11 @@ }, "nixpkgs_5": { "locked": { - "lastModified": 1706683685, - "narHash": "sha256-FtPPshEpxH/ewBOsdKBNhlsL2MLEFv1hEnQ19f/bFsQ=", + "lastModified": 1706925685, + "narHash": "sha256-hVInjWMmgH4yZgA4ZtbgJM1qEAel72SYhP5nOWX4UIM=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "5ad9903c16126a7d949101687af0aa589b1d7d3d", + "rev": "79a13f1437e149dc7be2d1290c74d378dad60814", "type": "github" }, "original": { @@ -226,6 +262,7 @@ "c_formatter_42": "c_formatter_42", "flake-utils": "flake-utils_2", "generic_c": "generic_c", + "nixGL": "nixGL", "nixpkgs": "nixpkgs_5" } }, diff --git a/flake.nix b/flake.nix index 24419ec..3a31d1f 100644 --- a/flake.nix +++ b/flake.nix @@ -5,6 +5,8 @@ flake-utils.url = "github:numtide/flake-utils"; generic_c.url = "github:maix0/generic_c"; c_formatter_42.url = "github:maix0/c_formatter_42-flake"; + nixGL.url = "github:guibou/nixGL"; + nixGL.inputs.nixpkgs.follows = "nixpkgs"; }; outputs = { self, @@ -12,10 +14,14 @@ flake-utils, generic_c, c_formatter_42, + nixGL, }: flake-utils.lib.eachDefaultSystem ( system: let - pkgs = nixpkgs.legacyPackages.${system}; + pkgs = import nixpkgs { + inherit system; + overlays = [nixGL.overlay]; + }; in { devShell = pkgs.mkShell { packages = [ @@ -24,14 +30,15 @@ pkgs.clang pkgs.clang-tools pkgs.norminette - generic_c.packages.${system}.default - c_formatter_42.packages.${system}.default pkgs.poppler_utils pkgs.minilibx pkgs.valgrind pkgs.libbsd pkgs.tree pkgs.fastmod + generic_c.packages.${system}.default + c_formatter_42.packages.${system}.default + pkgs.nixgl.nixGLIntel ]; }; } diff --git a/include/app/best_index_to_move.h b/include/app/best_index_to_move.h new file mode 100644 index 0000000..53e5ed7 --- /dev/null +++ b/include/app/best_index_to_move.h @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* best_index_to_move.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/08 14:18:43 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 14:20:01 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef BEST_INDEX_TO_MOVE_H +# define BEST_INDEX_TO_MOVE_H + +# include "app/best_move.h" +# include "app/state.h" +# include "me/types.h" +# include "me/vec/vec_i64.h" + +struct s_best_index_to_move +{ + t_state *state; + t_vec_i64 *from; + t_vec_i64 *to; + t_iter_pos_func f; + t_usize i; +}; + +#endif /* BEST_INDEX_TO_MOVE_H */ diff --git a/include/app/best_move.h b/include/app/best_move.h index afb748f..f78fb2b 100644 --- a/include/app/best_move.h +++ b/include/app/best_move.h @@ -6,36 +6,38 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/29 20:10:21 by maiboyer #+# #+# */ -/* Updated: 2024/02/02 22:48:02 by maiboyer ### ########.fr */ +/* Updated: 2024/02/08 13:30:30 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef BEST_MOVE_H -#define BEST_MOVE_H +# define BEST_MOVE_H -#include "app/state.h" -#include "me/vec/vec_i64.h" +# include "app/state.h" +# include "me/vec/vec_i64.h" -struct s_functions +struct s_functions { - void (*forward)(void *); - void (*reverse)(void *); + void (*forward)(void *); + void (*reverse)(void *); }; -typedef t_usize (*t_iter_pos_func)(t_vec_i64 *); +typedef t_usize (*t_iter_pos_func)(t_vec_i64 *); typedef struct s_best_move_args { - t_iter_pos_func iter_func; - t_usize index; - t_vec_i64 *from; - t_vec_i64 *to; - void *args; - struct s_functions main; - struct s_functions other; - struct s_functions both; -} t_best_move_args; + t_iter_pos_func iter_func; + t_usize index; + t_vec_i64 *from; + t_vec_i64 *to; + void *args; + struct s_functions main; + struct s_functions other; + struct s_functions both; + bool print_stuff; +} t_best_move_args; -void run_func_with_best_rotate_for_item(t_state *state, t_best_move_args data); +void run_func_with_best_rotate_for_item(t_state *state, + t_best_move_args data); #endif /* BEST_MOVE_H */ diff --git a/include/app/best_move_inner.h b/include/app/best_move_inner.h index c767e7a..902c125 100644 --- a/include/app/best_move_inner.h +++ b/include/app/best_move_inner.h @@ -6,23 +6,23 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/29 21:31:42 by maiboyer #+# #+# */ -/* Updated: 2024/02/02 22:47:44 by maiboyer ### ########.fr */ +/* Updated: 2024/02/08 14:22:49 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef BEST_MOVE_INNER_H -#define BEST_MOVE_INNER_H +# define BEST_MOVE_INNER_H -#include "app/best_move.h" -#include "app/rotate.h" -#include "app/state.h" -#include "me/types.h" -#include "me/vec/vec_i64.h" +# include "app/best_move.h" +# include "app/rotate.h" +# include "app/state.h" +# include "me/types.h" +# include "me/vec/vec_i64.h" -typedef void (*t_banana_func)(void *); +typedef void (*t_banana_func)(void *); -static inline t_banana_func choose_func(struct s_functions *funcs, - t_rotation rotate) +static inline t_banana_func choose_func(struct s_functions *funcs, + t_rotation rotate) { if (rotate.direction == FORWARD) return (funcs->forward); @@ -31,4 +31,28 @@ static inline t_banana_func choose_func(struct s_functions *funcs, return (NULL); } +static inline t_isize abs_diff(t_isize lhs, t_isize rhs) +{ + if (lhs > rhs) + return (lhs - rhs); + else + return (rhs - lhs); +} + +static inline t_isize min(t_isize lhs, t_isize rhs) +{ + if (lhs > rhs) + return (rhs); + else + return (lhs); +} + +static inline t_isize max(t_isize lhs, t_isize rhs) +{ + if (lhs < rhs) + return (rhs); + else + return (lhs); +} + #endif /* BEST_MOVE_INNER_H */ diff --git a/include/app/find_iter.h b/include/app/find_iter.h index 0f445c6..ff9b460 100644 --- a/include/app/find_iter.h +++ b/include/app/find_iter.h @@ -11,12 +11,12 @@ /* ************************************************************************** */ #ifndef FIND_ITER_H -#define FIND_ITER_H +# define FIND_ITER_H -#include "me/types.h" -#include "me/vec/vec_i64.h" +# include "me/types.h" +# include "me/vec/vec_i64.h" -t_usize min_iter_zero_pos(t_vec_i64 *vec); -t_usize max_iter_zero_pos(t_vec_i64 *vec); +t_usize min_iter_zero_pos(t_vec_i64 *vec); +t_usize max_iter_zero_pos(t_vec_i64 *vec); #endif /* FIND_ITER_H */ diff --git a/include/app/find_place.h b/include/app/find_place.h index 00484f6..234e7b1 100644 --- a/include/app/find_place.h +++ b/include/app/find_place.h @@ -11,19 +11,19 @@ /* ************************************************************************** */ #ifndef FIND_PLACE_H -#define FIND_PLACE_H +# define FIND_PLACE_H -#include "app/state.h" -#include "me/types.h" +# include "app/state.h" +# include "me/types.h" typedef struct s_find_place_iter_state { - t_usize current_index; - t_usize found_index; + t_usize current_index; + t_usize found_index; t_i64 to_find_elem; -} t_find_place_iter_state; +} t_find_place_iter_state; -t_usize find_place(t_i64 elem, t_state *state); +t_usize find_place(t_i64 elem, t_state *state); #endif /* FIND_PLACE_H */ diff --git a/include/app/iter_state.h b/include/app/iter_state.h index 8eb554d..acc14be 100644 --- a/include/app/iter_state.h +++ b/include/app/iter_state.h @@ -11,14 +11,14 @@ /* ************************************************************************** */ #ifndef ITER_STATE_H -#define ITER_STATE_H +# define ITER_STATE_H -#include "me/types.h" +# include "me/types.h" typedef struct s_iter_state { - t_usize pos; + t_usize pos; t_i64 elem; -} t_iter_state; +} t_iter_state; #endif /* ITER_STATE_H */ diff --git a/include/app/moves.h b/include/app/moves.h index b2f6efc..db9b4f7 100644 --- a/include/app/moves.h +++ b/include/app/moves.h @@ -13,7 +13,6 @@ #ifndef MOVES_H # define MOVES_H - # include "app/state.h" # include "app/types/type_move.h" # include "me/types.h" diff --git a/include/app/rotate.h b/include/app/rotate.h index 1d44033..f393485 100644 --- a/include/app/rotate.h +++ b/include/app/rotate.h @@ -6,53 +6,51 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/29 19:00:18 by maiboyer #+# #+# */ -/* Updated: 2024/02/02 22:42:31 by maiboyer ### ########.fr */ +/* Updated: 2024/02/08 13:33:09 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef ROTATE_H -#define ROTATE_H +# define ROTATE_H -#include "me/types.h" +# include "me/types.h" -enum e_rotation_direction +enum e_rotation_direction { FORWARD, REVERSE, }; typedef struct s_rotation { - t_usize value; - t_usize ring_size; - enum e_rotation_direction direction; -} t_rotation; + t_isize value; + t_isize ring_size; + enum e_rotation_direction direction; +} t_rotation; -static inline t_rotation forward(t_usize by, t_usize ring_size) +static inline t_rotation forward(t_isize by, t_isize ring_size) { - return ((t_rotation){ - .value = by, .ring_size = ring_size, .direction = FORWARD}); + return ((t_rotation){.value = by, .ring_size = ring_size, + .direction = FORWARD}); } -static inline t_rotation reverse(t_usize by, t_usize ring_size) +static inline t_rotation reverse(t_isize by, t_isize ring_size) { - return ((t_rotation){ - .value = by, .ring_size = ring_size, .direction = REVERSE}); + return ((t_rotation){.value = by, .ring_size = ring_size, + .direction = REVERSE}); } -static inline t_rotation flip(t_rotation rot) +static inline t_rotation flip(t_rotation rot) { - enum e_rotation_direction flipped; + enum e_rotation_direction flipped; if (rot.direction == FORWARD) flipped = REVERSE; else if (rot.direction == REVERSE) flipped = FORWARD; - else + else return (rot); - return ((t_rotation){.value = rot.ring_size - rot.value, - .ring_size = rot.ring_size, - .direction = flipped}); + .ring_size = rot.ring_size, .direction = flipped}); } #endif /* ROTATE_H */ diff --git a/include/app/state.h b/include/app/state.h index 63c630a..f05612a 100644 --- a/include/app/state.h +++ b/include/app/state.h @@ -11,25 +11,25 @@ /* ************************************************************************** */ #ifndef STATE_H -#define STATE_H +# define STATE_H -#include "me/types.h" -#include "me/vec/vec_i64.h" -#include "me/vec/vec_i64_bool.h" +# include "me/types.h" +# include "me/vec/vec_i64.h" +# include "me/vec/vec_i64_bool.h" typedef struct s_state { - t_vec_i64_bool sorted; - t_vec_i64 stack_a; - t_vec_i64 stack_b; -} t_state; + t_vec_i64_bool sorted; + t_vec_i64 stack_a; + t_vec_i64 stack_b; +} t_state; -t_state parses_arguments(t_usize count, t_str nums[]); -void free_state(t_state state); +t_state parses_arguments(t_usize count, t_str nums[]); +void free_state(t_state state); -static inline void make_sorted_true_for_elem(t_state *s, t_i64 elem) +static inline void make_sorted_true_for_elem(t_state *s, t_i64 elem) { - t_usize i; + t_usize i; i = 0; while (i < s->sorted.len) @@ -37,15 +37,15 @@ static inline void make_sorted_true_for_elem(t_state *s, t_i64 elem) if (s->sorted.buffer[i].value == elem) { s->sorted.buffer[i].active = true; - return; + return ; } i++; } } -static inline void make_sorted_true_from_stack(t_state *s, t_vec_i64 *stack) +static inline void make_sorted_true_from_stack(t_state *s, t_vec_i64 *stack) { - t_usize i; + t_usize i; i = 0; while (i < stack->len) @@ -54,9 +54,9 @@ static inline void make_sorted_true_from_stack(t_state *s, t_vec_i64 *stack) } } -static inline void make_sorted_all_false(t_state *s) +static inline void make_sorted_all_false(t_state *s) { - t_usize i; + t_usize i; i = 0; while (i < s->sorted.len) diff --git a/include/app/target.h b/include/app/target.h index 6cee43b..204044e 100644 --- a/include/app/target.h +++ b/include/app/target.h @@ -11,11 +11,11 @@ /* ************************************************************************** */ #ifndef TARGET_H -#define TARGET_H +# define TARGET_H -#include "app/rotate.h" -#include "me/types.h" +# include "app/rotate.h" +# include "me/types.h" -t_rotation target(t_usize from, t_usize to, t_usize ring_size); +t_rotation target(t_usize from, t_usize to, t_usize ring_size); #endif /* TARGET_H */ diff --git a/include/app/types/type_i64_bool.h b/include/app/types/type_i64_bool.h index 58ffce1..48af825 100644 --- a/include/app/types/type_i64_bool.h +++ b/include/app/types/type_i64_bool.h @@ -11,14 +11,14 @@ /* ************************************************************************** */ #ifndef TYPE_I64_BOOL_H -#define TYPE_I64_BOOL_H +# define TYPE_I64_BOOL_H -#include "me/types.h" +# include "me/types.h" typedef struct s_i64_bool { - t_i64 value; - bool active; -} t_i64_bool; + t_i64 value; + bool active; +} t_i64_bool; #endif /* TYPE_I64_BOOL_H */ diff --git a/include/me/convert/str_to_numbers.h b/include/me/convert/str_to_numbers.h index 180ad5c..9f90ddf 100644 --- a/include/me/convert/str_to_numbers.h +++ b/include/me/convert/str_to_numbers.h @@ -11,20 +11,20 @@ /* ************************************************************************** */ #ifndef STR_TO_NUMBERS_H -#define STR_TO_NUMBERS_H +# define STR_TO_NUMBERS_H -#include "me/types.h" +# include "me/types.h" -t_error str_to_isize(t_const_str str, t_u32 radix, t_isize *out); -t_error str_to_i64(t_const_str str, t_u32 radix, t_i64 *out); -t_error str_to_i32(t_const_str str, t_u32 radix, t_i32 *out); -t_error str_to_i16(t_const_str str, t_u32 radix, t_i16 *out); -t_error str_to_i8(t_const_str str, t_u32 radix, t_i8 *out); +t_error str_to_isize(t_const_str str, t_u32 radix, t_isize *out); +t_error str_to_i64(t_const_str str, t_u32 radix, t_i64 *out); +t_error str_to_i32(t_const_str str, t_u32 radix, t_i32 *out); +t_error str_to_i16(t_const_str str, t_u32 radix, t_i16 *out); +t_error str_to_i8(t_const_str str, t_u32 radix, t_i8 *out); -t_error str_to_usize(t_const_str str, t_u32 radix, t_usize *out); -t_error str_to_u64(t_const_str str, t_u32 radix, t_u64 *out); -t_error str_to_u32(t_const_str str, t_u32 radix, t_u32 *out); -t_error str_to_u16(t_const_str str, t_u32 radix, t_u16 *out); -t_error str_to_u8(t_const_str str, t_u32 radix, t_u8 *out); +t_error str_to_usize(t_const_str str, t_u32 radix, t_usize *out); +t_error str_to_u64(t_const_str str, t_u32 radix, t_u64 *out); +t_error str_to_u32(t_const_str str, t_u32 radix, t_u32 *out); +t_error str_to_u16(t_const_str str, t_u32 radix, t_u16 *out); +t_error str_to_u8(t_const_str str, t_u32 radix, t_u8 *out); #endif /* STR_TO_NUMBERS_H */ diff --git a/mecstd b/mecstd index f4c3ed9..4cb7e19 160000 --- a/mecstd +++ b/mecstd @@ -1 +1 @@ -Subproject commit f4c3ed9f01ed41b9579b3881fde43fe2a8b5993e +Subproject commit 4cb7e19ed8aaa0c910f38fb6947f91b363556b4c diff --git a/src.list b/src.list index 8a1324a..3b26f15 100644 --- a/src.list +++ b/src.list @@ -1,3 +1,4 @@ +app/best_index_to_move app/best_move app/do_move app/find_place diff --git a/src/app/best_index_to_move.c b/src/app/best_index_to_move.c new file mode 100644 index 0000000..1b37701 --- /dev/null +++ b/src/app/best_index_to_move.c @@ -0,0 +1,77 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* best_index_to_move.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/08 14:14:54 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 14:21:51 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/best_index_to_move.h" +#include "app/best_move.h" +#include "app/find_iter.h" +#include "app/rotate.h" +#include "app/state.h" +#include "app/target.h" +#include "me/printf/printf.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" +#include "me/vec/vec_i64_bool.h" +#include + +static void inc(void *s) +{ + t_usize *p; + + p = (t_usize *)s; + *p += 1; +} + +static inline t_usize count_move_for_index(struct s_best_index_to_move d) +{ + t_usize tmp; + + tmp = 0; + run_func_with_best_rotate_for_item(d.state, + (t_best_move_args){ + .index = d.i, + .args = &tmp, + .iter_func = d.f, + .from = d.from, + .to = d.to, + .other = {inc, inc}, + .main = {inc, inc}, + .both = {inc, inc}, + }); + return (tmp); +} + +t_usize best_index_to_move(t_state *state, t_vec_i64 *from, t_vec_i64 *to, + t_iter_pos_func f) +{ + t_usize min_val; + t_usize min_pos; + t_usize i; + t_usize tmp; + + if (from->len == 0) + return (0); + i = 0; + min_val = ~0; + min_pos = 0; + while (i < from->len) + { + tmp = count_move_for_index((struct s_best_index_to_move){state, from, + to, f, i}); + if (tmp < min_val) + { + min_val = tmp; + min_pos = i; + } + i++; + } + return (min_pos); +} diff --git a/src/app/best_move.c b/src/app/best_move.c index 0343954..2bbbfdf 100644 --- a/src/app/best_move.c +++ b/src/app/best_move.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/29 20:04:33 by maiboyer #+# #+# */ -/* Updated: 2024/02/02 23:01:46 by maiboyer ### ########.fr */ +/* Updated: 2024/02/08 14:22:55 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,54 +21,28 @@ #include "me/vec/vec_i64.h" #include -/*static inline t_usize abs_diff(t_usize lhs, t_usize rhs) -{ - if (lhs > rhs) - return (lhs - rhs); - else - return (rhs - lhs); -} -*/ - -static inline t_usize min(t_usize lhs, t_usize rhs) -{ - if (lhs > rhs) - return (rhs); - else - return (lhs); -} -static inline t_usize max(t_usize lhs, t_usize rhs) -{ - if (lhs < rhs) - return (rhs); - else - return (lhs); -} - -static inline t_usize move_count(t_rotation lhs, t_rotation rhs) +static inline t_isize move_count(t_rotation lhs, t_rotation rhs) { if (lhs.direction == rhs.direction) return (max(lhs.value, rhs.value)); else - return (lhs.value + rhs.value); + return (abs_diff(lhs.value, rhs.value)); } -// static -void find_least_move(t_rotation *main, t_rotation *other) +void find_least_move(t_rotation *main, t_rotation *other) { - t_usize main_fliped; - t_usize other_fliped; - t_usize none_fliped; - t_rotation tmp; - t_usize minimum; + t_isize main_fliped; + t_isize other_fliped; + t_isize none_fliped; + t_rotation tmp; + t_isize minimum; - // return; none_fliped = move_count(*main, *other); main_fliped = move_count(flip(*main), *other); other_fliped = move_count(*main, flip(*other)); minimum = min(none_fliped, min(main_fliped, other_fliped)); if (minimum == none_fliped) - return; + return ; else if (minimum == main_fliped) { tmp = flip(*main); @@ -80,62 +54,46 @@ void find_least_move(t_rotation *main, t_rotation *other) *other = tmp; } } -#include "me/printf/printf.h" -static void run_func_with_best_rotate_for_item_inner(t_state *state, - t_best_move_args data) +static inline void func(t_best_move_args *data, t_rotation *main, + t_rotation *other) { - t_usize target_index; - t_rotation main; - t_rotation other; - t_usize i; + t_isize i; - target_index = (find_place(data.from->buffer[data.index], state) + - (data.to->len - data.iter_func(data.to))) % - max(data.to->len, 1); + i = 0; + while (i++ < min(main->value, other->value)) + choose_func(&data->both, *main)(data->args); + i = 0; + if (main->value > other->value) + while (i++ < main->value - other->value) + choose_func(&data->main, *main)(data->args); + else + while (i++ < other->value - main->value) + choose_func(&data->other, *other)(data->args); +} + +void run_func_with_best_rotate_for_item(t_state *state, + t_best_move_args data) +{ + t_isize target_index; + t_rotation main; + t_rotation other; + t_isize i; + + target_index = (find_place(data.from->buffer[data.index], state) + + (data.to->len - data.iter_func(data.to))) % max(data.to->len, 1); main = target(0, data.index, data.from->len); - other = target(0, target_index, data.to->len); - if (main.direction != other.direction) - find_least_move(&main, &other); + other = target(target_index, 0, data.to->len); + find_least_move(&main, &other); if (main.direction == other.direction) - { - i = 0; - while (i++ < min(main.value, other.value)) - { - // me_eprintf("both\n"); - choose_func(&data.both, main)(data.args); - } - i = 0; - if (main.value > other.value) - while (i++ < main.value - other.value) - { - // me_eprintf("main\n"); - choose_func(&data.main, main)(data.args); - } - else - while (i++ < other.value - main.value) - { - // me_eprintf("other\n"); - choose_func(&data.other, other)(data.args); - } - } + func(&data, &main, &other); else { i = 0; while (i++ < main.value) - { - // me_eprintf("main\n"); choose_func(&data.main, main)(data.args); - } i = 0; while (i++ < other.value) - { - // me_eprintf("other\n"); choose_func(&data.other, other)(data.args); - } } } -void run_func_with_best_rotate_for_item(t_state *state, t_best_move_args data) -{ - run_func_with_best_rotate_for_item_inner(state, data); -} diff --git a/src/app/do_move.c b/src/app/do_move.c index 23f0e49..96b06b1 100644 --- a/src/app/do_move.c +++ b/src/app/do_move.c @@ -16,15 +16,14 @@ #include "me/types.h" #include "me/vec/vec_i64.h" -void push_a(void *s); -void push_b(void *s); -void swap_a(void *s); -void swap_b(void *s); -void swap_both(void *s); -void rotate_a(void *s); -void rotate_b(void *s); -void rotate_both(void *s); -void rev_rotate_a(void *s); -void rev_rotate_b(void *s); -void rev_rotate_both(void *s); - +void push_a(void *s); +void push_b(void *s); +void swap_a(void *s); +void swap_b(void *s); +void swap_both(void *s); +void rotate_a(void *s); +void rotate_b(void *s); +void rotate_both(void *s); +void rev_rotate_a(void *s); +void rev_rotate_b(void *s); +void rev_rotate_both(void *s); diff --git a/src/app/find_place.c b/src/app/find_place.c index 11d93ab..1a3efd6 100644 --- a/src/app/find_place.c +++ b/src/app/find_place.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/29 22:01:12 by maiboyer #+# #+# */ -/* Updated: 2024/02/02 23:00:09 by maiboyer ### ########.fr */ +/* Updated: 2024/02/08 14:06:05 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,25 +14,25 @@ #include "app/types/type_i64_bool.h" #include "me/vec/vec_i64_bool.h" -static void find_place_iter(t_usize index, t_i64_bool *elem, - t_find_place_iter_state *state) +static void find_place_iter(t_usize index, t_i64_bool *elem, + t_find_place_iter_state *state) { (void)(index); if (!(elem->active || elem->value == state->to_find_elem)) - return; + return ; if (elem->value == state->to_find_elem) state->found_index = state->current_index; state->current_index++; } -t_usize find_place(t_i64 elem, t_state *state) +t_usize find_place(t_i64 elem, t_state *state) { - t_find_place_iter_state iter_state; + t_find_place_iter_state iter_state; iter_state.current_index = 0; iter_state.found_index = 0; iter_state.to_find_elem = elem; - vec_i64_bool_iter(&state->sorted, (void(*))find_place_iter, &iter_state); + vec_i64_bool_iter(&state->sorted, (void (*)())find_place_iter, &iter_state); return (iter_state.found_index); } diff --git a/src/app/iter_find.c b/src/app/iter_find.c index 474e9cc..bd6d1cf 100644 --- a/src/app/iter_find.c +++ b/src/app/iter_find.c @@ -14,7 +14,7 @@ #include "me/types.h" #include "me/vec/vec_i64.h" -static void iter_min(t_usize index, t_i64 *elem, t_iter_state *state) +static void iter_min(t_usize index, t_i64 *elem, t_iter_state *state) { if (*elem < state->elem) { @@ -23,7 +23,7 @@ static void iter_min(t_usize index, t_i64 *elem, t_iter_state *state) } } -static void iter_max(t_usize index, t_i64 *elem, t_iter_state *state) +static void iter_max(t_usize index, t_i64 *elem, t_iter_state *state) { if (*elem > state->elem) { @@ -32,9 +32,9 @@ static void iter_max(t_usize index, t_i64 *elem, t_iter_state *state) } } -t_usize min_iter_zero_pos(t_vec_i64 *vec) +t_usize min_iter_zero_pos(t_vec_i64 *vec) { - t_iter_state state; + t_iter_state state; if (vec->len == 0) return (0); @@ -44,9 +44,9 @@ t_usize min_iter_zero_pos(t_vec_i64 *vec) return (state.pos); } -t_usize max_iter_zero_pos(t_vec_i64 *vec) +t_usize max_iter_zero_pos(t_vec_i64 *vec) { - t_iter_state state; + t_iter_state state; if (vec->len == 0) return (0); diff --git a/src/app/main.c b/src/app/main.c index 7bf85d4..deaf59f 100644 --- a/src/app/main.c +++ b/src/app/main.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/01 21:00:12 by maiboyer #+# #+# */ -/* Updated: 2024/02/02 22:06:51 by maiboyer ### ########.fr */ +/* Updated: 2024/02/08 13:50:09 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,23 +19,25 @@ #include "me/vec/vec_i64_bool.h" #include -bool sort_i64_bool(t_i64_bool *lhs, t_i64_bool *rhs) +void run_with_items(t_state *state); + +bool sort_i64_bool(t_i64_bool *lhs, t_i64_bool *rhs) { return (lhs->value <= rhs->value); } -void free_state(t_state state) + +void free_state(t_state state) { vec_i64_free(state.stack_a); vec_i64_free(state.stack_b); vec_i64_bool_free(state.sorted); } -t_state parses_arguments(t_usize count, t_str nums[]) +t_state parses_arguments(t_usize count, t_str nums[]) { - - t_state state; + t_state state; t_i32 atoi; - t_usize i; + t_usize i; state.stack_a = vec_i64_new(count, NULL); state.stack_b = vec_i64_new(count, NULL); @@ -45,28 +47,22 @@ t_state parses_arguments(t_usize count, t_str nums[]) { if (str_to_i32(nums[i], 10, &atoi)) (free_state(state), me_eprintf("Error:\nInvalid Number\n"), - exit(1)); + exit(1)); vec_i64_push(&state.stack_a, atoi); - vec_i64_bool_push(&state.sorted, - (t_i64_bool){.value = atoi, .active = false}); + vec_i64_bool_push(&state.sorted, (t_i64_bool){.value = atoi, + .active = false}); i++; } vec_i64_bool_sort(&state.sorted, sort_i64_bool); return (state); } -void run_with_items(t_state *state); - -int main(t_i32 argc, t_str argv[]) +int main(t_i32 argc, t_str argv[]) { - t_state state; + t_state state; - (argc--, argv++); + (void)(argc--, argv++); state = parses_arguments(argc, argv); run_with_items(&state); - for (t_usize i = 0; i < state.stack_a.len; i++) - { - //me_eprintf("%i\n", state.stack_a.buffer[i]); - } free_state(state); } diff --git a/src/app/moves.c b/src/app/moves.c index d1bc31c..1eef164 100644 --- a/src/app/moves.c +++ b/src/app/moves.c @@ -11,20 +11,19 @@ /* ************************************************************************** */ #include "app/moves.h" - #include "app/state.h" #include "app/types/type_move.h" -void push_a(t_state *s); -void push_b(t_state *s); -void swap_a(t_state *s); -void swap_b(t_state *s); -void rotate_a(t_state *s); -void rotate_b(t_state *s); -void rev_rotate_a(t_state *s); -void rev_rotate_b(t_state *s); +void push_a(t_state *s); +void push_b(t_state *s); +void swap_a(t_state *s); +void swap_b(t_state *s); +void rotate_a(t_state *s); +void rotate_b(t_state *s); +void rev_rotate_a(t_state *s); +void rev_rotate_b(t_state *s); -void do_move(t_move m, t_state *s) +void do_move(t_move m, t_state *s) { if (m & PUSH_A) push_a(s); @@ -44,7 +43,7 @@ void do_move(t_move m, t_state *s) rev_rotate_b(s); } -t_const_str get_str_for_move(t_move m) +t_const_str get_str_for_move(t_move m) { if (m & PUSH_A) return ("pa"); diff --git a/src/app/moves/push.c b/src/app/moves/push.c index a9ecd7f..6646044 100644 --- a/src/app/moves/push.c +++ b/src/app/moves/push.c @@ -14,26 +14,26 @@ #include "me/printf/printf.h" #include "me/vec/vec_i64.h" -static inline void push_inner(t_vec_i64 *to, t_vec_i64 *from, t_const_str tag, - t_const_str print) +static inline void push_inner(t_vec_i64 *to, t_vec_i64 *from, t_const_str tag, + t_const_str print) { - t_i64 e; + t_i64 e; (void)(tag); if (from->len == 0) - return; + return ; vec_i64_pop_front(from, &e); vec_i64_push_front(to, e); if (print) ft_printf("%s\n", print); } -void push_a(t_state *s) +void push_a(t_state *s) { push_inner(&s->stack_a, &s->stack_b, "Push A", "pa"); } -void push_b(t_state *s) +void push_b(t_state *s) { push_inner(&s->stack_b, &s->stack_a, "Push B", "pb"); } diff --git a/src/app/moves/rev_rotate.c b/src/app/moves/rev_rotate.c index 642c5d0..74de1c1 100644 --- a/src/app/moves/rev_rotate.c +++ b/src/app/moves/rev_rotate.c @@ -13,31 +13,31 @@ #include "app/state.h" #include "me/printf/printf.h" -static inline void rev_rotate_inner(t_vec_i64 *stack, t_const_str tag, - t_const_str print) +static inline void rev_rotate_inner(t_vec_i64 *stack, t_const_str tag, + t_const_str print) { - t_i64 e; + t_i64 e; (void)(tag); if (stack->len <= 1) - return; + return ; vec_i64_pop(stack, &e); vec_i64_push_front(stack, e); if (print) ft_printf("%s\n", print); } -void rev_rotate_a(t_state *s) +void rev_rotate_a(t_state *s) { rev_rotate_inner(&s->stack_a, "RevRotate A", "rra"); } -void rev_rotate_b(t_state *s) +void rev_rotate_b(t_state *s) { rev_rotate_inner(&s->stack_b, "RevRotate B", "rrb"); } -void rev_rotate_both(t_state *s) +void rev_rotate_both(t_state *s) { rev_rotate_inner(&s->stack_a, "RevRotate Both", NULL); rev_rotate_inner(&s->stack_b, "RevRotate Both", "rrr"); diff --git a/src/app/moves/rotate.c b/src/app/moves/rotate.c index 51afccc..7249a63 100644 --- a/src/app/moves/rotate.c +++ b/src/app/moves/rotate.c @@ -13,31 +13,31 @@ #include "app/state.h" #include "me/printf/printf.h" -static inline void rotate_inner(t_vec_i64 *stack, t_const_str tag, - t_const_str print) +static inline void rotate_inner(t_vec_i64 *stack, t_const_str tag, + t_const_str print) { - t_i64 e; + t_i64 e; (void)(tag); if (stack->len <= 1) - return; + return ; vec_i64_pop_front(stack, &e); vec_i64_push(stack, e); if (print) ft_printf("%s\n", print); } -void rotate_a(t_state *s) +void rotate_a(t_state *s) { rotate_inner(&s->stack_a, "Rotate A", "ra"); } -void rotate_b(t_state *s) +void rotate_b(t_state *s) { rotate_inner(&s->stack_b, "Rotate B", "rb"); } -void rotate_both(t_state *s) +void rotate_both(t_state *s) { me_eprintf("BOTHHHHHH!!!!"); rotate_inner(&s->stack_a, "Rotate Both", NULL); diff --git a/src/app/moves/swap.c b/src/app/moves/swap.c index 4b90c5d..730e4a6 100644 --- a/src/app/moves/swap.c +++ b/src/app/moves/swap.c @@ -14,15 +14,15 @@ #include "me/printf/printf.h" #include "me/vec/vec_i64.h" -static inline void swap_inner(t_vec_i64 *stack, t_const_str tag, - t_const_str print) +static inline void swap_inner(t_vec_i64 *stack, t_const_str tag, + t_const_str print) { - t_i64 first; - t_i64 second; + t_i64 first; + t_i64 second; (void)(tag); if (stack->len <= 1) - return; + return ; vec_i64_pop(stack, &first); vec_i64_pop(stack, &second); vec_i64_push(stack, first); @@ -31,17 +31,17 @@ static inline void swap_inner(t_vec_i64 *stack, t_const_str tag, ft_printf("%s\n", print); } -void swap_a(t_state *s) +void swap_a(t_state *s) { swap_inner(&s->stack_a, "Swap A", "sa"); } -void swap_b(t_state *s) +void swap_b(t_state *s) { swap_inner(&s->stack_b, "Swap B", "sb"); } -void swap_both(t_state *s) +void swap_both(t_state *s) { swap_inner(&s->stack_a, "Swap Both", NULL); swap_inner(&s->stack_b, "Swap Both", "ss"); diff --git a/src/app/rotate.c b/src/app/rotate.c index e2f2ed3..33c6652 100644 --- a/src/app/rotate.c +++ b/src/app/rotate.c @@ -12,4 +12,3 @@ #include "app/rotate.h" #include "me/types.h" - diff --git a/src/app/run_with_items.c b/src/app/run_with_items.c index eab39a6..45ed8df 100644 --- a/src/app/run_with_items.c +++ b/src/app/run_with_items.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/31 15:12:47 by maiboyer #+# #+# */ -/* Updated: 2024/02/02 22:55:08 by maiboyer ### ########.fr */ +/* Updated: 2024/02/08 14:22:15 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,65 +21,46 @@ #include "me/vec/vec_i64_bool.h" #include -void push_a(void *s); -void push_b(void *s); -void swap_a(void *s); -void swap_b(void *s); -void swap_both(void *s); -void rotate_a(void *s); -void rotate_b(void *s); -void rotate_both(void *s); -void rev_rotate_a(void *s); -void rev_rotate_b(void *s); -void rev_rotate_both(void *s); +void push_a(void *s); +void push_b(void *s); +void swap_a(void *s); +void swap_b(void *s); +void swap_both(void *s); +void rotate_a(void *s); +void rotate_b(void *s); +void rotate_both(void *s); +void rev_rotate_a(void *s); +void rev_rotate_b(void *s); +void rev_rotate_both(void *s); -static void inc(void *s) +t_usize best_index_to_move(t_state *state, t_vec_i64 *from, t_vec_i64 *to, + t_iter_pos_func f); + +static inline struct s_functions funcs( void (*f)(void *), void (*r)(void *)) { - t_usize *p; - - p = (t_usize *)s; - *p += 1; + return ((struct s_functions){.forward = f, .reverse = r}); } -t_usize best_index_to_move(t_state *state, t_vec_i64 *from, t_vec_i64 *to, - t_iter_pos_func f) +static inline void banana_first(t_state *state) { - t_usize min_val; - t_usize min_pos; - t_usize i; - t_usize tmp; + t_usize idx; - if (from->len == 0) - return (0); - i = 0; - min_val = 0; - min_pos = 0; - while (i < from->len) - { - tmp = 1; - run_func_with_best_rotate_for_item(state, (t_best_move_args){ - .index = i, - .args = &tmp, - .iter_func = f, - .from = from, - .to = to, - .other = {inc, inc}, - .main = {inc, inc}, - .both = {inc, inc}, - }); - if (tmp < min_val) - { - min_val = tmp; - min_pos = i; - } - i++; - } - return (min_pos); + idx = best_index_to_move(state, &state->stack_a, &state->stack_b, + &max_iter_zero_pos); + run_func_with_best_rotate_for_item(state, + (t_best_move_args){ + .index = idx, .iter_func = &max_iter_zero_pos, .from = \ +&state->stack_a, .to = &state->stack_b, .args = state, .both = \ +funcs(rotate_both, rev_rotate_both), .main = funcs(rotate_a, rev_rotate_a), \ +.other = funcs(rotate_b, rev_rotate_b), }); + push_b(state); + make_sorted_true_from_stack(state, &state->stack_b); } -bool is_sorted(t_vec_i64 *v) +bool is_sorted(t_vec_i64 *v) { - t_usize i; + t_usize i; + i = 1; while (i < v->len) { @@ -90,71 +71,35 @@ bool is_sorted(t_vec_i64 *v) return (true); } -void print_stack(t_const_str msg, t_vec_i64 *stack) +static inline void banana_second(t_state *state) { - return; - me_eprintf("[%s] = [\n", msg); + t_usize idx; - for (t_usize i = 0; i < stack->len; i++) - me_eprintf("\t%i\n", stack->buffer[i]); - me_eprintf("]\n"); + idx = best_index_to_move(state, &state->stack_b, &state->stack_a, + &min_iter_zero_pos); + run_func_with_best_rotate_for_item(state, (t_best_move_args){ + .index = idx, .from = &state->stack_b, \ +.to = &state->stack_a, .iter_func = &min_iter_zero_pos, .args = state, \ +.both = funcs(rotate_both, rev_rotate_both), .main = funcs(rotate_b, \ +rev_rotate_b), .other = funcs(rotate_a, rev_rotate_a), }); + push_a(state); + make_sorted_true_from_stack(state, &state->stack_a); } -void run_with_items(t_state *state) +void run_with_items(t_state *state) { - t_rotation rot; - t_usize idx; + t_rotation rot; if (is_sorted(&state->stack_a)) - return; + return ; while (state->stack_a.len > state->stack_b.len) - { - idx = best_index_to_move(state, &state->stack_a, &state->stack_b, - &max_iter_zero_pos); - run_func_with_best_rotate_for_item( - state, - (t_best_move_args){ - .index = idx, - .iter_func = &max_iter_zero_pos, - .from = &state->stack_a, - .to = &state->stack_b, - .args = state, - .both = {.forward = rotate_both, .reverse = rev_rotate_both}, - .main = {.forward = rotate_a, .reverse = rev_rotate_a}, - .other = {.forward = rotate_b, .reverse = rev_rotate_b}, - }); - push_b(state); - make_sorted_true_from_stack(state, &state->stack_b); - print_stack("f_stack_a", &state->stack_a); - print_stack("f_stack_b", &state->stack_b); - } + banana_first(state); while (state->stack_a.len != 0) - { push_b(state); - } vec_i64_bool_reverse(&state->sorted); make_sorted_all_false(state); while (state->stack_b.len != 0) - { - idx = best_index_to_move(state, &state->stack_b, &state->stack_a, - &min_iter_zero_pos); - run_func_with_best_rotate_for_item( - state, - (t_best_move_args){ - .index = idx, - .from = &state->stack_b, - .to = &state->stack_a, - .iter_func = &min_iter_zero_pos, - .args = state, - .both = {.forward = rotate_both, .reverse = rev_rotate_both}, - .main = {.forward = rotate_b, .reverse = rev_rotate_b}, - .other = {.forward = rotate_a, .reverse = rev_rotate_a}, - }); - push_a(state); - make_sorted_true_from_stack(state, &state->stack_a); - print_stack("s_stack_a", &state->stack_a); - print_stack("s_stack_b", &state->stack_b); - } + banana_second(state); rot = target(0, min_iter_zero_pos(&state->stack_a), state->stack_a.len); if (rot.value > flip(rot).value) rot = flip(rot); @@ -166,6 +111,4 @@ void run_with_items(t_state *state) rev_rotate_a(state); rot.value--; } - me_eprintf("is_sorted: %s\n", - is_sorted(&state->stack_a) ? "true" : "false"); } diff --git a/src/app/target.c b/src/app/target.c index 1fc1b55..1444454 100644 --- a/src/app/target.c +++ b/src/app/target.c @@ -10,10 +10,10 @@ /* */ /* ************************************************************************** */ -#include "app/target.h" #include "app/rotate.h" +#include "app/target.h" -t_rotation target(t_usize from, t_usize to, t_usize ring_size) +t_rotation target(t_usize from, t_usize to, t_usize ring_size) { if (ring_size == 0) ring_size++; From ebf62307e62a3c7b65754091242105c12f96409b Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Thu, 8 Feb 2024 14:34:02 +0100 Subject: [PATCH 14/29] removed debug eprintf --- src/app/moves/rotate.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/app/moves/rotate.c b/src/app/moves/rotate.c index 7249a63..46968db 100644 --- a/src/app/moves/rotate.c +++ b/src/app/moves/rotate.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 16:26:04 by maiboyer #+# #+# */ -/* Updated: 2024/02/02 22:34:51 by maiboyer ### ########.fr */ +/* Updated: 2024/02/08 14:30:21 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,7 +39,6 @@ void rotate_b(t_state *s) void rotate_both(t_state *s) { - me_eprintf("BOTHHHHHH!!!!"); rotate_inner(&s->stack_a, "Rotate Both", NULL); rotate_inner(&s->stack_b, "Rotate Both", "rr"); } From 9752ce84fb1268581700a01fc95c11a60542a104 Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Thu, 8 Feb 2024 18:49:19 +0100 Subject: [PATCH 15/29] update stuff --- .gitignore | 1 + input.toml | 2 +- mecstd | 2 +- src.list | 3 ++ src/app/main.c | 71 +++++++++++++++++++----- src/app/sort2.c | 33 ++++++++++++ src/app/sort3.c | 138 +++++++++++++++++++++++++++++++++++++++++++++++ src/app/sort5.c | 141 ++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 375 insertions(+), 16 deletions(-) create mode 100644 src/app/sort2.c create mode 100644 src/app/sort3.c create mode 100644 src/app/sort5.c diff --git a/.gitignore b/.gitignore index 2dab770..a9b1dd5 100644 --- a/.gitignore +++ b/.gitignore @@ -57,3 +57,4 @@ test output/ push_swap push_swap_visualizer +Push-Swap-Tester diff --git a/input.toml b/input.toml index c245ded..8125265 100644 --- a/input.toml +++ b/input.toml @@ -45,7 +45,7 @@ replace.C__TYPE__ = "t_i64" replace.C__UNSIGNED_TYPE__ = "t_u64" replace.C__PREFIX__ = "i64" replace.C__MAX__ = "9223372036854775807ll" -replace.C__MIN__ = "-9223372036854775808ll" +replace.C__MIN__ = "-(~9223372036854775807ll + 1)" replace.C__ZERO__ = "0ll" replace.C__SIGNED_TYPE__ = "true" diff --git a/mecstd b/mecstd index 4cb7e19..e9c4ce8 160000 --- a/mecstd +++ b/mecstd @@ -1 +1 @@ -Subproject commit 4cb7e19ed8aaa0c910f38fb6947f91b363556b4c +Subproject commit e9c4ce89ea79ba399f8995f3c14e66e78555661d diff --git a/src.list b/src.list index 3b26f15..4cb65a2 100644 --- a/src.list +++ b/src.list @@ -11,4 +11,7 @@ app/moves/rotate app/moves/swap app/rotate app/run_with_items +app/sort2 +app/sort3 +app/sort5 app/target diff --git a/src/app/main.c b/src/app/main.c index deaf59f..e3fb7cd 100644 --- a/src/app/main.c +++ b/src/app/main.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/01 21:00:12 by maiboyer #+# #+# */ -/* Updated: 2024/02/08 13:50:09 by maiboyer ### ########.fr */ +/* Updated: 2024/02/08 18:44:58 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,25 +19,51 @@ #include "me/vec/vec_i64_bool.h" #include -void run_with_items(t_state *state); +#define TESTER_ERROR(s) "" -bool sort_i64_bool(t_i64_bool *lhs, t_i64_bool *rhs) +// #define TESTER_ERROR(s) s + +void sort_3(t_state *state); +void sort_2(t_state *state); +void sort_5(t_state *state); + +void run_with_items(t_state *state); + +bool sort_i64_bool(t_i64_bool *lhs, t_i64_bool *rhs) { return (lhs->value <= rhs->value); } -void free_state(t_state state) +void free_state(t_state state) { vec_i64_free(state.stack_a); vec_i64_free(state.stack_b); vec_i64_bool_free(state.sorted); } -t_state parses_arguments(t_usize count, t_str nums[]) +bool duplicate_check(t_state *state) { - t_state state; + t_i64 last; + t_usize index; + + if (state->sorted.len == 0) + return (false); + index = 1; + last = state->sorted.buffer[0].value; + while (index < state->sorted.len) + { + if (last == state->sorted.buffer[index].value) + return (true); + last = state->sorted.buffer[index++].value; + } + return (false); +} + +t_state parses_arguments(t_usize count, t_str nums[]) +{ + t_state state; t_i32 atoi; - t_usize i; + t_usize i; state.stack_a = vec_i64_new(count, NULL); state.stack_b = vec_i64_new(count, NULL); @@ -46,23 +72,40 @@ t_state parses_arguments(t_usize count, t_str nums[]) while (i < count) { if (str_to_i32(nums[i], 10, &atoi)) - (free_state(state), me_eprintf("Error:\nInvalid Number\n"), - exit(1)); + (free_state(state), + me_eprintf("Error\n" TESTER_ERROR("Invalid Number\n")), exit(1)); vec_i64_push(&state.stack_a, atoi); - vec_i64_bool_push(&state.sorted, (t_i64_bool){.value = atoi, - .active = false}); + vec_i64_bool_push(&state.sorted, + (t_i64_bool){.value = atoi, .active = false}); i++; } vec_i64_bool_sort(&state.sorted, sort_i64_bool); + if (duplicate_check(&state)) + (free_state(state), + me_eprintf("Error\n" TESTER_ERROR("Duplicate Number\n")), exit(1)); return (state); } -int main(t_i32 argc, t_str argv[]) +bool is_sorted(t_vec_i64 *v); + +int main(t_i32 argc, t_str argv[]) { - t_state state; + t_state state; (void)(argc--, argv++); state = parses_arguments(argc, argv); - run_with_items(&state); + if (is_sorted(&state.stack_a)) + return (free_state(state), 0); + if (state.stack_a.len == 0) + (free_state(state), me_eprintf("Error\n" TESTER_ERROR("No Input\n")), + exit(1)); + if (state.stack_a.len == 2) + sort_2(&state); + else if (state.stack_a.len == 3) + sort_3(&state); + else if (state.stack_a.len == 5) + sort_5(&state); + else + run_with_items(&state); free_state(state); } diff --git a/src/app/sort2.c b/src/app/sort2.c new file mode 100644 index 0000000..a82f46c --- /dev/null +++ b/src/app/sort2.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sort2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/08 15:50:10 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 16:20:35 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/state.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" + +void push_a(void *s); +void push_b(void *s); +void swap_a(void *s); +void swap_b(void *s); +void swap_both(void *s); +void rotate_a(void *s); +void rotate_b(void *s); +void rotate_both(void *s); +void rev_rotate_a(void *s); +void rev_rotate_b(void *s); +void rev_rotate_both(void *s); + +void sort_2(t_state *state) +{ + if (state->stack_a.buffer[0] > state->stack_a.buffer[1]) + swap_a(state); +} diff --git a/src/app/sort3.c b/src/app/sort3.c new file mode 100644 index 0000000..080bfe4 --- /dev/null +++ b/src/app/sort3.c @@ -0,0 +1,138 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sort3.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/08 15:50:10 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 17:53:02 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/state.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" + +void push_a(void *s); +void push_b(void *s); +void swap_a(void *s); +void swap_b(void *s); +void swap_both(void *s); +void rotate_a(void *s); +void rotate_b(void *s); +void rotate_both(void *s); +void rev_rotate_a(void *s); +void rev_rotate_b(void *s); +void rev_rotate_both(void *s); + +/* +fn sort_three(state: &mut State, selector: StackSelector, min_first: bool) { + macro_rules! stack { + () => { + match selector { + StackSelector::B => &mut state.b, + StackSelector::A => &mut state.a, + } + }; + } + let [swap, rotate, rev_rotate] = match selector { + StackSelector::A => [sa, ra, rra], + StackSelector::B => [sb, rb, rrb], + }; + match stack!().len() { + 2 => { + let func = match min_first { + true => PartialOrd::gt, + false => PartialOrd::lt, + }; + + if func(&stack!()[0].clone(), &stack!()[1]) { + swap(state); + } + return ; + } + 3 => {} + 0 | 1 | 4.. => return, + } + let mut c = stack!().clone(); + macro_rules! comb { + ($i1:literal, $i2:literal, $i3:literal) => { + &[c[$i1 - 1], c[$i2 - 1], c[$i3 - 1]] + }; + } + c.make_contiguous().sort_unstable(); + if min_first { + c.make_contiguous().reverse(); + } + stack!().make_contiguous(); + + if (stack!() == comb![1, 2, 3] / * abc * /) +{ + swap(state); + rev_rotate(state); +} +else if (stack!() == comb ![ 1, 3, 2 ] / * acb * /) +{ + rotate(state); +} +else if (stack !() == comb ![ 2, 3, 1 ] / * bca * /) +{ + swap(state); +} +else if (stack !() == comb ![ 2, 1, 3 ] / * bac * /) +{ + rotate(state); +} +else if (stack !() == comb ![ 3, 2, 1 ] / * cba * /) +{ +} +else if (stack !() == comb ![ 3, 1, 2 ] / * cab * /) +{ + rotate(state); + swap(state); + rev_rotate(state); +} +} +*/ +// 2 3 1 +bool sort_i64(t_i64 *lhs, t_i64 *rhs) +{ + return (*lhs <= *rhs); +} + +void sort_3_inner(t_state *state, t_i64 data[3], t_i64 sorted[3]) +{ + if ((data[0] == sorted[0]) && (data[1] == sorted[1]) + && (data[2] == sorted[2])) + (swap_a(state), rev_rotate_a(state)); + else if ((data[0] == sorted[0]) && (data[1] == sorted[2]) + && (data[2] == sorted[1])) + rotate_a(state); + else if ((data[0] == sorted[1]) && (data[1] == sorted[2]) + && (data[2] == sorted[0])) + swap_a(state); + else if ((data[0] == sorted[1]) && (data[1] == sorted[0]) + && (data[2] == sorted[2])) + rev_rotate_a(state); + else if ((data[0] == sorted[2]) && (data[1] == sorted[0]) + && (data[2] == sorted[1])) + (rotate_a(state), swap_a(state), rev_rotate_a(state)); +} + +void sort_3(t_state *state) +{ + t_i64 array[3]; + t_vec_i64 why_do_i_do_this; + + why_do_i_do_this.free_func = NULL; + why_do_i_do_this.len = 3; + why_do_i_do_this.capacity = 3; + why_do_i_do_this.buffer = array; + array[0] = state->stack_a.buffer[0]; + array[1] = state->stack_a.buffer[1]; + array[2] = state->stack_a.buffer[2]; + vec_i64_sort(&why_do_i_do_this, sort_i64); + vec_i64_reverse(&why_do_i_do_this); + sort_3_inner(state, state->stack_a.buffer, array); +} diff --git a/src/app/sort5.c b/src/app/sort5.c new file mode 100644 index 0000000..5fb4704 --- /dev/null +++ b/src/app/sort5.c @@ -0,0 +1,141 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sort5.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/08 15:50:10 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 17:53:25 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/find_place.h" +#include "app/state.h" +#include "me/printf/printf.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" +#include "me/vec/vec_i64_bool.h" +#include "me/mem/mem_compare.h" +#include "me/mem/mem_copy.h" + +void push_a(void *s); +void push_b(void *s); +void swap_a(void *s); +void swap_b(void *s); +void swap_both(void *s); +void rotate_a(void *s); +void rotate_b(void *s); +void rotate_both(void *s); +void rev_rotate_a(void *s); +void rev_rotate_b(void *s); +void rev_rotate_both(void *s); +void sort_2(t_state *state); + +bool sort_i64(t_i64 *lhs, t_i64 *rhs); + +bool sort_i64_other_way(t_i64 *lhs, t_i64 *rhs) +{ + return (*lhs >= *rhs); +} + +void sort_5_inner(t_state *state, t_i64 data[3], t_i64 sorted[3]) +{ + if ((data[0] == sorted[0]) && (data[1] == sorted[1]) + && (data[2] == sorted[2])) + (swap_a(state), rev_rotate_a(state)); + else if ((data[0] == sorted[0]) && (data[1] == sorted[2]) + && (data[2] == sorted[1])) + rotate_a(state); + else if ((data[0] == sorted[1]) && (data[1] == sorted[2]) + && (data[2] == sorted[0])) + swap_a(state); + else if ((data[0] == sorted[1]) && (data[1] == sorted[0]) + && (data[2] == sorted[2])) + rev_rotate_a(state); + else if ((data[0] == sorted[2]) && (data[1] == sorted[0]) + && (data[2] == sorted[1])) + (rotate_a(state), swap_a(state), rev_rotate_a(state)); +} + +void sort_5_inner2(t_state *state) +{ + t_usize index; + + make_sorted_true_from_stack(state, &state->stack_a); + index = find_place(state->stack_b.buffer[0], state); + if (index == 0) + push_a(state); + else if (index == 1) + (push_a(state), swap_a(state)); + else if (index == 2) + (rotate_a(state), push_a(state), swap_a(state), rev_rotate_a(state)); + else if (index == 3) + (push_a(state), rotate_a(state)); + make_sorted_true_from_stack(state, &state->stack_a); + index = find_place(state->stack_b.buffer[0], state); + if (index == 0) + push_a(state); + else if (index == 1) + (push_a(state), swap_a(state)); + else if (index == 2) + (rotate_a(state), push_a(state), swap_a(state), rev_rotate_a(state)); + else if (index == 3) + (rev_rotate_a(state), push_a(state), rotate_a(state), rotate_a(state)); + else if (index == 4) + (push_a(state), rotate_a(state)); + make_sorted_true_from_stack(state, &state->stack_a); +} + +bool sort_5_specialized(t_state *state, t_i64 cur[5]) +{ + t_i64 target[5]; + t_vec_i64 why_do_i_do_this; + + why_do_i_do_this.free_func = NULL; + why_do_i_do_this.len = 5; + why_do_i_do_this.capacity = 5; + why_do_i_do_this.buffer = target; + mem_copy(target, state->stack_a.buffer, sizeof(t_i64) * 5); + vec_i64_sort(&why_do_i_do_this, sort_i64); + if (cur[0] == target[3 - 1] && cur[1] == target[4 - 1] && cur[2] == target[1 + - 1] && cur[3] == target[5 - 1] && cur[4] == target[2 - 1]) + return (ft_printf("pb\npb\nra\nsa\npa\nsa\npa\nsa\nrra\n"), true); + if (cur[0] == target[3 - 1] && cur[1] == target[4 - 1] && cur[2] == target[5 + - 1] && cur[3] == target[2 - 1] && cur[4] == target[1 - 1]) + return (ft_printf("pb\npb\nsb\nsa\npa\nsa\nrra\nrra\npa\nra\nra\n"), + true); + if (cur[0] == target[4 - 1] && cur[1] == target[3 - 1] && cur[2] == target[1 + - 1] && cur[3] == target[5 - 1] && cur[4] == target[2 - 1]) + return (ft_printf("pb\npb\nra\nsa\npa\nsa\nrra\nrra\npa\nra\nra\n"), + true); + if (cur[0] == target[4 - 1] && cur[1] == target[3 - 1] && cur[2] == target[5 + - 1] && cur[3] == target[2 - 1] && cur[4] == target[1 - 1]) + return (ft_printf("pb\npb\nsb\nsa\npa\nsa\npa\nsa\nrra\n"), true); + return (false); +} + +void sort_5(t_state *state) +{ + t_i64 array[3]; + t_vec_i64 why_do_i_do_this; + + if (sort_5_specialized(state, state->stack_a.buffer)) + return ; + push_b(state); + push_b(state); + if (state->stack_b.buffer[0] >= state->stack_a.buffer[1]) + swap_b(state); + why_do_i_do_this.free_func = NULL; + why_do_i_do_this.len = 3; + why_do_i_do_this.capacity = 3; + why_do_i_do_this.buffer = array; + array[0] = state->stack_a.buffer[0]; + array[1] = state->stack_a.buffer[1]; + array[2] = state->stack_a.buffer[2]; + vec_i64_sort(&why_do_i_do_this, sort_i64_other_way); + // vec_i64_reverse(&why_do_i_do_this); + sort_5_inner(state, state->stack_a.buffer, array); + // vec_i64_bool_reverse(&state->sorted); + sort_5_inner2(state); +} From f3f05882906ce02f5853844c8565890724776493 Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Thu, 8 Feb 2024 18:51:17 +0100 Subject: [PATCH 16/29] made sort5 norminette proof --- src/app/sort5.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/app/sort5.c b/src/app/sort5.c index 5fb4704..894482c 100644 --- a/src/app/sort5.c +++ b/src/app/sort5.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/08 15:50:10 by maiboyer #+# #+# */ -/* Updated: 2024/02/08 17:53:25 by maiboyer ### ########.fr */ +/* Updated: 2024/02/08 18:50:53 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -98,19 +98,19 @@ bool sort_5_specialized(t_state *state, t_i64 cur[5]) why_do_i_do_this.buffer = target; mem_copy(target, state->stack_a.buffer, sizeof(t_i64) * 5); vec_i64_sort(&why_do_i_do_this, sort_i64); - if (cur[0] == target[3 - 1] && cur[1] == target[4 - 1] && cur[2] == target[1 - - 1] && cur[3] == target[5 - 1] && cur[4] == target[2 - 1]) + if (cur[0] == target[3 - 1] && cur[1] == target[4 - 1] && cur[2] == \ + target[1 - 1] && cur[3] == target[5 - 1] && cur[4] == target[2 - 1]) return (ft_printf("pb\npb\nra\nsa\npa\nsa\npa\nsa\nrra\n"), true); - if (cur[0] == target[3 - 1] && cur[1] == target[4 - 1] && cur[2] == target[5 - - 1] && cur[3] == target[2 - 1] && cur[4] == target[1 - 1]) + if (cur[0] == target[3 - 1] && cur[1] == target[4 - 1] && cur[2] == \ + target[5 - 1] && cur[3] == target[2 - 1] && cur[4] == target[1 - 1]) return (ft_printf("pb\npb\nsb\nsa\npa\nsa\nrra\nrra\npa\nra\nra\n"), true); - if (cur[0] == target[4 - 1] && cur[1] == target[3 - 1] && cur[2] == target[1 - - 1] && cur[3] == target[5 - 1] && cur[4] == target[2 - 1]) + if (cur[0] == target[4 - 1] && cur[1] == target[3 - 1] && cur[2] == \ + target[1 - 1] && cur[3] == target[5 - 1] && cur[4] == target[2 - 1]) return (ft_printf("pb\npb\nra\nsa\npa\nsa\nrra\nrra\npa\nra\nra\n"), true); - if (cur[0] == target[4 - 1] && cur[1] == target[3 - 1] && cur[2] == target[5 - - 1] && cur[3] == target[2 - 1] && cur[4] == target[1 - 1]) + if (cur[0] == target[4 - 1] && cur[1] == target[3 - 1] && cur[2] == \ + target[5 - 1] && cur[3] == target[2 - 1] && cur[4] == target[1 - 1]) return (ft_printf("pb\npb\nsb\nsa\npa\nsa\npa\nsa\nrra\n"), true); return (false); } @@ -134,8 +134,6 @@ void sort_5(t_state *state) array[1] = state->stack_a.buffer[1]; array[2] = state->stack_a.buffer[2]; vec_i64_sort(&why_do_i_do_this, sort_i64_other_way); - // vec_i64_reverse(&why_do_i_do_this); sort_5_inner(state, state->stack_a.buffer, array); - // vec_i64_bool_reverse(&state->sorted); sort_5_inner2(state); } From d52896c79dae8a44d19c3635b63f63ba405de50e Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Thu, 8 Feb 2024 18:57:10 +0100 Subject: [PATCH 17/29] update --- mecstd | 2 +- src/app/main.c | 48 +++++++++++++++++++++--------------------------- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/mecstd b/mecstd index e9c4ce8..60b9db4 160000 --- a/mecstd +++ b/mecstd @@ -1 +1 @@ -Subproject commit e9c4ce89ea79ba399f8995f3c14e66e78555661d +Subproject commit 60b9db4c20aa957cff32ae58dfc8a2124f9b35d5 diff --git a/src/app/main.c b/src/app/main.c index e3fb7cd..3bc4a32 100644 --- a/src/app/main.c +++ b/src/app/main.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/01 21:00:12 by maiboyer #+# #+# */ -/* Updated: 2024/02/08 18:44:58 by maiboyer ### ########.fr */ +/* Updated: 2024/02/08 18:53:49 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,32 +19,28 @@ #include "me/vec/vec_i64_bool.h" #include -#define TESTER_ERROR(s) "" +void sort_3(t_state *state); +void sort_2(t_state *state); +void sort_5(t_state *state); -// #define TESTER_ERROR(s) s +void run_with_items(t_state *state); -void sort_3(t_state *state); -void sort_2(t_state *state); -void sort_5(t_state *state); - -void run_with_items(t_state *state); - -bool sort_i64_bool(t_i64_bool *lhs, t_i64_bool *rhs) +bool sort_i64_bool(t_i64_bool *lhs, t_i64_bool *rhs) { return (lhs->value <= rhs->value); } -void free_state(t_state state) +void free_state(t_state state) { vec_i64_free(state.stack_a); vec_i64_free(state.stack_b); vec_i64_bool_free(state.sorted); } -bool duplicate_check(t_state *state) +bool duplicate_check(t_state *state) { t_i64 last; - t_usize index; + t_usize index; if (state->sorted.len == 0) return (false); @@ -59,11 +55,11 @@ bool duplicate_check(t_state *state) return (false); } -t_state parses_arguments(t_usize count, t_str nums[]) +t_state parses_arguments(t_usize count, t_str nums[]) { - t_state state; + t_state state; t_i32 atoi; - t_usize i; + t_usize i; state.stack_a = vec_i64_new(count, NULL); state.stack_b = vec_i64_new(count, NULL); @@ -72,33 +68,31 @@ t_state parses_arguments(t_usize count, t_str nums[]) while (i < count) { if (str_to_i32(nums[i], 10, &atoi)) - (free_state(state), - me_eprintf("Error\n" TESTER_ERROR("Invalid Number\n")), exit(1)); + (free_state(state), me_eprintf("Error:\nInvalid Number\n"), + exit(1)); vec_i64_push(&state.stack_a, atoi); - vec_i64_bool_push(&state.sorted, - (t_i64_bool){.value = atoi, .active = false}); + vec_i64_bool_push(&state.sorted, (t_i64_bool){.value = atoi, + .active = false}); i++; } vec_i64_bool_sort(&state.sorted, sort_i64_bool); if (duplicate_check(&state)) - (free_state(state), - me_eprintf("Error\n" TESTER_ERROR("Duplicate Number\n")), exit(1)); + (free_state(state), me_eprintf("Error:\nDuplicate Number\n"), exit(1)); return (state); } -bool is_sorted(t_vec_i64 *v); +bool is_sorted(t_vec_i64 *v); -int main(t_i32 argc, t_str argv[]) +int main(t_i32 argc, t_str argv[]) { - t_state state; + t_state state; (void)(argc--, argv++); state = parses_arguments(argc, argv); if (is_sorted(&state.stack_a)) return (free_state(state), 0); if (state.stack_a.len == 0) - (free_state(state), me_eprintf("Error\n" TESTER_ERROR("No Input\n")), - exit(1)); + (free_state(state), me_eprintf("Error:\nNo Input\n"), exit(1)); if (state.stack_a.len == 2) sort_2(&state); else if (state.stack_a.len == 3) From f9807944563efb82cc5f47c9eeffeeb4b0231b8c Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Thu, 8 Feb 2024 18:58:17 +0100 Subject: [PATCH 18/29] updated so it works... --- .gitignore | 2 +- output/include/me/vec/vec_i64.h | 51 +++++++++ output/include/me/vec/vec_i64_bool.h | 60 +++++++++++ output/src/convert/str_to_i16.c | 129 +++++++++++++++++++++++ output/src/convert/str_to_i16_utils.c | 45 ++++++++ output/src/convert/str_to_i32.c | 129 +++++++++++++++++++++++ output/src/convert/str_to_i32_utils.c | 45 ++++++++ output/src/convert/str_to_i64.c | 129 +++++++++++++++++++++++ output/src/convert/str_to_i64_utils.c | 45 ++++++++ output/src/convert/str_to_i8.c | 129 +++++++++++++++++++++++ output/src/convert/str_to_i8_utils.c | 45 ++++++++ output/src/convert/str_to_u16.c | 129 +++++++++++++++++++++++ output/src/convert/str_to_u16_utils.c | 45 ++++++++ output/src/convert/str_to_u32.c | 129 +++++++++++++++++++++++ output/src/convert/str_to_u32_utils.c | 45 ++++++++ output/src/convert/str_to_u64.c | 129 +++++++++++++++++++++++ output/src/convert/str_to_u64_utils.c | 45 ++++++++ output/src/convert/str_to_u8.c | 129 +++++++++++++++++++++++ output/src/convert/str_to_u8_utils.c | 45 ++++++++ output/src/vec/vec_i64.c | 114 ++++++++++++++++++++ output/src/vec/vec_i64_bool.c | 115 ++++++++++++++++++++ output/src/vec/vec_i64_bool_functions2.c | 109 +++++++++++++++++++ output/src/vec/vec_i64_bool_functions3.c | 72 +++++++++++++ output/src/vec/vec_i64_bool_sort.c | 41 +++++++ output/src/vec/vec_i64_functions2.c | 106 +++++++++++++++++++ output/src/vec/vec_i64_functions3.c | 72 +++++++++++++ output/src/vec/vec_i64_sort.c | 40 +++++++ 27 files changed, 2173 insertions(+), 1 deletion(-) create mode 100644 output/include/me/vec/vec_i64.h create mode 100644 output/include/me/vec/vec_i64_bool.h create mode 100644 output/src/convert/str_to_i16.c create mode 100644 output/src/convert/str_to_i16_utils.c create mode 100644 output/src/convert/str_to_i32.c create mode 100644 output/src/convert/str_to_i32_utils.c create mode 100644 output/src/convert/str_to_i64.c create mode 100644 output/src/convert/str_to_i64_utils.c create mode 100644 output/src/convert/str_to_i8.c create mode 100644 output/src/convert/str_to_i8_utils.c create mode 100644 output/src/convert/str_to_u16.c create mode 100644 output/src/convert/str_to_u16_utils.c create mode 100644 output/src/convert/str_to_u32.c create mode 100644 output/src/convert/str_to_u32_utils.c create mode 100644 output/src/convert/str_to_u64.c create mode 100644 output/src/convert/str_to_u64_utils.c create mode 100644 output/src/convert/str_to_u8.c create mode 100644 output/src/convert/str_to_u8_utils.c create mode 100644 output/src/vec/vec_i64.c create mode 100644 output/src/vec/vec_i64_bool.c create mode 100644 output/src/vec/vec_i64_bool_functions2.c create mode 100644 output/src/vec/vec_i64_bool_functions3.c create mode 100644 output/src/vec/vec_i64_bool_sort.c create mode 100644 output/src/vec/vec_i64_functions2.c create mode 100644 output/src/vec/vec_i64_functions3.c create mode 100644 output/src/vec/vec_i64_sort.c diff --git a/.gitignore b/.gitignore index a9b1dd5..14b2bca 100644 --- a/.gitignore +++ b/.gitignore @@ -54,7 +54,7 @@ dkms.conf ./generic .direnv test -output/ +# /output/ push_swap push_swap_visualizer Push-Swap-Tester diff --git a/output/include/me/vec/vec_i64.h b/output/include/me/vec/vec_i64.h new file mode 100644 index 0000000..8a8f200 --- /dev/null +++ b/output/include/me/vec/vec_i64.h @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_i64.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/04 18:46:53 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 17:53:00 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef VEC_I64_H +# define VEC_I64_H + +# include "me/types.h" + +typedef bool (*t_vec_i64_sort_fn)(t_i64 *, t_i64 *); +typedef void (*t_free_i64_item)(t_i64); + +typedef struct s_vec_i64 +{ + t_free_i64_item free_func; + t_usize len; + t_usize capacity; + t_i64 *buffer; +} t_vec_i64; + +t_vec_i64 vec_i64_new(t_usize capacity, + t_free_i64_item free_function); +t_error vec_i64_push(t_vec_i64 *vec, t_i64 element); +t_error vec_i64_push_front(t_vec_i64 *vec, t_i64 element); +t_error vec_i64_pop(t_vec_i64 *vec, t_i64 *value); +t_error vec_i64_pop_front(t_vec_i64 *vec, t_i64 *value); +void vec_i64_free(t_vec_i64 vec); +t_error vec_i64_reserve(t_vec_i64 *vec, t_usize wanted_capacity); +t_error vec_i64_find(t_vec_i64 *vec, bool (*fn)(const t_i64 *), + t_usize *index); +t_error vec_i64_find_starting(t_vec_i64 *vec, + bool (*fn)(const t_i64 *), t_usize starting_index, + t_usize *index); +t_error vec_i64_all(t_vec_i64 *vec, bool (*fn)(const t_i64 *), + bool *result); +t_error vec_i64_any(t_vec_i64 *vec, bool (*fn)(const t_i64 *), + bool *result); +void vec_i64_iter(t_vec_i64 *vec, void (*fn)(t_usize index, + t_i64 *value, void *state), void *state); +void vec_i64_reverse(t_vec_i64 *vec); +void vec_i64_sort(t_vec_i64 *vec, t_vec_i64_sort_fn is_sorted); + +#endif diff --git a/output/include/me/vec/vec_i64_bool.h b/output/include/me/vec/vec_i64_bool.h new file mode 100644 index 0000000..42f456a --- /dev/null +++ b/output/include/me/vec/vec_i64_bool.h @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_i64_bool.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/04 18:46:53 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 17:53:00 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef VEC_I64_BOOL_H +# define VEC_I64_BOOL_H + +# include "app/types/type_i64_bool.h" +# include "me/types.h" + +typedef bool (*t_vec_i64_bool_sort_fn)(t_i64_bool *, + t_i64_bool *); +typedef void (*t_free_i64_bool_item)(t_i64_bool); + +typedef struct s_vec_i64_bool +{ + t_free_i64_bool_item free_func; + t_usize len; + t_usize capacity; + t_i64_bool *buffer; +} t_vec_i64_bool; + +t_vec_i64_bool vec_i64_bool_new(t_usize capacity, + t_free_i64_bool_item free_function); +t_error vec_i64_bool_push(t_vec_i64_bool *vec, + t_i64_bool element); +t_error vec_i64_bool_push_front(t_vec_i64_bool *vec, + t_i64_bool element); +t_error vec_i64_bool_pop(t_vec_i64_bool *vec, + t_i64_bool *value); +t_error vec_i64_bool_pop_front(t_vec_i64_bool *vec, + t_i64_bool *value); +void vec_i64_bool_free(t_vec_i64_bool vec); +t_error vec_i64_bool_reserve(t_vec_i64_bool *vec, + t_usize wanted_capacity); +t_error vec_i64_bool_find(t_vec_i64_bool *vec, + bool (*fn)(const t_i64_bool *), t_usize *index); +t_error vec_i64_bool_find_starting(t_vec_i64_bool *vec, + bool (*fn)(const t_i64_bool *), + t_usize starting_index, t_usize *index); +t_error vec_i64_bool_all(t_vec_i64_bool *vec, + bool (*fn)(const t_i64_bool *), bool *result); +t_error vec_i64_bool_any(t_vec_i64_bool *vec, + bool (*fn)(const t_i64_bool *), bool *result); +void vec_i64_bool_iter(t_vec_i64_bool *vec, + void (*fn)(t_usize index, t_i64_bool *value, + void *state), void *state); +void vec_i64_bool_reverse(t_vec_i64_bool *vec); +void vec_i64_bool_sort(t_vec_i64_bool *vec, + t_vec_i64_bool_sort_fn is_sorted); + +#endif diff --git a/output/src/convert/str_to_i16.c b/output/src/convert/str_to_i16.c new file mode 100644 index 0000000..b1d2bc8 --- /dev/null +++ b/output/src/convert/str_to_i16.c @@ -0,0 +1,129 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/string/str_len.h" +#include "me/types.h" + +#define OP_ADD 0b0001 +#define OP_SUB 0b0010 +#define OP_MUL 0b0100 +#define OP_CHK 0b1000 + +t_error checked_add_i16(t_i16 lhs, t_i16 rhs, t_i16 *out); +t_error checked_sub_i16(t_i16 lhs, t_i16 rhs, t_i16 *out); +t_error checked_mul_i16(t_i16 lhs, t_i16 rhs, t_i16 *out); + +static inline bool can_not_overflow(t_u32 radix, bool is_signed_type, + t_usize digits_len) +{ + return (radix <= 16 && digits_len <= sizeof(t_i16) * 2 + - (t_usize)is_signed_type); +} + +static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out) +{ + t_u32 digit; + + if (radix < 2 || radix > 36) + return (ERROR); + digit = ascii - '0'; + if (radix > 10 && digit >= 10) + { + if (!me_isalpha(ascii)) + return (ERROR); + digit = ((ascii | 0b100000) - 'a') + 10; + } + if (digit >= radix) + return (ERROR); + *out = digit; + return (NO_ERROR); +} + +static inline t_error do_operation(t_i16 digit, t_u8 op, t_i16 *result) +{ + t_i16 rhs; + t_i16 res; + + rhs = *result; + res = *result; + if (op & OP_CHK) + { + if (op & OP_MUL && checked_mul_i16(rhs, digit, &res)) + return (ERROR); + else if (op & OP_ADD && checked_add_i16(rhs, digit, &res)) + return (ERROR); + else if (op & OP_SUB && checked_sub_i16(rhs, digit, &res)) + return (ERROR); + } + else + { + if (op & OP_MUL) + res = rhs * digit; + else if (op & OP_ADD) + res = rhs + digit; + else if (op & OP_SUB) + res = rhs - digit; + } + *result = res; + return (NO_ERROR); +} + +static inline t_error loop_inner(t_const_str s, t_u32 radix, t_u8 op, + t_i16 *out) +{ + t_u32 digit; + t_i16 result; + + result = 0; + while (*s) + { + if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result)) + return (ERROR); + if (to_digit(*s, radix, &digit)) + return (ERROR); + if (do_operation(digit, op, &result)) + return (ERROR); + s++; + } + if (out) + *out = result; + return (NO_ERROR); +} + +t_error str_to_i16(t_const_str s, t_u32 radix, t_i16 *out) +{ + t_usize digits_len; + bool is_positive; + t_u8 op; + + if (radix < 2 || radix > 36) + return (ERROR); + digits_len = str_len(s); + is_positive = true; + if (digits_len == 0) + return (ERROR); + if ((s[0] == '-' || s[0] == '+') && s[1] == '\0') + return (ERROR); + else if (s[0] == '+') + is_positive = (s++, digits_len--, true); + else if (s[0] == '-' && true) + is_positive = (s++, digits_len--, false); + if (is_positive) + op = OP_ADD; + else + op = OP_SUB; + if (!can_not_overflow(radix, true, digits_len)) + op |= OP_CHK; + return (loop_inner(s, radix, op, out)); +} diff --git a/output/src/convert/str_to_i16_utils.c b/output/src/convert/str_to_i16_utils.c new file mode 100644 index 0000000..4ecbbe2 --- /dev/null +++ b/output/src/convert/str_to_i16_utils.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i16_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 18:55:47 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#include "me/types.h" + +t_error checked_add_i16(t_i16 lhs, t_i16 rhs, t_i16 *out) +{ + if (rhs > 0 && (lhs > 32767 - rhs)) + return (ERROR); + *out = (t_i16)(lhs + rhs); + return (NO_ERROR); +} + +t_error checked_sub_i16(t_i16 lhs, t_i16 rhs, t_i16 *out) +{ + if ((((rhs & (1 << (sizeof(t_i16) - 1)) || rhs == 0) || !true) && (lhs < \ + -32768 + rhs))) + return (ERROR); + *out = (t_i16)(lhs - rhs); + return (NO_ERROR); +} + +t_error checked_mul_i16(t_i16 lhs, t_i16 rhs, t_i16 *out) +{ + t_i16 mul; + + mul = lhs * rhs; + if (lhs != 0 && mul / lhs != rhs) + return (ERROR); + *out = mul; + return (NO_ERROR); +} diff --git a/output/src/convert/str_to_i32.c b/output/src/convert/str_to_i32.c new file mode 100644 index 0000000..4956029 --- /dev/null +++ b/output/src/convert/str_to_i32.c @@ -0,0 +1,129 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/string/str_len.h" +#include "me/types.h" + +#define OP_ADD 0b0001 +#define OP_SUB 0b0010 +#define OP_MUL 0b0100 +#define OP_CHK 0b1000 + +t_error checked_add_i32(t_i32 lhs, t_i32 rhs, t_i32 *out); +t_error checked_sub_i32(t_i32 lhs, t_i32 rhs, t_i32 *out); +t_error checked_mul_i32(t_i32 lhs, t_i32 rhs, t_i32 *out); + +static inline bool can_not_overflow(t_u32 radix, bool is_signed_type, + t_usize digits_len) +{ + return (radix <= 16 && digits_len <= sizeof(t_i32) * 2 + - (t_usize)is_signed_type); +} + +static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out) +{ + t_u32 digit; + + if (radix < 2 || radix > 36) + return (ERROR); + digit = ascii - '0'; + if (radix > 10 && digit >= 10) + { + if (!me_isalpha(ascii)) + return (ERROR); + digit = ((ascii | 0b100000) - 'a') + 10; + } + if (digit >= radix) + return (ERROR); + *out = digit; + return (NO_ERROR); +} + +static inline t_error do_operation(t_i32 digit, t_u8 op, t_i32 *result) +{ + t_i32 rhs; + t_i32 res; + + rhs = *result; + res = *result; + if (op & OP_CHK) + { + if (op & OP_MUL && checked_mul_i32(rhs, digit, &res)) + return (ERROR); + else if (op & OP_ADD && checked_add_i32(rhs, digit, &res)) + return (ERROR); + else if (op & OP_SUB && checked_sub_i32(rhs, digit, &res)) + return (ERROR); + } + else + { + if (op & OP_MUL) + res = rhs * digit; + else if (op & OP_ADD) + res = rhs + digit; + else if (op & OP_SUB) + res = rhs - digit; + } + *result = res; + return (NO_ERROR); +} + +static inline t_error loop_inner(t_const_str s, t_u32 radix, t_u8 op, + t_i32 *out) +{ + t_u32 digit; + t_i32 result; + + result = 0; + while (*s) + { + if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result)) + return (ERROR); + if (to_digit(*s, radix, &digit)) + return (ERROR); + if (do_operation(digit, op, &result)) + return (ERROR); + s++; + } + if (out) + *out = result; + return (NO_ERROR); +} + +t_error str_to_i32(t_const_str s, t_u32 radix, t_i32 *out) +{ + t_usize digits_len; + bool is_positive; + t_u8 op; + + if (radix < 2 || radix > 36) + return (ERROR); + digits_len = str_len(s); + is_positive = true; + if (digits_len == 0) + return (ERROR); + if ((s[0] == '-' || s[0] == '+') && s[1] == '\0') + return (ERROR); + else if (s[0] == '+') + is_positive = (s++, digits_len--, true); + else if (s[0] == '-' && true) + is_positive = (s++, digits_len--, false); + if (is_positive) + op = OP_ADD; + else + op = OP_SUB; + if (!can_not_overflow(radix, true, digits_len)) + op |= OP_CHK; + return (loop_inner(s, radix, op, out)); +} diff --git a/output/src/convert/str_to_i32_utils.c b/output/src/convert/str_to_i32_utils.c new file mode 100644 index 0000000..5b6f5fd --- /dev/null +++ b/output/src/convert/str_to_i32_utils.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i32_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 18:56:06 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#include "me/types.h" + +t_error checked_add_i32(t_i32 lhs, t_i32 rhs, t_i32 *out) +{ + if (rhs > 0 && (lhs > 2147483647 - rhs)) + return (ERROR); + *out = (t_i32)(lhs + rhs); + return (NO_ERROR); +} + +t_error checked_sub_i32(t_i32 lhs, t_i32 rhs, t_i32 *out) +{ + if ((((rhs & (1 << (sizeof(t_i32) - 1)) || rhs == 0) || !true) && (lhs < \ + -2147483648 + rhs))) + return (ERROR); + *out = (t_i32)(lhs - rhs); + return (NO_ERROR); +} + +t_error checked_mul_i32(t_i32 lhs, t_i32 rhs, t_i32 *out) +{ + t_i32 mul; + + mul = lhs * rhs; + if (lhs != 0 && mul / lhs != rhs) + return (ERROR); + *out = mul; + return (NO_ERROR); +} diff --git a/output/src/convert/str_to_i64.c b/output/src/convert/str_to_i64.c new file mode 100644 index 0000000..56a48cc --- /dev/null +++ b/output/src/convert/str_to_i64.c @@ -0,0 +1,129 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/string/str_len.h" +#include "me/types.h" + +#define OP_ADD 0b0001 +#define OP_SUB 0b0010 +#define OP_MUL 0b0100 +#define OP_CHK 0b1000 + +t_error checked_add_i64(t_i64 lhs, t_i64 rhs, t_i64 *out); +t_error checked_sub_i64(t_i64 lhs, t_i64 rhs, t_i64 *out); +t_error checked_mul_i64(t_i64 lhs, t_i64 rhs, t_i64 *out); + +static inline bool can_not_overflow(t_u32 radix, bool is_signed_type, + t_usize digits_len) +{ + return (radix <= 16 && digits_len <= sizeof(t_i64) * 2 + - (t_usize)is_signed_type); +} + +static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out) +{ + t_u32 digit; + + if (radix < 2 || radix > 36) + return (ERROR); + digit = ascii - '0'; + if (radix > 10 && digit >= 10) + { + if (!me_isalpha(ascii)) + return (ERROR); + digit = ((ascii | 0b100000) - 'a') + 10; + } + if (digit >= radix) + return (ERROR); + *out = digit; + return (NO_ERROR); +} + +static inline t_error do_operation(t_i64 digit, t_u8 op, t_i64 *result) +{ + t_i64 rhs; + t_i64 res; + + rhs = *result; + res = *result; + if (op & OP_CHK) + { + if (op & OP_MUL && checked_mul_i64(rhs, digit, &res)) + return (ERROR); + else if (op & OP_ADD && checked_add_i64(rhs, digit, &res)) + return (ERROR); + else if (op & OP_SUB && checked_sub_i64(rhs, digit, &res)) + return (ERROR); + } + else + { + if (op & OP_MUL) + res = rhs * digit; + else if (op & OP_ADD) + res = rhs + digit; + else if (op & OP_SUB) + res = rhs - digit; + } + *result = res; + return (NO_ERROR); +} + +static inline t_error loop_inner(t_const_str s, t_u32 radix, t_u8 op, + t_i64 *out) +{ + t_u32 digit; + t_i64 result; + + result = 0ll; + while (*s) + { + if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result)) + return (ERROR); + if (to_digit(*s, radix, &digit)) + return (ERROR); + if (do_operation(digit, op, &result)) + return (ERROR); + s++; + } + if (out) + *out = result; + return (NO_ERROR); +} + +t_error str_to_i64(t_const_str s, t_u32 radix, t_i64 *out) +{ + t_usize digits_len; + bool is_positive; + t_u8 op; + + if (radix < 2 || radix > 36) + return (ERROR); + digits_len = str_len(s); + is_positive = true; + if (digits_len == 0) + return (ERROR); + if ((s[0] == '-' || s[0] == '+') && s[1] == '\0') + return (ERROR); + else if (s[0] == '+') + is_positive = (s++, digits_len--, true); + else if (s[0] == '-' && true) + is_positive = (s++, digits_len--, false); + if (is_positive) + op = OP_ADD; + else + op = OP_SUB; + if (!can_not_overflow(radix, true, digits_len)) + op |= OP_CHK; + return (loop_inner(s, radix, op, out)); +} diff --git a/output/src/convert/str_to_i64_utils.c b/output/src/convert/str_to_i64_utils.c new file mode 100644 index 0000000..1dd268d --- /dev/null +++ b/output/src/convert/str_to_i64_utils.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 18:55:37 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#include "me/types.h" + +t_error checked_add_i64(t_i64 lhs, t_i64 rhs, t_i64 *out) +{ + if (rhs > 0 && (lhs > 9223372036854775807ll - rhs)) + return (ERROR); + *out = (t_i64)(lhs + rhs); + return (NO_ERROR); +} + +t_error checked_sub_i64(t_i64 lhs, t_i64 rhs, t_i64 *out) +{ + if ((((rhs & (1 << (sizeof(t_i64) - 1)) || rhs == 0) || !true) && (lhs < \ + -(~9223372036854775807ll + 1) + rhs))) + return (ERROR); + *out = (t_i64)(lhs - rhs); + return (NO_ERROR); +} + +t_error checked_mul_i64(t_i64 lhs, t_i64 rhs, t_i64 *out) +{ + t_i64 mul; + + mul = lhs * rhs; + if (lhs != 0 && mul / lhs != rhs) + return (ERROR); + *out = mul; + return (NO_ERROR); +} diff --git a/output/src/convert/str_to_i8.c b/output/src/convert/str_to_i8.c new file mode 100644 index 0000000..80f91a0 --- /dev/null +++ b/output/src/convert/str_to_i8.c @@ -0,0 +1,129 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/string/str_len.h" +#include "me/types.h" + +#define OP_ADD 0b0001 +#define OP_SUB 0b0010 +#define OP_MUL 0b0100 +#define OP_CHK 0b1000 + +t_error checked_add_i8(t_i8 lhs, t_i8 rhs, t_i8 *out); +t_error checked_sub_i8(t_i8 lhs, t_i8 rhs, t_i8 *out); +t_error checked_mul_i8(t_i8 lhs, t_i8 rhs, t_i8 *out); + +static inline bool can_not_overflow(t_u32 radix, bool is_signed_type, + t_usize digits_len) +{ + return (radix <= 16 && digits_len <= sizeof(t_i8) * 2 + - (t_usize)is_signed_type); +} + +static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out) +{ + t_u32 digit; + + if (radix < 2 || radix > 36) + return (ERROR); + digit = ascii - '0'; + if (radix > 10 && digit >= 10) + { + if (!me_isalpha(ascii)) + return (ERROR); + digit = ((ascii | 0b100000) - 'a') + 10; + } + if (digit >= radix) + return (ERROR); + *out = digit; + return (NO_ERROR); +} + +static inline t_error do_operation(t_i8 digit, t_u8 op, t_i8 *result) +{ + t_i8 rhs; + t_i8 res; + + rhs = *result; + res = *result; + if (op & OP_CHK) + { + if (op & OP_MUL && checked_mul_i8(rhs, digit, &res)) + return (ERROR); + else if (op & OP_ADD && checked_add_i8(rhs, digit, &res)) + return (ERROR); + else if (op & OP_SUB && checked_sub_i8(rhs, digit, &res)) + return (ERROR); + } + else + { + if (op & OP_MUL) + res = rhs * digit; + else if (op & OP_ADD) + res = rhs + digit; + else if (op & OP_SUB) + res = rhs - digit; + } + *result = res; + return (NO_ERROR); +} + +static inline t_error loop_inner(t_const_str s, t_u32 radix, t_u8 op, + t_i8 *out) +{ + t_u32 digit; + t_i8 result; + + result = 0; + while (*s) + { + if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result)) + return (ERROR); + if (to_digit(*s, radix, &digit)) + return (ERROR); + if (do_operation(digit, op, &result)) + return (ERROR); + s++; + } + if (out) + *out = result; + return (NO_ERROR); +} + +t_error str_to_i8(t_const_str s, t_u32 radix, t_i8 *out) +{ + t_usize digits_len; + bool is_positive; + t_u8 op; + + if (radix < 2 || radix > 36) + return (ERROR); + digits_len = str_len(s); + is_positive = true; + if (digits_len == 0) + return (ERROR); + if ((s[0] == '-' || s[0] == '+') && s[1] == '\0') + return (ERROR); + else if (s[0] == '+') + is_positive = (s++, digits_len--, true); + else if (s[0] == '-' && true) + is_positive = (s++, digits_len--, false); + if (is_positive) + op = OP_ADD; + else + op = OP_SUB; + if (!can_not_overflow(radix, true, digits_len)) + op |= OP_CHK; + return (loop_inner(s, radix, op, out)); +} diff --git a/output/src/convert/str_to_i8_utils.c b/output/src/convert/str_to_i8_utils.c new file mode 100644 index 0000000..2073dd6 --- /dev/null +++ b/output/src/convert/str_to_i8_utils.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#include "me/types.h" + +t_error checked_add_i8(t_i8 lhs, t_i8 rhs, t_i8 *out) +{ + if (rhs > 0 && (lhs > 127 - rhs)) + return (ERROR); + *out = (t_i8)(lhs + rhs); + return (NO_ERROR); +} + +t_error checked_sub_i8(t_i8 lhs, t_i8 rhs, t_i8 *out) +{ + if ((((rhs & (1 << (sizeof(t_i8) - 1)) || rhs == 0) || !true) && (lhs < -128 + + rhs))) + return (ERROR); + *out = (t_i8)(lhs - rhs); + return (NO_ERROR); +} + +t_error checked_mul_i8(t_i8 lhs, t_i8 rhs, t_i8 *out) +{ + t_i8 mul; + + mul = lhs * rhs; + if (lhs != 0 && mul / lhs != rhs) + return (ERROR); + *out = mul; + return (NO_ERROR); +} diff --git a/output/src/convert/str_to_u16.c b/output/src/convert/str_to_u16.c new file mode 100644 index 0000000..7c5bbaa --- /dev/null +++ b/output/src/convert/str_to_u16.c @@ -0,0 +1,129 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/string/str_len.h" +#include "me/types.h" + +#define OP_ADD 0b0001 +#define OP_SUB 0b0010 +#define OP_MUL 0b0100 +#define OP_CHK 0b1000 + +t_error checked_add_u16(t_u16 lhs, t_u16 rhs, t_u16 *out); +t_error checked_sub_u16(t_u16 lhs, t_u16 rhs, t_u16 *out); +t_error checked_mul_u16(t_u16 lhs, t_u16 rhs, t_u16 *out); + +static inline bool can_not_overflow(t_u32 radix, bool is_signed_type, + t_usize digits_len) +{ + return (radix <= 16 && digits_len <= sizeof(t_u16) * 2 + - (t_usize)is_signed_type); +} + +static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out) +{ + t_u32 digit; + + if (radix < 2 || radix > 36) + return (ERROR); + digit = ascii - '0'; + if (radix > 10 && digit >= 10) + { + if (!me_isalpha(ascii)) + return (ERROR); + digit = ((ascii | 0b100000) - 'a') + 10; + } + if (digit >= radix) + return (ERROR); + *out = digit; + return (NO_ERROR); +} + +static inline t_error do_operation(t_u16 digit, t_u8 op, t_u16 *result) +{ + t_u16 rhs; + t_u16 res; + + rhs = *result; + res = *result; + if (op & OP_CHK) + { + if (op & OP_MUL && checked_mul_u16(rhs, digit, &res)) + return (ERROR); + else if (op & OP_ADD && checked_add_u16(rhs, digit, &res)) + return (ERROR); + else if (op & OP_SUB && checked_sub_u16(rhs, digit, &res)) + return (ERROR); + } + else + { + if (op & OP_MUL) + res = rhs * digit; + else if (op & OP_ADD) + res = rhs + digit; + else if (op & OP_SUB) + res = rhs - digit; + } + *result = res; + return (NO_ERROR); +} + +static inline t_error loop_inner(t_const_str s, t_u32 radix, t_u8 op, + t_u16 *out) +{ + t_u32 digit; + t_u16 result; + + result = 0u; + while (*s) + { + if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result)) + return (ERROR); + if (to_digit(*s, radix, &digit)) + return (ERROR); + if (do_operation(digit, op, &result)) + return (ERROR); + s++; + } + if (out) + *out = result; + return (NO_ERROR); +} + +t_error str_to_u16(t_const_str s, t_u32 radix, t_u16 *out) +{ + t_usize digits_len; + bool is_positive; + t_u8 op; + + if (radix < 2 || radix > 36) + return (ERROR); + digits_len = str_len(s); + is_positive = true; + if (digits_len == 0) + return (ERROR); + if ((s[0] == '-' || s[0] == '+') && s[1] == '\0') + return (ERROR); + else if (s[0] == '+') + is_positive = (s++, digits_len--, true); + else if (s[0] == '-' && false) + is_positive = (s++, digits_len--, false); + if (is_positive) + op = OP_ADD; + else + op = OP_SUB; + if (!can_not_overflow(radix, false, digits_len)) + op |= OP_CHK; + return (loop_inner(s, radix, op, out)); +} diff --git a/output/src/convert/str_to_u16_utils.c b/output/src/convert/str_to_u16_utils.c new file mode 100644 index 0000000..85fd7b2 --- /dev/null +++ b/output/src/convert/str_to_u16_utils.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#include "me/types.h" + +t_error checked_add_u16(t_u16 lhs, t_u16 rhs, t_u16 *out) +{ + if (rhs > 0 && (lhs > 65535u - rhs)) + return (ERROR); + *out = (t_u16)(lhs + rhs); + return (NO_ERROR); +} + +t_error checked_sub_u16(t_u16 lhs, t_u16 rhs, t_u16 *out) +{ + if ((((rhs & (1 << (sizeof(t_u16) - 1)) || rhs == 0) || !false) && (lhs < 0u + + rhs))) + return (ERROR); + *out = (t_u16)(lhs - rhs); + return (NO_ERROR); +} + +t_error checked_mul_u16(t_u16 lhs, t_u16 rhs, t_u16 *out) +{ + t_u16 mul; + + mul = lhs * rhs; + if (lhs != 0 && mul / lhs != rhs) + return (ERROR); + *out = mul; + return (NO_ERROR); +} diff --git a/output/src/convert/str_to_u32.c b/output/src/convert/str_to_u32.c new file mode 100644 index 0000000..1b625d1 --- /dev/null +++ b/output/src/convert/str_to_u32.c @@ -0,0 +1,129 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/string/str_len.h" +#include "me/types.h" + +#define OP_ADD 0b0001 +#define OP_SUB 0b0010 +#define OP_MUL 0b0100 +#define OP_CHK 0b1000 + +t_error checked_add_u32(t_u32 lhs, t_u32 rhs, t_u32 *out); +t_error checked_sub_u32(t_u32 lhs, t_u32 rhs, t_u32 *out); +t_error checked_mul_u32(t_u32 lhs, t_u32 rhs, t_u32 *out); + +static inline bool can_not_overflow(t_u32 radix, bool is_signed_type, + t_usize digits_len) +{ + return (radix <= 16 && digits_len <= sizeof(t_u32) * 2 + - (t_usize)is_signed_type); +} + +static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out) +{ + t_u32 digit; + + if (radix < 2 || radix > 36) + return (ERROR); + digit = ascii - '0'; + if (radix > 10 && digit >= 10) + { + if (!me_isalpha(ascii)) + return (ERROR); + digit = ((ascii | 0b100000) - 'a') + 10; + } + if (digit >= radix) + return (ERROR); + *out = digit; + return (NO_ERROR); +} + +static inline t_error do_operation(t_u32 digit, t_u8 op, t_u32 *result) +{ + t_u32 rhs; + t_u32 res; + + rhs = *result; + res = *result; + if (op & OP_CHK) + { + if (op & OP_MUL && checked_mul_u32(rhs, digit, &res)) + return (ERROR); + else if (op & OP_ADD && checked_add_u32(rhs, digit, &res)) + return (ERROR); + else if (op & OP_SUB && checked_sub_u32(rhs, digit, &res)) + return (ERROR); + } + else + { + if (op & OP_MUL) + res = rhs * digit; + else if (op & OP_ADD) + res = rhs + digit; + else if (op & OP_SUB) + res = rhs - digit; + } + *result = res; + return (NO_ERROR); +} + +static inline t_error loop_inner(t_const_str s, t_u32 radix, t_u8 op, + t_u32 *out) +{ + t_u32 digit; + t_u32 result; + + result = 0u; + while (*s) + { + if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result)) + return (ERROR); + if (to_digit(*s, radix, &digit)) + return (ERROR); + if (do_operation(digit, op, &result)) + return (ERROR); + s++; + } + if (out) + *out = result; + return (NO_ERROR); +} + +t_error str_to_u32(t_const_str s, t_u32 radix, t_u32 *out) +{ + t_usize digits_len; + bool is_positive; + t_u8 op; + + if (radix < 2 || radix > 36) + return (ERROR); + digits_len = str_len(s); + is_positive = true; + if (digits_len == 0) + return (ERROR); + if ((s[0] == '-' || s[0] == '+') && s[1] == '\0') + return (ERROR); + else if (s[0] == '+') + is_positive = (s++, digits_len--, true); + else if (s[0] == '-' && false) + is_positive = (s++, digits_len--, false); + if (is_positive) + op = OP_ADD; + else + op = OP_SUB; + if (!can_not_overflow(radix, false, digits_len)) + op |= OP_CHK; + return (loop_inner(s, radix, op, out)); +} diff --git a/output/src/convert/str_to_u32_utils.c b/output/src/convert/str_to_u32_utils.c new file mode 100644 index 0000000..3a929d5 --- /dev/null +++ b/output/src/convert/str_to_u32_utils.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#include "me/types.h" + +t_error checked_add_u32(t_u32 lhs, t_u32 rhs, t_u32 *out) +{ + if (rhs > 0 && (lhs > 4294967295u - rhs)) + return (ERROR); + *out = (t_u32)(lhs + rhs); + return (NO_ERROR); +} + +t_error checked_sub_u32(t_u32 lhs, t_u32 rhs, t_u32 *out) +{ + if ((((rhs & (1 << (sizeof(t_u32) - 1)) || rhs == 0) || !false) && (lhs < 0u + + rhs))) + return (ERROR); + *out = (t_u32)(lhs - rhs); + return (NO_ERROR); +} + +t_error checked_mul_u32(t_u32 lhs, t_u32 rhs, t_u32 *out) +{ + t_u32 mul; + + mul = lhs * rhs; + if (lhs != 0 && mul / lhs != rhs) + return (ERROR); + *out = mul; + return (NO_ERROR); +} diff --git a/output/src/convert/str_to_u64.c b/output/src/convert/str_to_u64.c new file mode 100644 index 0000000..b744be8 --- /dev/null +++ b/output/src/convert/str_to_u64.c @@ -0,0 +1,129 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/string/str_len.h" +#include "me/types.h" + +#define OP_ADD 0b0001 +#define OP_SUB 0b0010 +#define OP_MUL 0b0100 +#define OP_CHK 0b1000 + +t_error checked_add_u64(t_u64 lhs, t_u64 rhs, t_u64 *out); +t_error checked_sub_u64(t_u64 lhs, t_u64 rhs, t_u64 *out); +t_error checked_mul_u64(t_u64 lhs, t_u64 rhs, t_u64 *out); + +static inline bool can_not_overflow(t_u32 radix, bool is_signed_type, + t_usize digits_len) +{ + return (radix <= 16 && digits_len <= sizeof(t_u64) * 2 + - (t_usize)is_signed_type); +} + +static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out) +{ + t_u32 digit; + + if (radix < 2 || radix > 36) + return (ERROR); + digit = ascii - '0'; + if (radix > 10 && digit >= 10) + { + if (!me_isalpha(ascii)) + return (ERROR); + digit = ((ascii | 0b100000) - 'a') + 10; + } + if (digit >= radix) + return (ERROR); + *out = digit; + return (NO_ERROR); +} + +static inline t_error do_operation(t_u64 digit, t_u8 op, t_u64 *result) +{ + t_u64 rhs; + t_u64 res; + + rhs = *result; + res = *result; + if (op & OP_CHK) + { + if (op & OP_MUL && checked_mul_u64(rhs, digit, &res)) + return (ERROR); + else if (op & OP_ADD && checked_add_u64(rhs, digit, &res)) + return (ERROR); + else if (op & OP_SUB && checked_sub_u64(rhs, digit, &res)) + return (ERROR); + } + else + { + if (op & OP_MUL) + res = rhs * digit; + else if (op & OP_ADD) + res = rhs + digit; + else if (op & OP_SUB) + res = rhs - digit; + } + *result = res; + return (NO_ERROR); +} + +static inline t_error loop_inner(t_const_str s, t_u32 radix, t_u8 op, + t_u64 *out) +{ + t_u32 digit; + t_u64 result; + + result = 0llu; + while (*s) + { + if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result)) + return (ERROR); + if (to_digit(*s, radix, &digit)) + return (ERROR); + if (do_operation(digit, op, &result)) + return (ERROR); + s++; + } + if (out) + *out = result; + return (NO_ERROR); +} + +t_error str_to_u64(t_const_str s, t_u32 radix, t_u64 *out) +{ + t_usize digits_len; + bool is_positive; + t_u8 op; + + if (radix < 2 || radix > 36) + return (ERROR); + digits_len = str_len(s); + is_positive = true; + if (digits_len == 0) + return (ERROR); + if ((s[0] == '-' || s[0] == '+') && s[1] == '\0') + return (ERROR); + else if (s[0] == '+') + is_positive = (s++, digits_len--, true); + else if (s[0] == '-' && false) + is_positive = (s++, digits_len--, false); + if (is_positive) + op = OP_ADD; + else + op = OP_SUB; + if (!can_not_overflow(radix, false, digits_len)) + op |= OP_CHK; + return (loop_inner(s, radix, op, out)); +} diff --git a/output/src/convert/str_to_u64_utils.c b/output/src/convert/str_to_u64_utils.c new file mode 100644 index 0000000..f2bb77f --- /dev/null +++ b/output/src/convert/str_to_u64_utils.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#include "me/types.h" + +t_error checked_add_u64(t_u64 lhs, t_u64 rhs, t_u64 *out) +{ + if (rhs > 0 && (lhs > 18446744073709551615llu - rhs)) + return (ERROR); + *out = (t_u64)(lhs + rhs); + return (NO_ERROR); +} + +t_error checked_sub_u64(t_u64 lhs, t_u64 rhs, t_u64 *out) +{ + if ((((rhs & (1 << (sizeof(t_u64) - 1)) || rhs == 0) || !false) + && (lhs < 0llu + rhs))) + return (ERROR); + *out = (t_u64)(lhs - rhs); + return (NO_ERROR); +} + +t_error checked_mul_u64(t_u64 lhs, t_u64 rhs, t_u64 *out) +{ + t_u64 mul; + + mul = lhs * rhs; + if (lhs != 0 && mul / lhs != rhs) + return (ERROR); + *out = mul; + return (NO_ERROR); +} diff --git a/output/src/convert/str_to_u8.c b/output/src/convert/str_to_u8.c new file mode 100644 index 0000000..8fac9fa --- /dev/null +++ b/output/src/convert/str_to_u8.c @@ -0,0 +1,129 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/string/str_len.h" +#include "me/types.h" + +#define OP_ADD 0b0001 +#define OP_SUB 0b0010 +#define OP_MUL 0b0100 +#define OP_CHK 0b1000 + +t_error checked_add_u8(t_u8 lhs, t_u8 rhs, t_u8 *out); +t_error checked_sub_u8(t_u8 lhs, t_u8 rhs, t_u8 *out); +t_error checked_mul_u8(t_u8 lhs, t_u8 rhs, t_u8 *out); + +static inline bool can_not_overflow(t_u32 radix, bool is_signed_type, + t_usize digits_len) +{ + return (radix <= 16 && digits_len <= sizeof(t_u8) * 2 + - (t_usize)is_signed_type); +} + +static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out) +{ + t_u32 digit; + + if (radix < 2 || radix > 36) + return (ERROR); + digit = ascii - '0'; + if (radix > 10 && digit >= 10) + { + if (!me_isalpha(ascii)) + return (ERROR); + digit = ((ascii | 0b100000) - 'a') + 10; + } + if (digit >= radix) + return (ERROR); + *out = digit; + return (NO_ERROR); +} + +static inline t_error do_operation(t_u8 digit, t_u8 op, t_u8 *result) +{ + t_u8 rhs; + t_u8 res; + + rhs = *result; + res = *result; + if (op & OP_CHK) + { + if (op & OP_MUL && checked_mul_u8(rhs, digit, &res)) + return (ERROR); + else if (op & OP_ADD && checked_add_u8(rhs, digit, &res)) + return (ERROR); + else if (op & OP_SUB && checked_sub_u8(rhs, digit, &res)) + return (ERROR); + } + else + { + if (op & OP_MUL) + res = rhs * digit; + else if (op & OP_ADD) + res = rhs + digit; + else if (op & OP_SUB) + res = rhs - digit; + } + *result = res; + return (NO_ERROR); +} + +static inline t_error loop_inner(t_const_str s, t_u32 radix, t_u8 op, + t_u8 *out) +{ + t_u32 digit; + t_u8 result; + + result = 0u; + while (*s) + { + if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result)) + return (ERROR); + if (to_digit(*s, radix, &digit)) + return (ERROR); + if (do_operation(digit, op, &result)) + return (ERROR); + s++; + } + if (out) + *out = result; + return (NO_ERROR); +} + +t_error str_to_u8(t_const_str s, t_u32 radix, t_u8 *out) +{ + t_usize digits_len; + bool is_positive; + t_u8 op; + + if (radix < 2 || radix > 36) + return (ERROR); + digits_len = str_len(s); + is_positive = true; + if (digits_len == 0) + return (ERROR); + if ((s[0] == '-' || s[0] == '+') && s[1] == '\0') + return (ERROR); + else if (s[0] == '+') + is_positive = (s++, digits_len--, true); + else if (s[0] == '-' && false) + is_positive = (s++, digits_len--, false); + if (is_positive) + op = OP_ADD; + else + op = OP_SUB; + if (!can_not_overflow(radix, false, digits_len)) + op |= OP_CHK; + return (loop_inner(s, radix, op, out)); +} diff --git a/output/src/convert/str_to_u8_utils.c b/output/src/convert/str_to_u8_utils.c new file mode 100644 index 0000000..14a4a04 --- /dev/null +++ b/output/src/convert/str_to_u8_utils.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#include "me/types.h" + +t_error checked_add_u8(t_u8 lhs, t_u8 rhs, t_u8 *out) +{ + if (rhs > 0 && (lhs > 128u - rhs)) + return (ERROR); + *out = (t_u8)(lhs + rhs); + return (NO_ERROR); +} + +t_error checked_sub_u8(t_u8 lhs, t_u8 rhs, t_u8 *out) +{ + if ((((rhs & (1 << (sizeof(t_u8) - 1)) || rhs == 0) || !false) && (lhs < 0u + + rhs))) + return (ERROR); + *out = (t_u8)(lhs - rhs); + return (NO_ERROR); +} + +t_error checked_mul_u8(t_u8 lhs, t_u8 rhs, t_u8 *out) +{ + t_u8 mul; + + mul = lhs * rhs; + if (lhs != 0 && mul / lhs != rhs) + return (ERROR); + *out = mul; + return (NO_ERROR); +} diff --git a/output/src/vec/vec_i64.c b/output/src/vec/vec_i64.c new file mode 100644 index 0000000..1a24dec --- /dev/null +++ b/output/src/vec/vec_i64.c @@ -0,0 +1,114 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/05 18:46:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 17:54:11 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_copy.h" +#include "me/mem/mem_set_zero.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" +#include + +t_vec_i64 vec_i64_new(t_usize capacity, t_free_i64_item free_function) +{ + t_vec_i64 out; + + out = (t_vec_i64){0}; + out.free_func = free_function; + out.buffer = mem_alloc_array(capacity, sizeof(t_i64)); + if (out.buffer) + out.capacity = capacity; + return (out); +} + +/// Return true in case of an error +t_error vec_i64_push(t_vec_i64 *vec, t_i64 element) +{ + t_i64 *temp_buffer; + size_t new_capacity; + + if (vec == NULL) + return (ERROR); + if (vec->len + 1 > vec->capacity) + { + new_capacity = (vec->capacity * 3) / 2 + 1; + while (vec->len + 1 > new_capacity) + new_capacity = (new_capacity * 3) / 2 + 1; + temp_buffer = mem_alloc_array(new_capacity, sizeof(t_i64)); + if (temp_buffer == NULL) + return (ERROR); + mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_i64)); + free(vec->buffer); + vec->buffer = temp_buffer; + vec->capacity = new_capacity; + } + vec->buffer[vec->len] = element; + vec->len += 1; + return (NO_ERROR); +} + +/// Return true in case of an error +t_error vec_i64_reserve(t_vec_i64 *vec, t_usize wanted_capacity) +{ + t_i64 *temp_buffer; + size_t new_capacity; + + if (vec == NULL) + return (ERROR); + if (wanted_capacity > vec->capacity) + { + new_capacity = (vec->capacity * 3) / 2 + 1; + while (wanted_capacity > new_capacity) + new_capacity = (new_capacity * 3) / 2 + 1; + temp_buffer = mem_alloc_array(new_capacity, sizeof(t_i64)); + if (temp_buffer == NULL) + return (ERROR); + mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_i64)); + free(vec->buffer); + vec->buffer = temp_buffer; + vec->capacity = new_capacity; + } + return (NO_ERROR); +} + +/// Return true if the vector is empty +/// This function is safe to call with value being NULL +t_error vec_i64_pop(t_vec_i64 *vec, t_i64 *value) +{ + t_i64 temp_value; + t_i64 *ptr; + + if (vec == NULL) + return (ERROR); + ptr = value; + if (vec->len == 0) + return (ERROR); + if (value == NULL) + ptr = &temp_value; + vec->len--; + *ptr = vec->buffer[vec->len]; + mem_set_zero(&vec->buffer[vec->len], sizeof(t_i64)); + return (NO_ERROR); +} + +/// This function is safe to call with `free_elem` being NULL +void vec_i64_free(t_vec_i64 vec) +{ + if (vec.free_func) + { + while (vec.len) + { + vec.free_func(vec.buffer[vec.len - 1]); + vec.len--; + } + } + free(vec.buffer); +} diff --git a/output/src/vec/vec_i64_bool.c b/output/src/vec/vec_i64_bool.c new file mode 100644 index 0000000..af10724 --- /dev/null +++ b/output/src/vec/vec_i64_bool.c @@ -0,0 +1,115 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_i64_bool.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/05 18:46:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 17:54:11 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_copy.h" +#include "me/mem/mem_set_zero.h" +#include "me/types.h" +#include "me/vec/vec_i64_bool.h" +#include + +t_vec_i64_bool vec_i64_bool_new(t_usize capacity, + t_free_i64_bool_item free_function) +{ + t_vec_i64_bool out; + + out = (t_vec_i64_bool){0}; + out.free_func = free_function; + out.buffer = mem_alloc_array(capacity, sizeof(t_i64_bool)); + if (out.buffer) + out.capacity = capacity; + return (out); +} + +/// Return true in case of an error +t_error vec_i64_bool_push(t_vec_i64_bool *vec, t_i64_bool element) +{ + t_i64_bool *temp_buffer; + size_t new_capacity; + + if (vec == NULL) + return (ERROR); + if (vec->len + 1 > vec->capacity) + { + new_capacity = (vec->capacity * 3) / 2 + 1; + while (vec->len + 1 > new_capacity) + new_capacity = (new_capacity * 3) / 2 + 1; + temp_buffer = mem_alloc_array(new_capacity, sizeof(t_i64_bool)); + if (temp_buffer == NULL) + return (ERROR); + mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_i64_bool)); + free(vec->buffer); + vec->buffer = temp_buffer; + vec->capacity = new_capacity; + } + vec->buffer[vec->len] = element; + vec->len += 1; + return (NO_ERROR); +} + +/// Return true in case of an error +t_error vec_i64_bool_reserve(t_vec_i64_bool *vec, t_usize wanted_capacity) +{ + t_i64_bool *temp_buffer; + size_t new_capacity; + + if (vec == NULL) + return (ERROR); + if (wanted_capacity > vec->capacity) + { + new_capacity = (vec->capacity * 3) / 2 + 1; + while (wanted_capacity > new_capacity) + new_capacity = (new_capacity * 3) / 2 + 1; + temp_buffer = mem_alloc_array(new_capacity, sizeof(t_i64_bool)); + if (temp_buffer == NULL) + return (ERROR); + mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_i64_bool)); + free(vec->buffer); + vec->buffer = temp_buffer; + vec->capacity = new_capacity; + } + return (NO_ERROR); +} + +/// Return true if the vector is empty +/// This function is safe to call with value being NULL +t_error vec_i64_bool_pop(t_vec_i64_bool *vec, t_i64_bool *value) +{ + t_i64_bool temp_value; + t_i64_bool *ptr; + + if (vec == NULL) + return (ERROR); + ptr = value; + if (vec->len == 0) + return (ERROR); + if (value == NULL) + ptr = &temp_value; + vec->len--; + *ptr = vec->buffer[vec->len]; + mem_set_zero(&vec->buffer[vec->len], sizeof(t_i64_bool)); + return (NO_ERROR); +} + +/// This function is safe to call with `free_elem` being NULL +void vec_i64_bool_free(t_vec_i64_bool vec) +{ + if (vec.free_func) + { + while (vec.len) + { + vec.free_func(vec.buffer[vec.len - 1]); + vec.len--; + } + } + free(vec.buffer); +} diff --git a/output/src/vec/vec_i64_bool_functions2.c b/output/src/vec/vec_i64_bool_functions2.c new file mode 100644 index 0000000..01927c2 --- /dev/null +++ b/output/src/vec/vec_i64_bool_functions2.c @@ -0,0 +1,109 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_i64_bool.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_copy.h" +#include "me/mem/mem_set_zero.h" +#include "me/types.h" +#include "me/vec/vec_i64_bool.h" +#include + +t_error vec_i64_bool_find(t_vec_i64_bool *vec, bool (*fn)(const t_i64_bool *), + t_usize *index) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || index == NULL) + return (ERROR); + idx = 0; + while (idx < vec->len) + { + if (fn(&vec->buffer[idx])) + { + *index = idx; + return (NO_ERROR); + } + idx++; + } + return (ERROR); +} + +t_error vec_i64_bool_find_starting(t_vec_i64_bool *vec, + bool (*fn)(const t_i64_bool *), t_usize starting_index, t_usize *index) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || index == NULL) + return (ERROR); + idx = starting_index; + while (idx < vec->len) + { + if (fn(&vec->buffer[idx])) + { + *index = idx; + return (NO_ERROR); + } + idx++; + } + return (ERROR); +} + +t_error vec_i64_bool_all(t_vec_i64_bool *vec, bool (*fn)(const t_i64_bool *), + bool *result) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || result == NULL) + return (ERROR); + idx = 0; + *result = true; + while (*result && idx < vec->len) + { + if (!fn(&vec->buffer[idx])) + *result = false; + idx++; + } + return (ERROR); +} + +t_error vec_i64_bool_any(t_vec_i64_bool *vec, bool (*fn)(const t_i64_bool *), + bool *result) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || result == NULL) + return (ERROR); + idx = 0; + *result = false; + while (*result && idx < vec->len) + { + if (fn(&vec->buffer[idx])) + *result = true; + idx++; + } + return (ERROR); +} + +void vec_i64_bool_iter(t_vec_i64_bool *vec, void (*fn)(t_usize index, + t_i64_bool *value, void *state), void *state) +{ + t_usize idx; + + if (vec == NULL || fn == NULL) + return ; + idx = 0; + while (idx < vec->len) + { + fn(idx, &vec->buffer[idx], state); + idx++; + } +} diff --git a/output/src/vec/vec_i64_bool_functions3.c b/output/src/vec/vec_i64_bool_functions3.c new file mode 100644 index 0000000..ee82947 --- /dev/null +++ b/output/src/vec/vec_i64_bool_functions3.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_i64_bool.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_copy.h" +#include "me/mem/mem_set_zero.h" +#include "me/types.h" +#include "me/vec/vec_i64_bool.h" +#include + +t_error vec_i64_bool_push_front(t_vec_i64_bool *vec, t_i64_bool element) +{ + t_usize i; + + if (vec->len == 0) + return (vec_i64_bool_push(vec, element)); + i = vec->len - 1; + if (vec->capacity < vec->len + 1 && vec_i64_bool_reserve(vec, 3 * vec->len + / 2 + 1)) + return (ERROR); + while (i > 0) + { + vec->buffer[i + 1] = vec->buffer[i]; + i--; + } + vec->buffer[1] = vec->buffer[0]; + vec->buffer[0] = element; + vec->len++; + return (NO_ERROR); +} + +t_error vec_i64_bool_pop_front(t_vec_i64_bool *vec, t_i64_bool *value) +{ + t_usize i; + + if (vec->len <= 1) + return (vec_i64_bool_pop(vec, value)); + i = 0; + *value = vec->buffer[0]; + vec->len--; + while (i < vec->len) + { + vec->buffer[i] = vec->buffer[i + 1]; + i++; + } + mem_set_zero(&vec->buffer[i], sizeof(*vec->buffer)); + return (NO_ERROR); +} + +void vec_i64_bool_reverse(t_vec_i64_bool *vec) +{ + t_i64_bool temporary; + t_usize i; + + i = 0; + while (i < vec->len / 2) + { + temporary = vec->buffer[vec->len - 1 - i]; + vec->buffer[vec->len - 1 - i] = vec->buffer[i]; + vec->buffer[i] = temporary; + i++; + } +} diff --git a/output/src/vec/vec_i64_bool_sort.c b/output/src/vec/vec_i64_bool_sort.c new file mode 100644 index 0000000..edbdfc1 --- /dev/null +++ b/output/src/vec/vec_i64_bool_sort.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* best_move.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/29 20:04:33 by maiboyer #+# #+# */ +/* Updated: 2024/01/31 14:25:00 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/types.h" +#include "me/vec/vec_i64_bool.h" + +void vec_i64_bool_sort(t_vec_i64_bool *v, + t_vec_i64_bool_sort_fn is_sorted_fn) +{ + t_usize sorted_part; + t_usize i; + t_i64_bool tmp; + + if (v == NULL) + return ; + sorted_part = v->len; + while (sorted_part > 0) + { + i = 0; + while (i < sorted_part - 1) + { + if (!is_sorted_fn(&v->buffer[i], &v->buffer[i + 1])) + { + tmp = v->buffer[i]; + v->buffer[i] = v->buffer[i + 1]; + v->buffer[i + 1] = tmp; + } + i++; + } + sorted_part--; + } +} diff --git a/output/src/vec/vec_i64_functions2.c b/output/src/vec/vec_i64_functions2.c new file mode 100644 index 0000000..d7180bc --- /dev/null +++ b/output/src/vec/vec_i64_functions2.c @@ -0,0 +1,106 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_copy.h" +#include "me/mem/mem_set_zero.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" +#include + +t_error vec_i64_find(t_vec_i64 *vec, bool (*fn)(const t_i64 *), t_usize *index) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || index == NULL) + return (ERROR); + idx = 0; + while (idx < vec->len) + { + if (fn(&vec->buffer[idx])) + { + *index = idx; + return (NO_ERROR); + } + idx++; + } + return (ERROR); +} + +t_error vec_i64_find_starting(t_vec_i64 *vec, bool (*fn)(const t_i64 *), + t_usize starting_index, t_usize *index) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || index == NULL) + return (ERROR); + idx = starting_index; + while (idx < vec->len) + { + if (fn(&vec->buffer[idx])) + { + *index = idx; + return (NO_ERROR); + } + idx++; + } + return (ERROR); +} + +t_error vec_i64_all(t_vec_i64 *vec, bool (*fn)(const t_i64 *), bool *result) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || result == NULL) + return (ERROR); + idx = 0; + *result = true; + while (*result && idx < vec->len) + { + if (!fn(&vec->buffer[idx])) + *result = false; + idx++; + } + return (ERROR); +} + +t_error vec_i64_any(t_vec_i64 *vec, bool (*fn)(const t_i64 *), bool *result) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || result == NULL) + return (ERROR); + idx = 0; + *result = false; + while (*result && idx < vec->len) + { + if (fn(&vec->buffer[idx])) + *result = true; + idx++; + } + return (ERROR); +} + +void vec_i64_iter(t_vec_i64 *vec, void (*fn)(t_usize index, t_i64 *value, + void *state), void *state) +{ + t_usize idx; + + if (vec == NULL || fn == NULL) + return ; + idx = 0; + while (idx < vec->len) + { + fn(idx, &vec->buffer[idx], state); + idx++; + } +} diff --git a/output/src/vec/vec_i64_functions3.c b/output/src/vec/vec_i64_functions3.c new file mode 100644 index 0000000..653eee8 --- /dev/null +++ b/output/src/vec/vec_i64_functions3.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_copy.h" +#include "me/mem/mem_set_zero.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" +#include + +t_error vec_i64_push_front(t_vec_i64 *vec, t_i64 element) +{ + t_usize i; + + if (vec->len == 0) + return (vec_i64_push(vec, element)); + i = vec->len - 1; + if (vec->capacity < vec->len + 1 && vec_i64_reserve(vec, 3 * vec->len / 2 + + 1)) + return (ERROR); + while (i > 0) + { + vec->buffer[i + 1] = vec->buffer[i]; + i--; + } + vec->buffer[1] = vec->buffer[0]; + vec->buffer[0] = element; + vec->len++; + return (NO_ERROR); +} + +t_error vec_i64_pop_front(t_vec_i64 *vec, t_i64 *value) +{ + t_usize i; + + if (vec->len <= 1) + return (vec_i64_pop(vec, value)); + i = 0; + *value = vec->buffer[0]; + vec->len--; + while (i < vec->len) + { + vec->buffer[i] = vec->buffer[i + 1]; + i++; + } + mem_set_zero(&vec->buffer[i], sizeof(*vec->buffer)); + return (NO_ERROR); +} + +void vec_i64_reverse(t_vec_i64 *vec) +{ + t_i64 temporary; + t_usize i; + + i = 0; + while (i < vec->len / 2) + { + temporary = vec->buffer[vec->len - 1 - i]; + vec->buffer[vec->len - 1 - i] = vec->buffer[i]; + vec->buffer[i] = temporary; + i++; + } +} diff --git a/output/src/vec/vec_i64_sort.c b/output/src/vec/vec_i64_sort.c new file mode 100644 index 0000000..d32b66b --- /dev/null +++ b/output/src/vec/vec_i64_sort.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* best_move.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/29 20:04:33 by maiboyer #+# #+# */ +/* Updated: 2024/01/31 14:25:00 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/types.h" +#include "me/vec/vec_i64.h" + +void vec_i64_sort(t_vec_i64 *v, t_vec_i64_sort_fn is_sorted_fn) +{ + t_usize sorted_part; + t_usize i; + t_i64 tmp; + + if (v == NULL) + return ; + sorted_part = v->len; + while (sorted_part > 0) + { + i = 0; + while (i < sorted_part - 1) + { + if (!is_sorted_fn(&v->buffer[i], &v->buffer[i + 1])) + { + tmp = v->buffer[i]; + v->buffer[i] = v->buffer[i + 1]; + v->buffer[i + 1] = tmp; + } + i++; + } + sorted_part--; + } +} From ee1a1f272b1e632548c6837a0dbbd4b2c5ba5715 Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Thu, 8 Feb 2024 22:05:19 +0100 Subject: [PATCH 19/29] update stuff --- include/app/state.h | 6 +++- src/app/main.c | 19 +++++++++---- src/app/moves/push.c | 4 +-- src/app/moves/rev_rotate.c | 4 +-- src/app/moves/rotate.c | 15 +++++----- src/app/moves/swap.c | 17 ++++++------ src/bonus/main.c | 56 ++++++++++++++++++++++++++++++++++++++ src/bonus/move1.c | 13 +++++++++ 8 files changed, 106 insertions(+), 28 deletions(-) create mode 100644 src/bonus/main.c create mode 100644 src/bonus/move1.c diff --git a/include/app/state.h b/include/app/state.h index f05612a..2703d0e 100644 --- a/include/app/state.h +++ b/include/app/state.h @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 14:27:25 by maiboyer #+# #+# */ -/* Updated: 2024/01/31 15:09:44 by maiboyer ### ########.fr */ +/* Updated: 2024/02/08 19:05:37 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,10 @@ # include "me/vec/vec_i64.h" # include "me/vec/vec_i64_bool.h" +#ifndef BONUS +#define BONUS 0 +#endif + typedef struct s_state { t_vec_i64_bool sorted; diff --git a/src/app/main.c b/src/app/main.c index 3bc4a32..0b7965f 100644 --- a/src/app/main.c +++ b/src/app/main.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/01 21:00:12 by maiboyer #+# #+# */ -/* Updated: 2024/02/08 18:53:49 by maiboyer ### ########.fr */ +/* Updated: 2024/02/08 19:05:25 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,14 @@ #include "me/vec/vec_i64_bool.h" #include +#if BONUS == 1 +# define ERR_INVALID_NUM "KO\n" +# define ERR_DUPLICATE "KO\n" +#else +# define ERR_INVALID_NUM "Error:\nInvalid Number\n" +# define ERR_DUPLICATE "Error:\nDuplicate Number\n" +#endif + void sort_3(t_state *state); void sort_2(t_state *state); void sort_5(t_state *state); @@ -68,8 +76,7 @@ t_state parses_arguments(t_usize count, t_str nums[]) while (i < count) { if (str_to_i32(nums[i], 10, &atoi)) - (free_state(state), me_eprintf("Error:\nInvalid Number\n"), - exit(1)); + (free_state(state), me_eprintf(ERR_INVALID_NUM), exit(1)); vec_i64_push(&state.stack_a, atoi); vec_i64_bool_push(&state.sorted, (t_i64_bool){.value = atoi, .active = false}); @@ -77,7 +84,7 @@ t_state parses_arguments(t_usize count, t_str nums[]) } vec_i64_bool_sort(&state.sorted, sort_i64_bool); if (duplicate_check(&state)) - (free_state(state), me_eprintf("Error:\nDuplicate Number\n"), exit(1)); + (free_state(state), me_eprintf(ERR_DUPLICATE), exit(1)); return (state); } @@ -92,8 +99,8 @@ int main(t_i32 argc, t_str argv[]) if (is_sorted(&state.stack_a)) return (free_state(state), 0); if (state.stack_a.len == 0) - (free_state(state), me_eprintf("Error:\nNo Input\n"), exit(1)); - if (state.stack_a.len == 2) + (free_state(state), exit(0)); + else if (state.stack_a.len == 2) sort_2(&state); else if (state.stack_a.len == 3) sort_3(&state); diff --git a/src/app/moves/push.c b/src/app/moves/push.c index 6646044..66529bd 100644 --- a/src/app/moves/push.c +++ b/src/app/moves/push.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 16:22:54 by maiboyer #+# #+# */ -/* Updated: 2024/02/02 21:40:17 by maiboyer ### ########.fr */ +/* Updated: 2024/02/08 19:15:58 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ #include "me/printf/printf.h" #include "me/vec/vec_i64.h" -static inline void push_inner(t_vec_i64 *to, t_vec_i64 *from, t_const_str tag, +void push_inner(t_vec_i64 *to, t_vec_i64 *from, t_const_str tag, t_const_str print) { t_i64 e; diff --git a/src/app/moves/rev_rotate.c b/src/app/moves/rev_rotate.c index 74de1c1..868341a 100644 --- a/src/app/moves/rev_rotate.c +++ b/src/app/moves/rev_rotate.c @@ -6,14 +6,14 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 16:26:04 by maiboyer #+# #+# */ -/* Updated: 2024/02/02 14:12:23 by maiboyer ### ########.fr */ +/* Updated: 2024/02/08 19:16:59 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ #include "app/state.h" #include "me/printf/printf.h" -static inline void rev_rotate_inner(t_vec_i64 *stack, t_const_str tag, +void rev_rotate_inner(t_vec_i64 *stack, t_const_str tag, t_const_str print) { t_i64 e; diff --git a/src/app/moves/rotate.c b/src/app/moves/rotate.c index 46968db..be95e5a 100644 --- a/src/app/moves/rotate.c +++ b/src/app/moves/rotate.c @@ -6,38 +6,37 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 16:26:04 by maiboyer #+# #+# */ -/* Updated: 2024/02/08 14:30:21 by maiboyer ### ########.fr */ +/* Updated: 2024/02/08 19:17:46 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ #include "app/state.h" #include "me/printf/printf.h" -static inline void rotate_inner(t_vec_i64 *stack, t_const_str tag, - t_const_str print) +void rotate_inner(t_vec_i64 *stack, t_const_str tag, t_const_str print) { - t_i64 e; + t_i64 e; (void)(tag); if (stack->len <= 1) - return ; + return; vec_i64_pop_front(stack, &e); vec_i64_push(stack, e); if (print) ft_printf("%s\n", print); } -void rotate_a(t_state *s) +void rotate_a(t_state *s) { rotate_inner(&s->stack_a, "Rotate A", "ra"); } -void rotate_b(t_state *s) +void rotate_b(t_state *s) { rotate_inner(&s->stack_b, "Rotate B", "rb"); } -void rotate_both(t_state *s) +void rotate_both(t_state *s) { rotate_inner(&s->stack_a, "Rotate Both", NULL); rotate_inner(&s->stack_b, "Rotate Both", "rr"); diff --git a/src/app/moves/swap.c b/src/app/moves/swap.c index 730e4a6..96e6d96 100644 --- a/src/app/moves/swap.c +++ b/src/app/moves/swap.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 16:26:04 by maiboyer #+# #+# */ -/* Updated: 2024/02/02 14:14:12 by maiboyer ### ########.fr */ +/* Updated: 2024/02/08 19:17:59 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,15 +14,14 @@ #include "me/printf/printf.h" #include "me/vec/vec_i64.h" -static inline void swap_inner(t_vec_i64 *stack, t_const_str tag, - t_const_str print) +void swap_inner(t_vec_i64 *stack, t_const_str tag, t_const_str print) { - t_i64 first; - t_i64 second; + t_i64 first; + t_i64 second; (void)(tag); if (stack->len <= 1) - return ; + return; vec_i64_pop(stack, &first); vec_i64_pop(stack, &second); vec_i64_push(stack, first); @@ -31,17 +30,17 @@ static inline void swap_inner(t_vec_i64 *stack, t_const_str tag, ft_printf("%s\n", print); } -void swap_a(t_state *s) +void swap_a(t_state *s) { swap_inner(&s->stack_a, "Swap A", "sa"); } -void swap_b(t_state *s) +void swap_b(t_state *s) { swap_inner(&s->stack_b, "Swap B", "sb"); } -void swap_both(t_state *s) +void swap_both(t_state *s) { swap_inner(&s->stack_a, "Swap Both", NULL); swap_inner(&s->stack_b, "Swap Both", "ss"); diff --git a/src/bonus/main.c b/src/bonus/main.c new file mode 100644 index 0000000..a03f049 --- /dev/null +++ b/src/bonus/main.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/08 18:59:33 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 22:05:12 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/state.h" +#include "me/buffered_str/buf_str.h" +#include "me/gnl/gnl.h" +#include "me/string/str_n_compare.h" + +#undef BONUS +#define BONUS 1 +#if BONUS + +void push_inner(t_vec_i64 *to, t_vec_i64 *from, t_const_str tag, + t_const_str print); +void swap_inner(t_vec_i64 *vec, t_const_str tag, t_const_str print); +void rotate_inner(t_vec_i64 *vec, t_const_str tag, t_const_str print); +void rev_rotate_inner(t_vec_i64 *vec, t_const_str tag, t_const_str print); + +void handle_operation(t_buffer_str s, t_state *state) +{ + if (str_n_compare(s.buf, "pa", 2)) + push_inner(&state->stack_a, &state->stack_b, NULL, NULL); + if (str_n_compare(s.buf, "pb", 2)) + push_inner(&state->stack_b, &state->stack_a, NULL, NULL); + if (str_n_compare(s.buf, "sa", 2) ) + push_inner(&state->stack_a, &state->stack_b, NULL, NULL); + if (str_n_compare(s.buf, "sb", 2)) + push_inner(&state->stack_b, &state->stack_a, NULL, NULL); +} + +int main(t_i32 argc, t_str argv[]) +{ + t_state state; + t_buffer_str s; + bool err; + (void)(argc--, argv++); + state = parses_arguments(argc, argv); + s = get_next_line(0, &err); + while (err) + { + handle_operation(s, &state); + str_free(s); + s = get_next_line(0, &err); + } +} + +#endif diff --git a/src/bonus/move1.c b/src/bonus/move1.c new file mode 100644 index 0000000..10ec74c --- /dev/null +++ b/src/bonus/move1.c @@ -0,0 +1,13 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* move1.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/08 19:15:21 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 19:15:21 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + + From 0774f4e358ede27a2528d362519ba3ddede31bfd Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Fri, 9 Feb 2024 15:57:57 +0100 Subject: [PATCH 20/29] made bonus work-ish --- .gitignore | 1 + Makefile | 12 +++-- mecstd | 2 +- src.list | 3 ++ src/app/actual_main.c | 26 ++++++++++ src/app/main.c | 77 ++++++++++++++++++++---------- src/app/main_bonus.c | 97 ++++++++++++++++++++++++++++++++++++++ src/app/moves/push.c | 2 +- src/app/moves/rev_rotate.c | 2 +- src/app/moves/rotate.c | 2 +- src/app/moves/swap.c | 14 +++--- src/app/sort5.c | 8 ++-- src/bonus/main.c | 56 ---------------------- 13 files changed, 203 insertions(+), 99 deletions(-) create mode 100644 src/app/actual_main.c create mode 100644 src/app/main_bonus.c delete mode 100644 src/bonus/main.c diff --git a/.gitignore b/.gitignore index 14b2bca..6ccc634 100644 --- a/.gitignore +++ b/.gitignore @@ -58,3 +58,4 @@ test push_swap push_swap_visualizer Push-Swap-Tester +checker diff --git a/Makefile b/Makefile index c8ff40e..bf3d702 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: maiboyer +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/03 13:20:01 by maiboyer #+# #+# # -# Updated: 2024/02/08 14:25:56 by maiboyer ### ########.fr # +# Updated: 2024/02/09 15:27:44 by maiboyer ### ########.fr # # # # **************************************************************************** # @@ -19,12 +19,14 @@ LIBS_DIR = . GENERIC_DIR = output/src GENERIC_INCLUDE = output/include +NAME = push_swap + ifeq ($(MAKECMDGOALS), bonus) - CFLAGS += -DBONUS - BUILD_DIR := $(BUILD_DIR)/bonus/ + CFLAGS += -DBONUS=1 + BUILD_DIR := $(BUILD_DIR)/bonus/ + NAME = checker endif -NAME = push_swap LIB_NAME ?= TARGET = $(NAME) CC ?= clang @@ -111,6 +113,8 @@ fclean: clean @printf $(COL_WHITE)Clearing\ Output\ $(COL_GRAY)%-28s$(COL_RESET)\ \ \($(LIB_NAME)$(NAME)\) @rm -f $(BUILD_DIR)$(NAME) + @rm -f push_swap + @rm -f checker @printf $(COL_GREEN)done$(COL_RESET)\\n re: fclean all diff --git a/mecstd b/mecstd index 60b9db4..caed78a 160000 --- a/mecstd +++ b/mecstd @@ -1 +1 @@ -Subproject commit 60b9db4c20aa957cff32ae58dfc8a2124f9b35d5 +Subproject commit caed78a6223582e876f0fc748ca18350f572f34e diff --git a/src.list b/src.list index 4cb65a2..2015edd 100644 --- a/src.list +++ b/src.list @@ -1,9 +1,11 @@ +app/actual_main app/best_index_to_move app/best_move app/do_move app/find_place app/iter_find app/main +app/main_bonus app/moves app/moves/push app/moves/rev_rotate @@ -15,3 +17,4 @@ app/sort2 app/sort3 app/sort5 app/target +bonus/move1 diff --git a/src/app/actual_main.c b/src/app/actual_main.c new file mode 100644 index 0000000..c0f79ce --- /dev/null +++ b/src/app/actual_main.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* actual_main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/09 15:10:39 by maiboyer #+# #+# */ +/* Updated: 2024/02/09 15:26:33 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/state.h" +#include "me/printf/printf.h" +#include "me/string/str_n_compare.h" +#include "me/types.h" + +int main_normal(t_i32 argc, t_str argv[]); +int main_checker(t_i32 argc, t_str argv[]); + +int main(t_i32 argc, t_str argv[]) +{ + if (BONUS) + return (main_checker(argc, argv)); + return (main_normal(argc, argv)); +} diff --git a/src/app/main.c b/src/app/main.c index 0b7965f..53d09c1 100644 --- a/src/app/main.c +++ b/src/app/main.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/01 21:00:12 by maiboyer #+# #+# */ -/* Updated: 2024/02/08 19:05:25 by maiboyer ### ########.fr */ +/* Updated: 2024/02/09 15:50:10 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,41 +14,71 @@ #include "app/types/type_i64_bool.h" #include "me/convert/str_to_numbers.h" #include "me/printf/printf.h" +#include "me/string/str_n_compare.h" #include "me/types.h" #include "me/vec/vec_i64.h" #include "me/vec/vec_i64_bool.h" +#include #include - #if BONUS == 1 -# define ERR_INVALID_NUM "KO\n" -# define ERR_DUPLICATE "KO\n" +// # define ERR_INVALID_NUM "KO1\n" +// # define ERR_DUPLICATE "KO2\n" +# define ERR_INVALID_NUM "Error\n" //:\nInvalid Number\n" +# define ERR_DUPLICATE "Error\n" // :\nDuplicate Number\n" + +t_usize print_error(t_const_str fmt, ...) +{ + va_list args; + t_usize ret; + + va_start(args, fmt); + ret = me_veprintf(fmt, &args); + va_end(args); + return (ret); +} + #else -# define ERR_INVALID_NUM "Error:\nInvalid Number\n" -# define ERR_DUPLICATE "Error:\nDuplicate Number\n" +// # define ERR_INVALID_NUM "Error:\nInvalid Number\n" +// # define ERR_DUPLICATE "Error:\nDuplicate Number\n" + +# define ERR_INVALID_NUM "Error\n" //:\nInvalid Number\n" +# define ERR_DUPLICATE "Error\n" // :\nDuplicate Number\n" + +t_usize print_error(t_const_str fmt, ...) +{ + va_list args; + t_usize ret; + + va_start(args, fmt); + ret = me_veprintf(fmt, &args); + va_end(args); + return (ret); +} + #endif -void sort_3(t_state *state); -void sort_2(t_state *state); -void sort_5(t_state *state); +void sort_3(t_state *state); +void sort_2(t_state *state); +void sort_5(t_state *state); +bool is_sorted(t_vec_i64 *v); +void run_with_items(t_state *state); -void run_with_items(t_state *state); - -bool sort_i64_bool(t_i64_bool *lhs, t_i64_bool *rhs) +bool sort_i64_bool(t_i64_bool *lhs, t_i64_bool *rhs) { return (lhs->value <= rhs->value); } -void free_state(t_state state) +void free_state(t_state state) { vec_i64_free(state.stack_a); vec_i64_free(state.stack_b); vec_i64_bool_free(state.sorted); } -bool duplicate_check(t_state *state) +bool duplicate_check(t_state *state) { t_i64 last; - t_usize index; + t_usize index; if (state->sorted.len == 0) return (false); @@ -63,11 +93,11 @@ bool duplicate_check(t_state *state) return (false); } -t_state parses_arguments(t_usize count, t_str nums[]) +t_state parses_arguments(t_usize count, t_str nums[]) { - t_state state; + t_state state; t_i32 atoi; - t_usize i; + t_usize i; state.stack_a = vec_i64_new(count, NULL); state.stack_b = vec_i64_new(count, NULL); @@ -78,8 +108,8 @@ t_state parses_arguments(t_usize count, t_str nums[]) if (str_to_i32(nums[i], 10, &atoi)) (free_state(state), me_eprintf(ERR_INVALID_NUM), exit(1)); vec_i64_push(&state.stack_a, atoi); - vec_i64_bool_push(&state.sorted, (t_i64_bool){.value = atoi, - .active = false}); + vec_i64_bool_push(&state.sorted, + (t_i64_bool){.value = atoi, .active = false}); i++; } vec_i64_bool_sort(&state.sorted, sort_i64_bool); @@ -88,11 +118,9 @@ t_state parses_arguments(t_usize count, t_str nums[]) return (state); } -bool is_sorted(t_vec_i64 *v); - -int main(t_i32 argc, t_str argv[]) +int main_normal(t_i32 argc, t_str argv[]) { - t_state state; + t_state state; (void)(argc--, argv++); state = parses_arguments(argc, argv); @@ -109,4 +137,5 @@ int main(t_i32 argc, t_str argv[]) else run_with_items(&state); free_state(state); + return (0); } diff --git a/src/app/main_bonus.c b/src/app/main_bonus.c new file mode 100644 index 0000000..51132f6 --- /dev/null +++ b/src/app/main_bonus.c @@ -0,0 +1,97 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/08 18:59:33 by maiboyer #+# #+# */ +/* Updated: 2024/02/09 15:51:09 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/state.h" +#include "me/buffered_str/buf_str.h" +#include "me/gnl/gnl.h" +#include "me/printf/printf.h" +#include "me/string/str_n_compare.h" +#include "me/vec/vec_i64.h" + +#undef BONUS +#define BONUS 1 +#if BONUS + +void push_inner(t_vec_i64 *to, t_vec_i64 *from, t_const_str tag, + t_const_str print); +void swap_inner(t_vec_i64 *vec, t_const_str tag, t_const_str print); +void rotate_inner(t_vec_i64 *vec, t_const_str tag, t_const_str print); +void rev_rotate_inner(t_vec_i64 *vec, t_const_str tag, t_const_str print); +bool is_sorted(t_vec_i64 *v); + +t_error handle_operation(t_buffer_str s, t_state *state) +{ + t_usize op; + + op = 0; + if (s.buf == NULL) + return (ERROR); + if (str_n_compare(s.buf, "pa", 2) == 0) + push_inner(&state->stack_a, &state->stack_b, NULL, (op++, NULL)); + if (str_n_compare(s.buf, "pb", 2) == 0) + push_inner(&state->stack_b, &state->stack_a, NULL, (op++, NULL)); + + if (str_n_compare(s.buf, "sa", 2) == 0 || + str_n_compare(s.buf, "ss", 2) == 0) + swap_inner(&state->stack_a, NULL, (op++, NULL)); + if (str_n_compare(s.buf, "sb", 2) == 0 || + str_n_compare(s.buf, "ss", 2) == 0) + swap_inner(&state->stack_b, NULL, (op++, NULL)); + + if (str_n_compare(s.buf, "ra", 2) == 0 || + str_n_compare(s.buf, "rr", 2) == 0) + rotate_inner(&state->stack_a, NULL, (op++, NULL)); + if (str_n_compare(s.buf, "rb", 2) == 0 || + str_n_compare(s.buf, "rr", 2) == 0) + rotate_inner(&state->stack_b, NULL, (op++, NULL)); + + if (str_n_compare(s.buf, "rra", 3) == 0 || + str_n_compare(s.buf, "rrr", 3) == 0) + rev_rotate_inner(&state->stack_a, NULL, (op++, NULL)); + if (str_n_compare(s.buf, "rrb", 3) == 0 || + str_n_compare(s.buf, "rrr", 3) == 0) + rev_rotate_inner(&state->stack_b, NULL, (op++, NULL)); + if (op == 0) + return (ERROR); + return (NO_ERROR); +} + +int main_checker(t_i32 argc, t_str argv[]) +{ + t_state state; + t_buffer_str s; + bool err; + (void)(argc--, argv++); + state = parses_arguments(argc, argv); + s = get_next_line(0, &err); + while (!err) + { + if (handle_operation(s, &state)) + { + (str_free(s), free_state(state)); + me_printf("KO\n"); + return (1); + } + str_free(s); + s = get_next_line(0, &err); + } + str_free(s); + if (is_sorted(&state.stack_a) && state.stack_b.len == 0) + me_printf("OK\n"); + else + me_printf("KO\n"); + + free_state(state); + return (0); +} + +#endif diff --git a/src/app/moves/push.c b/src/app/moves/push.c index 66529bd..dc53491 100644 --- a/src/app/moves/push.c +++ b/src/app/moves/push.c @@ -25,7 +25,7 @@ void push_inner(t_vec_i64 *to, t_vec_i64 *from, t_const_str tag, vec_i64_pop_front(from, &e); vec_i64_push_front(to, e); if (print) - ft_printf("%s\n", print); + me_printf("%s\n", print); } void push_a(t_state *s) diff --git a/src/app/moves/rev_rotate.c b/src/app/moves/rev_rotate.c index 868341a..e10e112 100644 --- a/src/app/moves/rev_rotate.c +++ b/src/app/moves/rev_rotate.c @@ -24,7 +24,7 @@ void rev_rotate_inner(t_vec_i64 *stack, t_const_str tag, vec_i64_pop(stack, &e); vec_i64_push_front(stack, e); if (print) - ft_printf("%s\n", print); + me_printf("%s\n", print); } void rev_rotate_a(t_state *s) diff --git a/src/app/moves/rotate.c b/src/app/moves/rotate.c index be95e5a..5fb0bbd 100644 --- a/src/app/moves/rotate.c +++ b/src/app/moves/rotate.c @@ -23,7 +23,7 @@ void rotate_inner(t_vec_i64 *stack, t_const_str tag, t_const_str print) vec_i64_pop_front(stack, &e); vec_i64_push(stack, e); if (print) - ft_printf("%s\n", print); + me_printf("%s\n", print); } void rotate_a(t_state *s) diff --git a/src/app/moves/swap.c b/src/app/moves/swap.c index 96e6d96..cb58326 100644 --- a/src/app/moves/swap.c +++ b/src/app/moves/swap.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 16:26:04 by maiboyer #+# #+# */ -/* Updated: 2024/02/08 19:17:59 by maiboyer ### ########.fr */ +/* Updated: 2024/02/09 15:40:37 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,14 +20,14 @@ void swap_inner(t_vec_i64 *stack, t_const_str tag, t_const_str print) t_i64 second; (void)(tag); - if (stack->len <= 1) + if (stack->len < 2) return; - vec_i64_pop(stack, &first); - vec_i64_pop(stack, &second); - vec_i64_push(stack, first); - vec_i64_push(stack, second); + vec_i64_pop_front(stack, &first); + vec_i64_pop_front(stack, &second); + vec_i64_push_front(stack, first); + vec_i64_push_front(stack, second); if (print) - ft_printf("%s\n", print); + me_printf("%s\n", print); } void swap_a(t_state *s) diff --git a/src/app/sort5.c b/src/app/sort5.c index 894482c..8c23853 100644 --- a/src/app/sort5.c +++ b/src/app/sort5.c @@ -100,18 +100,18 @@ bool sort_5_specialized(t_state *state, t_i64 cur[5]) vec_i64_sort(&why_do_i_do_this, sort_i64); if (cur[0] == target[3 - 1] && cur[1] == target[4 - 1] && cur[2] == \ target[1 - 1] && cur[3] == target[5 - 1] && cur[4] == target[2 - 1]) - return (ft_printf("pb\npb\nra\nsa\npa\nsa\npa\nsa\nrra\n"), true); + return (me_printf("pb\npb\nra\nsa\npa\nsa\npa\nsa\nrra\n"), true); if (cur[0] == target[3 - 1] && cur[1] == target[4 - 1] && cur[2] == \ target[5 - 1] && cur[3] == target[2 - 1] && cur[4] == target[1 - 1]) - return (ft_printf("pb\npb\nsb\nsa\npa\nsa\nrra\nrra\npa\nra\nra\n"), + return (me_printf("pb\npb\nsb\nsa\npa\nsa\nrra\nrra\npa\nra\nra\n"), true); if (cur[0] == target[4 - 1] && cur[1] == target[3 - 1] && cur[2] == \ target[1 - 1] && cur[3] == target[5 - 1] && cur[4] == target[2 - 1]) - return (ft_printf("pb\npb\nra\nsa\npa\nsa\nrra\nrra\npa\nra\nra\n"), + return (me_printf("pb\npb\nra\nsa\npa\nsa\nrra\nrra\npa\nra\nra\n"), true); if (cur[0] == target[4 - 1] && cur[1] == target[3 - 1] && cur[2] == \ target[5 - 1] && cur[3] == target[2 - 1] && cur[4] == target[1 - 1]) - return (ft_printf("pb\npb\nsb\nsa\npa\nsa\npa\nsa\nrra\n"), true); + return (me_printf("pb\npb\nsb\nsa\npa\nsa\npa\nsa\nrra\n"), true); return (false); } diff --git a/src/bonus/main.c b/src/bonus/main.c deleted file mode 100644 index a03f049..0000000 --- a/src/bonus/main.c +++ /dev/null @@ -1,56 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* main.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: maiboyer +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/02/08 18:59:33 by maiboyer #+# #+# */ -/* Updated: 2024/02/08 22:05:12 by maiboyer ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "app/state.h" -#include "me/buffered_str/buf_str.h" -#include "me/gnl/gnl.h" -#include "me/string/str_n_compare.h" - -#undef BONUS -#define BONUS 1 -#if BONUS - -void push_inner(t_vec_i64 *to, t_vec_i64 *from, t_const_str tag, - t_const_str print); -void swap_inner(t_vec_i64 *vec, t_const_str tag, t_const_str print); -void rotate_inner(t_vec_i64 *vec, t_const_str tag, t_const_str print); -void rev_rotate_inner(t_vec_i64 *vec, t_const_str tag, t_const_str print); - -void handle_operation(t_buffer_str s, t_state *state) -{ - if (str_n_compare(s.buf, "pa", 2)) - push_inner(&state->stack_a, &state->stack_b, NULL, NULL); - if (str_n_compare(s.buf, "pb", 2)) - push_inner(&state->stack_b, &state->stack_a, NULL, NULL); - if (str_n_compare(s.buf, "sa", 2) ) - push_inner(&state->stack_a, &state->stack_b, NULL, NULL); - if (str_n_compare(s.buf, "sb", 2)) - push_inner(&state->stack_b, &state->stack_a, NULL, NULL); -} - -int main(t_i32 argc, t_str argv[]) -{ - t_state state; - t_buffer_str s; - bool err; - (void)(argc--, argv++); - state = parses_arguments(argc, argv); - s = get_next_line(0, &err); - while (err) - { - handle_operation(s, &state); - str_free(s); - s = get_next_line(0, &err); - } -} - -#endif From aecb67a24d83870f6444986c913d2af25cf4d653 Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Mon, 12 Feb 2024 20:49:08 +0100 Subject: [PATCH 21/29] updated --- mecstd | 2 +- src/app/main_bonus.c | 18 ++++++++++++++---- src/app/moves/rev_rotate.c | 4 ++-- src/app/moves/rotate.c | 4 ++-- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/mecstd b/mecstd index caed78a..32a8770 160000 --- a/mecstd +++ b/mecstd @@ -1 +1 @@ -Subproject commit caed78a6223582e876f0fc748ca18350f572f34e +Subproject commit 32a87701115a9134535f0a4e70923a68fdcbcaa0 diff --git a/src/app/main_bonus.c b/src/app/main_bonus.c index 51132f6..305ef73 100644 --- a/src/app/main_bonus.c +++ b/src/app/main_bonus.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/08 18:59:33 by maiboyer #+# #+# */ -/* Updated: 2024/02/09 15:51:09 by maiboyer ### ########.fr */ +/* Updated: 2024/02/10 18:55:20 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -70,25 +70,35 @@ int main_checker(t_i32 argc, t_str argv[]) t_state state; t_buffer_str s; bool err; + t_usize count; + (void)(argc--, argv++); + err = false; state = parses_arguments(argc, argv); s = get_next_line(0, &err); - while (!err) + count = 1; + while (!err && s.buf != NULL) { + + if (s.len != 0 && s.buf[s.len - 1] == '\n') + str_pop(&s); + //me_printf("line[%u]: '%s'\n", count, s.buf); if (handle_operation(s, &state)) { + me_printf("KO: error\n"); + me_printf("ERROR[%u] = '%s'\n", count, s.buf); (str_free(s), free_state(state)); - me_printf("KO\n"); return (1); } str_free(s); s = get_next_line(0, &err); + count++; } str_free(s); if (is_sorted(&state.stack_a) && state.stack_b.len == 0) me_printf("OK\n"); else - me_printf("KO\n"); + me_printf("KO: Not sorted\n"); free_state(state); return (0); diff --git a/src/app/moves/rev_rotate.c b/src/app/moves/rev_rotate.c index e10e112..f630ea0 100644 --- a/src/app/moves/rev_rotate.c +++ b/src/app/moves/rev_rotate.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 16:26:04 by maiboyer #+# #+# */ -/* Updated: 2024/02/08 19:16:59 by maiboyer ### ########.fr */ +/* Updated: 2024/02/10 18:39:03 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ void rev_rotate_inner(t_vec_i64 *stack, t_const_str tag, t_i64 e; (void)(tag); - if (stack->len <= 1) + if (stack->len < 2) return ; vec_i64_pop(stack, &e); vec_i64_push_front(stack, e); diff --git a/src/app/moves/rotate.c b/src/app/moves/rotate.c index 5fb0bbd..ff314ce 100644 --- a/src/app/moves/rotate.c +++ b/src/app/moves/rotate.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 16:26:04 by maiboyer #+# #+# */ -/* Updated: 2024/02/08 19:17:46 by maiboyer ### ########.fr */ +/* Updated: 2024/02/10 18:38:52 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ void rotate_inner(t_vec_i64 *stack, t_const_str tag, t_const_str print) t_i64 e; (void)(tag); - if (stack->len <= 1) + if (stack->len < 2) return; vec_i64_pop_front(stack, &e); vec_i64_push(stack, e); From a1a0f2ea23bcd3913bdfb595c385c4a41567fe83 Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Tue, 13 Feb 2024 20:30:29 +0100 Subject: [PATCH 22/29] update to have check.sh --- check.sh | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100755 check.sh diff --git a/check.sh b/check.sh new file mode 100755 index 0000000..b0639d9 --- /dev/null +++ b/check.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +ARGS="133 193 -16 -234 239 85 -223 -100 178 212 -202 -102 -199 -233 227 86 -166 224 160 -21 -56 102 -62 62 -127 89 127 -246 1 140 38 157 179 220 154 -1 125 240 -238 78 -175 222 -145 -60 115 67 -220 80 -136 -134 -159 35 136 192 47 117 -51 -140 37 6 -26 -188 213 -204 -210 14 -70 112 201 -191 -22 219 15 -143 -131 -41 -152 -187 24 215 235 -147 170 -168 12 107 105 130 -91 230 -174 -176 -224 -35 5 -110 -180 60 -59 -24 94 -196 -39 21 131 -18 165 -2 -219 -198 -19 -11 244 -151 -80 43 -17 -244 -65 124 9 -153 118 -6 -235 81 225 121 217 -43 30 218 -111 163 -9 23 214 166 207 238 -217 -161 -165 116 -170 26 -44 -106 245 -155 48 -177 75 -66 -89 109 22 56 58 -53 203 -79 39 184 -94 -231 45 246 93 13 -128 -179 -236 -139 114 63 -32 196 -205 57 249 -243 233 247 -46 -90 -47 -201 -144 221 -172 66 -38 -101 -232 175 164 -215 -195 -126 209 -108 -64 126 134 -77 -69 113 146 -211 2 88 -28 -132 -250 77 -181 191 198 183 -239 -52 -118 187 -109 4 -208 73 -96 -81 87 188 122 -120 -29 83 18 92 -83 100 -68 -221 -186 189 41 68 -150 129 -49 -137 -99 -123 -42 150 -192 -82 -158 104 -61 145 16 96 -115 -27 -119 97 -203 -240 -173 -185 -227 -98 72 -237 -7 -105 -76 149 -218 -182 54 76 61 211 173 7 185 -10 19 -15 -84 -48 36 53 -67 -63 155 -169 -138 -141 -78 33 -85 234 138 -146 223 158 135 91 108 55 32 90 226 -230 -40 177 -34 -112 -190 144 -104 -241 71 237 -226 95 208 -31 180 59 52 151 153 -135 46 -50 -154 204 132 119 70 110 199 -225 169 194 -148 148 241 -37 27 -23 176 232 -156 206 200 -167 -14 3 -209 159 -75 -248 111 -73 34 -113 -184 25 -86 -122 106 11 162 -133 -88 -245 147 -178 -197 190 -117 84 210 -54 -247 98 -157 64 -95 -129 10 -207 -72 182 -71 181 51 79 -87 242 20 123 82 99 -130 49 101 216 -216 -93 -97 172 -125 -163 -200 -8 -206 197 -242 -194 -212 168 161 -45 103 -160 195 -249 -25 69 137 167 152 -20 -142 243 -55 -171 -36 156 -12 -149 29 28 -183 50 120 -222 17 174 -4 -74 143 236 -189 44 -228 -162 40 229 228 -124 248 231 -5 -193 65 -13 139 128 -92 31 -103 142 -3 -33 205 -164 186 -121 -57 0 -229 42 -114 -213 202 -58 141 -107 -30 -116 171 -214 74 8" + +echo -n "Mine: " +./push_swap $ARGS |./checker $ARGS +echo -n "Their: " +./push_swap $ARGS | ./Push-Swap-Tester/checker_linux $ARGS From 938e9ffd0f8c5eeedabaf7444a53361c68468894 Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Wed, 14 Feb 2024 18:29:29 +0100 Subject: [PATCH 23/29] update --- mecstd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mecstd b/mecstd index 32a8770..de6e15d 160000 --- a/mecstd +++ b/mecstd @@ -1 +1 @@ -Subproject commit 32a87701115a9134535f0a4e70923a68fdcbcaa0 +Subproject commit de6e15da1a6981623327a41f8772b036134e1e91 From 35ef014aa52869092161e7d6546717cf725e3048 Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Thu, 15 Feb 2024 17:10:24 +0100 Subject: [PATCH 24/29] updated mecstd to be back to master --- mecstd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mecstd b/mecstd index de6e15d..caed78a 160000 --- a/mecstd +++ b/mecstd @@ -1 +1 @@ -Subproject commit de6e15da1a6981623327a41f8772b036134e1e91 +Subproject commit caed78a6223582e876f0fc748ca18350f572f34e From d941a20c30dbda3d6fb224f2bf04f7709287904a Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Thu, 15 Feb 2024 17:11:30 +0100 Subject: [PATCH 25/29] started norminette --- include/app/state.h | 6 ++--- mecstd | 2 +- src/app/actual_main.c | 6 ++--- src/app/main.c | 46 +++++++++++++++++----------------- src/app/main_bonus.c | 57 +++++++++++++++++++----------------------- src/app/moves/rotate.c | 12 ++++----- src/app/moves/swap.c | 14 +++++------ src/bonus/move1.c | 2 -- 8 files changed, 69 insertions(+), 76 deletions(-) diff --git a/include/app/state.h b/include/app/state.h index 2703d0e..2261e43 100644 --- a/include/app/state.h +++ b/include/app/state.h @@ -17,9 +17,9 @@ # include "me/vec/vec_i64.h" # include "me/vec/vec_i64_bool.h" -#ifndef BONUS -#define BONUS 0 -#endif +# ifndef BONUS +# define BONUS 0 +# endif typedef struct s_state { diff --git a/mecstd b/mecstd index caed78a..07ea967 160000 --- a/mecstd +++ b/mecstd @@ -1 +1 @@ -Subproject commit caed78a6223582e876f0fc748ca18350f572f34e +Subproject commit 07ea9679e2413366502a73ea9391d89847d6b3c2 diff --git a/src/app/actual_main.c b/src/app/actual_main.c index c0f79ce..6804f8b 100644 --- a/src/app/actual_main.c +++ b/src/app/actual_main.c @@ -15,10 +15,10 @@ #include "me/string/str_n_compare.h" #include "me/types.h" -int main_normal(t_i32 argc, t_str argv[]); -int main_checker(t_i32 argc, t_str argv[]); +int main_normal(t_i32 argc, t_str argv[]); +int main_checker(t_i32 argc, t_str argv[]); -int main(t_i32 argc, t_str argv[]) +int main(t_i32 argc, t_str argv[]) { if (BONUS) return (main_checker(argc, argv)); diff --git a/src/app/main.c b/src/app/main.c index 53d09c1..f6c0d0f 100644 --- a/src/app/main.c +++ b/src/app/main.c @@ -26,10 +26,10 @@ # define ERR_INVALID_NUM "Error\n" //:\nInvalid Number\n" # define ERR_DUPLICATE "Error\n" // :\nDuplicate Number\n" -t_usize print_error(t_const_str fmt, ...) +t_usize print_error(t_const_str fmt, ...) { - va_list args; - t_usize ret; + va_list args; + t_usize ret; va_start(args, fmt); ret = me_veprintf(fmt, &args); @@ -42,12 +42,12 @@ t_usize print_error(t_const_str fmt, ...) // # define ERR_DUPLICATE "Error:\nDuplicate Number\n" # define ERR_INVALID_NUM "Error\n" //:\nInvalid Number\n" -# define ERR_DUPLICATE "Error\n" // :\nDuplicate Number\n" +# define ERR_DUPLICATE "Error\n" // :\nDuplicate Number\n" -t_usize print_error(t_const_str fmt, ...) +t_usize print_error(t_const_str fmt, ...) { - va_list args; - t_usize ret; + va_list args; + t_usize ret; va_start(args, fmt); ret = me_veprintf(fmt, &args); @@ -57,28 +57,28 @@ t_usize print_error(t_const_str fmt, ...) #endif -void sort_3(t_state *state); -void sort_2(t_state *state); -void sort_5(t_state *state); -bool is_sorted(t_vec_i64 *v); -void run_with_items(t_state *state); +void sort_3(t_state *state); +void sort_2(t_state *state); +void sort_5(t_state *state); +bool is_sorted(t_vec_i64 *v); +void run_with_items(t_state *state); -bool sort_i64_bool(t_i64_bool *lhs, t_i64_bool *rhs) +bool sort_i64_bool(t_i64_bool *lhs, t_i64_bool *rhs) { return (lhs->value <= rhs->value); } -void free_state(t_state state) +void free_state(t_state state) { vec_i64_free(state.stack_a); vec_i64_free(state.stack_b); vec_i64_bool_free(state.sorted); } -bool duplicate_check(t_state *state) +bool duplicate_check(t_state *state) { t_i64 last; - t_usize index; + t_usize index; if (state->sorted.len == 0) return (false); @@ -93,11 +93,11 @@ bool duplicate_check(t_state *state) return (false); } -t_state parses_arguments(t_usize count, t_str nums[]) +t_state parses_arguments(t_usize count, t_str nums[]) { - t_state state; + t_state state; t_i32 atoi; - t_usize i; + t_usize i; state.stack_a = vec_i64_new(count, NULL); state.stack_b = vec_i64_new(count, NULL); @@ -108,8 +108,8 @@ t_state parses_arguments(t_usize count, t_str nums[]) if (str_to_i32(nums[i], 10, &atoi)) (free_state(state), me_eprintf(ERR_INVALID_NUM), exit(1)); vec_i64_push(&state.stack_a, atoi); - vec_i64_bool_push(&state.sorted, - (t_i64_bool){.value = atoi, .active = false}); + vec_i64_bool_push(&state.sorted, (t_i64_bool){.value = atoi, + .active = false}); i++; } vec_i64_bool_sort(&state.sorted, sort_i64_bool); @@ -118,9 +118,9 @@ t_state parses_arguments(t_usize count, t_str nums[]) return (state); } -int main_normal(t_i32 argc, t_str argv[]) +int main_normal(t_i32 argc, t_str argv[]) { - t_state state; + t_state state; (void)(argc--, argv++); state = parses_arguments(argc, argv); diff --git a/src/app/main_bonus.c b/src/app/main_bonus.c index 305ef73..3968e34 100644 --- a/src/app/main_bonus.c +++ b/src/app/main_bonus.c @@ -21,16 +21,16 @@ #define BONUS 1 #if BONUS -void push_inner(t_vec_i64 *to, t_vec_i64 *from, t_const_str tag, - t_const_str print); -void swap_inner(t_vec_i64 *vec, t_const_str tag, t_const_str print); -void rotate_inner(t_vec_i64 *vec, t_const_str tag, t_const_str print); -void rev_rotate_inner(t_vec_i64 *vec, t_const_str tag, t_const_str print); -bool is_sorted(t_vec_i64 *v); +void push_inner(t_vec_i64 *to, t_vec_i64 *from, t_const_str tag, + t_const_str print); +void swap_inner(t_vec_i64 *vec, t_const_str tag, t_const_str print); +void rotate_inner(t_vec_i64 *vec, t_const_str tag, t_const_str print); +void rev_rotate_inner(t_vec_i64 *vec, t_const_str tag, t_const_str print); +bool is_sorted(t_vec_i64 *v); -t_error handle_operation(t_buffer_str s, t_state *state) +t_error handle_operation(t_buffer_str s, t_state *state) { - t_usize op; + t_usize op; op = 0; if (s.buf == NULL) @@ -39,38 +39,35 @@ t_error handle_operation(t_buffer_str s, t_state *state) push_inner(&state->stack_a, &state->stack_b, NULL, (op++, NULL)); if (str_n_compare(s.buf, "pb", 2) == 0) push_inner(&state->stack_b, &state->stack_a, NULL, (op++, NULL)); - - if (str_n_compare(s.buf, "sa", 2) == 0 || - str_n_compare(s.buf, "ss", 2) == 0) + if (str_n_compare(s.buf, "sa", 2) == 0 || str_n_compare(s.buf, "ss", + 2) == 0) swap_inner(&state->stack_a, NULL, (op++, NULL)); - if (str_n_compare(s.buf, "sb", 2) == 0 || - str_n_compare(s.buf, "ss", 2) == 0) + if (str_n_compare(s.buf, "sb", 2) == 0 || str_n_compare(s.buf, "ss", + 2) == 0) swap_inner(&state->stack_b, NULL, (op++, NULL)); - - if (str_n_compare(s.buf, "ra", 2) == 0 || - str_n_compare(s.buf, "rr", 2) == 0) + if (str_n_compare(s.buf, "ra", 2) == 0 || str_n_compare(s.buf, "rr", + 2) == 0) rotate_inner(&state->stack_a, NULL, (op++, NULL)); - if (str_n_compare(s.buf, "rb", 2) == 0 || - str_n_compare(s.buf, "rr", 2) == 0) + if (str_n_compare(s.buf, "rb", 2) == 0 || str_n_compare(s.buf, "rr", + 2) == 0) rotate_inner(&state->stack_b, NULL, (op++, NULL)); - - if (str_n_compare(s.buf, "rra", 3) == 0 || - str_n_compare(s.buf, "rrr", 3) == 0) + if (str_n_compare(s.buf, "rra", 3) == 0 || str_n_compare(s.buf, "rrr", + 3) == 0) rev_rotate_inner(&state->stack_a, NULL, (op++, NULL)); - if (str_n_compare(s.buf, "rrb", 3) == 0 || - str_n_compare(s.buf, "rrr", 3) == 0) + if (str_n_compare(s.buf, "rrb", 3) == 0 || str_n_compare(s.buf, "rrr", + 3) == 0) rev_rotate_inner(&state->stack_b, NULL, (op++, NULL)); if (op == 0) return (ERROR); return (NO_ERROR); } -int main_checker(t_i32 argc, t_str argv[]) +int main_checker(t_i32 argc, t_str argv[]) { - t_state state; - t_buffer_str s; - bool err; - t_usize count; + t_state state; + t_buffer_str s; + bool err; + t_usize count; (void)(argc--, argv++); err = false; @@ -79,10 +76,9 @@ int main_checker(t_i32 argc, t_str argv[]) count = 1; while (!err && s.buf != NULL) { - if (s.len != 0 && s.buf[s.len - 1] == '\n') str_pop(&s); - //me_printf("line[%u]: '%s'\n", count, s.buf); + // me_printf("line[%u]: '%s'\n", count, s.buf); if (handle_operation(s, &state)) { me_printf("KO: error\n"); @@ -99,7 +95,6 @@ int main_checker(t_i32 argc, t_str argv[]) me_printf("OK\n"); else me_printf("KO: Not sorted\n"); - free_state(state); return (0); } diff --git a/src/app/moves/rotate.c b/src/app/moves/rotate.c index ff314ce..86cd2aa 100644 --- a/src/app/moves/rotate.c +++ b/src/app/moves/rotate.c @@ -13,30 +13,30 @@ #include "app/state.h" #include "me/printf/printf.h" -void rotate_inner(t_vec_i64 *stack, t_const_str tag, t_const_str print) +void rotate_inner(t_vec_i64 *stack, t_const_str tag, t_const_str print) { - t_i64 e; + t_i64 e; (void)(tag); if (stack->len < 2) - return; + return ; vec_i64_pop_front(stack, &e); vec_i64_push(stack, e); if (print) me_printf("%s\n", print); } -void rotate_a(t_state *s) +void rotate_a(t_state *s) { rotate_inner(&s->stack_a, "Rotate A", "ra"); } -void rotate_b(t_state *s) +void rotate_b(t_state *s) { rotate_inner(&s->stack_b, "Rotate B", "rb"); } -void rotate_both(t_state *s) +void rotate_both(t_state *s) { rotate_inner(&s->stack_a, "Rotate Both", NULL); rotate_inner(&s->stack_b, "Rotate Both", "rr"); diff --git a/src/app/moves/swap.c b/src/app/moves/swap.c index cb58326..934cedd 100644 --- a/src/app/moves/swap.c +++ b/src/app/moves/swap.c @@ -14,14 +14,14 @@ #include "me/printf/printf.h" #include "me/vec/vec_i64.h" -void swap_inner(t_vec_i64 *stack, t_const_str tag, t_const_str print) +void swap_inner(t_vec_i64 *stack, t_const_str tag, t_const_str print) { - t_i64 first; - t_i64 second; + t_i64 first; + t_i64 second; (void)(tag); if (stack->len < 2) - return; + return ; vec_i64_pop_front(stack, &first); vec_i64_pop_front(stack, &second); vec_i64_push_front(stack, first); @@ -30,17 +30,17 @@ void swap_inner(t_vec_i64 *stack, t_const_str tag, t_const_str print) me_printf("%s\n", print); } -void swap_a(t_state *s) +void swap_a(t_state *s) { swap_inner(&s->stack_a, "Swap A", "sa"); } -void swap_b(t_state *s) +void swap_b(t_state *s) { swap_inner(&s->stack_b, "Swap B", "sb"); } -void swap_both(t_state *s) +void swap_both(t_state *s) { swap_inner(&s->stack_a, "Swap Both", NULL); swap_inner(&s->stack_b, "Swap Both", "ss"); diff --git a/src/bonus/move1.c b/src/bonus/move1.c index 10ec74c..6ca0754 100644 --- a/src/bonus/move1.c +++ b/src/bonus/move1.c @@ -9,5 +9,3 @@ /* Updated: 2024/02/08 19:15:21 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ - - From a679064e4fcc47606c830d46c19e6e6df8bd6b62 Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Thu, 15 Feb 2024 17:48:09 +0100 Subject: [PATCH 26/29] a fine project ! --- check.sh | 2 +- src.list | 1 + src/app/main.c | 93 ++++---------------------------------------- src/app/main_bonus.c | 77 ++++++++++++++++++------------------ src/app/state.c | 91 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 141 insertions(+), 123 deletions(-) create mode 100644 src/app/state.c diff --git a/check.sh b/check.sh index b0639d9..ded0e01 100755 --- a/check.sh +++ b/check.sh @@ -3,6 +3,6 @@ ARGS="133 193 -16 -234 239 85 -223 -100 178 212 -202 -102 -199 -233 227 86 -166 224 160 -21 -56 102 -62 62 -127 89 127 -246 1 140 38 157 179 220 154 -1 125 240 -238 78 -175 222 -145 -60 115 67 -220 80 -136 -134 -159 35 136 192 47 117 -51 -140 37 6 -26 -188 213 -204 -210 14 -70 112 201 -191 -22 219 15 -143 -131 -41 -152 -187 24 215 235 -147 170 -168 12 107 105 130 -91 230 -174 -176 -224 -35 5 -110 -180 60 -59 -24 94 -196 -39 21 131 -18 165 -2 -219 -198 -19 -11 244 -151 -80 43 -17 -244 -65 124 9 -153 118 -6 -235 81 225 121 217 -43 30 218 -111 163 -9 23 214 166 207 238 -217 -161 -165 116 -170 26 -44 -106 245 -155 48 -177 75 -66 -89 109 22 56 58 -53 203 -79 39 184 -94 -231 45 246 93 13 -128 -179 -236 -139 114 63 -32 196 -205 57 249 -243 233 247 -46 -90 -47 -201 -144 221 -172 66 -38 -101 -232 175 164 -215 -195 -126 209 -108 -64 126 134 -77 -69 113 146 -211 2 88 -28 -132 -250 77 -181 191 198 183 -239 -52 -118 187 -109 4 -208 73 -96 -81 87 188 122 -120 -29 83 18 92 -83 100 -68 -221 -186 189 41 68 -150 129 -49 -137 -99 -123 -42 150 -192 -82 -158 104 -61 145 16 96 -115 -27 -119 97 -203 -240 -173 -185 -227 -98 72 -237 -7 -105 -76 149 -218 -182 54 76 61 211 173 7 185 -10 19 -15 -84 -48 36 53 -67 -63 155 -169 -138 -141 -78 33 -85 234 138 -146 223 158 135 91 108 55 32 90 226 -230 -40 177 -34 -112 -190 144 -104 -241 71 237 -226 95 208 -31 180 59 52 151 153 -135 46 -50 -154 204 132 119 70 110 199 -225 169 194 -148 148 241 -37 27 -23 176 232 -156 206 200 -167 -14 3 -209 159 -75 -248 111 -73 34 -113 -184 25 -86 -122 106 11 162 -133 -88 -245 147 -178 -197 190 -117 84 210 -54 -247 98 -157 64 -95 -129 10 -207 -72 182 -71 181 51 79 -87 242 20 123 82 99 -130 49 101 216 -216 -93 -97 172 -125 -163 -200 -8 -206 197 -242 -194 -212 168 161 -45 103 -160 195 -249 -25 69 137 167 152 -20 -142 243 -55 -171 -36 156 -12 -149 29 28 -183 50 120 -222 17 174 -4 -74 143 236 -189 44 -228 -162 40 229 228 -124 248 231 -5 -193 65 -13 139 128 -92 31 -103 142 -3 -33 205 -164 186 -121 -57 0 -229 42 -114 -213 202 -58 141 -107 -30 -116 171 -214 74 8" echo -n "Mine: " -./push_swap $ARGS |./checker $ARGS +./push_swap $ARGS | valgrind --track-origins=yes ./checker $ARGS echo -n "Their: " ./push_swap $ARGS | ./Push-Swap-Tester/checker_linux $ARGS diff --git a/src.list b/src.list index 2015edd..e78502d 100644 --- a/src.list +++ b/src.list @@ -16,5 +16,6 @@ app/run_with_items app/sort2 app/sort3 app/sort5 +app/state app/target bonus/move1 diff --git a/src/app/main.c b/src/app/main.c index f6c0d0f..e234e3e 100644 --- a/src/app/main.c +++ b/src/app/main.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/01 21:00:12 by maiboyer #+# #+# */ -/* Updated: 2024/02/09 15:50:10 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 17:42:03 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,40 +20,18 @@ #include "me/vec/vec_i64_bool.h" #include #include + #if BONUS == 1 -// # define ERR_INVALID_NUM "KO1\n" -// # define ERR_DUPLICATE "KO2\n" -# define ERR_INVALID_NUM "Error\n" //:\nInvalid Number\n" -# define ERR_DUPLICATE "Error\n" // :\nDuplicate Number\n" -t_usize print_error(t_const_str fmt, ...) -{ - va_list args; - t_usize ret; - - va_start(args, fmt); - ret = me_veprintf(fmt, &args); - va_end(args); - return (ret); -} +# define ERR_INVALID_NUM "Error\n" +# define ERR_DUPLICATE "Error\n" #else -// # define ERR_INVALID_NUM "Error:\nInvalid Number\n" -// # define ERR_DUPLICATE "Error:\nDuplicate Number\n" -# define ERR_INVALID_NUM "Error\n" //:\nInvalid Number\n" -# define ERR_DUPLICATE "Error\n" // :\nDuplicate Number\n" - -t_usize print_error(t_const_str fmt, ...) -{ - va_list args; - t_usize ret; - - va_start(args, fmt); - ret = me_veprintf(fmt, &args); - va_end(args); - return (ret); -} +//# define ERR_INVALID_NUM "Error:\nInvalid Number\n" +//# define ERR_DUPLICATE "Error:\nDuplicate Number\n" +# define ERR_INVALID_NUM "Error\n" +# define ERR_DUPLICATE "Error\n" #endif @@ -63,61 +41,6 @@ void sort_5(t_state *state); bool is_sorted(t_vec_i64 *v); void run_with_items(t_state *state); -bool sort_i64_bool(t_i64_bool *lhs, t_i64_bool *rhs) -{ - return (lhs->value <= rhs->value); -} - -void free_state(t_state state) -{ - vec_i64_free(state.stack_a); - vec_i64_free(state.stack_b); - vec_i64_bool_free(state.sorted); -} - -bool duplicate_check(t_state *state) -{ - t_i64 last; - t_usize index; - - if (state->sorted.len == 0) - return (false); - index = 1; - last = state->sorted.buffer[0].value; - while (index < state->sorted.len) - { - if (last == state->sorted.buffer[index].value) - return (true); - last = state->sorted.buffer[index++].value; - } - return (false); -} - -t_state parses_arguments(t_usize count, t_str nums[]) -{ - t_state state; - t_i32 atoi; - t_usize i; - - state.stack_a = vec_i64_new(count, NULL); - state.stack_b = vec_i64_new(count, NULL); - state.sorted = vec_i64_bool_new(count, NULL); - i = 0; - while (i < count) - { - if (str_to_i32(nums[i], 10, &atoi)) - (free_state(state), me_eprintf(ERR_INVALID_NUM), exit(1)); - vec_i64_push(&state.stack_a, atoi); - vec_i64_bool_push(&state.sorted, (t_i64_bool){.value = atoi, - .active = false}); - i++; - } - vec_i64_bool_sort(&state.sorted, sort_i64_bool); - if (duplicate_check(&state)) - (free_state(state), me_eprintf(ERR_DUPLICATE), exit(1)); - return (state); -} - int main_normal(t_i32 argc, t_str argv[]) { t_state state; diff --git a/src/app/main_bonus.c b/src/app/main_bonus.c index 3968e34..4de3a02 100644 --- a/src/app/main_bonus.c +++ b/src/app/main_bonus.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/08 18:59:33 by maiboyer #+# #+# */ -/* Updated: 2024/02/10 18:55:20 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 17:47:33 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,8 +17,6 @@ #include "me/string/str_n_compare.h" #include "me/vec/vec_i64.h" -#undef BONUS -#define BONUS 1 #if BONUS void push_inner(t_vec_i64 *to, t_vec_i64 *from, t_const_str tag, @@ -28,6 +26,16 @@ void rotate_inner(t_vec_i64 *vec, t_const_str tag, t_const_str print); void rev_rotate_inner(t_vec_i64 *vec, t_const_str tag, t_const_str print); bool is_sorted(t_vec_i64 *v); +bool does_match_strings(t_const_str in, t_const_str s1, t_const_str s2) +{ + bool res; + + res = (str_n_compare(in, s1, 4) == 0); + if (s2 != NULL) + res = (res || (str_n_compare(in, s2, 4) == 0)); + return (res); +} + t_error handle_operation(t_buffer_str s, t_state *state) { t_usize op; @@ -35,67 +43,62 @@ t_error handle_operation(t_buffer_str s, t_state *state) op = 0; if (s.buf == NULL) return (ERROR); - if (str_n_compare(s.buf, "pa", 2) == 0) + if (does_match_strings(s.buf, "pa", NULL)) push_inner(&state->stack_a, &state->stack_b, NULL, (op++, NULL)); - if (str_n_compare(s.buf, "pb", 2) == 0) + if (does_match_strings(s.buf, "pb", NULL)) push_inner(&state->stack_b, &state->stack_a, NULL, (op++, NULL)); - if (str_n_compare(s.buf, "sa", 2) == 0 || str_n_compare(s.buf, "ss", - 2) == 0) + if (does_match_strings(s.buf, "sa", "ss")) swap_inner(&state->stack_a, NULL, (op++, NULL)); - if (str_n_compare(s.buf, "sb", 2) == 0 || str_n_compare(s.buf, "ss", - 2) == 0) + if (does_match_strings(s.buf, "sb", "ss")) swap_inner(&state->stack_b, NULL, (op++, NULL)); - if (str_n_compare(s.buf, "ra", 2) == 0 || str_n_compare(s.buf, "rr", - 2) == 0) + if (does_match_strings(s.buf, "ra", "rr")) rotate_inner(&state->stack_a, NULL, (op++, NULL)); - if (str_n_compare(s.buf, "rb", 2) == 0 || str_n_compare(s.buf, "rr", - 2) == 0) + if (does_match_strings(s.buf, "rb", "rr")) rotate_inner(&state->stack_b, NULL, (op++, NULL)); - if (str_n_compare(s.buf, "rra", 3) == 0 || str_n_compare(s.buf, "rrr", - 3) == 0) + if (does_match_strings(s.buf, "rra", "rrr")) rev_rotate_inner(&state->stack_a, NULL, (op++, NULL)); - if (str_n_compare(s.buf, "rrb", 3) == 0 || str_n_compare(s.buf, "rrr", - 3) == 0) + if (does_match_strings(s.buf, "rrb", "rrr")) rev_rotate_inner(&state->stack_b, NULL, (op++, NULL)); if (op == 0) return (ERROR); return (NO_ERROR); } +void handle_end(t_state state, t_buffer_str s) +{ + str_free(s); + if (is_sorted(&state.stack_a) && state.stack_b.len == 0) + me_printf("OK\n"); + else + me_printf("KO\n"); + free_state(state); +} + int main_checker(t_i32 argc, t_str argv[]) { t_state state; t_buffer_str s; bool err; - t_usize count; (void)(argc--, argv++); + s.buf = ""; err = false; state = parses_arguments(argc, argv); - s = get_next_line(0, &err); - count = 1; - while (!err && s.buf != NULL) + while (!err) { + s = get_next_line(0, &err); + if (err) + break ; if (s.len != 0 && s.buf[s.len - 1] == '\n') str_pop(&s); - // me_printf("line[%u]: '%s'\n", count, s.buf); + if (s.buf[0] == '\0') + continue ; if (handle_operation(s, &state)) - { - me_printf("KO: error\n"); - me_printf("ERROR[%u] = '%s'\n", count, s.buf); - (str_free(s), free_state(state)); - return (1); - } - str_free(s); - s = get_next_line(0, &err); - count++; + return (me_printf("KO\n"), str_free(s), free_state(state), 1); + if (s.buf != NULL) + str_free(s); } - str_free(s); - if (is_sorted(&state.stack_a) && state.stack_b.len == 0) - me_printf("OK\n"); - else - me_printf("KO: Not sorted\n"); - free_state(state); + handle_end(state, s); return (0); } diff --git a/src/app/state.c b/src/app/state.c new file mode 100644 index 0000000..7d5e2dc --- /dev/null +++ b/src/app/state.c @@ -0,0 +1,91 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* state.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:00:12 by maiboyer #+# #+# */ +/* Updated: 2024/02/15 17:42:13 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/state.h" +#include "app/types/type_i64_bool.h" +#include "me/convert/str_to_numbers.h" +#include "me/printf/printf.h" +#include "me/string/str_n_compare.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" +#include "me/vec/vec_i64_bool.h" +#include +#include + +#if BONUS == 1 + +# define ERR_INVALID_NUM "Error\n" +# define ERR_DUPLICATE "Error\n" + +#else + +//# define ERR_INVALID_NUM "Error:\nInvalid Number\n" +//# define ERR_DUPLICATE "Error:\nDuplicate Number\n" +# define ERR_INVALID_NUM "Error\n" +# define ERR_DUPLICATE "Error\n" + +#endif + +bool sort_i64_bool(t_i64_bool *lhs, t_i64_bool *rhs) +{ + return (lhs->value <= rhs->value); +} + +void free_state(t_state state) +{ + vec_i64_free(state.stack_a); + vec_i64_free(state.stack_b); + vec_i64_bool_free(state.sorted); +} + +bool duplicate_check(t_state *state) +{ + t_i64 last; + t_usize index; + + if (state->sorted.len == 0) + return (false); + index = 1; + last = state->sorted.buffer[0].value; + while (index < state->sorted.len) + { + if (last == state->sorted.buffer[index].value) + return (true); + last = state->sorted.buffer[index++].value; + } + return (false); +} + +t_state parses_arguments(t_usize count, t_str nums[]) +{ + t_state state; + t_i32 atoi; + t_usize i; + + state.stack_a = vec_i64_new(count, NULL); + state.stack_b = vec_i64_new(count, NULL); + state.sorted = vec_i64_bool_new(count, NULL); + i = 0; + while (i < count) + { + if (str_to_i32(nums[i], 10, &atoi)) + (free_state(state), me_eprintf(ERR_INVALID_NUM), exit(1)); + vec_i64_push(&state.stack_a, atoi); + vec_i64_bool_push(&state.sorted, (t_i64_bool){.value = atoi, + .active = false}); + i++; + } + vec_i64_bool_sort(&state.sorted, sort_i64_bool); + if (duplicate_check(&state)) + (free_state(state), me_eprintf(ERR_DUPLICATE), exit(1)); + return (state); +} From 92796351f3117cb3897db4a5116d93c72187dba4 Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Thu, 15 Feb 2024 17:59:03 +0100 Subject: [PATCH 27/29] first push ? --- .gitignore | 6 + Makefile | 276 +++++++++++++++++++++ include/app/best_index_to_move.h | 30 +++ include/app/best_move.h | 43 ++++ include/app/best_move_inner.h | 58 +++++ include/app/find_iter.h | 22 ++ include/app/find_place.h | 29 +++ include/app/iter_state.h | 24 ++ include/app/moves.h | 23 ++ include/app/rotate.h | 56 +++++ include/app/state.h | 73 ++++++ include/app/target.h | 21 ++ include/app/types/type_i64_bool.h | 24 ++ include/app/types/type_move.h | 31 +++ include/me/buffered_str/buf_str.h | 49 ++++ include/me/char/isalnum.h | 20 ++ include/me/char/isalpha.h | 20 ++ include/me/char/isascii.h | 20 ++ include/me/char/isdigit.h | 20 ++ include/me/char/islower.h | 20 ++ include/me/char/isprint.h | 20 ++ include/me/char/isspace.h | 20 ++ include/me/char/isupper.h | 20 ++ include/me/char/tolower.h | 20 ++ include/me/char/toupper.h | 20 ++ include/me/convert/atoi.h | 21 ++ include/me/convert/itoa.h | 20 ++ include/me/convert/str_to_numbers.h | 30 +++ include/me/fs/close.h | 20 ++ include/me/fs/open.h | 22 ++ include/me/fs/putchar_fd.h | 20 ++ include/me/fs/putendl_fd.h | 20 ++ include/me/fs/putnbr_fd.h | 20 ++ include/me/fs/putstr_fd.h | 20 ++ include/me/fs/read.h | 20 ++ include/me/fs/read_to_vec.h | 25 ++ include/me/fs/write.h | 20 ++ include/me/gnl/gnl.h | 43 ++++ include/me/hash/hasher.h | 52 ++++ include/me/hash/sip.h | 38 +++ include/me/hash/sip/sip_utils.h | 24 ++ include/me/img/qoi.h | 305 ++++++++++++++++++++++++ include/me/img/qoi/qoi_decode.h | 86 +++++++ include/me/img/qoi/qoi_encode.h | 155 ++++++++++++ include/me/img/qoi/qoi_utils.h | 56 +++++ include/me/list/list_add_back.h | 20 ++ include/me/list/list_add_front.h | 19 ++ include/me/list/list_alloc_node.h | 20 ++ include/me/list/list_free_all.h | 20 ++ include/me/list/list_free_one.h | 20 ++ include/me/list/list_get_last.h | 20 ++ include/me/list/list_iter.h | 20 ++ include/me/list/list_map.h | 20 ++ include/me/list/list_size.h | 20 ++ include/me/mem/mem_alloc.h | 20 ++ include/me/mem/mem_alloc_array.h | 20 ++ include/me/mem/mem_compare.h | 20 ++ include/me/mem/mem_copy.h | 20 ++ include/me/mem/mem_find.h | 20 ++ include/me/mem/mem_find_bytes.h | 21 ++ include/me/mem/mem_move.h | 20 ++ include/me/mem/mem_set.h | 20 ++ include/me/mem/mem_set_zero.h | 20 ++ include/me/num/u16.h | 21 ++ include/me/num/u32.h | 21 ++ include/me/num/u64.h | 24 ++ include/me/num/u8.h | 21 ++ include/me/num/usize.h | 21 ++ include/me/printf/formatter/formatter.h | 27 +++ include/me/printf/formatter/utils.h | 76 ++++++ include/me/printf/matchers/matchers.h | 52 ++++ include/me/printf/printf.h | 69 ++++++ include/me/string/str_clone.h | 20 ++ include/me/string/str_find_chr.h | 20 ++ include/me/string/str_find_rev_chr.h | 20 ++ include/me/string/str_find_str.h | 20 ++ include/me/string/str_iter.h | 20 ++ include/me/string/str_join.h | 20 ++ include/me/string/str_l_cat.h | 20 ++ include/me/string/str_l_copy.h | 20 ++ include/me/string/str_len.h | 20 ++ include/me/string/str_map.h | 20 ++ include/me/string/str_n_compare.h | 20 ++ include/me/string/str_n_find_str.h | 20 ++ include/me/string/str_split.h | 20 ++ include/me/string/str_substring.h | 20 ++ include/me/string/str_trim.h | 20 ++ include/me/types.h | 49 ++++ include/me/vec/vec_buf_str.h | 60 +++++ include/me/vec/vec_i64.h | 51 ++++ include/me/vec/vec_i64_bool.h | 60 +++++ include/me/vec/vec_u8.h | 50 ++++ include/me/vec2/vec2.h | 60 +++++ src/app/actual_main.c | 26 ++ src/app/best_index_to_move.c | 77 ++++++ src/app/best_move.c | 99 ++++++++ src/app/do_move.c | 29 +++ src/app/find_place.c | 52 ++++ src/app/iter_find.c | 57 +++++ src/app/main.c | 64 +++++ src/app/main_bonus.c | 105 ++++++++ src/app/moves.c | 71 ++++++ src/app/moves/push.c | 39 +++ src/app/moves/rev_rotate.c | 44 ++++ src/app/moves/rotate.c | 43 ++++ src/app/moves/swap.c | 47 ++++ src/app/rotate.c | 14 ++ src/app/run_with_items.c | 114 +++++++++ src/app/sort2.c | 33 +++ src/app/sort3.c | 138 +++++++++++ src/app/sort5.c | 139 +++++++++++ src/app/state.c | 91 +++++++ src/app/target.c | 24 ++ src/bonus/move1.c | 11 + src/buffered_str/mod.c | 82 +++++++ src/char/isalnum.c | 20 ++ src/char/isalpha.c | 18 ++ src/char/isascii.c | 18 ++ src/char/isdigit.c | 18 ++ src/char/islower.c | 18 ++ src/char/isprint.c | 18 ++ src/char/isspace.c | 19 ++ src/char/isupper.c | 18 ++ src/char/tolower.c | 22 ++ src/char/toupper.c | 22 ++ src/convert/atoi.c | 61 +++++ src/convert/itoa.c | 69 ++++++ src/convert/str_to_i16.c | 129 ++++++++++ src/convert/str_to_i16_utils.c | 45 ++++ src/convert/str_to_i32.c | 129 ++++++++++ src/convert/str_to_i32_utils.c | 45 ++++ src/convert/str_to_i64.c | 129 ++++++++++ src/convert/str_to_i64_utils.c | 45 ++++ src/convert/str_to_i8.c | 129 ++++++++++ src/convert/str_to_i8_utils.c | 45 ++++ src/convert/str_to_u16.c | 129 ++++++++++ src/convert/str_to_u16_utils.c | 45 ++++ src/convert/str_to_u32.c | 129 ++++++++++ src/convert/str_to_u32_utils.c | 45 ++++ src/convert/str_to_u64.c | 129 ++++++++++ src/convert/str_to_u64_utils.c | 45 ++++ src/convert/str_to_u8.c | 129 ++++++++++ src/convert/str_to_u8_utils.c | 45 ++++ src/fs/close.c | 27 +++ src/fs/open.c | 60 +++++ src/fs/putchar_fd.c | 20 ++ src/fs/putendl_fd.c | 23 ++ src/fs/putnbr_fd.c | 55 +++++ src/fs/putstr_fd.c | 22 ++ src/fs/read.c | 24 ++ src/fs/read_to_vec.c | 43 ++++ src/fs/write.c | 19 ++ src/gnl/get_next_line.c | 128 ++++++++++ src/hash/hash_signed.c | 39 +++ src/hash/hash_unsigned.c | 34 +++ src/hash/hasher.c | 29 +++ src/hash/sip/sip13.c | 31 +++ src/hash/sip/sip_utils.c | 106 ++++++++ src/hash/sip/sip_utils2.c | 68 ++++++ src/img/qoi/qoi_decode.c | 73 ++++++ src/img/qoi/qoi_encode.c | 29 +++ src/img/qoi/qoi_fs.c | 49 ++++ src/img/qoi/qoi_utils.c | 36 +++ src/list/list_add_back.c | 22 ++ src/list/list_add_front.c | 20 ++ src/list/list_alloc_node.c | 26 ++ src/list/list_free_all.c | 30 +++ src/list/list_free_one.c | 22 ++ src/list/list_get_last.c | 25 ++ src/list/list_iter.c | 22 ++ src/list/list_map.c | 40 ++++ src/list/list_size.c | 28 +++ src/mem/mem_alloc.c | 26 ++ src/mem/mem_alloc_array.c | 25 ++ src/mem/mem_compare.c | 31 +++ src/mem/mem_copy.c | 32 +++ src/mem/mem_find.c | 29 +++ src/mem/mem_find_bytes.c | 39 +++ src/mem/mem_move.c | 42 ++++ src/mem/mem_set.c | 27 +++ src/mem/mem_set_zero.c | 27 +++ src/num/u16/rotate.c | 29 +++ src/num/u32/rotate.c | 29 +++ src/num/u64/from_bytes.c | 57 +++++ src/num/u64/rotate.c | 29 +++ src/num/u8/rotate.c | 29 +++ src/num/usize/rotate.c | 29 +++ src/printf/formatter/char.c | 70 ++++++ src/printf/formatter/decimal.c | 89 +++++++ src/printf/formatter/hex.c | 67 ++++++ src/printf/formatter/oct.c | 60 +++++ src/printf/formatter/ptr.c | 74 ++++++ src/printf/formatter/unsigned_decimal.c | 76 ++++++ src/printf/formatter/utils.c | 136 +++++++++++ src/printf/formatter/utils2.c | 96 ++++++++ src/printf/formatter/utils3.c | 53 ++++ src/printf/formatter/utils_numbers.c | 36 +++ src/printf/matchers.c | 120 ++++++++++ src/printf/printf.c | 120 ++++++++++ src/printf/vprintf.c | 51 ++++ src/string/str_clone.c | 30 +++ src/string/str_find_chr.c | 29 +++ src/string/str_find_rev_chr.c | 28 +++ src/string/str_find_str.c | 56 +++++ src/string/str_iter.c | 27 +++ src/string/str_join.c | 34 +++ src/string/str_l_cat.c | 108 +++++++++ src/string/str_l_copy.c | 104 ++++++++ src/string/str_len.c | 23 ++ src/string/str_map.c | 33 +++ src/string/str_n_compare.c | 27 +++ src/string/str_n_find_str.c | 51 ++++ src/string/str_split.c | 93 ++++++++ src/string/str_substring.c | 38 +++ src/string/str_trim.c | 44 ++++ src/vec/vec_buf_str.c | 115 +++++++++ src/vec/vec_buf_str_functions2.c | 110 +++++++++ src/vec/vec_buf_str_functions3.c | 72 ++++++ src/vec/vec_i64.c | 114 +++++++++ src/vec/vec_i64_bool.c | 115 +++++++++ src/vec/vec_i64_bool_functions2.c | 109 +++++++++ src/vec/vec_i64_bool_functions3.c | 72 ++++++ src/vec/vec_i64_bool_sort.c | 41 ++++ src/vec/vec_i64_functions2.c | 106 ++++++++ src/vec/vec_i64_functions3.c | 72 ++++++ src/vec/vec_i64_sort.c | 40 ++++ src/vec/vec_u8.c | 114 +++++++++ src/vec/vec_u8_functions2.c | 106 ++++++++ src/vec/vec_u8_functions3.c | 72 ++++++ 229 files changed, 11077 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 include/app/best_index_to_move.h create mode 100644 include/app/best_move.h create mode 100644 include/app/best_move_inner.h create mode 100644 include/app/find_iter.h create mode 100644 include/app/find_place.h create mode 100644 include/app/iter_state.h create mode 100644 include/app/moves.h create mode 100644 include/app/rotate.h create mode 100644 include/app/state.h create mode 100644 include/app/target.h create mode 100644 include/app/types/type_i64_bool.h create mode 100644 include/app/types/type_move.h create mode 100644 include/me/buffered_str/buf_str.h create mode 100644 include/me/char/isalnum.h create mode 100644 include/me/char/isalpha.h create mode 100644 include/me/char/isascii.h create mode 100644 include/me/char/isdigit.h create mode 100644 include/me/char/islower.h create mode 100644 include/me/char/isprint.h create mode 100644 include/me/char/isspace.h create mode 100644 include/me/char/isupper.h create mode 100644 include/me/char/tolower.h create mode 100644 include/me/char/toupper.h create mode 100644 include/me/convert/atoi.h create mode 100644 include/me/convert/itoa.h create mode 100644 include/me/convert/str_to_numbers.h create mode 100644 include/me/fs/close.h create mode 100644 include/me/fs/open.h create mode 100644 include/me/fs/putchar_fd.h create mode 100644 include/me/fs/putendl_fd.h create mode 100644 include/me/fs/putnbr_fd.h create mode 100644 include/me/fs/putstr_fd.h create mode 100644 include/me/fs/read.h create mode 100644 include/me/fs/read_to_vec.h create mode 100644 include/me/fs/write.h create mode 100644 include/me/gnl/gnl.h create mode 100644 include/me/hash/hasher.h create mode 100644 include/me/hash/sip.h create mode 100644 include/me/hash/sip/sip_utils.h create mode 100644 include/me/img/qoi.h create mode 100644 include/me/img/qoi/qoi_decode.h create mode 100644 include/me/img/qoi/qoi_encode.h create mode 100644 include/me/img/qoi/qoi_utils.h create mode 100644 include/me/list/list_add_back.h create mode 100644 include/me/list/list_add_front.h create mode 100644 include/me/list/list_alloc_node.h create mode 100644 include/me/list/list_free_all.h create mode 100644 include/me/list/list_free_one.h create mode 100644 include/me/list/list_get_last.h create mode 100644 include/me/list/list_iter.h create mode 100644 include/me/list/list_map.h create mode 100644 include/me/list/list_size.h create mode 100644 include/me/mem/mem_alloc.h create mode 100644 include/me/mem/mem_alloc_array.h create mode 100644 include/me/mem/mem_compare.h create mode 100644 include/me/mem/mem_copy.h create mode 100644 include/me/mem/mem_find.h create mode 100644 include/me/mem/mem_find_bytes.h create mode 100644 include/me/mem/mem_move.h create mode 100644 include/me/mem/mem_set.h create mode 100644 include/me/mem/mem_set_zero.h create mode 100644 include/me/num/u16.h create mode 100644 include/me/num/u32.h create mode 100644 include/me/num/u64.h create mode 100644 include/me/num/u8.h create mode 100644 include/me/num/usize.h create mode 100644 include/me/printf/formatter/formatter.h create mode 100644 include/me/printf/formatter/utils.h create mode 100644 include/me/printf/matchers/matchers.h create mode 100644 include/me/printf/printf.h create mode 100644 include/me/string/str_clone.h create mode 100644 include/me/string/str_find_chr.h create mode 100644 include/me/string/str_find_rev_chr.h create mode 100644 include/me/string/str_find_str.h create mode 100644 include/me/string/str_iter.h create mode 100644 include/me/string/str_join.h create mode 100644 include/me/string/str_l_cat.h create mode 100644 include/me/string/str_l_copy.h create mode 100644 include/me/string/str_len.h create mode 100644 include/me/string/str_map.h create mode 100644 include/me/string/str_n_compare.h create mode 100644 include/me/string/str_n_find_str.h create mode 100644 include/me/string/str_split.h create mode 100644 include/me/string/str_substring.h create mode 100644 include/me/string/str_trim.h create mode 100644 include/me/types.h create mode 100644 include/me/vec/vec_buf_str.h create mode 100644 include/me/vec/vec_i64.h create mode 100644 include/me/vec/vec_i64_bool.h create mode 100644 include/me/vec/vec_u8.h create mode 100644 include/me/vec2/vec2.h create mode 100644 src/app/actual_main.c create mode 100644 src/app/best_index_to_move.c create mode 100644 src/app/best_move.c create mode 100644 src/app/do_move.c create mode 100644 src/app/find_place.c create mode 100644 src/app/iter_find.c create mode 100644 src/app/main.c create mode 100644 src/app/main_bonus.c create mode 100644 src/app/moves.c create mode 100644 src/app/moves/push.c create mode 100644 src/app/moves/rev_rotate.c create mode 100644 src/app/moves/rotate.c create mode 100644 src/app/moves/swap.c create mode 100644 src/app/rotate.c create mode 100644 src/app/run_with_items.c create mode 100644 src/app/sort2.c create mode 100644 src/app/sort3.c create mode 100644 src/app/sort5.c create mode 100644 src/app/state.c create mode 100644 src/app/target.c create mode 100644 src/bonus/move1.c create mode 100644 src/buffered_str/mod.c create mode 100644 src/char/isalnum.c create mode 100644 src/char/isalpha.c create mode 100644 src/char/isascii.c create mode 100644 src/char/isdigit.c create mode 100644 src/char/islower.c create mode 100644 src/char/isprint.c create mode 100644 src/char/isspace.c create mode 100644 src/char/isupper.c create mode 100644 src/char/tolower.c create mode 100644 src/char/toupper.c create mode 100644 src/convert/atoi.c create mode 100644 src/convert/itoa.c create mode 100644 src/convert/str_to_i16.c create mode 100644 src/convert/str_to_i16_utils.c create mode 100644 src/convert/str_to_i32.c create mode 100644 src/convert/str_to_i32_utils.c create mode 100644 src/convert/str_to_i64.c create mode 100644 src/convert/str_to_i64_utils.c create mode 100644 src/convert/str_to_i8.c create mode 100644 src/convert/str_to_i8_utils.c create mode 100644 src/convert/str_to_u16.c create mode 100644 src/convert/str_to_u16_utils.c create mode 100644 src/convert/str_to_u32.c create mode 100644 src/convert/str_to_u32_utils.c create mode 100644 src/convert/str_to_u64.c create mode 100644 src/convert/str_to_u64_utils.c create mode 100644 src/convert/str_to_u8.c create mode 100644 src/convert/str_to_u8_utils.c create mode 100644 src/fs/close.c create mode 100644 src/fs/open.c create mode 100644 src/fs/putchar_fd.c create mode 100644 src/fs/putendl_fd.c create mode 100644 src/fs/putnbr_fd.c create mode 100644 src/fs/putstr_fd.c create mode 100644 src/fs/read.c create mode 100644 src/fs/read_to_vec.c create mode 100644 src/fs/write.c create mode 100644 src/gnl/get_next_line.c create mode 100644 src/hash/hash_signed.c create mode 100644 src/hash/hash_unsigned.c create mode 100644 src/hash/hasher.c create mode 100644 src/hash/sip/sip13.c create mode 100644 src/hash/sip/sip_utils.c create mode 100644 src/hash/sip/sip_utils2.c create mode 100644 src/img/qoi/qoi_decode.c create mode 100644 src/img/qoi/qoi_encode.c create mode 100644 src/img/qoi/qoi_fs.c create mode 100644 src/img/qoi/qoi_utils.c create mode 100644 src/list/list_add_back.c create mode 100644 src/list/list_add_front.c create mode 100644 src/list/list_alloc_node.c create mode 100644 src/list/list_free_all.c create mode 100644 src/list/list_free_one.c create mode 100644 src/list/list_get_last.c create mode 100644 src/list/list_iter.c create mode 100644 src/list/list_map.c create mode 100644 src/list/list_size.c create mode 100644 src/mem/mem_alloc.c create mode 100644 src/mem/mem_alloc_array.c create mode 100644 src/mem/mem_compare.c create mode 100644 src/mem/mem_copy.c create mode 100644 src/mem/mem_find.c create mode 100644 src/mem/mem_find_bytes.c create mode 100644 src/mem/mem_move.c create mode 100644 src/mem/mem_set.c create mode 100644 src/mem/mem_set_zero.c create mode 100644 src/num/u16/rotate.c create mode 100644 src/num/u32/rotate.c create mode 100644 src/num/u64/from_bytes.c create mode 100644 src/num/u64/rotate.c create mode 100644 src/num/u8/rotate.c create mode 100644 src/num/usize/rotate.c create mode 100644 src/printf/formatter/char.c create mode 100644 src/printf/formatter/decimal.c create mode 100644 src/printf/formatter/hex.c create mode 100644 src/printf/formatter/oct.c create mode 100644 src/printf/formatter/ptr.c create mode 100644 src/printf/formatter/unsigned_decimal.c create mode 100644 src/printf/formatter/utils.c create mode 100644 src/printf/formatter/utils2.c create mode 100644 src/printf/formatter/utils3.c create mode 100644 src/printf/formatter/utils_numbers.c create mode 100644 src/printf/matchers.c create mode 100644 src/printf/printf.c create mode 100644 src/printf/vprintf.c create mode 100644 src/string/str_clone.c create mode 100644 src/string/str_find_chr.c create mode 100644 src/string/str_find_rev_chr.c create mode 100644 src/string/str_find_str.c create mode 100644 src/string/str_iter.c create mode 100644 src/string/str_join.c create mode 100644 src/string/str_l_cat.c create mode 100644 src/string/str_l_copy.c create mode 100644 src/string/str_len.c create mode 100644 src/string/str_map.c create mode 100644 src/string/str_n_compare.c create mode 100644 src/string/str_n_find_str.c create mode 100644 src/string/str_split.c create mode 100644 src/string/str_substring.c create mode 100644 src/string/str_trim.c create mode 100644 src/vec/vec_buf_str.c create mode 100644 src/vec/vec_buf_str_functions2.c create mode 100644 src/vec/vec_buf_str_functions3.c create mode 100644 src/vec/vec_i64.c create mode 100644 src/vec/vec_i64_bool.c create mode 100644 src/vec/vec_i64_bool_functions2.c create mode 100644 src/vec/vec_i64_bool_functions3.c create mode 100644 src/vec/vec_i64_bool_sort.c create mode 100644 src/vec/vec_i64_functions2.c create mode 100644 src/vec/vec_i64_functions3.c create mode 100644 src/vec/vec_i64_sort.c create mode 100644 src/vec/vec_u8.c create mode 100644 src/vec/vec_u8_functions2.c create mode 100644 src/vec/vec_u8_functions3.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..41e62e3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +build +/checker +/push_swap +*.list +*.o +*.d diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d12c11a --- /dev/null +++ b/Makefile @@ -0,0 +1,276 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: maiboyer +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/11/03 13:20:01 by maiboyer #+# #+# # +# Updated: 2024/02/15 17:58:38 by maiboyer ### ########.fr # +# # +# **************************************************************************** # + +define_module = $(addprefix $(1)/, $(2)) + +BUILD_DIR = build +SRC_DIR = src +INCLUDE_DIR = include +LIBS_DIR = . +GENERIC_DIR = +GENERIC_INCLUDE = + +NAME = push_swap + +ifeq ($(MAKECMDGOALS), bonus) + CFLAGS += -DBONUS=1 + BUILD_DIR := $(BUILD_DIR)/bonus/ + NAME = checker +endif + +LIB_NAME ?= +TARGET = $(NAME) +CC ?= clang +CFLAGS += -Wall -Werror -Wextra -g3 -L$(BUILD_DIR) -Wno-unused-command-line-argument -MMD +BONUS_FILES = +LIBS_NAME = +SUBJECT_URL = 'https://cdn.intra.42.fr/pdf/pdf/118610/en.subject.pdf' + +GENERIC_FILES = +SRC_FILES = \ +app/actual_main \ +app/best_index_to_move \ +app/best_move \ +app/do_move \ +app/find_place \ +app/iter_find \ +app/main \ +app/main_bonus \ +app/moves \ +app/moves/push \ +app/moves/rev_rotate \ +app/moves/rotate \ +app/moves/swap \ +app/rotate \ +app/run_with_items \ +app/sort2 \ +app/sort3 \ +app/sort5 \ +app/state \ +app/target \ +bonus/move1 \ +buffered_str/mod \ +char/isalnum \ +char/isalpha \ +char/isascii \ +char/isdigit \ +char/islower \ +char/isprint \ +char/isspace \ +char/isupper \ +char/tolower \ +char/toupper \ +convert/atoi \ +convert/itoa \ +convert/str_to_i16 \ +convert/str_to_i16_utils \ +convert/str_to_i32 \ +convert/str_to_i32_utils \ +convert/str_to_i64 \ +convert/str_to_i64_utils \ +convert/str_to_i8 \ +convert/str_to_i8_utils \ +convert/str_to_u16 \ +convert/str_to_u16_utils \ +convert/str_to_u32 \ +convert/str_to_u32_utils \ +convert/str_to_u64 \ +convert/str_to_u64_utils \ +convert/str_to_u8 \ +convert/str_to_u8_utils \ +fs/close \ +fs/open \ +fs/putchar_fd \ +fs/putendl_fd \ +fs/putnbr_fd \ +fs/putstr_fd \ +fs/read \ +fs/read_to_vec \ +fs/write \ +gnl/get_next_line \ +hash/hasher \ +hash/hash_signed \ +hash/hash_unsigned \ +hash/sip/sip13 \ +hash/sip/sip_utils \ +hash/sip/sip_utils2 \ +img/qoi/qoi_decode \ +img/qoi/qoi_encode \ +img/qoi/qoi_fs \ +img/qoi/qoi_utils \ +list/list_add_back \ +list/list_add_front \ +list/list_alloc_node \ +list/list_free_all \ +list/list_free_one \ +list/list_get_last \ +list/list_iter \ +list/list_map \ +list/list_size \ +mem/mem_alloc \ +mem/mem_alloc_array \ +mem/mem_compare \ +mem/mem_copy \ +mem/mem_find \ +mem/mem_find_bytes \ +mem/mem_move \ +mem/mem_set \ +mem/mem_set_zero \ +num/u16/rotate \ +num/u32/rotate \ +num/u64/from_bytes \ +num/u64/rotate \ +num/u8/rotate \ +num/usize/rotate \ +printf/formatter/char \ +printf/formatter/decimal \ +printf/formatter/hex \ +printf/formatter/oct \ +printf/formatter/ptr \ +printf/formatter/unsigned_decimal \ +printf/formatter/utils \ +printf/formatter/utils2 \ +printf/formatter/utils3 \ +printf/formatter/utils_numbers \ +printf/matchers \ +printf/printf \ +printf/vprintf \ +string/str_clone \ +string/str_find_chr \ +string/str_find_rev_chr \ +string/str_find_str \ +string/str_iter \ +string/str_join \ +string/str_l_cat \ +string/str_l_copy \ +string/str_len \ +string/str_map \ +string/str_n_compare \ +string/str_n_find_str \ +string/str_split \ +string/str_substring \ +string/str_trim \ +vec/vec_buf_str \ +vec/vec_buf_str_functions2 \ +vec/vec_buf_str_functions3 \ +vec/vec_i64 \ +vec/vec_i64_bool \ +vec/vec_i64_bool_functions2 \ +vec/vec_i64_bool_functions3 \ +vec/vec_i64_bool_sort \ +vec/vec_i64_functions2 \ +vec/vec_i64_functions3 \ +vec/vec_i64_sort \ +vec/vec_u8 \ +vec/vec_u8_functions2 \ +vec/vec_u8_functions3 + +BONUS = $(addsuffix .c,$(addprefix $(SRC_DIR)/,$(BONUS_FILES))) +SRC = $(addsuffix .c,$(addprefix $(SRC_DIR)/,$(SRC_FILES))) \ + $(addsuffix .c,$(addprefix $(GENERIC_DIR)/,$(GENERIC_FILES))) +BONUS_OBJ = $(addsuffix .o,$(addprefix $(BUILD_DIR)/,$(BONUS_FILES))) +OBJ = $(addsuffix .o,$(addprefix $(BUILD_DIR)/,$(SRC_FILES))) \ + $(addsuffix .o,$(addprefix $(BUILD_DIR)/,$(GENERIC_FILES))) +DEPS = $(addsuffix .d,$(addprefix $(BUILD_DIR)/,$(SRC_FILES))) \ + $(addsuffix .d,$(addprefix $(BUILD_DIR)/,$(GENERIC_FILES))) +LIBS = $(addprefix $(LIBS_DIR)/,$(LIBS_NAME)) +INCLUDES = $(addprefix -I,$(INCLUDE_DIR) $(GENERIC_INCLUDE) $(LIBS) $(addsuffix /include,$(LIBS))) +COL_GRAY = \\e[90m +COL_WHITE = \\e[37m +COL_GREEN = \\e[32m +COL_BOLD = \\e[1m +COL_RESET = \\e[0m + + +.PHONY: remove_name clean all fclean bonus format re generate_filelist subject get_lib + +all: $(NAME) + +bonus: all + @printf \\n$(COL_GRAY)Building\ Output\ \(with\ bonus\)\ $(COL_WHITE)$(COL_BOLD)%-28s$(COL_RESET)\ \ + $(NAME) + @printf $(COL_RESET)$(COL_GREEN)done$(COL_RESET)\\n + +get_lib: + @printf $(LIB_NAME)/$(NAME) + +$(NAME): $(OBJ) libs_build + @printf \\n$(COL_GRAY)Building\ Output\ $(COL_WHITE)$(COL_BOLD)%-28s$(COL_RESET)\ \ + $(NAME) + @$(CC) $(INCLUDES) $(OBJ) $(CFLAGS) -o $(NAME) + @#ar rcs $(BUILD_DIR)/$(NAME) $(OBJ) + @printf $(COL_GREEN)done$(COL_RESET)\\n + +libs_build: + @- $(foreach LIB,$(LIBS),\ + printf \\n; \ + printf $(COL_GRAY)Building\ library\ $(COL_RESET); \ + printf $(COL_WHITE)$(COL_BOLD)%-25s$(COL_RESET)\\n $(LIB); \ + make LIB_NAME=$(LIB)/ BUILD_DIR=$(realpath $(BUILD_DIR)) -C $(LIB) --no-print-directory all; \ + printf \\n; \ + ) + +$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c + @mkdir -p $(dir $@) + @printf $(COL_GRAY)Building\ $(COL_BOLD)$(COL_WHITE)%-50s\ $(LIB_NAME)$< + @$(CC) $(CFLAGS) $(WERROR) $(INCLUDES) -c $< -o $@ + @printf $(COL_RESET)$(COL_GREEN)done$(COL_RESET)\\n + +$(BUILD_DIR)/%.o: $(GENERIC_DIR)/%.c + @mkdir -p $(dir $@) + @printf $(COL_GRAY)Building\ $(COL_BOLD)$(COL_WHITE)%-50s\ $(LIB_NAME)$< + @$(CC) $(CFLAGS) $(WERROR) $(INCLUDES) -c $< -o $@ + @printf $(COL_RESET)$(COL_GREEN)done$(COL_RESET)\\n + +clean: + @- $(foreach LIB,$(LIBS), \ + make clean LIB_NAME=$(LIB)/ BUILD_DIR=$(realpath $(BUILD_DIR)) -C $(LIB) --no-print-directory || true;\ + ) + @- $(if $(LIB_NAME),,\ + printf $(COL_WHITE)Clearing\ Artefacts\ ; \ + printf $(COL_GRAY)\%-25s$(COL_RESET)\ \($(BUILD_DIR)\); \ + rm -rf $(BUILD_DIR); \ + printf $(COL_GREEN)done$(COL_RESET)\\n; \ + ) + @echo >/dev/null + +fclean: clean + @- $(foreach LIB,$(LIBS), \ + make fclean LIB_NAME=$(LIB)/ BUILD_DIR=$(realpath $(BUILD_DIR)) -C $(LIB) --no-print-directory || true;\ + ) + @printf $(COL_WHITE)Clearing\ Output\ $(COL_GRAY)%-28s$(COL_RESET)\ \ + \($(LIB_NAME)$(NAME)\) + @rm -f $(BUILD_DIR)$(NAME) + @rm -f push_swap + @rm -f checker + @printf $(COL_GREEN)done$(COL_RESET)\\n + +re: fclean all + +generate_filelist: + @/usr/bin/env zsh -c "tree -iFf --noreport output | rg '^output/src/(.*)\.c\$$' --replace '\$$1' | sort -u" > ./gen.list + @/usr/bin/env zsh -c "tree -iFf --noreport src | rg '^src/(.*)\.c\$$' --replace '\$$1' | sort -u" > ./src.list + +format: + @zsh -c "c_formatter_42 **/*.c **/*.h" + +subject: subject.txt + @bat --plain subject.txt + +subject.txt: + @curl $(SUBJECT_URL) | pdftotext -layout -nopgbrk - subject.txt + +fuck_raphael: + @echo "Oh que oui~~~\net jte nioc" + +-include $(DEPS) + diff --git a/include/app/best_index_to_move.h b/include/app/best_index_to_move.h new file mode 100644 index 0000000..53e5ed7 --- /dev/null +++ b/include/app/best_index_to_move.h @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* best_index_to_move.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/08 14:18:43 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 14:20:01 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef BEST_INDEX_TO_MOVE_H +# define BEST_INDEX_TO_MOVE_H + +# include "app/best_move.h" +# include "app/state.h" +# include "me/types.h" +# include "me/vec/vec_i64.h" + +struct s_best_index_to_move +{ + t_state *state; + t_vec_i64 *from; + t_vec_i64 *to; + t_iter_pos_func f; + t_usize i; +}; + +#endif /* BEST_INDEX_TO_MOVE_H */ diff --git a/include/app/best_move.h b/include/app/best_move.h new file mode 100644 index 0000000..f78fb2b --- /dev/null +++ b/include/app/best_move.h @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* best_move.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/29 20:10:21 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 13:30:30 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef BEST_MOVE_H +# define BEST_MOVE_H + +# include "app/state.h" +# include "me/vec/vec_i64.h" + +struct s_functions +{ + void (*forward)(void *); + void (*reverse)(void *); +}; + +typedef t_usize (*t_iter_pos_func)(t_vec_i64 *); + +typedef struct s_best_move_args +{ + t_iter_pos_func iter_func; + t_usize index; + t_vec_i64 *from; + t_vec_i64 *to; + void *args; + struct s_functions main; + struct s_functions other; + struct s_functions both; + bool print_stuff; +} t_best_move_args; + +void run_func_with_best_rotate_for_item(t_state *state, + t_best_move_args data); + +#endif /* BEST_MOVE_H */ diff --git a/include/app/best_move_inner.h b/include/app/best_move_inner.h new file mode 100644 index 0000000..902c125 --- /dev/null +++ b/include/app/best_move_inner.h @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* best_move_inner.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/29 21:31:42 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 14:22:49 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef BEST_MOVE_INNER_H +# define BEST_MOVE_INNER_H + +# include "app/best_move.h" +# include "app/rotate.h" +# include "app/state.h" +# include "me/types.h" +# include "me/vec/vec_i64.h" + +typedef void (*t_banana_func)(void *); + +static inline t_banana_func choose_func(struct s_functions *funcs, + t_rotation rotate) +{ + if (rotate.direction == FORWARD) + return (funcs->forward); + else if (rotate.direction == REVERSE) + return (funcs->reverse); + return (NULL); +} + +static inline t_isize abs_diff(t_isize lhs, t_isize rhs) +{ + if (lhs > rhs) + return (lhs - rhs); + else + return (rhs - lhs); +} + +static inline t_isize min(t_isize lhs, t_isize rhs) +{ + if (lhs > rhs) + return (rhs); + else + return (lhs); +} + +static inline t_isize max(t_isize lhs, t_isize rhs) +{ + if (lhs < rhs) + return (rhs); + else + return (lhs); +} + +#endif /* BEST_MOVE_INNER_H */ diff --git a/include/app/find_iter.h b/include/app/find_iter.h new file mode 100644 index 0000000..ff9b460 --- /dev/null +++ b/include/app/find_iter.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* find_iter.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/31 14:24:10 by maiboyer #+# #+# */ +/* Updated: 2024/01/31 14:24:47 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FIND_ITER_H +# define FIND_ITER_H + +# include "me/types.h" +# include "me/vec/vec_i64.h" + +t_usize min_iter_zero_pos(t_vec_i64 *vec); +t_usize max_iter_zero_pos(t_vec_i64 *vec); + +#endif /* FIND_ITER_H */ diff --git a/include/app/find_place.h b/include/app/find_place.h new file mode 100644 index 0000000..234e7b1 --- /dev/null +++ b/include/app/find_place.h @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* find_place.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/29 22:00:27 by maiboyer #+# #+# */ +/* Updated: 2024/01/29 22:15:08 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FIND_PLACE_H +# define FIND_PLACE_H + +# include "app/state.h" +# include "me/types.h" + +typedef struct s_find_place_iter_state +{ + t_usize current_index; + t_usize found_index; + t_i64 to_find_elem; + +} t_find_place_iter_state; + +t_usize find_place(t_i64 elem, t_state *state); + +#endif /* FIND_PLACE_H */ diff --git a/include/app/iter_state.h b/include/app/iter_state.h new file mode 100644 index 0000000..acc14be --- /dev/null +++ b/include/app/iter_state.h @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* iter_state.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/31 14:19:06 by maiboyer #+# #+# */ +/* Updated: 2024/01/31 14:19:50 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ITER_STATE_H +# define ITER_STATE_H + +# include "me/types.h" + +typedef struct s_iter_state +{ + t_usize pos; + t_i64 elem; +} t_iter_state; + +#endif /* ITER_STATE_H */ diff --git a/include/app/moves.h b/include/app/moves.h new file mode 100644 index 0000000..db9b4f7 --- /dev/null +++ b/include/app/moves.h @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* moves.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/11 16:13:08 by maiboyer #+# #+# */ +/* Updated: 2024/01/29 19:07:15 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MOVES_H +# define MOVES_H + +# include "app/state.h" +# include "app/types/type_move.h" +# include "me/types.h" + +void do_move(t_move m, t_state *s); +t_const_str get_str_for_move(t_move m); + +#endif /* MOVES_H */ diff --git a/include/app/rotate.h b/include/app/rotate.h new file mode 100644 index 0000000..f393485 --- /dev/null +++ b/include/app/rotate.h @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rotate.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/29 19:00:18 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 13:33:09 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ROTATE_H +# define ROTATE_H + +# include "me/types.h" + +enum e_rotation_direction +{ + FORWARD, + REVERSE, +}; +typedef struct s_rotation +{ + t_isize value; + t_isize ring_size; + enum e_rotation_direction direction; +} t_rotation; + +static inline t_rotation forward(t_isize by, t_isize ring_size) +{ + return ((t_rotation){.value = by, .ring_size = ring_size, + .direction = FORWARD}); +} + +static inline t_rotation reverse(t_isize by, t_isize ring_size) +{ + return ((t_rotation){.value = by, .ring_size = ring_size, + .direction = REVERSE}); +} + +static inline t_rotation flip(t_rotation rot) +{ + enum e_rotation_direction flipped; + + if (rot.direction == FORWARD) + flipped = REVERSE; + else if (rot.direction == REVERSE) + flipped = FORWARD; + else + return (rot); + return ((t_rotation){.value = rot.ring_size - rot.value, + .ring_size = rot.ring_size, .direction = flipped}); +} + +#endif /* ROTATE_H */ diff --git a/include/app/state.h b/include/app/state.h new file mode 100644 index 0000000..2261e43 --- /dev/null +++ b/include/app/state.h @@ -0,0 +1,73 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* state.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/11 14:27:25 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 19:05:37 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STATE_H +# define STATE_H + +# include "me/types.h" +# include "me/vec/vec_i64.h" +# include "me/vec/vec_i64_bool.h" + +# ifndef BONUS +# define BONUS 0 +# endif + +typedef struct s_state +{ + t_vec_i64_bool sorted; + t_vec_i64 stack_a; + t_vec_i64 stack_b; +} t_state; + +t_state parses_arguments(t_usize count, t_str nums[]); +void free_state(t_state state); + +static inline void make_sorted_true_for_elem(t_state *s, t_i64 elem) +{ + t_usize i; + + i = 0; + while (i < s->sorted.len) + { + if (s->sorted.buffer[i].value == elem) + { + s->sorted.buffer[i].active = true; + return ; + } + i++; + } +} + +static inline void make_sorted_true_from_stack(t_state *s, t_vec_i64 *stack) +{ + t_usize i; + + i = 0; + while (i < stack->len) + { + make_sorted_true_for_elem(s, stack->buffer[i++]); + } +} + +static inline void make_sorted_all_false(t_state *s) +{ + t_usize i; + + i = 0; + while (i < s->sorted.len) + { + s->sorted.buffer[i].active = false; + i++; + } +} + +#endif /* STATE_H */ diff --git a/include/app/target.h b/include/app/target.h new file mode 100644 index 0000000..204044e --- /dev/null +++ b/include/app/target.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* target.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/29 20:30:43 by maiboyer #+# #+# */ +/* Updated: 2024/01/29 20:31:28 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef TARGET_H +# define TARGET_H + +# include "app/rotate.h" +# include "me/types.h" + +t_rotation target(t_usize from, t_usize to, t_usize ring_size); + +#endif /* TARGET_H */ diff --git a/include/app/types/type_i64_bool.h b/include/app/types/type_i64_bool.h new file mode 100644 index 0000000..48af825 --- /dev/null +++ b/include/app/types/type_i64_bool.h @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* type_i64_bool.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/29 19:14:52 by maiboyer #+# #+# */ +/* Updated: 2024/01/29 19:15:56 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef TYPE_I64_BOOL_H +# define TYPE_I64_BOOL_H + +# include "me/types.h" + +typedef struct s_i64_bool +{ + t_i64 value; + bool active; +} t_i64_bool; + +#endif /* TYPE_I64_BOOL_H */ diff --git a/include/app/types/type_move.h b/include/app/types/type_move.h new file mode 100644 index 0000000..761a82a --- /dev/null +++ b/include/app/types/type_move.h @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* type_move.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/12 21:26:56 by maiboyer #+# #+# */ +/* Updated: 2024/01/12 21:27:08 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef TYPE_MOVE_H +# define TYPE_MOVE_H + +typedef enum e_move +{ + SWAP_A = 1 << 0, + SWAP_B = 1 << 1, + SWAP_BOTH = SWAP_A | SWAP_B, + PUSH_A = 1 << 2, + PUSH_B = 1 << 3, + ROTATE_A = 1 << 4, + ROTATE_B = 1 << 5, + ROTATE_BOTH = ROTATE_A | ROTATE_B, + REVERSE_ROTATE_A = 1 << 6, + REVERSE_ROTATE_B = 1 << 7, + REVERSE_ROTATE_BOTH = REVERSE_ROTATE_A | REVERSE_ROTATE_B, +} t_move; + +#endif /* TYPE_MOVE_H */ diff --git a/include/me/buffered_str/buf_str.h b/include/me/buffered_str/buf_str.h new file mode 100644 index 0000000..6bf1092 --- /dev/null +++ b/include/me/buffered_str/buf_str.h @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* buf_str.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/16 17:54:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/31 15:34:29 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef BUF_STR_H +# define BUF_STR_H +# include "me/types.h" + +typedef struct s_buffer_str +{ + t_str buf; + t_usize capacity; + t_usize len; +} t_buffer_str; + +bool push_str_buffer(t_buffer_str *buf, t_const_str to_push); +bool push_str_char(t_buffer_str *buf, char to_push); +void str_clear(t_buffer_str *buf); +t_buffer_str alloc_new_buffer(t_usize capacity); + +static inline void str_free(t_buffer_str buf) +{ + void free(void *); + + free(buf.buf); +} + +static inline char str_pop(t_buffer_str *buf) +{ + char c; + + c = '\0'; + if (buf->buf && buf->len) + { + c = buf->buf[buf->len - 1]; + buf->buf[buf->len - 1] = '\0'; + } + return (c); +} + +#endif diff --git a/include/me/char/isalnum.h b/include/me/char/isalnum.h new file mode 100644 index 0000000..f38c8e8 --- /dev/null +++ b/include/me/char/isalnum.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* isalnum.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 16:19:40 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:39:39 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ISALNUM_H +# define ISALNUM_H + +# include "me/types.h" + +bool me_isalnum(char chr); + +#endif diff --git a/include/me/char/isalpha.h b/include/me/char/isalpha.h new file mode 100644 index 0000000..680e197 --- /dev/null +++ b/include/me/char/isalpha.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* isalpha.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 16:19:40 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:39:36 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ISALPHA_H +# define ISALPHA_H + +# include "me/types.h" + +bool me_isalpha(char chr); + +#endif diff --git a/include/me/char/isascii.h b/include/me/char/isascii.h new file mode 100644 index 0000000..dfa1961 --- /dev/null +++ b/include/me/char/isascii.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* isascii.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 17:51:01 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:39:33 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ISASCII_H +# define ISASCII_H + +# include "me/types.h" + +bool me_isascii(char chr); + +#endif diff --git a/include/me/char/isdigit.h b/include/me/char/isdigit.h new file mode 100644 index 0000000..fbed927 --- /dev/null +++ b/include/me/char/isdigit.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* isdigit.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 16:19:40 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:41:55 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ISDIGIT_H +# define ISDIGIT_H + +# include "me/types.h" + +bool me_isdigit(char chr); + +#endif diff --git a/include/me/char/islower.h b/include/me/char/islower.h new file mode 100644 index 0000000..de8eedf --- /dev/null +++ b/include/me/char/islower.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* islower.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 16:19:40 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:48:25 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ISLOWER_H +# define ISLOWER_H + +# include "me/types.h" + +bool me_islower(char chr); + +#endif diff --git a/include/me/char/isprint.h b/include/me/char/isprint.h new file mode 100644 index 0000000..30c6b6f --- /dev/null +++ b/include/me/char/isprint.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* isprint.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 16:19:40 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:44:49 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ISPRINT_H +# define ISPRINT_H + +# include "me/types.h" + +bool me_isprint(char chr); + +#endif diff --git a/include/me/char/isspace.h b/include/me/char/isspace.h new file mode 100644 index 0000000..b2aa5c5 --- /dev/null +++ b/include/me/char/isspace.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* isspace.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 14:26:25 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:45:02 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ISSPACE_H +# define ISSPACE_H + +# include "me/types.h" + +bool me_isspace(char chr); + +#endif diff --git a/include/me/char/isupper.h b/include/me/char/isupper.h new file mode 100644 index 0000000..4c485dd --- /dev/null +++ b/include/me/char/isupper.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* isupper.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 16:19:40 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:45:16 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ISUPPER_H +# define ISUPPER_H + +# include "me/types.h" + +bool me_isupper(char chr); + +#endif diff --git a/include/me/char/tolower.h b/include/me/char/tolower.h new file mode 100644 index 0000000..764f5a4 --- /dev/null +++ b/include/me/char/tolower.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tolower.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 16:47:50 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:45:33 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef TOLOWER_H +# define TOLOWER_H + +# include "me/types.h" + +bool me_tolower(char chr); + +#endif diff --git a/include/me/char/toupper.h b/include/me/char/toupper.h new file mode 100644 index 0000000..0e5a3a0 --- /dev/null +++ b/include/me/char/toupper.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* toupper.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 16:47:50 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:45:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef TOUPPER_H +# define TOUPPER_H + +# include "me/types.h" + +bool me_toupper(char chr); + +#endif diff --git a/include/me/convert/atoi.h b/include/me/convert/atoi.h new file mode 100644 index 0000000..1cce430 --- /dev/null +++ b/include/me/convert/atoi.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* atoi.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 14:14:00 by maiboyer #+# #+# */ +/* Updated: 2024/01/11 15:36:12 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ATOI_H +# define ATOI_H + +# include "me/types.h" + +t_i32 me_atoi(t_const_str str); +t_i64 me_atoi_64(t_const_str str); + +#endif diff --git a/include/me/convert/itoa.h b/include/me/convert/itoa.h new file mode 100644 index 0000000..a4afbf6 --- /dev/null +++ b/include/me/convert/itoa.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* itoa.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/03 21:05:46 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:48:37 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ITOA_H +# define ITOA_H + +# include "me/types.h" + +t_str me_itoa(t_i32 nb); + +#endif diff --git a/include/me/convert/str_to_numbers.h b/include/me/convert/str_to_numbers.h new file mode 100644 index 0000000..9f90ddf --- /dev/null +++ b/include/me/convert/str_to_numbers.h @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_numbers.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:08:13 by maiboyer #+# #+# */ +/* Updated: 2024/02/02 00:22:50 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR_TO_NUMBERS_H +# define STR_TO_NUMBERS_H + +# include "me/types.h" + +t_error str_to_isize(t_const_str str, t_u32 radix, t_isize *out); +t_error str_to_i64(t_const_str str, t_u32 radix, t_i64 *out); +t_error str_to_i32(t_const_str str, t_u32 radix, t_i32 *out); +t_error str_to_i16(t_const_str str, t_u32 radix, t_i16 *out); +t_error str_to_i8(t_const_str str, t_u32 radix, t_i8 *out); + +t_error str_to_usize(t_const_str str, t_u32 radix, t_usize *out); +t_error str_to_u64(t_const_str str, t_u32 radix, t_u64 *out); +t_error str_to_u32(t_const_str str, t_u32 radix, t_u32 *out); +t_error str_to_u16(t_const_str str, t_u32 radix, t_u16 *out); +t_error str_to_u8(t_const_str str, t_u32 radix, t_u8 *out); + +#endif /* STR_TO_NUMBERS_H */ diff --git a/include/me/fs/close.h b/include/me/fs/close.h new file mode 100644 index 0000000..c764d79 --- /dev/null +++ b/include/me/fs/close.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* close.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 15:56:56 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:49:22 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef CLOSE_H +# define CLOSE_H + +# include "me/types.h" + +bool me_close(t_file file, t_i32 *error); + +#endif diff --git a/include/me/fs/open.h b/include/me/fs/open.h new file mode 100644 index 0000000..1f2f8f0 --- /dev/null +++ b/include/me/fs/open.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* open.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/03 15:29:38 by maiboyer #+# #+# */ +/* Updated: 2024/01/05 16:43:37 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef OPEN_H +# define OPEN_H + +# include "me/types.h" + +t_error me_open(t_const_str path, bool read, bool write, t_file *file_out); +t_error me_open_truncate(t_const_str path, t_file *file_out); +t_error me_open_create(t_const_str path, t_file *file_out); + +#endif diff --git a/include/me/fs/putchar_fd.h b/include/me/fs/putchar_fd.h new file mode 100644 index 0000000..03039ac --- /dev/null +++ b/include/me/fs/putchar_fd.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* putchar_fd.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 04:42:45 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:49:06 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef PUTCHAR_FD_H +# define PUTCHAR_FD_H + +# include "me/types.h" + +void me_putchar_fd(char chr, t_file file); + +#endif diff --git a/include/me/fs/putendl_fd.h b/include/me/fs/putendl_fd.h new file mode 100644 index 0000000..0325e1e --- /dev/null +++ b/include/me/fs/putendl_fd.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* putendl_fd.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 04:42:45 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:31:54 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef PUTENDL_FD_H +# define PUTENDL_FD_H + +# include "me/types.h" + +void me_putendl_fd(t_str str, t_file file); + +#endif \ No newline at end of file diff --git a/include/me/fs/putnbr_fd.h b/include/me/fs/putnbr_fd.h new file mode 100644 index 0000000..933e945 --- /dev/null +++ b/include/me/fs/putnbr_fd.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* putnbr_fd.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 12:45:06 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:32:41 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef PUTNBR_FD_H +# define PUTNBR_FD_H + +# include "me/types.h" + +void me_putnbr_fd(t_i32 n, t_file file); + +#endif \ No newline at end of file diff --git a/include/me/fs/putstr_fd.h b/include/me/fs/putstr_fd.h new file mode 100644 index 0000000..5ada222 --- /dev/null +++ b/include/me/fs/putstr_fd.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* putstr_fd.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 04:42:45 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:35:53 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef PUTSTR_FD_H +# define PUTSTR_FD_H + +# include "me/types.h" + +void me_putstr_fd(t_str str, t_file file); + +#endif diff --git a/include/me/fs/read.h b/include/me/fs/read.h new file mode 100644 index 0000000..7fc2cd7 --- /dev/null +++ b/include/me/fs/read.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* read.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/03 15:21:19 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:37:03 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef READ_H +# define READ_H + +# include "me/types.h" + +t_usize me_read(t_file fd, t_u8 *buffer, t_i64 buffer_max, bool *eof_out); + +#endif \ No newline at end of file diff --git a/include/me/fs/read_to_vec.h b/include/me/fs/read_to_vec.h new file mode 100644 index 0000000..94375ae --- /dev/null +++ b/include/me/fs/read_to_vec.h @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* read_to_vec.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/24 18:50:37 by maiboyer #+# #+# */ +/* Updated: 2023/12/24 18:57:36 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef READ_TO_VEC_H +# define READ_TO_VEC_H + +# include "me/types.h" +# include "me/vec/vec_u8.h" + +# ifndef READ_BUFFER_SIZE +# define READ_BUFFER_SIZE 4096 +# endif + +bool read_to_vec(t_const_str path, t_vec_u8 *out); + +#endif diff --git a/include/me/fs/write.h b/include/me/fs/write.h new file mode 100644 index 0000000..66ea307 --- /dev/null +++ b/include/me/fs/write.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* write.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/03 15:27:33 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:36:48 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef WRITE_H +# define WRITE_H + +# include "me/types.h" + +bool me_write(t_file fd, t_u8 *buffer, t_i64 size); + +#endif diff --git a/include/me/gnl/gnl.h b/include/me/gnl/gnl.h new file mode 100644 index 0000000..2933c2d --- /dev/null +++ b/include/me/gnl/gnl.h @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* gnl.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/23 14:40:15 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 19:21:56 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef GNL_H +# define GNL_H + +# ifndef BUFFER_SIZE +# define BUFFER_SIZE 128 +# endif + +# ifndef BUFFER_LENGTH +# define BUFFER_LENGTH 512 +# endif + +# include "me/buffered_str/buf_str.h" +# include "me/types.h" + +typedef struct s_static_buffer +{ + t_file fd; + bool used; + char buf[BUFFER_SIZE + 1]; + bool init; +} t_static_buffer; + +typedef struct s_copy_flags +{ + bool error; + bool empty_read; +} t_copy_flags; + +t_buffer_str get_next_line(t_file fd, bool *error); + +#endif diff --git a/include/me/hash/hasher.h b/include/me/hash/hasher.h new file mode 100644 index 0000000..d9a9c1c --- /dev/null +++ b/include/me/hash/hasher.h @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* hasher.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maix +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/10 19:50:39 by maix #+# #+# */ +/* Updated: 2023/12/27 16:44:09 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef HASHER_H +# define HASHER_H + +# include "me/types.h" + +typedef void (*t_hash_bytes)(void *hasher, t_u8 *bytes, + t_usize count); +typedef t_u64 (*t_hasher_finish)(void *hasher); +typedef t_u64 (*t_hasher_reset_and_finish)(void *hasher); + +typedef struct s_hasher +{ + void *hasher; + t_hash_bytes hash_bytes; + t_hasher_finish finish; + t_hasher_reset_and_finish reset_and_finish; + +} t_hasher; + +void hasher_write_bytes(t_hasher *hasher, + t_u8 *bytes, t_usize count); + +void hasher_write_u8(t_hasher *hasher, t_u8 n); +void hasher_write_u16(t_hasher *hasher, t_u16 n); +void hasher_write_u32(t_hasher *hasher, t_u32 n); +void hasher_write_u64(t_hasher *hasher, t_u64 n); +void hasher_write_usize(t_hasher *hasher, t_usize n); + +void hasher_write_i8(t_hasher *hasher, t_i8 n); +void hasher_write_i16(t_hasher *hasher, t_i16 n); +void hasher_write_i32(t_hasher *hasher, t_i32 n); +void hasher_write_i64(t_hasher *hasher, t_i64 n); +void hasher_write_isize(t_hasher *hasher, t_isize n); + +void hasher_write_str(t_hasher *hasher, t_str str); + +t_u64 hasher_finish(t_hasher *hasher); +t_u64 hasher_reset_and_finish(t_hasher *hasher); + +#endif diff --git a/include/me/hash/sip.h b/include/me/hash/sip.h new file mode 100644 index 0000000..3fed310 --- /dev/null +++ b/include/me/hash/sip.h @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sip.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maix +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/10 19:33:46 by maix #+# #+# */ +/* Updated: 2023/12/11 15:10:07 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef SIP_H +# define SIP_H +# include "me/hash/hasher.h" +# include "me/types.h" + +typedef struct s_sip_state +{ + t_u64 v0; + t_u64 v2; + t_u64 v1; + t_u64 v3; +} t_sip_state; + +typedef struct s_sip13 +{ + t_u64 k0; + t_u64 k1; + t_usize length; + t_u64 tail; + t_usize ntail; + t_sip_state state; +} t_sip13; + +t_hasher hasher_sip13_new(void); + +#endif diff --git a/include/me/hash/sip/sip_utils.h b/include/me/hash/sip/sip_utils.h new file mode 100644 index 0000000..2843533 --- /dev/null +++ b/include/me/hash/sip/sip_utils.h @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sip_utils.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maix +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/10 19:34:10 by maix #+# #+# */ +/* Updated: 2023/12/27 16:48:51 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef SIP_UTILS_H +# define SIP_UTILS_H +# include "me/hash/sip.h" +# include "me/types.h" + +void compress(t_sip_state *state); +t_sip_state create_state_with_key(t_u64 k0, t_u64 k1); +t_u64 sip13_finish(t_sip13 *hasher); +t_u64 sip13_reset_and_finish(t_sip13 *hasher); +void sip13_write_bytes(t_sip13 *self, t_u8 *msg, t_usize count); + +#endif diff --git a/include/me/img/qoi.h b/include/me/img/qoi.h new file mode 100644 index 0000000..d679be6 --- /dev/null +++ b/include/me/img/qoi.h @@ -0,0 +1,305 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* qoi.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/24 17:39:36 by maiboyer #+# #+# */ +/* Updated: 2023/12/25 18:35:05 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* + +Copyright (c) 2021, Dominic Szablewski - https://phoboslab.org +SPDX-License-Identifier: MIT + + +QOI - The "Quite OK Image" format for fast, lossless image compression + +-- About + +QOI encodes and decodes images in a lossless format. Compared to stb_image and +stb_image_write QOI offers 20x-50x faster encoding, 3x-4x faster decoding and +20% better compression. + + +-- Synopsis + +// Define `QOI_IMPLEMENTATION` in *one* C/C++ file before including this +// library to create the implementation. + +#define QOI_IMPLEMENTATION +#include "qoi.h" + +// Encode and store an RGBA buffer to the file system. The t_qoi_desc describes +// the input pixel data. +qoi_write("image_new.qoi", rgba_pixels, &(t_qoi_desc){ + .width = 1920, + .height = 1080, + .channels = 4, + .colorspace = QOI_SRGB +}); + +// Load and decode a QOI image from the file system t_i32o a 32bbp RGBA buffer. +// The t_qoi_desc struct will be filled with the width, height, + number of channels +// and colorspace read from the file header. +t_qoi_desc desc; +void *rgba_pixels = qoi_read("image.qoi", &desc, 4); + + + +-- Documentation + +This library provides the following functions; +- qoi_read -- read and decode a QOI file +- qoi_decode -- decode the raw bytes of a QOI image from memory +- qoi_write -- encode and write a QOI file +- qoi_encode -- encode an rgba buffer t_i32o a QOI image in memory + +See the function declaration below for the signature and more information. + +If you don't want/need the qoi_read and qoi_write functions, you can define +QOI_NO_STDIO before including this library. + +This library uses malloc() and free(). To supply your own malloc implementation +you can define QOI_MALLOC and QOI_FREE before including this library. + +This library uses memset() to zero-initialize the index. To supply your own +implementation you can define QOI_ZEROARR before including this library. + + +-- Data Format + +A QOI file has a 14 byte header, followed by any number of data "chunks" and an +8-byte end marker. + +struct qoi_header_t { + char magic[4]; // magic bytes "qoif" + ut_i3232_t width; // image width in pixels (BE) + ut_i3232_t height; // sprite.height in pixels (BE) + ut_i328_t channels; // 3 = RGB, 4 = RGBA + ut_i328_t colorspace; // 0 = sRGB with linear alpha, 1 = all channels +linear +}; + +Images are encoded row by row, left to right, top to bottom. The decoder and +encoder start with {r: 0, g: 0, b: 0, a: 255} as the previous pixel value. An +image is complete when all pixels specified by width * height have been covered. + +Pixels are encoded as + - a run of the previous pixel + - an index t_i32o an array of previously seen pixels + - a difference to the previous pixel value in r,g,b + - full r,g,b or r,g,b,a values + +The color channels are assumed to not be premultiplied with the alpha channel +("un-premultiplied alpha"). + +A running array[64] (zero-initialized) of previously seen pixel values is +mat_i32ained by the encoder and decoder. Each pixel that is seen by the encoder +and decoder is put t_i32o this array at the position formed by a hash function +of the color value. In the encoder, if the pixel value at the index matches the +current pixel, this index position is written to the stream as QOI_OP_INDEX. +The hash function for the index is: + + index_position = (r * 3 + g * 5 + b * 7 + a * 11) % 64 + +Each chunk starts with a 2- or 8-bit tag, followed by a number of data bits. The +bit length of chunks is divisible by 8 - i.e. all chunks are byte aligned. All +values encoded in these data bits have the most significant bit on the left. + +The 8-bit tags have precedence over the 2-bit tags. A decoder must check for the +presence of an 8-bit tag first. + +The byte stream's end is marked with 7 0x00 bytes followed a single 0x01 byte. + + +The possible chunks are: + + +.- QOI_OP_INDEX ----------. +| Byte[0] | +| 7 6 5 4 3 2 1 0 | +|-------+-----------------| +| 0 0 | index | +`-------------------------` +2-bit tag b00 +6-bit index t_i32o the color index array: 0..63 + +A valid encoder must not issue 2 or more consecutive QOI_OP_INDEX chunks to the +same index. QOI_OP_RUN should be used instead. + + +.- QOI_OP_DIFF -----------. +| Byte[0] | +| 7 6 5 4 3 2 1 0 | +|-------+-----+-----+-----| +| 0 1 | dr | dg | db | +`-------------------------` +2-bit tag b01 +2-bit red channel difference from the previous pixel between -2..1 +2-bit green channel difference from the previous pixel between -2..1 +2-bit blue channel difference from the previous pixel between -2..1 + +The difference to the current channel values are using a wraparound operation, +so "1 - 2" will result in 255, while "255 + 1" will result in 0. + +Values are stored as unsigned t_i32egers with a bias of 2. E.g. -2 is stored as +0 (b00). 1 is stored as 3 (b11). + +The alpha value remains unchanged from the previous pixel. + + +.- QOI_OP_LUMA -------------------------------------. +| Byte[0] | Byte[1] | +| 7 6 5 4 3 2 1 0 | 7 6 5 4 3 2 1 0 | +|-------+-----------------+-------------+-----------| +| 1 0 | green diff | dr - dg | db - dg | +`---------------------------------------------------` +2-bit tag b10 +6-bit green channel difference from the previous pixel -32..31 +4-bit red channel difference minus green channel difference -8..7 +4-bit blue channel difference minus green channel difference -8..7 + +The green channel is used to indicate the general direction of change and is +encoded in 6 bits. The red and blue channels (dr and db) base their diffs off +of the green channel difference and are encoded in 4 bits. I.e.: + dr_dg = (cur_px.r - prev_px.r) - (cur_px.g - prev_px.g) + db_dg = (cur_px.b - prev_px.b) - (cur_px.g - prev_px.g) + +The difference to the current channel values are using a wraparound operation, +so "10 - 13" will result in 253, while "250 + 7" will result in 1. + +Values are stored as unsigned t_i32egers with a bias of 32 for the green channel +and a bias of 8 for the red and blue channel. + +The alpha value remains unchanged from the previous pixel. + + +.- QOI_OP_RUN ------------. +| Byte[0] | +| 7 6 5 4 3 2 1 0 | +|-------+-----------------| +| 1 1 | run | +`-------------------------` +2-bit tag b11 +6-bit run-length repeating the previous pixel: 1..62 + +The run-length is stored with a bias of -1. Note that the run-lengths 63 and 64 +(b111110 and b111111) are illegal as they are occupied by the QOI_OP_RGB and +QOI_OP_RGBA tags. + + +.- QOI_OP_RGB ------------------------------------------. +| Byte[0] | Byte[1] | Byte[2] | Byte[3] | +| 7 6 5 4 3 2 1 0 | 7 .. 0 | 7 .. 0 | 7 .. 0 | +|-------------------------+---------+---------+---------| +| 1 1 1 1 1 1 1 0 | red | green | blue | +`-------------------------------------------------------` +8-bit tag b11111110 +8-bit red channel value +8-bit green channel value +8-bit blue channel value + +The alpha value remains unchanged from the previous pixel. + + +.- QOI_OP_RGBA ---------------------------------------------------. +| Byte[0] | Byte[1] | Byte[2] | Byte[3] | Byte[4] | +| 7 6 5 4 3 2 1 0 | 7 .. 0 | 7 .. 0 | 7 .. 0 | 7 .. 0 | +|-------------------------+---------+---------+---------+---------| +| 1 1 1 1 1 1 1 1 | red | green | blue | alpha | +`-----------------------------------------------------------------` +8-bit tag b11111111 +8-bit red channel value +8-bit green channel value +8-bit blue channel value +8-bit alpha channel value + +*/ + +/* ----------------------------------------------------------------------------- +Header - Public functions */ + +#ifndef QOI_H +# define QOI_H + +# include "me/mem/mem_alloc.h" +# include "me/mem/mem_set_zero.h" +# include "me/types.h" +/* A pot_i32er to a t_qoi_desc struct has to be supplied to all of qoi's +functions. It describes either the input format (for qoi_write and qoi_encode), +or is filled with the description read from the file header (for qoi_read and +qoi_decode). + +The colorspace in this t_qoi_desc is an enum where + 0 = sRGB, i.e. gamma scaled RGB channels and a linear alpha channel + 1 = all channels are linear +You may use the constants QOI_SRGB or QOI_LINEAR. The colorspace is purely +informative. It will be saved to the file header, but does not affect +how chunks are en-/decoded. */ + +# define QOI_SRGB 0 +# define QOI_LINEAR 1 + +typedef struct s_t_qoi_desc +{ + t_u32 width; + t_u32 height; + t_u8 channels; + t_u8 colorspace; +} t_qoi_desc; + +# ifndef QOI_NO_STDIO + +/* Encode raw RGB or RGBA pixels t_i32o a QOI image and write it to the file +system. The t_qoi_desc struct must be filled with the image width, height, +number of channels (3 = RGB, 4 = RGBA) and the colorspace. + +The function returns 0 on failure (invalid parameters, or fopen or malloc +failed) or the number of bytes written on success. */ + +t_i32 qoi_write(t_const_str filename, const void *data, + const t_qoi_desc *desc); + +/* Read and decode a QOI image from the file system. If channels is 0, the +number of channels from the file header is used. If channels is 3 or 4 the +output format will be forced t_i32o this number of channels. + +The function either returns NULL on failure (invalid data, or malloc or fopen +failed) or a pot_i32er to the decoded pixels. On success, the t_qoi_desc struct +will be filled with the description from the file header. + +The returned pixel data should be free()d after use. */ + +void *qoi_read(t_const_str filename, t_qoi_desc *desc, + t_i32 channels); + +# endif /* QOI_NO_STDIO */ + +/* Encode raw RGB or RGBA pixels t_i32o a QOI image in memory. + +The function either returns NULL on failure (invalid parameters or malloc +failed) or a pot_i32er to the encoded data on success. On success the out_len +is set to the size in bytes of the encoded data. + +The returned qoi data should be free()d after use. */ + +void *qoi_encode(const void *data, const t_qoi_desc *desc, + t_i32 *out_len); + +/* Decode a QOI image from memory. + +The function either returns NULL on failure (invalid parameters or malloc +failed) or a pot_i32er to the decoded pixels. On success, the t_qoi_desc struct +is filled with the description from the file header. + +The returned pixel data should be free()d after use. */ + +void *qoi_decode(const void *data, t_i32 size, t_qoi_desc *desc, + t_i32 channels); + +#endif /* QOI_H */ diff --git a/include/me/img/qoi/qoi_decode.h b/include/me/img/qoi/qoi_decode.h new file mode 100644 index 0000000..d11ebe9 --- /dev/null +++ b/include/me/img/qoi/qoi_decode.h @@ -0,0 +1,86 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* qoi_decode.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/25 22:33:19 by maiboyer #+# #+# */ +/* Updated: 2023/12/25 22:58:27 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef QOI_DECODE_H +# define QOI_DECODE_H + +# include "me/img/qoi.h" +# include "me/img/qoi/qoi_utils.h" + +typedef struct s_decode_vals +{ + t_i32 px_len; + t_i32 chunks_len; + t_i32 px_pos; + t_i32 p; + t_i32 run; + t_i32 b1; + t_i32 b2; + t_i32 vg; + t_u32 header_magic; + t_qoi_rgba index[64]; + t_qoi_rgba px; + +} t_decode_vals; + +static inline void qoi_op_luma_decode(t_decode_vals *vals, const t_u8 *bytes) +{ + vals->b2 = bytes[vals->p++]; + vals->vg = (vals->b1 & 0x3f) - 32; + vals->px.rgba.r += vals->vg - 8 + ((vals->b2 >> 4) & 0x0f); + vals->px.rgba.g += vals->vg; + vals->px.rgba.b += vals->vg - 8 + (vals->b2 & 0x0f); +} + +static inline void qoi_op_diff_decode(t_decode_vals *vals, const t_u8 *bytes) +{ + (void)(bytes); + vals->px.rgba.r += ((vals->b1 >> 4) & 0x03) - 2; + vals->px.rgba.g += ((vals->b1 >> 2) & 0x03) - 2; + vals->px.rgba.b += (vals->b1 & 0x03) - 2; +} + +static inline void qoi_op_rgba_decode(t_decode_vals *vals, const t_u8 *bytes) +{ + vals->px.rgba.r = bytes[vals->p++]; + vals->px.rgba.g = bytes[vals->p++]; + vals->px.rgba.b = bytes[vals->p++]; + vals->px.rgba.a = bytes[vals->p++]; +} + +static inline void qoi_op_rgb_decode(t_decode_vals *vals, const t_u8 *bytes) +{ + vals->px.rgba.r = bytes[vals->p++]; + vals->px.rgba.g = bytes[vals->p++]; + vals->px.rgba.b = bytes[vals->p++]; +} + +static inline void qoi_decode_inner_inner(t_decode_vals *vals, \ + const t_u8 *bytes) +{ + vals->b1 = bytes[vals->p++]; + if (vals->b1 == QOI_OP_RGB) + qoi_op_rgb_decode(vals, bytes); + else if (vals->b1 == QOI_OP_RGBA) + qoi_op_rgba_decode(vals, bytes); + else if ((vals->b1 & QOI_MASK_2) == QOI_OP_INDEX) + vals->px = vals->index[vals->b1]; + else if ((vals->b1 & QOI_MASK_2) == QOI_OP_DIFF) + qoi_op_diff_decode(vals, bytes); + else if ((vals->b1 & QOI_MASK_2) == QOI_OP_LUMA) + qoi_op_luma_decode(vals, bytes); + else if ((vals->b1 & QOI_MASK_2) == QOI_OP_RUN) + vals->run = (vals->b1 & 0x3f); + vals->index[qoi_color_hash(vals->px) % 64] = vals->px; +} + +#endif /* QOI_DECODE_H */ diff --git a/include/me/img/qoi/qoi_encode.h b/include/me/img/qoi/qoi_encode.h new file mode 100644 index 0000000..bea2a22 --- /dev/null +++ b/include/me/img/qoi/qoi_encode.h @@ -0,0 +1,155 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* qoi_encode.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/25 22:36:06 by maiboyer #+# #+# */ +/* Updated: 2023/12/25 22:56:36 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef QOI_ENCODE_H +# define QOI_ENCODE_H + +# include "me/img/qoi.h" +# include "me/img/qoi/qoi_utils.h" + +typedef struct s_encode_vals +{ + t_i32 i; + t_i32 max_size; + t_i32 p; + t_i32 run; + t_i32 px_len; + t_i32 px_end; + t_i32 px_pos; + t_i32 channels; + t_i32 index_pos; + t_qoi_rgba index[64]; + t_qoi_rgba px; + t_qoi_rgba px_prev; +} t_encode_vals; + +typedef struct s_v +{ + t_i8 r; + t_i8 g; + t_i8 b; + t_i8 g_r; + t_i8 g_b; +} t_v; + +static inline void qoi_encode_if_thingy1(t_encode_vals *vals, + t_v *v, t_u8 *bytes) +{ + v->r = vals->px.rgba.r - vals->px_prev.rgba.r; + v->g = vals->px.rgba.g - vals->px_prev.rgba.g; + v->b = vals->px.rgba.b - vals->px_prev.rgba.b; + v->g_r = v->r - v->g; + v->g_b = v->b - v->g; + if (v->r > -3 && v->r < 2 && v->g > -3 && v->g < 2 && v->b > -3 + && v->b < 2) + bytes[vals->p++] = QOI_OP_DIFF | (v->r + 2) << 4 | (v->g + + 2) << 2 | (v->b + 2); + else if (v->g_r > -9 && v->g_r < 8 && v->g > -33 && v->g < 32 + && v->g_b > -9 && v->g_b < 8) + { + bytes[vals->p++] = QOI_OP_LUMA | (v->g + 32); + bytes[vals->p++] = (v->g_r + 8) << 4 | (v->g_b + 8); + } + else + { + bytes[vals->p++] = QOI_OP_RGB; + bytes[vals->p++] = vals->px.rgba.r; + bytes[vals->p++] = vals->px.rgba.g; + bytes[vals->p++] = vals->px.rgba.b; + } +} + +static inline void qoi_encode_inner_inner_inner(t_encode_vals *vals, + t_v *v, t_u8 *bytes) +{ + if (vals->run > 0) + { + bytes[vals->p++] = QOI_OP_RUN | (vals->run - 1); + vals->run = 0; + } + vals->index_pos = qoi_color_hash(vals->px) % 64; + if (vals->index[vals->index_pos].v == vals->px.v) + bytes[vals->p++] = QOI_OP_INDEX | vals->index_pos; + else + { + vals->index[vals->index_pos] = vals->px; + if (vals->px.rgba.a == vals->px_prev.rgba.a) + qoi_encode_if_thingy1(vals, v, bytes); + else + { + bytes[vals->p++] = QOI_OP_RGBA; + bytes[vals->p++] = vals->px.rgba.r; + bytes[vals->p++] = vals->px.rgba.g; + bytes[vals->p++] = vals->px.rgba.b; + bytes[vals->p++] = vals->px.rgba.a; + } + } +} + +static inline void qoi_encode_inner_inner(t_encode_vals *vals, + const t_u8 *pixels, t_u8 *bytes) +{ + t_v v; + + v = (t_v){0}; + while (vals->px_pos < vals->px_len) + { + vals->px.rgba.r = pixels[vals->px_pos + 0]; + vals->px.rgba.g = pixels[vals->px_pos + 1]; + vals->px.rgba.b = pixels[vals->px_pos + 2]; + if (vals->channels == 4) + vals->px.rgba.a = pixels[vals->px_pos + 3]; + if (vals->px.v == vals->px_prev.v) + { + vals->run++; + if (vals->run == 62 || vals->px_pos == vals->px_end) + { + bytes[vals->p++] = QOI_OP_RUN | (vals->run - 1); + vals->run = 0; + } + } + else + qoi_encode_inner_inner_inner(vals, &v, bytes); + vals->px_prev = vals->px; + vals->px_pos += vals->channels; + } +} + +static inline void *qoi_encode_inner(t_encode_vals *vals, + const t_qoi_desc *desc, + const t_u8 *pixels, t_i32 *out_len) +{ + t_u8 *bytes; + + bytes = (t_u8 *)mem_alloc(vals->max_size); + if (!bytes) + return (NULL); + qoi_write_32(bytes, &vals->p, QOI_MAGIC); + qoi_write_32(bytes, &vals->p, desc->width); + qoi_write_32(bytes, &vals->p, desc->height); + bytes[vals->p++] = desc->channels; + bytes[vals->p++] = desc->colorspace; + mem_set_zero(vals->index, sizeof(vals->index)); + vals->px_prev.rgba.a = 255; + vals->px = vals->px_prev; + vals->px_len = desc->width * desc->height * desc->channels; + vals->px_end = vals->px_len - desc->channels; + vals->channels = desc->channels; + qoi_encode_inner_inner(vals, pixels, bytes); + vals->i = 0; + while (vals->i < (t_i32) sizeof(t_u8[8])) + bytes[vals->p++] = ((t_u8[8]){0, 0, 0, 0, 0, 0, 0, 1})[vals->i++]; + *out_len = vals->p; + return (bytes); +} + +#endif /* QOI_ENCODE_H */ diff --git a/include/me/img/qoi/qoi_utils.h b/include/me/img/qoi/qoi_utils.h new file mode 100644 index 0000000..543296b --- /dev/null +++ b/include/me/img/qoi/qoi_utils.h @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* qoi_utils.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/24 18:59:56 by maiboyer #+# #+# */ +/* Updated: 2023/12/25 22:33:07 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef QOI_UTILS_H +# define QOI_UTILS_H + +# include "me/img/qoi.h" +# include "me/types.h" + +# define QOI_OP_INDEX 0x00 /* 00xxxxxx */ +# define QOI_OP_DIFF 0x40 /* 01xxxxxx */ +# define QOI_OP_LUMA 0x80 /* 10xxxxxx */ +# define QOI_OP_RUN 0xc0 /* 11xxxxxx */ +# define QOI_OP_RGB 0xfe /* 11111110 */ +# define QOI_OP_RGBA 0xff /* 11111111 */ +# define QOI_MASK_2 0xc0 /* 11000000 */ +# define QOI_MAGIC 0x716f6966u +//(((t_u32)'q') << 24 | ((t_u32)'o') << 16 | ((t_u32)'i') << 8 | ((t_u32)'f')) +# define QOI_HEADER_SIZE 14 +/* 2GB is the max file size that this implementation can safely handle. We guard +against anything larger than that, assuming the worst case with 5 bytes per +pixel, rounded down to a nice clean value. 400 million pixels ought to be +enough for anybody. */ +# define QOI_PIXELS_MAX 400000000u + +typedef union u_qoi_rgba +{ + struct s_qoi_rgba + { + t_u8 r; + t_u8 g; + t_u8 b; + t_u8 a; + } rgba; + t_u32 v; +} t_qoi_rgba; + +void qoi_write_32(t_u8 *bytes, t_i32 *p, t_u32 v); + +t_u32 qoi_read_32(const t_u8 *bytes, t_i32 *p); + +static inline t_u32 qoi_color_hash(t_qoi_rgba c) +{ + return (c.rgba.r * 3 + c.rgba.g * 5 + c.rgba.b * 7 + c.rgba.a * 11); +} + +#endif /* QOI_UTILS_H */ diff --git a/include/me/list/list_add_back.h b/include/me/list/list_add_back.h new file mode 100644 index 0000000..97dd2ef --- /dev/null +++ b/include/me/list/list_add_back.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_add_back.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 20:38:45 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:27:56 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIST_ADD_BACK_H +# define LIST_ADD_BACK_H + +# include "me/types.h" + +void list_add_back(t_list **list, t_list *new); + +#endif diff --git a/include/me/list/list_add_front.h b/include/me/list/list_add_front.h new file mode 100644 index 0000000..8cc7fbc --- /dev/null +++ b/include/me/list/list_add_front.h @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_add_front.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 20:15:23 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:28:15 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIST_ADD_FRONT_H +# define LIST_ADD_FRONT_H + +# include "me/types.h" + +void list_add_front(t_list **lst, t_list *new); +#endif \ No newline at end of file diff --git a/include/me/list/list_alloc_node.h b/include/me/list/list_alloc_node.h new file mode 100644 index 0000000..a84fb05 --- /dev/null +++ b/include/me/list/list_alloc_node.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_alloc_node.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 19:57:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:28:30 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIST_ALLOC_NODE_H +# define LIST_ALLOC_NODE_H + +# include "me/types.h" + +t_list *list_alloc_node(void *content); + +#endif diff --git a/include/me/list/list_free_all.h b/include/me/list/list_free_all.h new file mode 100644 index 0000000..151eb5f --- /dev/null +++ b/include/me/list/list_free_all.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_free_all.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 21:35:20 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:28:47 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIST_FREE_ALL_H +# define LIST_FREE_ALL_H + +# include "me/types.h" + +void list_free_all(t_list **lst, void (*del)(void *)); + +#endif diff --git a/include/me/list/list_free_one.h b/include/me/list/list_free_one.h new file mode 100644 index 0000000..cac9897 --- /dev/null +++ b/include/me/list/list_free_one.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_free_one.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 21:30:20 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:29:15 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIST_FREE_ONE_H +# define LIST_FREE_ONE_H + +# include "me/types.h" + +void list_free_one(t_list *lst, void (*del)(void *)); + +#endif diff --git a/include/me/list/list_get_last.h b/include/me/list/list_get_last.h new file mode 100644 index 0000000..b0172ee --- /dev/null +++ b/include/me/list/list_get_last.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_get_last.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 20:37:08 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:29:37 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIST_GET_LAST_H +# define LIST_GET_LAST_H + +# include "me/types.h" + +t_list *list_get_last(t_list *list); + +#endif \ No newline at end of file diff --git a/include/me/list/list_iter.h b/include/me/list/list_iter.h new file mode 100644 index 0000000..1dc5c0b --- /dev/null +++ b/include/me/list/list_iter.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_iter.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 21:39:05 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:29:51 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIST_ITER_H +# define LIST_ITER_H + +# include "me/types.h" + +void list_iter(t_list *list, void (*f)(void *)); + +#endif diff --git a/include/me/list/list_map.h b/include/me/list/list_map.h new file mode 100644 index 0000000..996f672 --- /dev/null +++ b/include/me/list/list_map.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_map.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 21:40:24 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:30:08 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIST_MAP_H +# define LIST_MAP_H + +# include "me/types.h" + +t_list *list_map(t_list *lst, void *(*f)(void *), void (*del)(void *)); + +#endif \ No newline at end of file diff --git a/include/me/list/list_size.h b/include/me/list/list_size.h new file mode 100644 index 0000000..758131e --- /dev/null +++ b/include/me/list/list_size.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_size.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 20:23:19 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:30:23 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIST_SIZE_H +# define LIST_SIZE_H + +# include "me/types.h" + +t_usize list_size(t_list *lst); + +#endif diff --git a/include/me/mem/mem_alloc.h b/include/me/mem/mem_alloc.h new file mode 100644 index 0000000..1afc6fb --- /dev/null +++ b/include/me/mem/mem_alloc.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_alloc.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/06 14:47:49 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:41:31 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MEM_ALLOC_H +# define MEM_ALLOC_H + +# include "me/types.h" + +void *mem_alloc(t_usize size); + +#endif diff --git a/include/me/mem/mem_alloc_array.h b/include/me/mem/mem_alloc_array.h new file mode 100644 index 0000000..2979ef3 --- /dev/null +++ b/include/me/mem/mem_alloc_array.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_alloc_array.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 15:53:21 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:19:45 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MEM_ALLOC_ARRAY_H +# define MEM_ALLOC_ARRAY_H + +# include "me/types.h" + +void *mem_alloc_array(t_usize item_count, t_usize item_size); + +#endif diff --git a/include/me/mem/mem_compare.h b/include/me/mem/mem_compare.h new file mode 100644 index 0000000..1ae3c3e --- /dev/null +++ b/include/me/mem/mem_compare.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_compare.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:16:02 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:21:14 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MEM_COMPARE_H +# define MEM_COMPARE_H + +# include "me/types.h" + +t_i32 mem_compare(const void *lhs, const void *rhs, t_usize count); + +#endif \ No newline at end of file diff --git a/include/me/mem/mem_copy.h b/include/me/mem/mem_copy.h new file mode 100644 index 0000000..59d7124 --- /dev/null +++ b/include/me/mem/mem_copy.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_copy.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:16:02 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:21:31 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MEM_COPY_H +# define MEM_COPY_H + +# include "me/types.h" + +void *mem_copy(void *destination, const void *source, t_usize count); + +#endif \ No newline at end of file diff --git a/include/me/mem/mem_find.h b/include/me/mem/mem_find.h new file mode 100644 index 0000000..048d844 --- /dev/null +++ b/include/me/mem/mem_find.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_find.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:16:02 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:24:03 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MEM_FIND_H +# define MEM_FIND_H + +# include "me/types.h" + +void *mem_find(void *buf, t_u8 find, t_usize count); + +#endif \ No newline at end of file diff --git a/include/me/mem/mem_find_bytes.h b/include/me/mem/mem_find_bytes.h new file mode 100644 index 0000000..5f220b6 --- /dev/null +++ b/include/me/mem/mem_find_bytes.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_find_bytes.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:16:02 by maiboyer #+# #+# */ +/* Updated: 2024/01/06 18:24:04 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MEM_FIND_BYTES_H +# define MEM_FIND_BYTES_H + +# include "me/types.h" + +void *mem_find_bytes(void *buf, t_u8 *find, t_usize find_count, + t_usize count); + +#endif diff --git a/include/me/mem/mem_move.h b/include/me/mem/mem_move.h new file mode 100644 index 0000000..b2bce61 --- /dev/null +++ b/include/me/mem/mem_move.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_move.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:16:02 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:24:26 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MEM_MOVE_H +# define MEM_MOVE_H + +# include "me/types.h" + +void *mem_move(void *destination, const void *source, t_usize count); + +#endif \ No newline at end of file diff --git a/include/me/mem/mem_set.h b/include/me/mem/mem_set.h new file mode 100644 index 0000000..c6702a1 --- /dev/null +++ b/include/me/mem/mem_set.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_set.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:16:02 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:25:19 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MEM_SET_H +# define MEM_SET_H + +# include "me/types.h" + +void mem_set(void *buf, t_u8 byte, t_usize count); + +#endif \ No newline at end of file diff --git a/include/me/mem/mem_set_zero.h b/include/me/mem/mem_set_zero.h new file mode 100644 index 0000000..dc38393 --- /dev/null +++ b/include/me/mem/mem_set_zero.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_set_zero.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:58:11 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:24:51 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MEM_SET_ZERO_H +# define MEM_SET_ZERO_H + +# include "me/types.h" + +void mem_set_zero(void *buf, t_usize count); + +#endif \ No newline at end of file diff --git a/include/me/num/u16.h b/include/me/num/u16.h new file mode 100644 index 0000000..e2791fd --- /dev/null +++ b/include/me/num/u16.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* u16.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 14:10:03 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 14:16:58 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef U16_H +# define U16_H + +# include "me/types.h" + +t_u16 u16_rotate_left(t_u16 n, t_usize by); +t_u16 u16_rotate_right(t_u16 n, t_usize by); + +#endif diff --git a/include/me/num/u32.h b/include/me/num/u32.h new file mode 100644 index 0000000..4eab4f8 --- /dev/null +++ b/include/me/num/u32.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* u32.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 14:10:03 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 14:17:08 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef U32_H +# define U32_H + +# include "me/types.h" + +t_u32 u32_rotate_left(t_u32 n, t_usize by); +t_u32 u32_rotate_right(t_u32 n, t_usize by); + +#endif diff --git a/include/me/num/u64.h b/include/me/num/u64.h new file mode 100644 index 0000000..5ff2137 --- /dev/null +++ b/include/me/num/u64.h @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* u64.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 14:10:03 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 17:59:34 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef U64_H +# define U64_H + +# include "me/types.h" + +t_u64 u64_rotate_left(t_u64 n, t_usize by); +t_u64 u64_rotate_right(t_u64 n, t_usize by); + +t_u64 u64_from_7bytes(t_u8 *bytes, t_usize start, t_usize len); +t_u64 u64_from_bytes(t_u8 *bytes, t_usize len); + +#endif diff --git a/include/me/num/u8.h b/include/me/num/u8.h new file mode 100644 index 0000000..fb254ae --- /dev/null +++ b/include/me/num/u8.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* u8.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 14:10:03 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 14:17:24 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef U8_H +# define U8_H + +# include "me/types.h" + +t_u8 u8_rotate_left(t_u8 n, t_usize by); +t_u8 u8_rotate_right(t_u8 n, t_usize by); + +#endif diff --git a/include/me/num/usize.h b/include/me/num/usize.h new file mode 100644 index 0000000..2c9cc6b --- /dev/null +++ b/include/me/num/usize.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* usize.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 14:10:03 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 14:17:32 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef USIZE_H +# define USIZE_H + +# include "me/types.h" + +t_usize usize_rotate_left(t_usize n, t_usize by); +t_usize usize_rotate_right(t_usize n, t_usize by); + +#endif diff --git a/include/me/printf/formatter/formatter.h b/include/me/printf/formatter/formatter.h new file mode 100644 index 0000000..67346c5 --- /dev/null +++ b/include/me/printf/formatter/formatter.h @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* formatter.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/16 18:18:19 by maiboyer #+# #+# */ +/* Updated: 2023/11/18 19:11:23 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FORMATTER_H +# define FORMATTER_H +# include "me/printf/printf.h" +# include "me/types.h" + +void printf_x_low(t_printf_arg data, t_printf_func f); +void printf_x_up(t_printf_arg data, t_printf_func f); +void printf_o(t_printf_arg data, t_printf_func f); +void printf_d(t_printf_arg data, t_printf_func f); +void printf_u(t_printf_arg data, t_printf_func f); +void printf_c(t_printf_arg data, t_printf_func f); +void printf_s(t_printf_arg data, t_printf_func f); +void printf_p(t_printf_arg data, t_printf_func f); + +#endif diff --git a/include/me/printf/formatter/utils.h b/include/me/printf/formatter/utils.h new file mode 100644 index 0000000..150d5aa --- /dev/null +++ b/include/me/printf/formatter/utils.h @@ -0,0 +1,76 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utils.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/16 17:58:41 by maiboyer #+# #+# */ +/* Updated: 2023/12/01 21:24:21 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef UTILS_H +# define UTILS_H + +# include "me/printf/matchers/matchers.h" +# include "me/printf/printf.h" +# include "me/types.h" +# include + +typedef struct s_prec_strs +{ + t_str *out; + t_str *pretty; + bool free_out; +} t_prec_strs; + +typedef struct s_pad_and_stuff_args +{ + t_usize fill_zero; + t_usize fill; + t_usize len; + t_usize pretty_len; + t_usize sign_len; + t_str pretty; + t_str str; + t_str sign; + bool allow_zero_fill; + +} t_pad_and_stuff_args; +typedef struct s_pad_inner_args +{ + void *p_args; + t_usize fmt_len; + t_printf_func f; + va_list *arguments; + t_matcher_list *matchers; +} t_pad_inner_args; + +void set_var_for_pad_and_stuff(t_pad_and_stuff_args *a, + t_printf_arg *d); +void print_with_func(t_pad_and_stuff_args *a, t_printf_arg *d, + t_printf_func f, t_const_str t); +void pad_and_stuff(t_pad_and_stuff_args a, t_printf_arg d, + t_printf_func f); +void handle_prec_and_align(t_const_str fmt, t_usize *c_idx, + t_printf_arg *c_arg); +bool handle_atoi_stuff(t_const_str fmt, t_usize *c_idx, + t_usize *nxt, t_printf_arg *c_arg); +void set_params2(t_const_str fmt, t_usize *c_idx, t_usize *nxt, + t_printf_arg *c_arg); +bool set_params(t_const_str fmt, t_usize *c_idx, t_usize *nxt, + t_printf_arg *c_arg); +void ret_reset(t_usize *c_idx, t_usize *nxt, t_const_str fmt); +t_printf_arg print_substr(t_usize *c_idx, t_usize *nxt, t_const_str fmt, + t_pad_inner_args extra); +void pad_inner(t_const_str fmt, t_usize *c_idx, t_usize *nxt, + t_pad_inner_args extra); +void advance_atoi(t_const_str fmt, t_usize *idx); +void me_printf_str_inner(t_const_str fmt, t_printf_func f, + va_list *arguments, void *p_args); +void print_sign_if_needed(t_pad_and_stuff_args a, t_printf_arg d, + t_printf_func f); +void handle_weird_precision_stuff(t_printf_arg *data, + t_prec_strs strs, t_usize value); +#endif diff --git a/include/me/printf/matchers/matchers.h b/include/me/printf/matchers/matchers.h new file mode 100644 index 0000000..7dc4239 --- /dev/null +++ b/include/me/printf/matchers/matchers.h @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* matchers.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/16 18:09:07 by maiboyer #+# #+# */ +/* Updated: 2023/11/18 18:10:33 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MATCHERS_H +# define MATCHERS_H + +# include "me/printf/printf.h" +# include "me/types.h" +# include +# define PRINTF_BUFFER_CHUNK 20 + +typedef struct s_matcher_tmp +{ + char chr_val; + t_i64 i64_val; + t_u64 u64_val; +} t_matcher_tmp_val; + +typedef void (*t_matcher_func)(t_printf_arg data, + t_printf_func f); +typedef struct s_matcher +{ + t_const_str matcher; + t_usize matcher_len; + t_printf_type arg_type; + t_matcher_func f; +} t_matcher; + +typedef struct s_matcher_list +{ + t_matcher data[PRINTF_BUFFER_CHUNK]; + struct s_matcher_list *next; +} t_matcher_list; + +t_matcher_list *get_matchers(void); +bool insert_matcher(t_matcher matcher); +t_matcher *find_matcher(t_const_str fmt, + t_matcher_list *matchers, t_usize *c_idx); +void call_matcher(t_matcher *matcher, + t_printf_arg matcher_arguments, va_list args, + t_printf_func f); + +#endif diff --git a/include/me/printf/printf.h b/include/me/printf/printf.h new file mode 100644 index 0000000..f12bbd6 --- /dev/null +++ b/include/me/printf/printf.h @@ -0,0 +1,69 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* printf.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/16 18:10:27 by maiboyer #+# #+# */ +/* Updated: 2024/02/09 15:06:53 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef PRINTF_H + +# define PRINTF_H +# include "me/types.h" +# include + +typedef struct s_fprintf_arg +{ + t_usize total_print; + t_file fd; +} t_fprintf_arg; + +typedef enum e_printf_flags +{ + PRECISION = 1 << 1, + ALIGN = 1 << 2, + ZERO_ALIGN = 1 << 3, + SIGN = 1 << 4, +} t_printf_flags; + +typedef enum e_printf_type +{ + CHAR = 1 << 0, + STR = 1 << 1, + U64 = 1 << 2, + I64 = 1 << 3, + VOID_PTR = 1 << 4, + I32 = 1 << 5, + U32 = 1 << 6, +} t_printf_type; + +typedef struct s_printf_extra_args +{ + t_u64 precision; + t_u64 align; + bool left_align; + bool space_align; + bool pretty; +} t_printf_extra_args; + +typedef struct s_printf_args +{ + void *argument; + void *p_args; + t_printf_extra_args extra; + t_printf_flags flags; +} t_printf_arg; + +typedef void (*t_printf_func)(t_const_str to_write, + t_usize to_write_len, void *p_args); + +t_usize me_printf(t_const_str fmt, ...); +t_usize me_eprintf(t_const_str fmt, ...); +t_usize me_vprintf(t_const_str fmt, va_list *args); +t_usize me_veprintf(t_const_str fmt, va_list *args); + +#endif diff --git a/include/me/string/str_clone.h b/include/me/string/str_clone.h new file mode 100644 index 0000000..a152316 --- /dev/null +++ b/include/me/string/str_clone.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_clone.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 16:05:48 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 17:30:19 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR_CLONE_H +# define STR_CLONE_H + +# include "me/types.h" + +t_str str_clone(t_const_str source); + +#endif diff --git a/include/me/string/str_find_chr.h b/include/me/string/str_find_chr.h new file mode 100644 index 0000000..0c583d6 --- /dev/null +++ b/include/me/string/str_find_chr.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_find_chr.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 17:29:13 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:07:01 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR_FIND_CHR_H +# define STR_FIND_CHR_H + +# include "me/types.h" + +char *str_find_chr(t_const_str str, char chr); + +#endif \ No newline at end of file diff --git a/include/me/string/str_find_rev_chr.h b/include/me/string/str_find_rev_chr.h new file mode 100644 index 0000000..5dcd107 --- /dev/null +++ b/include/me/string/str_find_rev_chr.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_find_rev_chr.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 17:29:13 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:07:15 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR_FIND_REV_CHR_H +# define STR_FIND_REV_CHR_H + +# include "me/types.h" + +char *str_find_rev_chr(t_const_str str, char chr); + +#endif \ No newline at end of file diff --git a/include/me/string/str_find_str.h b/include/me/string/str_find_str.h new file mode 100644 index 0000000..30bd878 --- /dev/null +++ b/include/me/string/str_find_str.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_find_str.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/10 11:11:01 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:53:44 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR_FIND_STR_H +# define STR_FIND_STR_H + +# include "me/types.h" + +const char *str_find_str(t_const_str str, t_const_str to_find); + +#endif diff --git a/include/me/string/str_iter.h b/include/me/string/str_iter.h new file mode 100644 index 0000000..e9bf1f5 --- /dev/null +++ b/include/me/string/str_iter.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_iter.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 18:26:00 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:08:09 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR_ITER_H +# define STR_ITER_H + +# include "me/types.h" + +void str_iter(t_str s, void (*f)(t_usize, char *)); + +#endif \ No newline at end of file diff --git a/include/me/string/str_join.h b/include/me/string/str_join.h new file mode 100644 index 0000000..408eb2c --- /dev/null +++ b/include/me/string/str_join.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_join.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 23:02:58 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:08:32 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR_JOIN_H +# define STR_JOIN_H + +# include "me/types.h" + +t_str str_join(t_const_str s1, t_const_str s2); + +#endif \ No newline at end of file diff --git a/include/me/string/str_l_cat.h b/include/me/string/str_l_cat.h new file mode 100644 index 0000000..fa752cb --- /dev/null +++ b/include/me/string/str_l_cat.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_l_cat.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/09 18:01:09 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:09:07 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR_L_CAT_H +# define STR_L_CAT_H + +# include "me/types.h" + +t_usize str_l_cat(t_str dest, t_const_str src, t_usize buffer_size); + +#endif diff --git a/include/me/string/str_l_copy.h b/include/me/string/str_l_copy.h new file mode 100644 index 0000000..a75fbb4 --- /dev/null +++ b/include/me/string/str_l_copy.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_l_copy.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/09 18:01:09 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:09:38 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR_L_COPY_H +# define STR_L_COPY_H + +# include "me/types.h" + +t_usize str_l_copy(t_str dest, t_const_str src, t_usize buffer_size); + +#endif \ No newline at end of file diff --git a/include/me/string/str_len.h b/include/me/string/str_len.h new file mode 100644 index 0000000..a1dddf3 --- /dev/null +++ b/include/me/string/str_len.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_len.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 17:07:41 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 16:08:07 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR_LEN_H +# define STR_LEN_H + +# include "me/types.h" + +t_usize str_len(t_const_str str); + +#endif diff --git a/include/me/string/str_map.h b/include/me/string/str_map.h new file mode 100644 index 0000000..a64e3d2 --- /dev/null +++ b/include/me/string/str_map.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_map.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 18:26:00 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:11:40 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR_MAP_H +# define STR_MAP_H + +# include "me/types.h" + +t_str str_map(t_const_str s, char (*f)(t_usize, char)); + +#endif \ No newline at end of file diff --git a/include/me/string/str_n_compare.h b/include/me/string/str_n_compare.h new file mode 100644 index 0000000..c07c04f --- /dev/null +++ b/include/me/string/str_n_compare.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_n_compare.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 18:53:47 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:17:14 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR_N_COMPARE_H +# define STR_N_COMPARE_H + +# include "me/types.h" + +t_i32 str_n_compare(t_const_str lhs, t_const_str rhs, t_usize n); + +#endif \ No newline at end of file diff --git a/include/me/string/str_n_find_str.h b/include/me/string/str_n_find_str.h new file mode 100644 index 0000000..eea578b --- /dev/null +++ b/include/me/string/str_n_find_str.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_n_find_str.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/10 11:11:01 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:53:35 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR_N_FIND_STR_H +# define STR_N_FIND_STR_H + +# include "me/types.h" + +const char *str_n_find_str(t_const_str str, t_const_str to_find, t_usize len); + +#endif diff --git a/include/me/string/str_split.h b/include/me/string/str_split.h new file mode 100644 index 0000000..578f1dd --- /dev/null +++ b/include/me/string/str_split.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_split.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/17 15:56:59 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:18:07 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR_SPLIT_H +# define STR_SPLIT_H + +# include "me/types.h" + +t_str *str_split(t_const_str str, char chr); + +#endif \ No newline at end of file diff --git a/include/me/string/str_substring.h b/include/me/string/str_substring.h new file mode 100644 index 0000000..da9cb41 --- /dev/null +++ b/include/me/string/str_substring.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_substring.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 22:42:55 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:18:41 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR_SUBSTRING_H +# define STR_SUBSTRING_H + +# include "me/types.h" + +t_str str_substring(t_const_str str, t_usize start, t_usize len); + +#endif \ No newline at end of file diff --git a/include/me/string/str_trim.h b/include/me/string/str_trim.h new file mode 100644 index 0000000..e7682d3 --- /dev/null +++ b/include/me/string/str_trim.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_trim.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 23:43:42 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:19:11 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR_TRIM_H +# define STR_TRIM_H + +# include "me/types.h" + +t_str str_trim(t_const_str str, t_const_str charset); + +#endif \ No newline at end of file diff --git a/include/me/types.h b/include/me/types.h new file mode 100644 index 0000000..55a0360 --- /dev/null +++ b/include/me/types.h @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* types.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/03 14:31:12 by maiboyer #+# #+# */ +/* Updated: 2024/01/05 00:08:41 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef TYPES_H +# define TYPES_H + +# include +# include +# include + +typedef char *t_str; +typedef const char *t_const_str; + +typedef unsigned char t_u8; +typedef char t_i8; +typedef unsigned short t_u16; +typedef short t_i16; +typedef int t_i32; +typedef unsigned int t_u32; +typedef unsigned long long t_u64; +typedef long long t_i64; +typedef ssize_t t_isize; +typedef size_t t_usize; + +typedef float t_f32; +typedef double t_f64; + +typedef int t_file; +typedef struct s_list +{ + void *content; + struct s_list *next; +} t_list; + +typedef bool t_error; + +# define ERROR 1 +# define NO_ERROR 0 + +#endif diff --git a/include/me/vec/vec_buf_str.h b/include/me/vec/vec_buf_str.h new file mode 100644 index 0000000..083cb24 --- /dev/null +++ b/include/me/vec/vec_buf_str.h @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_buf_str.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/04 18:46:53 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 17:53:00 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef VEC_BUF_STR_H +# define VEC_BUF_STR_H + +# include "me/buffered_str/buf_str.h" +# include "me/types.h" + +typedef bool (*t_vec_buf_str_sort_fn)(t_buffer_str *, + t_buffer_str *); +typedef void (*t_free_buf_str_item)(t_buffer_str); + +typedef struct s_vec_buf_str +{ + t_free_buf_str_item free_func; + t_usize len; + t_usize capacity; + t_buffer_str *buffer; +} t_vec_buf_str; + +t_vec_buf_str vec_buf_str_new(t_usize capacity, + t_free_buf_str_item free_function); +t_error vec_buf_str_push(t_vec_buf_str *vec, + t_buffer_str element); +t_error vec_buf_str_push_front(t_vec_buf_str *vec, + t_buffer_str element); +t_error vec_buf_str_pop(t_vec_buf_str *vec, + t_buffer_str *value); +t_error vec_buf_str_pop_front(t_vec_buf_str *vec, + t_buffer_str *value); +void vec_buf_str_free(t_vec_buf_str vec); +t_error vec_buf_str_reserve(t_vec_buf_str *vec, + t_usize wanted_capacity); +t_error vec_buf_str_find(t_vec_buf_str *vec, + bool (*fn)(const t_buffer_str *), t_usize *index); +t_error vec_buf_str_find_starting(t_vec_buf_str *vec, + bool (*fn)(const t_buffer_str *), + t_usize starting_index, t_usize *index); +t_error vec_buf_str_all(t_vec_buf_str *vec, + bool (*fn)(const t_buffer_str *), bool *result); +t_error vec_buf_str_any(t_vec_buf_str *vec, + bool (*fn)(const t_buffer_str *), bool *result); +void vec_buf_str_iter(t_vec_buf_str *vec, + void (*fn)(t_usize index, t_buffer_str *value, + void *state), void *state); +void vec_buf_str_reverse(t_vec_buf_str *vec); +void vec_buf_str_sort(t_vec_buf_str *vec, + t_vec_buf_str_sort_fn is_sorted); + +#endif diff --git a/include/me/vec/vec_i64.h b/include/me/vec/vec_i64.h new file mode 100644 index 0000000..8a8f200 --- /dev/null +++ b/include/me/vec/vec_i64.h @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_i64.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/04 18:46:53 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 17:53:00 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef VEC_I64_H +# define VEC_I64_H + +# include "me/types.h" + +typedef bool (*t_vec_i64_sort_fn)(t_i64 *, t_i64 *); +typedef void (*t_free_i64_item)(t_i64); + +typedef struct s_vec_i64 +{ + t_free_i64_item free_func; + t_usize len; + t_usize capacity; + t_i64 *buffer; +} t_vec_i64; + +t_vec_i64 vec_i64_new(t_usize capacity, + t_free_i64_item free_function); +t_error vec_i64_push(t_vec_i64 *vec, t_i64 element); +t_error vec_i64_push_front(t_vec_i64 *vec, t_i64 element); +t_error vec_i64_pop(t_vec_i64 *vec, t_i64 *value); +t_error vec_i64_pop_front(t_vec_i64 *vec, t_i64 *value); +void vec_i64_free(t_vec_i64 vec); +t_error vec_i64_reserve(t_vec_i64 *vec, t_usize wanted_capacity); +t_error vec_i64_find(t_vec_i64 *vec, bool (*fn)(const t_i64 *), + t_usize *index); +t_error vec_i64_find_starting(t_vec_i64 *vec, + bool (*fn)(const t_i64 *), t_usize starting_index, + t_usize *index); +t_error vec_i64_all(t_vec_i64 *vec, bool (*fn)(const t_i64 *), + bool *result); +t_error vec_i64_any(t_vec_i64 *vec, bool (*fn)(const t_i64 *), + bool *result); +void vec_i64_iter(t_vec_i64 *vec, void (*fn)(t_usize index, + t_i64 *value, void *state), void *state); +void vec_i64_reverse(t_vec_i64 *vec); +void vec_i64_sort(t_vec_i64 *vec, t_vec_i64_sort_fn is_sorted); + +#endif diff --git a/include/me/vec/vec_i64_bool.h b/include/me/vec/vec_i64_bool.h new file mode 100644 index 0000000..42f456a --- /dev/null +++ b/include/me/vec/vec_i64_bool.h @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_i64_bool.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/04 18:46:53 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 17:53:00 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef VEC_I64_BOOL_H +# define VEC_I64_BOOL_H + +# include "app/types/type_i64_bool.h" +# include "me/types.h" + +typedef bool (*t_vec_i64_bool_sort_fn)(t_i64_bool *, + t_i64_bool *); +typedef void (*t_free_i64_bool_item)(t_i64_bool); + +typedef struct s_vec_i64_bool +{ + t_free_i64_bool_item free_func; + t_usize len; + t_usize capacity; + t_i64_bool *buffer; +} t_vec_i64_bool; + +t_vec_i64_bool vec_i64_bool_new(t_usize capacity, + t_free_i64_bool_item free_function); +t_error vec_i64_bool_push(t_vec_i64_bool *vec, + t_i64_bool element); +t_error vec_i64_bool_push_front(t_vec_i64_bool *vec, + t_i64_bool element); +t_error vec_i64_bool_pop(t_vec_i64_bool *vec, + t_i64_bool *value); +t_error vec_i64_bool_pop_front(t_vec_i64_bool *vec, + t_i64_bool *value); +void vec_i64_bool_free(t_vec_i64_bool vec); +t_error vec_i64_bool_reserve(t_vec_i64_bool *vec, + t_usize wanted_capacity); +t_error vec_i64_bool_find(t_vec_i64_bool *vec, + bool (*fn)(const t_i64_bool *), t_usize *index); +t_error vec_i64_bool_find_starting(t_vec_i64_bool *vec, + bool (*fn)(const t_i64_bool *), + t_usize starting_index, t_usize *index); +t_error vec_i64_bool_all(t_vec_i64_bool *vec, + bool (*fn)(const t_i64_bool *), bool *result); +t_error vec_i64_bool_any(t_vec_i64_bool *vec, + bool (*fn)(const t_i64_bool *), bool *result); +void vec_i64_bool_iter(t_vec_i64_bool *vec, + void (*fn)(t_usize index, t_i64_bool *value, + void *state), void *state); +void vec_i64_bool_reverse(t_vec_i64_bool *vec); +void vec_i64_bool_sort(t_vec_i64_bool *vec, + t_vec_i64_bool_sort_fn is_sorted); + +#endif diff --git a/include/me/vec/vec_u8.h b/include/me/vec/vec_u8.h new file mode 100644 index 0000000..fe8ec03 --- /dev/null +++ b/include/me/vec/vec_u8.h @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_u8.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/04 18:46:53 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 17:53:00 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef VEC_U8_H +# define VEC_U8_H + +# include "me/types.h" + +typedef bool (*t_vec_u8_sort_fn)(t_u8 *, t_u8 *); +typedef void (*t_free_u8_item)(t_u8); + +typedef struct s_vec_u8 +{ + t_free_u8_item free_func; + t_usize len; + t_usize capacity; + t_u8 *buffer; +} t_vec_u8; + +t_vec_u8 vec_u8_new(t_usize capacity, t_free_u8_item free_function); +t_error vec_u8_push(t_vec_u8 *vec, t_u8 element); +t_error vec_u8_push_front(t_vec_u8 *vec, t_u8 element); +t_error vec_u8_pop(t_vec_u8 *vec, t_u8 *value); +t_error vec_u8_pop_front(t_vec_u8 *vec, t_u8 *value); +void vec_u8_free(t_vec_u8 vec); +t_error vec_u8_reserve(t_vec_u8 *vec, t_usize wanted_capacity); +t_error vec_u8_find(t_vec_u8 *vec, bool (*fn)(const t_u8 *), + t_usize *index); +t_error vec_u8_find_starting(t_vec_u8 *vec, + bool (*fn)(const t_u8 *), t_usize starting_index, + t_usize *index); +t_error vec_u8_all(t_vec_u8 *vec, bool (*fn)(const t_u8 *), + bool *result); +t_error vec_u8_any(t_vec_u8 *vec, bool (*fn)(const t_u8 *), + bool *result); +void vec_u8_iter(t_vec_u8 *vec, void (*fn)(t_usize index, + t_u8 *value, void *state), void *state); +void vec_u8_reverse(t_vec_u8 *vec); +void vec_u8_sort(t_vec_u8 *vec, t_vec_u8_sort_fn is_sorted); + +#endif diff --git a/include/me/vec2/vec2.h b/include/me/vec2/vec2.h new file mode 100644 index 0000000..81a0e52 --- /dev/null +++ b/include/me/vec2/vec2.h @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec2.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 14:54:39 by maiboyer #+# #+# */ +/* Updated: 2023/12/31 15:07:54 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef VEC2_H +# define VEC2_H + +# include "me/types.h" + +typedef struct s_vi2d +{ + t_i32 x; + t_i32 y; +} t_vi2d; + +typedef struct s_vu2d +{ + t_u32 x; + t_u32 y; +} t_vu2d; + +typedef struct s_vf2d +{ + t_f32 x; + t_f32 y; +} t_vf2d; + +static inline t_vf2d vf2d(t_f32 x, t_f32 y) +{ + return ((t_vf2d){.x = x, .y = y}); +} + +static inline t_vi2d vi2d(t_i32 x, t_i32 y) +{ + return ((t_vi2d){.x = x, .y = y}); +} + +static inline t_vu2d vu2d(t_u32 x, t_u32 y) +{ + return ((t_vu2d){.x = x, .y = y}); +} + +static inline t_vi2d vi2d_add(t_vi2d lhs, t_vi2d rhs) +{ + return ((t_vi2d){.x = lhs.x + rhs.x, .y = lhs.y + rhs.y}); +} + +static inline t_vi2d vi2d_sub(t_vi2d lhs, t_vi2d rhs) +{ + return ((t_vi2d){.x = lhs.x - rhs.x, .y = lhs.y - rhs.y}); +} +#endif /* VEC2_H */ diff --git a/src/app/actual_main.c b/src/app/actual_main.c new file mode 100644 index 0000000..6804f8b --- /dev/null +++ b/src/app/actual_main.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* actual_main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/09 15:10:39 by maiboyer #+# #+# */ +/* Updated: 2024/02/09 15:26:33 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/state.h" +#include "me/printf/printf.h" +#include "me/string/str_n_compare.h" +#include "me/types.h" + +int main_normal(t_i32 argc, t_str argv[]); +int main_checker(t_i32 argc, t_str argv[]); + +int main(t_i32 argc, t_str argv[]) +{ + if (BONUS) + return (main_checker(argc, argv)); + return (main_normal(argc, argv)); +} diff --git a/src/app/best_index_to_move.c b/src/app/best_index_to_move.c new file mode 100644 index 0000000..1b37701 --- /dev/null +++ b/src/app/best_index_to_move.c @@ -0,0 +1,77 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* best_index_to_move.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/08 14:14:54 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 14:21:51 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/best_index_to_move.h" +#include "app/best_move.h" +#include "app/find_iter.h" +#include "app/rotate.h" +#include "app/state.h" +#include "app/target.h" +#include "me/printf/printf.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" +#include "me/vec/vec_i64_bool.h" +#include + +static void inc(void *s) +{ + t_usize *p; + + p = (t_usize *)s; + *p += 1; +} + +static inline t_usize count_move_for_index(struct s_best_index_to_move d) +{ + t_usize tmp; + + tmp = 0; + run_func_with_best_rotate_for_item(d.state, + (t_best_move_args){ + .index = d.i, + .args = &tmp, + .iter_func = d.f, + .from = d.from, + .to = d.to, + .other = {inc, inc}, + .main = {inc, inc}, + .both = {inc, inc}, + }); + return (tmp); +} + +t_usize best_index_to_move(t_state *state, t_vec_i64 *from, t_vec_i64 *to, + t_iter_pos_func f) +{ + t_usize min_val; + t_usize min_pos; + t_usize i; + t_usize tmp; + + if (from->len == 0) + return (0); + i = 0; + min_val = ~0; + min_pos = 0; + while (i < from->len) + { + tmp = count_move_for_index((struct s_best_index_to_move){state, from, + to, f, i}); + if (tmp < min_val) + { + min_val = tmp; + min_pos = i; + } + i++; + } + return (min_pos); +} diff --git a/src/app/best_move.c b/src/app/best_move.c new file mode 100644 index 0000000..2bbbfdf --- /dev/null +++ b/src/app/best_move.c @@ -0,0 +1,99 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* best_move.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/29 20:04:33 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 14:22:55 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/best_move.h" +#include "app/best_move_inner.h" +#include "app/find_iter.h" +#include "app/find_place.h" +#include "app/rotate.h" +#include "app/state.h" +#include "app/target.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" +#include + +static inline t_isize move_count(t_rotation lhs, t_rotation rhs) +{ + if (lhs.direction == rhs.direction) + return (max(lhs.value, rhs.value)); + else + return (abs_diff(lhs.value, rhs.value)); +} + +void find_least_move(t_rotation *main, t_rotation *other) +{ + t_isize main_fliped; + t_isize other_fliped; + t_isize none_fliped; + t_rotation tmp; + t_isize minimum; + + none_fliped = move_count(*main, *other); + main_fliped = move_count(flip(*main), *other); + other_fliped = move_count(*main, flip(*other)); + minimum = min(none_fliped, min(main_fliped, other_fliped)); + if (minimum == none_fliped) + return ; + else if (minimum == main_fliped) + { + tmp = flip(*main); + *main = tmp; + } + else if (minimum == other_fliped) + { + tmp = flip(*other); + *other = tmp; + } +} + +static inline void func(t_best_move_args *data, t_rotation *main, + t_rotation *other) +{ + t_isize i; + + i = 0; + while (i++ < min(main->value, other->value)) + choose_func(&data->both, *main)(data->args); + i = 0; + if (main->value > other->value) + while (i++ < main->value - other->value) + choose_func(&data->main, *main)(data->args); + else + while (i++ < other->value - main->value) + choose_func(&data->other, *other)(data->args); +} + +void run_func_with_best_rotate_for_item(t_state *state, + t_best_move_args data) +{ + t_isize target_index; + t_rotation main; + t_rotation other; + t_isize i; + + target_index = (find_place(data.from->buffer[data.index], state) + + (data.to->len - data.iter_func(data.to))) % max(data.to->len, 1); + main = target(0, data.index, data.from->len); + other = target(target_index, 0, data.to->len); + find_least_move(&main, &other); + if (main.direction == other.direction) + func(&data, &main, &other); + else + { + i = 0; + while (i++ < main.value) + choose_func(&data.main, main)(data.args); + i = 0; + while (i++ < other.value) + choose_func(&data.other, other)(data.args); + } +} diff --git a/src/app/do_move.c b/src/app/do_move.c new file mode 100644 index 0000000..96b06b1 --- /dev/null +++ b/src/app/do_move.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* do_move.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/31 14:25:57 by maiboyer #+# #+# */ +/* Updated: 2024/02/02 22:54:12 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/best_move.h" +#include "app/moves.h" +#include "app/state.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" + +void push_a(void *s); +void push_b(void *s); +void swap_a(void *s); +void swap_b(void *s); +void swap_both(void *s); +void rotate_a(void *s); +void rotate_b(void *s); +void rotate_both(void *s); +void rev_rotate_a(void *s); +void rev_rotate_b(void *s); +void rev_rotate_both(void *s); diff --git a/src/app/find_place.c b/src/app/find_place.c new file mode 100644 index 0000000..1a3efd6 --- /dev/null +++ b/src/app/find_place.c @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* find_place.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/29 22:01:12 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 14:06:05 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/find_place.h" +#include "app/types/type_i64_bool.h" +#include "me/vec/vec_i64_bool.h" + +static void find_place_iter(t_usize index, t_i64_bool *elem, + t_find_place_iter_state *state) +{ + (void)(index); + if (!(elem->active || elem->value == state->to_find_elem)) + return ; + if (elem->value == state->to_find_elem) + state->found_index = state->current_index; + state->current_index++; +} + +t_usize find_place(t_i64 elem, t_state *state) +{ + t_find_place_iter_state iter_state; + + iter_state.current_index = 0; + iter_state.found_index = 0; + iter_state.to_find_elem = elem; + vec_i64_bool_iter(&state->sorted, (void (*)())find_place_iter, &iter_state); + return (iter_state.found_index); +} + +/* +state + .sorted + .iter() + .copied() + .filter(|&(e, active)| active || elem == e) + .enumerate() + .find(|(_, (e, _))| *e == elem) + .map(|(i, _)| i) + .unwrap_or_else(|| { + println!("why..."); + 0 + }) +*/ diff --git a/src/app/iter_find.c b/src/app/iter_find.c new file mode 100644 index 0000000..bd6d1cf --- /dev/null +++ b/src/app/iter_find.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* iter_find.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/31 14:17:53 by maiboyer #+# #+# */ +/* Updated: 2024/01/31 14:23:26 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/iter_state.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" + +static void iter_min(t_usize index, t_i64 *elem, t_iter_state *state) +{ + if (*elem < state->elem) + { + state->elem = *elem; + state->pos = index; + } +} + +static void iter_max(t_usize index, t_i64 *elem, t_iter_state *state) +{ + if (*elem > state->elem) + { + state->elem = *elem; + state->pos = index; + } +} + +t_usize min_iter_zero_pos(t_vec_i64 *vec) +{ + t_iter_state state; + + if (vec->len == 0) + return (0); + state.pos = 0; + state.elem = vec->buffer[0]; + vec_i64_iter(vec, (void (*)())iter_min, &state); + return (state.pos); +} + +t_usize max_iter_zero_pos(t_vec_i64 *vec) +{ + t_iter_state state; + + if (vec->len == 0) + return (0); + state.pos = 0; + state.elem = vec->buffer[0]; + vec_i64_iter(vec, (void (*)())iter_max, &state); + return (state.pos); +} diff --git a/src/app/main.c b/src/app/main.c new file mode 100644 index 0000000..e234e3e --- /dev/null +++ b/src/app/main.c @@ -0,0 +1,64 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:00:12 by maiboyer #+# #+# */ +/* Updated: 2024/02/15 17:42:03 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/state.h" +#include "app/types/type_i64_bool.h" +#include "me/convert/str_to_numbers.h" +#include "me/printf/printf.h" +#include "me/string/str_n_compare.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" +#include "me/vec/vec_i64_bool.h" +#include +#include + +#if BONUS == 1 + +# define ERR_INVALID_NUM "Error\n" +# define ERR_DUPLICATE "Error\n" + +#else + +//# define ERR_INVALID_NUM "Error:\nInvalid Number\n" +//# define ERR_DUPLICATE "Error:\nDuplicate Number\n" +# define ERR_INVALID_NUM "Error\n" +# define ERR_DUPLICATE "Error\n" + +#endif + +void sort_3(t_state *state); +void sort_2(t_state *state); +void sort_5(t_state *state); +bool is_sorted(t_vec_i64 *v); +void run_with_items(t_state *state); + +int main_normal(t_i32 argc, t_str argv[]) +{ + t_state state; + + (void)(argc--, argv++); + state = parses_arguments(argc, argv); + if (is_sorted(&state.stack_a)) + return (free_state(state), 0); + if (state.stack_a.len == 0) + (free_state(state), exit(0)); + else if (state.stack_a.len == 2) + sort_2(&state); + else if (state.stack_a.len == 3) + sort_3(&state); + else if (state.stack_a.len == 5) + sort_5(&state); + else + run_with_items(&state); + free_state(state); + return (0); +} diff --git a/src/app/main_bonus.c b/src/app/main_bonus.c new file mode 100644 index 0000000..4de3a02 --- /dev/null +++ b/src/app/main_bonus.c @@ -0,0 +1,105 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/08 18:59:33 by maiboyer #+# #+# */ +/* Updated: 2024/02/15 17:47:33 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/state.h" +#include "me/buffered_str/buf_str.h" +#include "me/gnl/gnl.h" +#include "me/printf/printf.h" +#include "me/string/str_n_compare.h" +#include "me/vec/vec_i64.h" + +#if BONUS + +void push_inner(t_vec_i64 *to, t_vec_i64 *from, t_const_str tag, + t_const_str print); +void swap_inner(t_vec_i64 *vec, t_const_str tag, t_const_str print); +void rotate_inner(t_vec_i64 *vec, t_const_str tag, t_const_str print); +void rev_rotate_inner(t_vec_i64 *vec, t_const_str tag, t_const_str print); +bool is_sorted(t_vec_i64 *v); + +bool does_match_strings(t_const_str in, t_const_str s1, t_const_str s2) +{ + bool res; + + res = (str_n_compare(in, s1, 4) == 0); + if (s2 != NULL) + res = (res || (str_n_compare(in, s2, 4) == 0)); + return (res); +} + +t_error handle_operation(t_buffer_str s, t_state *state) +{ + t_usize op; + + op = 0; + if (s.buf == NULL) + return (ERROR); + if (does_match_strings(s.buf, "pa", NULL)) + push_inner(&state->stack_a, &state->stack_b, NULL, (op++, NULL)); + if (does_match_strings(s.buf, "pb", NULL)) + push_inner(&state->stack_b, &state->stack_a, NULL, (op++, NULL)); + if (does_match_strings(s.buf, "sa", "ss")) + swap_inner(&state->stack_a, NULL, (op++, NULL)); + if (does_match_strings(s.buf, "sb", "ss")) + swap_inner(&state->stack_b, NULL, (op++, NULL)); + if (does_match_strings(s.buf, "ra", "rr")) + rotate_inner(&state->stack_a, NULL, (op++, NULL)); + if (does_match_strings(s.buf, "rb", "rr")) + rotate_inner(&state->stack_b, NULL, (op++, NULL)); + if (does_match_strings(s.buf, "rra", "rrr")) + rev_rotate_inner(&state->stack_a, NULL, (op++, NULL)); + if (does_match_strings(s.buf, "rrb", "rrr")) + rev_rotate_inner(&state->stack_b, NULL, (op++, NULL)); + if (op == 0) + return (ERROR); + return (NO_ERROR); +} + +void handle_end(t_state state, t_buffer_str s) +{ + str_free(s); + if (is_sorted(&state.stack_a) && state.stack_b.len == 0) + me_printf("OK\n"); + else + me_printf("KO\n"); + free_state(state); +} + +int main_checker(t_i32 argc, t_str argv[]) +{ + t_state state; + t_buffer_str s; + bool err; + + (void)(argc--, argv++); + s.buf = ""; + err = false; + state = parses_arguments(argc, argv); + while (!err) + { + s = get_next_line(0, &err); + if (err) + break ; + if (s.len != 0 && s.buf[s.len - 1] == '\n') + str_pop(&s); + if (s.buf[0] == '\0') + continue ; + if (handle_operation(s, &state)) + return (me_printf("KO\n"), str_free(s), free_state(state), 1); + if (s.buf != NULL) + str_free(s); + } + handle_end(state, s); + return (0); +} + +#endif diff --git a/src/app/moves.c b/src/app/moves.c new file mode 100644 index 0000000..1eef164 --- /dev/null +++ b/src/app/moves.c @@ -0,0 +1,71 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* moves.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/11 16:17:26 by maiboyer #+# #+# */ +/* Updated: 2024/01/29 18:58:48 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/moves.h" +#include "app/state.h" +#include "app/types/type_move.h" + +void push_a(t_state *s); +void push_b(t_state *s); +void swap_a(t_state *s); +void swap_b(t_state *s); +void rotate_a(t_state *s); +void rotate_b(t_state *s); +void rev_rotate_a(t_state *s); +void rev_rotate_b(t_state *s); + +void do_move(t_move m, t_state *s) +{ + if (m & PUSH_A) + push_a(s); + if (m & PUSH_B) + push_b(s); + if (m & SWAP_A) + swap_a(s); + if (m & SWAP_B) + swap_b(s); + if (m & ROTATE_A) + rotate_a(s); + if (m & ROTATE_B) + rotate_b(s); + if (m & REVERSE_ROTATE_A) + rev_rotate_a(s); + if (m & REVERSE_ROTATE_B) + rev_rotate_b(s); +} + +t_const_str get_str_for_move(t_move m) +{ + if (m & PUSH_A) + return ("pa"); + if (m & PUSH_B) + return ("pb"); + if (m & SWAP_A && m & SWAP_B) + return ("ss"); + if (m & SWAP_A) + return ("sa"); + if (m & SWAP_B) + return ("sb"); + if (m & ROTATE_A && m & ROTATE_B) + return ("rr"); + if (m & ROTATE_A) + return ("ra"); + if (m & ROTATE_B) + return ("rb"); + if (m & REVERSE_ROTATE_A && m & REVERSE_ROTATE_B) + return ("rrr"); + if (m & REVERSE_ROTATE_A) + return ("rra"); + if (m & REVERSE_ROTATE_B) + return ("rrb"); + return (""); +} diff --git a/src/app/moves/push.c b/src/app/moves/push.c new file mode 100644 index 0000000..dc53491 --- /dev/null +++ b/src/app/moves/push.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* push.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/11 16:22:54 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 19:15:58 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/state.h" +#include "me/printf/printf.h" +#include "me/vec/vec_i64.h" + +void push_inner(t_vec_i64 *to, t_vec_i64 *from, t_const_str tag, + t_const_str print) +{ + t_i64 e; + + (void)(tag); + if (from->len == 0) + return ; + vec_i64_pop_front(from, &e); + vec_i64_push_front(to, e); + if (print) + me_printf("%s\n", print); +} + +void push_a(t_state *s) +{ + push_inner(&s->stack_a, &s->stack_b, "Push A", "pa"); +} + +void push_b(t_state *s) +{ + push_inner(&s->stack_b, &s->stack_a, "Push B", "pb"); +} diff --git a/src/app/moves/rev_rotate.c b/src/app/moves/rev_rotate.c new file mode 100644 index 0000000..f630ea0 --- /dev/null +++ b/src/app/moves/rev_rotate.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rev_rotate.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/11 16:26:04 by maiboyer #+# #+# */ +/* Updated: 2024/02/10 18:39:03 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/state.h" +#include "me/printf/printf.h" + +void rev_rotate_inner(t_vec_i64 *stack, t_const_str tag, + t_const_str print) +{ + t_i64 e; + + (void)(tag); + if (stack->len < 2) + return ; + vec_i64_pop(stack, &e); + vec_i64_push_front(stack, e); + if (print) + me_printf("%s\n", print); +} + +void rev_rotate_a(t_state *s) +{ + rev_rotate_inner(&s->stack_a, "RevRotate A", "rra"); +} + +void rev_rotate_b(t_state *s) +{ + rev_rotate_inner(&s->stack_b, "RevRotate B", "rrb"); +} + +void rev_rotate_both(t_state *s) +{ + rev_rotate_inner(&s->stack_a, "RevRotate Both", NULL); + rev_rotate_inner(&s->stack_b, "RevRotate Both", "rrr"); +} diff --git a/src/app/moves/rotate.c b/src/app/moves/rotate.c new file mode 100644 index 0000000..86cd2aa --- /dev/null +++ b/src/app/moves/rotate.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rotate.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/11 16:26:04 by maiboyer #+# #+# */ +/* Updated: 2024/02/10 18:38:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/state.h" +#include "me/printf/printf.h" + +void rotate_inner(t_vec_i64 *stack, t_const_str tag, t_const_str print) +{ + t_i64 e; + + (void)(tag); + if (stack->len < 2) + return ; + vec_i64_pop_front(stack, &e); + vec_i64_push(stack, e); + if (print) + me_printf("%s\n", print); +} + +void rotate_a(t_state *s) +{ + rotate_inner(&s->stack_a, "Rotate A", "ra"); +} + +void rotate_b(t_state *s) +{ + rotate_inner(&s->stack_b, "Rotate B", "rb"); +} + +void rotate_both(t_state *s) +{ + rotate_inner(&s->stack_a, "Rotate Both", NULL); + rotate_inner(&s->stack_b, "Rotate Both", "rr"); +} diff --git a/src/app/moves/swap.c b/src/app/moves/swap.c new file mode 100644 index 0000000..934cedd --- /dev/null +++ b/src/app/moves/swap.c @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* swap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/11 16:26:04 by maiboyer #+# #+# */ +/* Updated: 2024/02/09 15:40:37 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/state.h" +#include "me/printf/printf.h" +#include "me/vec/vec_i64.h" + +void swap_inner(t_vec_i64 *stack, t_const_str tag, t_const_str print) +{ + t_i64 first; + t_i64 second; + + (void)(tag); + if (stack->len < 2) + return ; + vec_i64_pop_front(stack, &first); + vec_i64_pop_front(stack, &second); + vec_i64_push_front(stack, first); + vec_i64_push_front(stack, second); + if (print) + me_printf("%s\n", print); +} + +void swap_a(t_state *s) +{ + swap_inner(&s->stack_a, "Swap A", "sa"); +} + +void swap_b(t_state *s) +{ + swap_inner(&s->stack_b, "Swap B", "sb"); +} + +void swap_both(t_state *s) +{ + swap_inner(&s->stack_a, "Swap Both", NULL); + swap_inner(&s->stack_b, "Swap Both", "ss"); +} diff --git a/src/app/rotate.c b/src/app/rotate.c new file mode 100644 index 0000000..33c6652 --- /dev/null +++ b/src/app/rotate.c @@ -0,0 +1,14 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rotate.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/29 18:59:56 by maiboyer #+# #+# */ +/* Updated: 2024/01/29 20:04:18 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/rotate.h" +#include "me/types.h" diff --git a/src/app/run_with_items.c b/src/app/run_with_items.c new file mode 100644 index 0000000..45ed8df --- /dev/null +++ b/src/app/run_with_items.c @@ -0,0 +1,114 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* run_with_items.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/31 15:12:47 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 14:22:15 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/best_move.h" +#include "app/find_iter.h" +#include "app/rotate.h" +#include "app/state.h" +#include "app/target.h" +#include "me/printf/printf.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" +#include "me/vec/vec_i64_bool.h" +#include + +void push_a(void *s); +void push_b(void *s); +void swap_a(void *s); +void swap_b(void *s); +void swap_both(void *s); +void rotate_a(void *s); +void rotate_b(void *s); +void rotate_both(void *s); +void rev_rotate_a(void *s); +void rev_rotate_b(void *s); +void rev_rotate_both(void *s); + +t_usize best_index_to_move(t_state *state, t_vec_i64 *from, t_vec_i64 *to, + t_iter_pos_func f); + +static inline struct s_functions funcs( void (*f)(void *), void (*r)(void *)) +{ + return ((struct s_functions){.forward = f, .reverse = r}); +} + +static inline void banana_first(t_state *state) +{ + t_usize idx; + + idx = best_index_to_move(state, &state->stack_a, &state->stack_b, + &max_iter_zero_pos); + run_func_with_best_rotate_for_item(state, + (t_best_move_args){ + .index = idx, .iter_func = &max_iter_zero_pos, .from = \ +&state->stack_a, .to = &state->stack_b, .args = state, .both = \ +funcs(rotate_both, rev_rotate_both), .main = funcs(rotate_a, rev_rotate_a), \ +.other = funcs(rotate_b, rev_rotate_b), }); + push_b(state); + make_sorted_true_from_stack(state, &state->stack_b); +} + +bool is_sorted(t_vec_i64 *v) +{ + t_usize i; + + i = 1; + while (i < v->len) + { + if (v->buffer[i - 1] > v->buffer[i]) + return (false); + i++; + } + return (true); +} + +static inline void banana_second(t_state *state) +{ + t_usize idx; + + idx = best_index_to_move(state, &state->stack_b, &state->stack_a, + &min_iter_zero_pos); + run_func_with_best_rotate_for_item(state, (t_best_move_args){ + .index = idx, .from = &state->stack_b, \ +.to = &state->stack_a, .iter_func = &min_iter_zero_pos, .args = state, \ +.both = funcs(rotate_both, rev_rotate_both), .main = funcs(rotate_b, \ +rev_rotate_b), .other = funcs(rotate_a, rev_rotate_a), }); + push_a(state); + make_sorted_true_from_stack(state, &state->stack_a); +} + +void run_with_items(t_state *state) +{ + t_rotation rot; + + if (is_sorted(&state->stack_a)) + return ; + while (state->stack_a.len > state->stack_b.len) + banana_first(state); + while (state->stack_a.len != 0) + push_b(state); + vec_i64_bool_reverse(&state->sorted); + make_sorted_all_false(state); + while (state->stack_b.len != 0) + banana_second(state); + rot = target(0, min_iter_zero_pos(&state->stack_a), state->stack_a.len); + if (rot.value > flip(rot).value) + rot = flip(rot); + while (rot.value > 0) + { + if (rot.direction == FORWARD) + rotate_a(state); + else + rev_rotate_a(state); + rot.value--; + } +} diff --git a/src/app/sort2.c b/src/app/sort2.c new file mode 100644 index 0000000..a82f46c --- /dev/null +++ b/src/app/sort2.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sort2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/08 15:50:10 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 16:20:35 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/state.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" + +void push_a(void *s); +void push_b(void *s); +void swap_a(void *s); +void swap_b(void *s); +void swap_both(void *s); +void rotate_a(void *s); +void rotate_b(void *s); +void rotate_both(void *s); +void rev_rotate_a(void *s); +void rev_rotate_b(void *s); +void rev_rotate_both(void *s); + +void sort_2(t_state *state) +{ + if (state->stack_a.buffer[0] > state->stack_a.buffer[1]) + swap_a(state); +} diff --git a/src/app/sort3.c b/src/app/sort3.c new file mode 100644 index 0000000..080bfe4 --- /dev/null +++ b/src/app/sort3.c @@ -0,0 +1,138 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sort3.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/08 15:50:10 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 17:53:02 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/state.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" + +void push_a(void *s); +void push_b(void *s); +void swap_a(void *s); +void swap_b(void *s); +void swap_both(void *s); +void rotate_a(void *s); +void rotate_b(void *s); +void rotate_both(void *s); +void rev_rotate_a(void *s); +void rev_rotate_b(void *s); +void rev_rotate_both(void *s); + +/* +fn sort_three(state: &mut State, selector: StackSelector, min_first: bool) { + macro_rules! stack { + () => { + match selector { + StackSelector::B => &mut state.b, + StackSelector::A => &mut state.a, + } + }; + } + let [swap, rotate, rev_rotate] = match selector { + StackSelector::A => [sa, ra, rra], + StackSelector::B => [sb, rb, rrb], + }; + match stack!().len() { + 2 => { + let func = match min_first { + true => PartialOrd::gt, + false => PartialOrd::lt, + }; + + if func(&stack!()[0].clone(), &stack!()[1]) { + swap(state); + } + return ; + } + 3 => {} + 0 | 1 | 4.. => return, + } + let mut c = stack!().clone(); + macro_rules! comb { + ($i1:literal, $i2:literal, $i3:literal) => { + &[c[$i1 - 1], c[$i2 - 1], c[$i3 - 1]] + }; + } + c.make_contiguous().sort_unstable(); + if min_first { + c.make_contiguous().reverse(); + } + stack!().make_contiguous(); + + if (stack!() == comb![1, 2, 3] / * abc * /) +{ + swap(state); + rev_rotate(state); +} +else if (stack!() == comb ![ 1, 3, 2 ] / * acb * /) +{ + rotate(state); +} +else if (stack !() == comb ![ 2, 3, 1 ] / * bca * /) +{ + swap(state); +} +else if (stack !() == comb ![ 2, 1, 3 ] / * bac * /) +{ + rotate(state); +} +else if (stack !() == comb ![ 3, 2, 1 ] / * cba * /) +{ +} +else if (stack !() == comb ![ 3, 1, 2 ] / * cab * /) +{ + rotate(state); + swap(state); + rev_rotate(state); +} +} +*/ +// 2 3 1 +bool sort_i64(t_i64 *lhs, t_i64 *rhs) +{ + return (*lhs <= *rhs); +} + +void sort_3_inner(t_state *state, t_i64 data[3], t_i64 sorted[3]) +{ + if ((data[0] == sorted[0]) && (data[1] == sorted[1]) + && (data[2] == sorted[2])) + (swap_a(state), rev_rotate_a(state)); + else if ((data[0] == sorted[0]) && (data[1] == sorted[2]) + && (data[2] == sorted[1])) + rotate_a(state); + else if ((data[0] == sorted[1]) && (data[1] == sorted[2]) + && (data[2] == sorted[0])) + swap_a(state); + else if ((data[0] == sorted[1]) && (data[1] == sorted[0]) + && (data[2] == sorted[2])) + rev_rotate_a(state); + else if ((data[0] == sorted[2]) && (data[1] == sorted[0]) + && (data[2] == sorted[1])) + (rotate_a(state), swap_a(state), rev_rotate_a(state)); +} + +void sort_3(t_state *state) +{ + t_i64 array[3]; + t_vec_i64 why_do_i_do_this; + + why_do_i_do_this.free_func = NULL; + why_do_i_do_this.len = 3; + why_do_i_do_this.capacity = 3; + why_do_i_do_this.buffer = array; + array[0] = state->stack_a.buffer[0]; + array[1] = state->stack_a.buffer[1]; + array[2] = state->stack_a.buffer[2]; + vec_i64_sort(&why_do_i_do_this, sort_i64); + vec_i64_reverse(&why_do_i_do_this); + sort_3_inner(state, state->stack_a.buffer, array); +} diff --git a/src/app/sort5.c b/src/app/sort5.c new file mode 100644 index 0000000..8c23853 --- /dev/null +++ b/src/app/sort5.c @@ -0,0 +1,139 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sort5.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/08 15:50:10 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 18:50:53 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/find_place.h" +#include "app/state.h" +#include "me/printf/printf.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" +#include "me/vec/vec_i64_bool.h" +#include "me/mem/mem_compare.h" +#include "me/mem/mem_copy.h" + +void push_a(void *s); +void push_b(void *s); +void swap_a(void *s); +void swap_b(void *s); +void swap_both(void *s); +void rotate_a(void *s); +void rotate_b(void *s); +void rotate_both(void *s); +void rev_rotate_a(void *s); +void rev_rotate_b(void *s); +void rev_rotate_both(void *s); +void sort_2(t_state *state); + +bool sort_i64(t_i64 *lhs, t_i64 *rhs); + +bool sort_i64_other_way(t_i64 *lhs, t_i64 *rhs) +{ + return (*lhs >= *rhs); +} + +void sort_5_inner(t_state *state, t_i64 data[3], t_i64 sorted[3]) +{ + if ((data[0] == sorted[0]) && (data[1] == sorted[1]) + && (data[2] == sorted[2])) + (swap_a(state), rev_rotate_a(state)); + else if ((data[0] == sorted[0]) && (data[1] == sorted[2]) + && (data[2] == sorted[1])) + rotate_a(state); + else if ((data[0] == sorted[1]) && (data[1] == sorted[2]) + && (data[2] == sorted[0])) + swap_a(state); + else if ((data[0] == sorted[1]) && (data[1] == sorted[0]) + && (data[2] == sorted[2])) + rev_rotate_a(state); + else if ((data[0] == sorted[2]) && (data[1] == sorted[0]) + && (data[2] == sorted[1])) + (rotate_a(state), swap_a(state), rev_rotate_a(state)); +} + +void sort_5_inner2(t_state *state) +{ + t_usize index; + + make_sorted_true_from_stack(state, &state->stack_a); + index = find_place(state->stack_b.buffer[0], state); + if (index == 0) + push_a(state); + else if (index == 1) + (push_a(state), swap_a(state)); + else if (index == 2) + (rotate_a(state), push_a(state), swap_a(state), rev_rotate_a(state)); + else if (index == 3) + (push_a(state), rotate_a(state)); + make_sorted_true_from_stack(state, &state->stack_a); + index = find_place(state->stack_b.buffer[0], state); + if (index == 0) + push_a(state); + else if (index == 1) + (push_a(state), swap_a(state)); + else if (index == 2) + (rotate_a(state), push_a(state), swap_a(state), rev_rotate_a(state)); + else if (index == 3) + (rev_rotate_a(state), push_a(state), rotate_a(state), rotate_a(state)); + else if (index == 4) + (push_a(state), rotate_a(state)); + make_sorted_true_from_stack(state, &state->stack_a); +} + +bool sort_5_specialized(t_state *state, t_i64 cur[5]) +{ + t_i64 target[5]; + t_vec_i64 why_do_i_do_this; + + why_do_i_do_this.free_func = NULL; + why_do_i_do_this.len = 5; + why_do_i_do_this.capacity = 5; + why_do_i_do_this.buffer = target; + mem_copy(target, state->stack_a.buffer, sizeof(t_i64) * 5); + vec_i64_sort(&why_do_i_do_this, sort_i64); + if (cur[0] == target[3 - 1] && cur[1] == target[4 - 1] && cur[2] == \ + target[1 - 1] && cur[3] == target[5 - 1] && cur[4] == target[2 - 1]) + return (me_printf("pb\npb\nra\nsa\npa\nsa\npa\nsa\nrra\n"), true); + if (cur[0] == target[3 - 1] && cur[1] == target[4 - 1] && cur[2] == \ + target[5 - 1] && cur[3] == target[2 - 1] && cur[4] == target[1 - 1]) + return (me_printf("pb\npb\nsb\nsa\npa\nsa\nrra\nrra\npa\nra\nra\n"), + true); + if (cur[0] == target[4 - 1] && cur[1] == target[3 - 1] && cur[2] == \ + target[1 - 1] && cur[3] == target[5 - 1] && cur[4] == target[2 - 1]) + return (me_printf("pb\npb\nra\nsa\npa\nsa\nrra\nrra\npa\nra\nra\n"), + true); + if (cur[0] == target[4 - 1] && cur[1] == target[3 - 1] && cur[2] == \ + target[5 - 1] && cur[3] == target[2 - 1] && cur[4] == target[1 - 1]) + return (me_printf("pb\npb\nsb\nsa\npa\nsa\npa\nsa\nrra\n"), true); + return (false); +} + +void sort_5(t_state *state) +{ + t_i64 array[3]; + t_vec_i64 why_do_i_do_this; + + if (sort_5_specialized(state, state->stack_a.buffer)) + return ; + push_b(state); + push_b(state); + if (state->stack_b.buffer[0] >= state->stack_a.buffer[1]) + swap_b(state); + why_do_i_do_this.free_func = NULL; + why_do_i_do_this.len = 3; + why_do_i_do_this.capacity = 3; + why_do_i_do_this.buffer = array; + array[0] = state->stack_a.buffer[0]; + array[1] = state->stack_a.buffer[1]; + array[2] = state->stack_a.buffer[2]; + vec_i64_sort(&why_do_i_do_this, sort_i64_other_way); + sort_5_inner(state, state->stack_a.buffer, array); + sort_5_inner2(state); +} diff --git a/src/app/state.c b/src/app/state.c new file mode 100644 index 0000000..7d5e2dc --- /dev/null +++ b/src/app/state.c @@ -0,0 +1,91 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* state.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:00:12 by maiboyer #+# #+# */ +/* Updated: 2024/02/15 17:42:13 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/state.h" +#include "app/types/type_i64_bool.h" +#include "me/convert/str_to_numbers.h" +#include "me/printf/printf.h" +#include "me/string/str_n_compare.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" +#include "me/vec/vec_i64_bool.h" +#include +#include + +#if BONUS == 1 + +# define ERR_INVALID_NUM "Error\n" +# define ERR_DUPLICATE "Error\n" + +#else + +//# define ERR_INVALID_NUM "Error:\nInvalid Number\n" +//# define ERR_DUPLICATE "Error:\nDuplicate Number\n" +# define ERR_INVALID_NUM "Error\n" +# define ERR_DUPLICATE "Error\n" + +#endif + +bool sort_i64_bool(t_i64_bool *lhs, t_i64_bool *rhs) +{ + return (lhs->value <= rhs->value); +} + +void free_state(t_state state) +{ + vec_i64_free(state.stack_a); + vec_i64_free(state.stack_b); + vec_i64_bool_free(state.sorted); +} + +bool duplicate_check(t_state *state) +{ + t_i64 last; + t_usize index; + + if (state->sorted.len == 0) + return (false); + index = 1; + last = state->sorted.buffer[0].value; + while (index < state->sorted.len) + { + if (last == state->sorted.buffer[index].value) + return (true); + last = state->sorted.buffer[index++].value; + } + return (false); +} + +t_state parses_arguments(t_usize count, t_str nums[]) +{ + t_state state; + t_i32 atoi; + t_usize i; + + state.stack_a = vec_i64_new(count, NULL); + state.stack_b = vec_i64_new(count, NULL); + state.sorted = vec_i64_bool_new(count, NULL); + i = 0; + while (i < count) + { + if (str_to_i32(nums[i], 10, &atoi)) + (free_state(state), me_eprintf(ERR_INVALID_NUM), exit(1)); + vec_i64_push(&state.stack_a, atoi); + vec_i64_bool_push(&state.sorted, (t_i64_bool){.value = atoi, + .active = false}); + i++; + } + vec_i64_bool_sort(&state.sorted, sort_i64_bool); + if (duplicate_check(&state)) + (free_state(state), me_eprintf(ERR_DUPLICATE), exit(1)); + return (state); +} diff --git a/src/app/target.c b/src/app/target.c new file mode 100644 index 0000000..1444454 --- /dev/null +++ b/src/app/target.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* target.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/29 20:30:27 by maiboyer #+# #+# */ +/* Updated: 2024/02/02 22:40:55 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/rotate.h" +#include "app/target.h" + +t_rotation target(t_usize from, t_usize to, t_usize ring_size) +{ + if (ring_size == 0) + ring_size++; + if (from > to) + return (reverse(from - to, ring_size)); + else + return (forward(to - from, ring_size)); +} diff --git a/src/bonus/move1.c b/src/bonus/move1.c new file mode 100644 index 0000000..6ca0754 --- /dev/null +++ b/src/bonus/move1.c @@ -0,0 +1,11 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* move1.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/08 19:15:21 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 19:15:21 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ diff --git a/src/buffered_str/mod.c b/src/buffered_str/mod.c new file mode 100644 index 0000000..f408679 --- /dev/null +++ b/src/buffered_str/mod.c @@ -0,0 +1,82 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mod.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/16 17:52:12 by maiboyer #+# #+# */ +/* Updated: 2023/12/26 19:54:07 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/buffered_str/buf_str.h" +#include "me/mem/mem_alloc.h" +#include "me/mem/mem_set_zero.h" +#include "me/string/str_l_cat.h" +#include "me/string/str_l_copy.h" +#include "me/string/str_len.h" +#include "me/types.h" +#include + +bool push_str_buffer(t_buffer_str *buf, t_const_str to_push) +{ + t_usize to_push_len; + t_str temp_buffer; + t_usize new_capacity; + + if (buf == NULL || to_push == NULL) + return (true); + to_push_len = str_len(to_push); + while (buf->len + to_push_len + 2 > buf->capacity) + { + new_capacity = (buf->capacity * 3) / 2 + 1; + temp_buffer = mem_alloc(new_capacity); + if (temp_buffer == NULL) + return (true); + str_l_copy(temp_buffer, buf->buf, new_capacity); + free(buf->buf); + buf->buf = temp_buffer; + buf->capacity = new_capacity; + } + buf->len += to_push_len; + str_l_cat(buf->buf, to_push, buf->capacity); + return (false); +} + +bool push_str_char(t_buffer_str *buf, char to_push) +{ + char push_str[2]; + + push_str[0] = to_push; + push_str[1] = 0; + return (push_str_buffer(buf, push_str)); +} + +void str_clear(t_buffer_str *buf) +{ + mem_set_zero(buf->buf, buf->capacity); + buf->len = 0; + return ; +} + +t_buffer_str alloc_new_buffer(t_usize capacity) +{ + t_buffer_str out; + t_str buf; + + if (capacity == 0) + capacity = 16; + buf = mem_alloc(sizeof(char) * capacity); + if (buf == NULL) + { + out.buf = NULL; + out.capacity = 0; + out.len = 0; + return (out); + } + out.buf = buf; + out.capacity = capacity; + out.len = 0; + return (out); +} diff --git a/src/char/isalnum.c b/src/char/isalnum.c new file mode 100644 index 0000000..c4ebcfc --- /dev/null +++ b/src/char/isalnum.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* isalnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 16:19:40 by maiboyer #+# #+# */ +/* Updated: 2023/11/09 19:38:51 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalnum.h" +#include "me/char/isalpha.h" +#include "me/char/isdigit.h" + +bool me_isalnum(char chr) +{ + return (me_isalpha(chr) || me_isdigit(chr)); +} diff --git a/src/char/isalpha.c b/src/char/isalpha.c new file mode 100644 index 0000000..bffddf0 --- /dev/null +++ b/src/char/isalpha.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* isalpha.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 16:19:40 by maiboyer #+# #+# */ +/* Updated: 2023/11/08 04:01:25 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" + +bool me_isalpha(char chr) +{ + return (('z' >= chr && chr >= 'a') || ('Z' >= chr && chr >= 'A')); +} diff --git a/src/char/isascii.c b/src/char/isascii.c new file mode 100644 index 0000000..208a84f --- /dev/null +++ b/src/char/isascii.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* isascii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 17:51:01 by maiboyer #+# #+# */ +/* Updated: 2023/11/09 18:25:14 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isascii.h" + +bool me_isascii(char chr) +{ + return (0 <= chr && chr <= 127); +} diff --git a/src/char/isdigit.c b/src/char/isdigit.c new file mode 100644 index 0000000..f75d219 --- /dev/null +++ b/src/char/isdigit.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 16:19:40 by maiboyer #+# #+# */ +/* Updated: 2023/11/04 16:59:06 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isdigit.h" + +bool me_isdigit(char chr) +{ + return (chr >= '0' && chr <= '9'); +} diff --git a/src/char/islower.c b/src/char/islower.c new file mode 100644 index 0000000..384e88c --- /dev/null +++ b/src/char/islower.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* islower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 16:19:40 by maiboyer #+# #+# */ +/* Updated: 2023/11/04 16:58:56 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/islower.h" + +bool me_islower(char chr) +{ + return (chr >= 'a' && chr <= 'z'); +} diff --git a/src/char/isprint.c b/src/char/isprint.c new file mode 100644 index 0000000..f45c9ee --- /dev/null +++ b/src/char/isprint.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* isprint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 16:19:40 by maiboyer #+# #+# */ +/* Updated: 2023/11/04 16:42:38 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isprint.h" + +bool me_isprint(char chr) +{ + return (chr >= ' ' && chr <= '~'); +} diff --git a/src/char/isspace.c b/src/char/isspace.c new file mode 100644 index 0000000..1757f34 --- /dev/null +++ b/src/char/isspace.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* isspace.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 14:26:25 by maiboyer #+# #+# */ +/* Updated: 2023/11/08 02:39:24 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isspace.h" + +bool me_isspace(char chr) +{ + return (chr == ' ' || chr == '\f' || chr == '\n' || chr == '\r' + || chr == '\t' || chr == '\v'); +} diff --git a/src/char/isupper.c b/src/char/isupper.c new file mode 100644 index 0000000..405cb09 --- /dev/null +++ b/src/char/isupper.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* isupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 16:19:40 by maiboyer #+# #+# */ +/* Updated: 2023/11/08 04:01:59 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isupper.h" + +bool me_isupper(char chr) +{ + return ('Z' >= chr && chr >= 'A'); +} diff --git a/src/char/tolower.c b/src/char/tolower.c new file mode 100644 index 0000000..4ee817c --- /dev/null +++ b/src/char/tolower.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tolower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 16:47:50 by maiboyer #+# #+# */ +/* Updated: 2023/11/04 16:56:03 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isupper.h" +#include "me/char/tolower.h" + +bool me_tolower(char chr) +{ + if (me_isupper(chr)) + return (chr + ('a' - 'A')); + else + return (chr); +} diff --git a/src/char/toupper.c b/src/char/toupper.c new file mode 100644 index 0000000..9888b7e --- /dev/null +++ b/src/char/toupper.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* toupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 16:47:50 by maiboyer #+# #+# */ +/* Updated: 2023/11/04 16:51:28 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/islower.h" +#include "me/char/toupper.h" + +bool me_toupper(char chr) +{ + if (me_islower(chr)) + return (chr - ('a' - 'A')); + else + return (chr); +} diff --git a/src/convert/atoi.c b/src/convert/atoi.c new file mode 100644 index 0000000..076e6ef --- /dev/null +++ b/src/convert/atoi.c @@ -0,0 +1,61 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 14:14:00 by maiboyer #+# #+# */ +/* Updated: 2024/01/11 15:37:28 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isdigit.h" +#include "me/char/isspace.h" +#include "me/convert/atoi.h" + +t_i32 me_atoi(t_const_str str) +{ + t_u64 out; + t_u64 sign; + t_usize i; + + out = 0; + i = 0; + sign = 1; + while (me_isspace(str[i])) + i++; + if (str[i] == '+' || str[i] == '-') + if (str[i++] == '-') + sign = -1; + while (me_isdigit(str[i])) + { + out *= 10; + out += str[i] - '0'; + i++; + } + return ((t_i32)(out * sign)); +} + +t_i64 me_atoi_64(t_const_str str) +{ + t_u64 out; + t_u64 sign; + t_usize i; + + out = 0; + i = 0; + sign = 1; + while (me_isspace(str[i])) + i++; + if (str[i] == '+' || str[i] == '-') + if (str[i++] == '-') + sign = -1; + while (me_isdigit(str[i])) + { + out *= 10; + out += str[i] - '0'; + i++; + } + return ((t_i64)(out * sign)); +} diff --git a/src/convert/itoa.c b/src/convert/itoa.c new file mode 100644 index 0000000..426d84c --- /dev/null +++ b/src/convert/itoa.c @@ -0,0 +1,69 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/03 21:05:46 by maiboyer #+# #+# */ +/* Updated: 2023/11/10 14:56:42 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/convert/itoa.h" +#include "me/mem/mem_set.h" +#include "me/string/str_clone.h" +#include + +static void me_itoa_inner(t_u64 nb, t_str out) +{ + t_i32 modulus; + bool need_print; + char c; + t_usize idx; + + need_print = false; + modulus = 1000000000; + idx = 0; + while (modulus) + { + c = (char)(nb / modulus) + '0'; + if (c != '0' || need_print) + { + out[idx++] = c; + need_print = true; + } + nb = nb % modulus; + modulus /= 10; + } +} + +t_str me_itoa(t_i32 nb) +{ + char out[12]; + t_u64 n; + + n = (t_u64)nb; + mem_set(out, 0, 12); + if (nb < 0) + { + out[0] = '-'; + me_itoa_inner(-n, out + 1); + } + else if (nb == 0) + out[0] = '0'; + else + me_itoa_inner(n, out); + return (str_clone(out)); +} +/*R +int main(void) +{ + me_putnbr(-2147483648); + write(1, "\n", 1); + me_putnbr(0); + write(1, "\n", 1); + me_putnbr(12345); + return (0); +} +R*/ diff --git a/src/convert/str_to_i16.c b/src/convert/str_to_i16.c new file mode 100644 index 0000000..b1d2bc8 --- /dev/null +++ b/src/convert/str_to_i16.c @@ -0,0 +1,129 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/string/str_len.h" +#include "me/types.h" + +#define OP_ADD 0b0001 +#define OP_SUB 0b0010 +#define OP_MUL 0b0100 +#define OP_CHK 0b1000 + +t_error checked_add_i16(t_i16 lhs, t_i16 rhs, t_i16 *out); +t_error checked_sub_i16(t_i16 lhs, t_i16 rhs, t_i16 *out); +t_error checked_mul_i16(t_i16 lhs, t_i16 rhs, t_i16 *out); + +static inline bool can_not_overflow(t_u32 radix, bool is_signed_type, + t_usize digits_len) +{ + return (radix <= 16 && digits_len <= sizeof(t_i16) * 2 + - (t_usize)is_signed_type); +} + +static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out) +{ + t_u32 digit; + + if (radix < 2 || radix > 36) + return (ERROR); + digit = ascii - '0'; + if (radix > 10 && digit >= 10) + { + if (!me_isalpha(ascii)) + return (ERROR); + digit = ((ascii | 0b100000) - 'a') + 10; + } + if (digit >= radix) + return (ERROR); + *out = digit; + return (NO_ERROR); +} + +static inline t_error do_operation(t_i16 digit, t_u8 op, t_i16 *result) +{ + t_i16 rhs; + t_i16 res; + + rhs = *result; + res = *result; + if (op & OP_CHK) + { + if (op & OP_MUL && checked_mul_i16(rhs, digit, &res)) + return (ERROR); + else if (op & OP_ADD && checked_add_i16(rhs, digit, &res)) + return (ERROR); + else if (op & OP_SUB && checked_sub_i16(rhs, digit, &res)) + return (ERROR); + } + else + { + if (op & OP_MUL) + res = rhs * digit; + else if (op & OP_ADD) + res = rhs + digit; + else if (op & OP_SUB) + res = rhs - digit; + } + *result = res; + return (NO_ERROR); +} + +static inline t_error loop_inner(t_const_str s, t_u32 radix, t_u8 op, + t_i16 *out) +{ + t_u32 digit; + t_i16 result; + + result = 0; + while (*s) + { + if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result)) + return (ERROR); + if (to_digit(*s, radix, &digit)) + return (ERROR); + if (do_operation(digit, op, &result)) + return (ERROR); + s++; + } + if (out) + *out = result; + return (NO_ERROR); +} + +t_error str_to_i16(t_const_str s, t_u32 radix, t_i16 *out) +{ + t_usize digits_len; + bool is_positive; + t_u8 op; + + if (radix < 2 || radix > 36) + return (ERROR); + digits_len = str_len(s); + is_positive = true; + if (digits_len == 0) + return (ERROR); + if ((s[0] == '-' || s[0] == '+') && s[1] == '\0') + return (ERROR); + else if (s[0] == '+') + is_positive = (s++, digits_len--, true); + else if (s[0] == '-' && true) + is_positive = (s++, digits_len--, false); + if (is_positive) + op = OP_ADD; + else + op = OP_SUB; + if (!can_not_overflow(radix, true, digits_len)) + op |= OP_CHK; + return (loop_inner(s, radix, op, out)); +} diff --git a/src/convert/str_to_i16_utils.c b/src/convert/str_to_i16_utils.c new file mode 100644 index 0000000..4ecbbe2 --- /dev/null +++ b/src/convert/str_to_i16_utils.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i16_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 18:55:47 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#include "me/types.h" + +t_error checked_add_i16(t_i16 lhs, t_i16 rhs, t_i16 *out) +{ + if (rhs > 0 && (lhs > 32767 - rhs)) + return (ERROR); + *out = (t_i16)(lhs + rhs); + return (NO_ERROR); +} + +t_error checked_sub_i16(t_i16 lhs, t_i16 rhs, t_i16 *out) +{ + if ((((rhs & (1 << (sizeof(t_i16) - 1)) || rhs == 0) || !true) && (lhs < \ + -32768 + rhs))) + return (ERROR); + *out = (t_i16)(lhs - rhs); + return (NO_ERROR); +} + +t_error checked_mul_i16(t_i16 lhs, t_i16 rhs, t_i16 *out) +{ + t_i16 mul; + + mul = lhs * rhs; + if (lhs != 0 && mul / lhs != rhs) + return (ERROR); + *out = mul; + return (NO_ERROR); +} diff --git a/src/convert/str_to_i32.c b/src/convert/str_to_i32.c new file mode 100644 index 0000000..4956029 --- /dev/null +++ b/src/convert/str_to_i32.c @@ -0,0 +1,129 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/string/str_len.h" +#include "me/types.h" + +#define OP_ADD 0b0001 +#define OP_SUB 0b0010 +#define OP_MUL 0b0100 +#define OP_CHK 0b1000 + +t_error checked_add_i32(t_i32 lhs, t_i32 rhs, t_i32 *out); +t_error checked_sub_i32(t_i32 lhs, t_i32 rhs, t_i32 *out); +t_error checked_mul_i32(t_i32 lhs, t_i32 rhs, t_i32 *out); + +static inline bool can_not_overflow(t_u32 radix, bool is_signed_type, + t_usize digits_len) +{ + return (radix <= 16 && digits_len <= sizeof(t_i32) * 2 + - (t_usize)is_signed_type); +} + +static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out) +{ + t_u32 digit; + + if (radix < 2 || radix > 36) + return (ERROR); + digit = ascii - '0'; + if (radix > 10 && digit >= 10) + { + if (!me_isalpha(ascii)) + return (ERROR); + digit = ((ascii | 0b100000) - 'a') + 10; + } + if (digit >= radix) + return (ERROR); + *out = digit; + return (NO_ERROR); +} + +static inline t_error do_operation(t_i32 digit, t_u8 op, t_i32 *result) +{ + t_i32 rhs; + t_i32 res; + + rhs = *result; + res = *result; + if (op & OP_CHK) + { + if (op & OP_MUL && checked_mul_i32(rhs, digit, &res)) + return (ERROR); + else if (op & OP_ADD && checked_add_i32(rhs, digit, &res)) + return (ERROR); + else if (op & OP_SUB && checked_sub_i32(rhs, digit, &res)) + return (ERROR); + } + else + { + if (op & OP_MUL) + res = rhs * digit; + else if (op & OP_ADD) + res = rhs + digit; + else if (op & OP_SUB) + res = rhs - digit; + } + *result = res; + return (NO_ERROR); +} + +static inline t_error loop_inner(t_const_str s, t_u32 radix, t_u8 op, + t_i32 *out) +{ + t_u32 digit; + t_i32 result; + + result = 0; + while (*s) + { + if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result)) + return (ERROR); + if (to_digit(*s, radix, &digit)) + return (ERROR); + if (do_operation(digit, op, &result)) + return (ERROR); + s++; + } + if (out) + *out = result; + return (NO_ERROR); +} + +t_error str_to_i32(t_const_str s, t_u32 radix, t_i32 *out) +{ + t_usize digits_len; + bool is_positive; + t_u8 op; + + if (radix < 2 || radix > 36) + return (ERROR); + digits_len = str_len(s); + is_positive = true; + if (digits_len == 0) + return (ERROR); + if ((s[0] == '-' || s[0] == '+') && s[1] == '\0') + return (ERROR); + else if (s[0] == '+') + is_positive = (s++, digits_len--, true); + else if (s[0] == '-' && true) + is_positive = (s++, digits_len--, false); + if (is_positive) + op = OP_ADD; + else + op = OP_SUB; + if (!can_not_overflow(radix, true, digits_len)) + op |= OP_CHK; + return (loop_inner(s, radix, op, out)); +} diff --git a/src/convert/str_to_i32_utils.c b/src/convert/str_to_i32_utils.c new file mode 100644 index 0000000..5b6f5fd --- /dev/null +++ b/src/convert/str_to_i32_utils.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i32_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 18:56:06 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#include "me/types.h" + +t_error checked_add_i32(t_i32 lhs, t_i32 rhs, t_i32 *out) +{ + if (rhs > 0 && (lhs > 2147483647 - rhs)) + return (ERROR); + *out = (t_i32)(lhs + rhs); + return (NO_ERROR); +} + +t_error checked_sub_i32(t_i32 lhs, t_i32 rhs, t_i32 *out) +{ + if ((((rhs & (1 << (sizeof(t_i32) - 1)) || rhs == 0) || !true) && (lhs < \ + -2147483648 + rhs))) + return (ERROR); + *out = (t_i32)(lhs - rhs); + return (NO_ERROR); +} + +t_error checked_mul_i32(t_i32 lhs, t_i32 rhs, t_i32 *out) +{ + t_i32 mul; + + mul = lhs * rhs; + if (lhs != 0 && mul / lhs != rhs) + return (ERROR); + *out = mul; + return (NO_ERROR); +} diff --git a/src/convert/str_to_i64.c b/src/convert/str_to_i64.c new file mode 100644 index 0000000..56a48cc --- /dev/null +++ b/src/convert/str_to_i64.c @@ -0,0 +1,129 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/string/str_len.h" +#include "me/types.h" + +#define OP_ADD 0b0001 +#define OP_SUB 0b0010 +#define OP_MUL 0b0100 +#define OP_CHK 0b1000 + +t_error checked_add_i64(t_i64 lhs, t_i64 rhs, t_i64 *out); +t_error checked_sub_i64(t_i64 lhs, t_i64 rhs, t_i64 *out); +t_error checked_mul_i64(t_i64 lhs, t_i64 rhs, t_i64 *out); + +static inline bool can_not_overflow(t_u32 radix, bool is_signed_type, + t_usize digits_len) +{ + return (radix <= 16 && digits_len <= sizeof(t_i64) * 2 + - (t_usize)is_signed_type); +} + +static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out) +{ + t_u32 digit; + + if (radix < 2 || radix > 36) + return (ERROR); + digit = ascii - '0'; + if (radix > 10 && digit >= 10) + { + if (!me_isalpha(ascii)) + return (ERROR); + digit = ((ascii | 0b100000) - 'a') + 10; + } + if (digit >= radix) + return (ERROR); + *out = digit; + return (NO_ERROR); +} + +static inline t_error do_operation(t_i64 digit, t_u8 op, t_i64 *result) +{ + t_i64 rhs; + t_i64 res; + + rhs = *result; + res = *result; + if (op & OP_CHK) + { + if (op & OP_MUL && checked_mul_i64(rhs, digit, &res)) + return (ERROR); + else if (op & OP_ADD && checked_add_i64(rhs, digit, &res)) + return (ERROR); + else if (op & OP_SUB && checked_sub_i64(rhs, digit, &res)) + return (ERROR); + } + else + { + if (op & OP_MUL) + res = rhs * digit; + else if (op & OP_ADD) + res = rhs + digit; + else if (op & OP_SUB) + res = rhs - digit; + } + *result = res; + return (NO_ERROR); +} + +static inline t_error loop_inner(t_const_str s, t_u32 radix, t_u8 op, + t_i64 *out) +{ + t_u32 digit; + t_i64 result; + + result = 0ll; + while (*s) + { + if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result)) + return (ERROR); + if (to_digit(*s, radix, &digit)) + return (ERROR); + if (do_operation(digit, op, &result)) + return (ERROR); + s++; + } + if (out) + *out = result; + return (NO_ERROR); +} + +t_error str_to_i64(t_const_str s, t_u32 radix, t_i64 *out) +{ + t_usize digits_len; + bool is_positive; + t_u8 op; + + if (radix < 2 || radix > 36) + return (ERROR); + digits_len = str_len(s); + is_positive = true; + if (digits_len == 0) + return (ERROR); + if ((s[0] == '-' || s[0] == '+') && s[1] == '\0') + return (ERROR); + else if (s[0] == '+') + is_positive = (s++, digits_len--, true); + else if (s[0] == '-' && true) + is_positive = (s++, digits_len--, false); + if (is_positive) + op = OP_ADD; + else + op = OP_SUB; + if (!can_not_overflow(radix, true, digits_len)) + op |= OP_CHK; + return (loop_inner(s, radix, op, out)); +} diff --git a/src/convert/str_to_i64_utils.c b/src/convert/str_to_i64_utils.c new file mode 100644 index 0000000..1dd268d --- /dev/null +++ b/src/convert/str_to_i64_utils.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 18:55:37 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#include "me/types.h" + +t_error checked_add_i64(t_i64 lhs, t_i64 rhs, t_i64 *out) +{ + if (rhs > 0 && (lhs > 9223372036854775807ll - rhs)) + return (ERROR); + *out = (t_i64)(lhs + rhs); + return (NO_ERROR); +} + +t_error checked_sub_i64(t_i64 lhs, t_i64 rhs, t_i64 *out) +{ + if ((((rhs & (1 << (sizeof(t_i64) - 1)) || rhs == 0) || !true) && (lhs < \ + -(~9223372036854775807ll + 1) + rhs))) + return (ERROR); + *out = (t_i64)(lhs - rhs); + return (NO_ERROR); +} + +t_error checked_mul_i64(t_i64 lhs, t_i64 rhs, t_i64 *out) +{ + t_i64 mul; + + mul = lhs * rhs; + if (lhs != 0 && mul / lhs != rhs) + return (ERROR); + *out = mul; + return (NO_ERROR); +} diff --git a/src/convert/str_to_i8.c b/src/convert/str_to_i8.c new file mode 100644 index 0000000..80f91a0 --- /dev/null +++ b/src/convert/str_to_i8.c @@ -0,0 +1,129 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/string/str_len.h" +#include "me/types.h" + +#define OP_ADD 0b0001 +#define OP_SUB 0b0010 +#define OP_MUL 0b0100 +#define OP_CHK 0b1000 + +t_error checked_add_i8(t_i8 lhs, t_i8 rhs, t_i8 *out); +t_error checked_sub_i8(t_i8 lhs, t_i8 rhs, t_i8 *out); +t_error checked_mul_i8(t_i8 lhs, t_i8 rhs, t_i8 *out); + +static inline bool can_not_overflow(t_u32 radix, bool is_signed_type, + t_usize digits_len) +{ + return (radix <= 16 && digits_len <= sizeof(t_i8) * 2 + - (t_usize)is_signed_type); +} + +static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out) +{ + t_u32 digit; + + if (radix < 2 || radix > 36) + return (ERROR); + digit = ascii - '0'; + if (radix > 10 && digit >= 10) + { + if (!me_isalpha(ascii)) + return (ERROR); + digit = ((ascii | 0b100000) - 'a') + 10; + } + if (digit >= radix) + return (ERROR); + *out = digit; + return (NO_ERROR); +} + +static inline t_error do_operation(t_i8 digit, t_u8 op, t_i8 *result) +{ + t_i8 rhs; + t_i8 res; + + rhs = *result; + res = *result; + if (op & OP_CHK) + { + if (op & OP_MUL && checked_mul_i8(rhs, digit, &res)) + return (ERROR); + else if (op & OP_ADD && checked_add_i8(rhs, digit, &res)) + return (ERROR); + else if (op & OP_SUB && checked_sub_i8(rhs, digit, &res)) + return (ERROR); + } + else + { + if (op & OP_MUL) + res = rhs * digit; + else if (op & OP_ADD) + res = rhs + digit; + else if (op & OP_SUB) + res = rhs - digit; + } + *result = res; + return (NO_ERROR); +} + +static inline t_error loop_inner(t_const_str s, t_u32 radix, t_u8 op, + t_i8 *out) +{ + t_u32 digit; + t_i8 result; + + result = 0; + while (*s) + { + if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result)) + return (ERROR); + if (to_digit(*s, radix, &digit)) + return (ERROR); + if (do_operation(digit, op, &result)) + return (ERROR); + s++; + } + if (out) + *out = result; + return (NO_ERROR); +} + +t_error str_to_i8(t_const_str s, t_u32 radix, t_i8 *out) +{ + t_usize digits_len; + bool is_positive; + t_u8 op; + + if (radix < 2 || radix > 36) + return (ERROR); + digits_len = str_len(s); + is_positive = true; + if (digits_len == 0) + return (ERROR); + if ((s[0] == '-' || s[0] == '+') && s[1] == '\0') + return (ERROR); + else if (s[0] == '+') + is_positive = (s++, digits_len--, true); + else if (s[0] == '-' && true) + is_positive = (s++, digits_len--, false); + if (is_positive) + op = OP_ADD; + else + op = OP_SUB; + if (!can_not_overflow(radix, true, digits_len)) + op |= OP_CHK; + return (loop_inner(s, radix, op, out)); +} diff --git a/src/convert/str_to_i8_utils.c b/src/convert/str_to_i8_utils.c new file mode 100644 index 0000000..2073dd6 --- /dev/null +++ b/src/convert/str_to_i8_utils.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#include "me/types.h" + +t_error checked_add_i8(t_i8 lhs, t_i8 rhs, t_i8 *out) +{ + if (rhs > 0 && (lhs > 127 - rhs)) + return (ERROR); + *out = (t_i8)(lhs + rhs); + return (NO_ERROR); +} + +t_error checked_sub_i8(t_i8 lhs, t_i8 rhs, t_i8 *out) +{ + if ((((rhs & (1 << (sizeof(t_i8) - 1)) || rhs == 0) || !true) && (lhs < -128 + + rhs))) + return (ERROR); + *out = (t_i8)(lhs - rhs); + return (NO_ERROR); +} + +t_error checked_mul_i8(t_i8 lhs, t_i8 rhs, t_i8 *out) +{ + t_i8 mul; + + mul = lhs * rhs; + if (lhs != 0 && mul / lhs != rhs) + return (ERROR); + *out = mul; + return (NO_ERROR); +} diff --git a/src/convert/str_to_u16.c b/src/convert/str_to_u16.c new file mode 100644 index 0000000..7c5bbaa --- /dev/null +++ b/src/convert/str_to_u16.c @@ -0,0 +1,129 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/string/str_len.h" +#include "me/types.h" + +#define OP_ADD 0b0001 +#define OP_SUB 0b0010 +#define OP_MUL 0b0100 +#define OP_CHK 0b1000 + +t_error checked_add_u16(t_u16 lhs, t_u16 rhs, t_u16 *out); +t_error checked_sub_u16(t_u16 lhs, t_u16 rhs, t_u16 *out); +t_error checked_mul_u16(t_u16 lhs, t_u16 rhs, t_u16 *out); + +static inline bool can_not_overflow(t_u32 radix, bool is_signed_type, + t_usize digits_len) +{ + return (radix <= 16 && digits_len <= sizeof(t_u16) * 2 + - (t_usize)is_signed_type); +} + +static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out) +{ + t_u32 digit; + + if (radix < 2 || radix > 36) + return (ERROR); + digit = ascii - '0'; + if (radix > 10 && digit >= 10) + { + if (!me_isalpha(ascii)) + return (ERROR); + digit = ((ascii | 0b100000) - 'a') + 10; + } + if (digit >= radix) + return (ERROR); + *out = digit; + return (NO_ERROR); +} + +static inline t_error do_operation(t_u16 digit, t_u8 op, t_u16 *result) +{ + t_u16 rhs; + t_u16 res; + + rhs = *result; + res = *result; + if (op & OP_CHK) + { + if (op & OP_MUL && checked_mul_u16(rhs, digit, &res)) + return (ERROR); + else if (op & OP_ADD && checked_add_u16(rhs, digit, &res)) + return (ERROR); + else if (op & OP_SUB && checked_sub_u16(rhs, digit, &res)) + return (ERROR); + } + else + { + if (op & OP_MUL) + res = rhs * digit; + else if (op & OP_ADD) + res = rhs + digit; + else if (op & OP_SUB) + res = rhs - digit; + } + *result = res; + return (NO_ERROR); +} + +static inline t_error loop_inner(t_const_str s, t_u32 radix, t_u8 op, + t_u16 *out) +{ + t_u32 digit; + t_u16 result; + + result = 0u; + while (*s) + { + if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result)) + return (ERROR); + if (to_digit(*s, radix, &digit)) + return (ERROR); + if (do_operation(digit, op, &result)) + return (ERROR); + s++; + } + if (out) + *out = result; + return (NO_ERROR); +} + +t_error str_to_u16(t_const_str s, t_u32 radix, t_u16 *out) +{ + t_usize digits_len; + bool is_positive; + t_u8 op; + + if (radix < 2 || radix > 36) + return (ERROR); + digits_len = str_len(s); + is_positive = true; + if (digits_len == 0) + return (ERROR); + if ((s[0] == '-' || s[0] == '+') && s[1] == '\0') + return (ERROR); + else if (s[0] == '+') + is_positive = (s++, digits_len--, true); + else if (s[0] == '-' && false) + is_positive = (s++, digits_len--, false); + if (is_positive) + op = OP_ADD; + else + op = OP_SUB; + if (!can_not_overflow(radix, false, digits_len)) + op |= OP_CHK; + return (loop_inner(s, radix, op, out)); +} diff --git a/src/convert/str_to_u16_utils.c b/src/convert/str_to_u16_utils.c new file mode 100644 index 0000000..85fd7b2 --- /dev/null +++ b/src/convert/str_to_u16_utils.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#include "me/types.h" + +t_error checked_add_u16(t_u16 lhs, t_u16 rhs, t_u16 *out) +{ + if (rhs > 0 && (lhs > 65535u - rhs)) + return (ERROR); + *out = (t_u16)(lhs + rhs); + return (NO_ERROR); +} + +t_error checked_sub_u16(t_u16 lhs, t_u16 rhs, t_u16 *out) +{ + if ((((rhs & (1 << (sizeof(t_u16) - 1)) || rhs == 0) || !false) && (lhs < 0u + + rhs))) + return (ERROR); + *out = (t_u16)(lhs - rhs); + return (NO_ERROR); +} + +t_error checked_mul_u16(t_u16 lhs, t_u16 rhs, t_u16 *out) +{ + t_u16 mul; + + mul = lhs * rhs; + if (lhs != 0 && mul / lhs != rhs) + return (ERROR); + *out = mul; + return (NO_ERROR); +} diff --git a/src/convert/str_to_u32.c b/src/convert/str_to_u32.c new file mode 100644 index 0000000..1b625d1 --- /dev/null +++ b/src/convert/str_to_u32.c @@ -0,0 +1,129 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/string/str_len.h" +#include "me/types.h" + +#define OP_ADD 0b0001 +#define OP_SUB 0b0010 +#define OP_MUL 0b0100 +#define OP_CHK 0b1000 + +t_error checked_add_u32(t_u32 lhs, t_u32 rhs, t_u32 *out); +t_error checked_sub_u32(t_u32 lhs, t_u32 rhs, t_u32 *out); +t_error checked_mul_u32(t_u32 lhs, t_u32 rhs, t_u32 *out); + +static inline bool can_not_overflow(t_u32 radix, bool is_signed_type, + t_usize digits_len) +{ + return (radix <= 16 && digits_len <= sizeof(t_u32) * 2 + - (t_usize)is_signed_type); +} + +static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out) +{ + t_u32 digit; + + if (radix < 2 || radix > 36) + return (ERROR); + digit = ascii - '0'; + if (radix > 10 && digit >= 10) + { + if (!me_isalpha(ascii)) + return (ERROR); + digit = ((ascii | 0b100000) - 'a') + 10; + } + if (digit >= radix) + return (ERROR); + *out = digit; + return (NO_ERROR); +} + +static inline t_error do_operation(t_u32 digit, t_u8 op, t_u32 *result) +{ + t_u32 rhs; + t_u32 res; + + rhs = *result; + res = *result; + if (op & OP_CHK) + { + if (op & OP_MUL && checked_mul_u32(rhs, digit, &res)) + return (ERROR); + else if (op & OP_ADD && checked_add_u32(rhs, digit, &res)) + return (ERROR); + else if (op & OP_SUB && checked_sub_u32(rhs, digit, &res)) + return (ERROR); + } + else + { + if (op & OP_MUL) + res = rhs * digit; + else if (op & OP_ADD) + res = rhs + digit; + else if (op & OP_SUB) + res = rhs - digit; + } + *result = res; + return (NO_ERROR); +} + +static inline t_error loop_inner(t_const_str s, t_u32 radix, t_u8 op, + t_u32 *out) +{ + t_u32 digit; + t_u32 result; + + result = 0u; + while (*s) + { + if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result)) + return (ERROR); + if (to_digit(*s, radix, &digit)) + return (ERROR); + if (do_operation(digit, op, &result)) + return (ERROR); + s++; + } + if (out) + *out = result; + return (NO_ERROR); +} + +t_error str_to_u32(t_const_str s, t_u32 radix, t_u32 *out) +{ + t_usize digits_len; + bool is_positive; + t_u8 op; + + if (radix < 2 || radix > 36) + return (ERROR); + digits_len = str_len(s); + is_positive = true; + if (digits_len == 0) + return (ERROR); + if ((s[0] == '-' || s[0] == '+') && s[1] == '\0') + return (ERROR); + else if (s[0] == '+') + is_positive = (s++, digits_len--, true); + else if (s[0] == '-' && false) + is_positive = (s++, digits_len--, false); + if (is_positive) + op = OP_ADD; + else + op = OP_SUB; + if (!can_not_overflow(radix, false, digits_len)) + op |= OP_CHK; + return (loop_inner(s, radix, op, out)); +} diff --git a/src/convert/str_to_u32_utils.c b/src/convert/str_to_u32_utils.c new file mode 100644 index 0000000..3a929d5 --- /dev/null +++ b/src/convert/str_to_u32_utils.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#include "me/types.h" + +t_error checked_add_u32(t_u32 lhs, t_u32 rhs, t_u32 *out) +{ + if (rhs > 0 && (lhs > 4294967295u - rhs)) + return (ERROR); + *out = (t_u32)(lhs + rhs); + return (NO_ERROR); +} + +t_error checked_sub_u32(t_u32 lhs, t_u32 rhs, t_u32 *out) +{ + if ((((rhs & (1 << (sizeof(t_u32) - 1)) || rhs == 0) || !false) && (lhs < 0u + + rhs))) + return (ERROR); + *out = (t_u32)(lhs - rhs); + return (NO_ERROR); +} + +t_error checked_mul_u32(t_u32 lhs, t_u32 rhs, t_u32 *out) +{ + t_u32 mul; + + mul = lhs * rhs; + if (lhs != 0 && mul / lhs != rhs) + return (ERROR); + *out = mul; + return (NO_ERROR); +} diff --git a/src/convert/str_to_u64.c b/src/convert/str_to_u64.c new file mode 100644 index 0000000..b744be8 --- /dev/null +++ b/src/convert/str_to_u64.c @@ -0,0 +1,129 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/string/str_len.h" +#include "me/types.h" + +#define OP_ADD 0b0001 +#define OP_SUB 0b0010 +#define OP_MUL 0b0100 +#define OP_CHK 0b1000 + +t_error checked_add_u64(t_u64 lhs, t_u64 rhs, t_u64 *out); +t_error checked_sub_u64(t_u64 lhs, t_u64 rhs, t_u64 *out); +t_error checked_mul_u64(t_u64 lhs, t_u64 rhs, t_u64 *out); + +static inline bool can_not_overflow(t_u32 radix, bool is_signed_type, + t_usize digits_len) +{ + return (radix <= 16 && digits_len <= sizeof(t_u64) * 2 + - (t_usize)is_signed_type); +} + +static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out) +{ + t_u32 digit; + + if (radix < 2 || radix > 36) + return (ERROR); + digit = ascii - '0'; + if (radix > 10 && digit >= 10) + { + if (!me_isalpha(ascii)) + return (ERROR); + digit = ((ascii | 0b100000) - 'a') + 10; + } + if (digit >= radix) + return (ERROR); + *out = digit; + return (NO_ERROR); +} + +static inline t_error do_operation(t_u64 digit, t_u8 op, t_u64 *result) +{ + t_u64 rhs; + t_u64 res; + + rhs = *result; + res = *result; + if (op & OP_CHK) + { + if (op & OP_MUL && checked_mul_u64(rhs, digit, &res)) + return (ERROR); + else if (op & OP_ADD && checked_add_u64(rhs, digit, &res)) + return (ERROR); + else if (op & OP_SUB && checked_sub_u64(rhs, digit, &res)) + return (ERROR); + } + else + { + if (op & OP_MUL) + res = rhs * digit; + else if (op & OP_ADD) + res = rhs + digit; + else if (op & OP_SUB) + res = rhs - digit; + } + *result = res; + return (NO_ERROR); +} + +static inline t_error loop_inner(t_const_str s, t_u32 radix, t_u8 op, + t_u64 *out) +{ + t_u32 digit; + t_u64 result; + + result = 0llu; + while (*s) + { + if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result)) + return (ERROR); + if (to_digit(*s, radix, &digit)) + return (ERROR); + if (do_operation(digit, op, &result)) + return (ERROR); + s++; + } + if (out) + *out = result; + return (NO_ERROR); +} + +t_error str_to_u64(t_const_str s, t_u32 radix, t_u64 *out) +{ + t_usize digits_len; + bool is_positive; + t_u8 op; + + if (radix < 2 || radix > 36) + return (ERROR); + digits_len = str_len(s); + is_positive = true; + if (digits_len == 0) + return (ERROR); + if ((s[0] == '-' || s[0] == '+') && s[1] == '\0') + return (ERROR); + else if (s[0] == '+') + is_positive = (s++, digits_len--, true); + else if (s[0] == '-' && false) + is_positive = (s++, digits_len--, false); + if (is_positive) + op = OP_ADD; + else + op = OP_SUB; + if (!can_not_overflow(radix, false, digits_len)) + op |= OP_CHK; + return (loop_inner(s, radix, op, out)); +} diff --git a/src/convert/str_to_u64_utils.c b/src/convert/str_to_u64_utils.c new file mode 100644 index 0000000..f2bb77f --- /dev/null +++ b/src/convert/str_to_u64_utils.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#include "me/types.h" + +t_error checked_add_u64(t_u64 lhs, t_u64 rhs, t_u64 *out) +{ + if (rhs > 0 && (lhs > 18446744073709551615llu - rhs)) + return (ERROR); + *out = (t_u64)(lhs + rhs); + return (NO_ERROR); +} + +t_error checked_sub_u64(t_u64 lhs, t_u64 rhs, t_u64 *out) +{ + if ((((rhs & (1 << (sizeof(t_u64) - 1)) || rhs == 0) || !false) + && (lhs < 0llu + rhs))) + return (ERROR); + *out = (t_u64)(lhs - rhs); + return (NO_ERROR); +} + +t_error checked_mul_u64(t_u64 lhs, t_u64 rhs, t_u64 *out) +{ + t_u64 mul; + + mul = lhs * rhs; + if (lhs != 0 && mul / lhs != rhs) + return (ERROR); + *out = mul; + return (NO_ERROR); +} diff --git a/src/convert/str_to_u8.c b/src/convert/str_to_u8.c new file mode 100644 index 0000000..8fac9fa --- /dev/null +++ b/src/convert/str_to_u8.c @@ -0,0 +1,129 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/string/str_len.h" +#include "me/types.h" + +#define OP_ADD 0b0001 +#define OP_SUB 0b0010 +#define OP_MUL 0b0100 +#define OP_CHK 0b1000 + +t_error checked_add_u8(t_u8 lhs, t_u8 rhs, t_u8 *out); +t_error checked_sub_u8(t_u8 lhs, t_u8 rhs, t_u8 *out); +t_error checked_mul_u8(t_u8 lhs, t_u8 rhs, t_u8 *out); + +static inline bool can_not_overflow(t_u32 radix, bool is_signed_type, + t_usize digits_len) +{ + return (radix <= 16 && digits_len <= sizeof(t_u8) * 2 + - (t_usize)is_signed_type); +} + +static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out) +{ + t_u32 digit; + + if (radix < 2 || radix > 36) + return (ERROR); + digit = ascii - '0'; + if (radix > 10 && digit >= 10) + { + if (!me_isalpha(ascii)) + return (ERROR); + digit = ((ascii | 0b100000) - 'a') + 10; + } + if (digit >= radix) + return (ERROR); + *out = digit; + return (NO_ERROR); +} + +static inline t_error do_operation(t_u8 digit, t_u8 op, t_u8 *result) +{ + t_u8 rhs; + t_u8 res; + + rhs = *result; + res = *result; + if (op & OP_CHK) + { + if (op & OP_MUL && checked_mul_u8(rhs, digit, &res)) + return (ERROR); + else if (op & OP_ADD && checked_add_u8(rhs, digit, &res)) + return (ERROR); + else if (op & OP_SUB && checked_sub_u8(rhs, digit, &res)) + return (ERROR); + } + else + { + if (op & OP_MUL) + res = rhs * digit; + else if (op & OP_ADD) + res = rhs + digit; + else if (op & OP_SUB) + res = rhs - digit; + } + *result = res; + return (NO_ERROR); +} + +static inline t_error loop_inner(t_const_str s, t_u32 radix, t_u8 op, + t_u8 *out) +{ + t_u32 digit; + t_u8 result; + + result = 0u; + while (*s) + { + if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result)) + return (ERROR); + if (to_digit(*s, radix, &digit)) + return (ERROR); + if (do_operation(digit, op, &result)) + return (ERROR); + s++; + } + if (out) + *out = result; + return (NO_ERROR); +} + +t_error str_to_u8(t_const_str s, t_u32 radix, t_u8 *out) +{ + t_usize digits_len; + bool is_positive; + t_u8 op; + + if (radix < 2 || radix > 36) + return (ERROR); + digits_len = str_len(s); + is_positive = true; + if (digits_len == 0) + return (ERROR); + if ((s[0] == '-' || s[0] == '+') && s[1] == '\0') + return (ERROR); + else if (s[0] == '+') + is_positive = (s++, digits_len--, true); + else if (s[0] == '-' && false) + is_positive = (s++, digits_len--, false); + if (is_positive) + op = OP_ADD; + else + op = OP_SUB; + if (!can_not_overflow(radix, false, digits_len)) + op |= OP_CHK; + return (loop_inner(s, radix, op, out)); +} diff --git a/src/convert/str_to_u8_utils.c b/src/convert/str_to_u8_utils.c new file mode 100644 index 0000000..14a4a04 --- /dev/null +++ b/src/convert/str_to_u8_utils.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#include "me/types.h" + +t_error checked_add_u8(t_u8 lhs, t_u8 rhs, t_u8 *out) +{ + if (rhs > 0 && (lhs > 128u - rhs)) + return (ERROR); + *out = (t_u8)(lhs + rhs); + return (NO_ERROR); +} + +t_error checked_sub_u8(t_u8 lhs, t_u8 rhs, t_u8 *out) +{ + if ((((rhs & (1 << (sizeof(t_u8) - 1)) || rhs == 0) || !false) && (lhs < 0u + + rhs))) + return (ERROR); + *out = (t_u8)(lhs - rhs); + return (NO_ERROR); +} + +t_error checked_mul_u8(t_u8 lhs, t_u8 rhs, t_u8 *out) +{ + t_u8 mul; + + mul = lhs * rhs; + if (lhs != 0 && mul / lhs != rhs) + return (ERROR); + *out = mul; + return (NO_ERROR); +} diff --git a/src/fs/close.c b/src/fs/close.c new file mode 100644 index 0000000..1b4ef62 --- /dev/null +++ b/src/fs/close.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* close.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 15:56:56 by maiboyer #+# #+# */ +/* Updated: 2023/12/10 19:05:48 by maix ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/fs/close.h" +#include "me/types.h" +#include + +bool me_close(t_file file, t_i32 *error) +{ + t_i32 res; + bool out; + + res = close(file); + out = res != 0; + if (res && error != NULL) + *error = res; + return (out); +} diff --git a/src/fs/open.c b/src/fs/open.c new file mode 100644 index 0000000..96d6200 --- /dev/null +++ b/src/fs/open.c @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* open.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/03 15:29:38 by maiboyer #+# #+# */ +/* Updated: 2024/01/06 18:19:11 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/fs/open.h" +#include + +t_error me_open(t_const_str path, bool read, bool write, t_file *file_out) +{ + t_file out; + int flags; + + flags = 0; + if (read && write) + flags = O_RDWR; + else if (read) + flags = O_RDONLY; + else if (write) + flags = O_WRONLY; + out = open(path, flags, 0666); + if (out < 0) + return (ERROR); + *file_out = out; + return (NO_ERROR); +} + +t_error me_open_truncate(t_const_str path, t_file *file_out) +{ + t_file out; + int flags; + + unlink(path); + flags = O_WRONLY | O_CREAT | O_TRUNC; + out = open(path, flags, 0666); + if (out < 0) + return (ERROR); + *file_out = out; + return (NO_ERROR); +} + +t_error me_open_create(t_const_str path, t_file *file_out) +{ + t_file out; + int flags; + + flags = O_WRONLY | O_CREAT | O_APPEND; + out = open(path, flags, 0666); + if (out < 0) + return (ERROR); + *file_out = out; + return (NO_ERROR); +} diff --git a/src/fs/putchar_fd.c b/src/fs/putchar_fd.c new file mode 100644 index 0000000..2a10051 --- /dev/null +++ b/src/fs/putchar_fd.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* putchar_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 04:42:45 by maiboyer #+# #+# */ +/* Updated: 2023/11/08 13:22:51 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/fs/putchar_fd.h" +#include "me/fs/write.h" +#include "me/string/str_len.h" + +void me_putchar_fd(char chr, t_file file) +{ + me_write(file, (t_u8 *)&chr, 1); +} diff --git a/src/fs/putendl_fd.c b/src/fs/putendl_fd.c new file mode 100644 index 0000000..ba94ff5 --- /dev/null +++ b/src/fs/putendl_fd.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* putendl_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 04:42:45 by maiboyer #+# #+# */ +/* Updated: 2023/11/10 16:23:27 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/fs/putstr_fd.h" +#include "me/fs/write.h" +#include "me/string/str_len.h" + +void me_putendl_fd(t_str str, t_file file) +{ + if (str == NULL) + return ; + me_write(file, (t_u8 *)str, str_len(str)); + me_write(file, (t_u8 *)"\n", 1); +} diff --git a/src/fs/putnbr_fd.c b/src/fs/putnbr_fd.c new file mode 100644 index 0000000..62143ad --- /dev/null +++ b/src/fs/putnbr_fd.c @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* putnbr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 12:45:06 by maiboyer #+# #+# */ +/* Updated: 2023/11/08 13:11:40 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/fs/putnbr_fd.h" +#include "me/fs/write.h" +#include "me/mem/mem_set.h" + +static void me_inner(t_u64 nb, t_str out, t_usize *idx) +{ + bool need_print; + t_u64 modulus; + char c; + + modulus = 1000000000; + need_print = false; + while (modulus) + { + c = (char)(nb / modulus); + if (c != 0 || need_print) + { + out[(*idx)++] = c + '0'; + need_print = true; + } + nb = nb % modulus; + modulus /= 10; + } +} + +void me_putnbr_fd(t_i32 n, t_file file) +{ + t_usize idx; + t_u64 nb; + char out[15]; + + nb = (t_u64)n; + idx = 0; + if (nb < 0) + { + out[idx++] = '-'; + nb = -nb; + } + if (nb == 0) + out[idx++] = '0'; + me_inner(nb, out, &idx); + me_write(file, (t_u8 *)out, idx); +} diff --git a/src/fs/putstr_fd.c b/src/fs/putstr_fd.c new file mode 100644 index 0000000..065b490 --- /dev/null +++ b/src/fs/putstr_fd.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* putstr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 04:42:45 by maiboyer #+# #+# */ +/* Updated: 2023/11/10 16:23:44 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/fs/putstr_fd.h" +#include "me/fs/write.h" +#include "me/string/str_len.h" + +void me_putstr_fd(t_str str, t_file file) +{ + if (str == NULL) + return ; + me_write(file, (t_u8 *)str, str_len(str)); +} diff --git a/src/fs/read.c b/src/fs/read.c new file mode 100644 index 0000000..6635d6b --- /dev/null +++ b/src/fs/read.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* read.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/03 15:21:19 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 18:08:10 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/fs/read.h" +#include + +t_usize me_read(t_file fd, t_u8 *buffer, t_i64 buffer_max, bool *eof_out) +{ + ssize_t out; + + out = read(fd, buffer, buffer_max); + if (out == 0 && buffer_max != 0 && eof_out != NULL) + *eof_out = true; + return (out); +} diff --git a/src/fs/read_to_vec.c b/src/fs/read_to_vec.c new file mode 100644 index 0000000..b04f316 --- /dev/null +++ b/src/fs/read_to_vec.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* read_to_vec.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/24 18:38:47 by maiboyer #+# #+# */ +/* Updated: 2023/12/30 18:15:58 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/fs/open.h" +#include "me/fs/read.h" +#include "me/mem/mem_copy.h" +#include "me/vec/vec_u8.h" + +#define READ_BUFFER_SIZE 4096 + +bool read_to_vec(t_const_str path, t_vec_u8 *out) +{ + t_u8 temp_buffer[READ_BUFFER_SIZE]; + t_isize read_amount; + t_file f; + bool eof; + t_usize current_size; + + eof = false; + current_size = 0; + if (out == NULL || me_open(path, true, false, &f)) + return (true); + *out = vec_u8_new(READ_BUFFER_SIZE, NULL); + while (!eof) + { + read_amount = me_read(f, temp_buffer, READ_BUFFER_SIZE, &eof); + if (read_amount < 0) + return (true); + vec_u8_reserve(out, current_size + (t_usize)read_amount); + mem_copy(&out->buffer[out->len], temp_buffer, (t_usize)read_amount); + out->len += (t_usize)read_amount; + } + return (false); +} diff --git a/src/fs/write.c b/src/fs/write.c new file mode 100644 index 0000000..a51e5fe --- /dev/null +++ b/src/fs/write.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* write.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/03 15:27:33 by maiboyer #+# #+# */ +/* Updated: 2023/11/03 15:44:38 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/fs/write.h" +#include + +bool me_write(t_file fd, t_u8 *buffer, t_i64 size) +{ + return (write(fd, buffer, size) < 0); +} diff --git a/src/gnl/get_next_line.c b/src/gnl/get_next_line.c new file mode 100644 index 0000000..74ec66f --- /dev/null +++ b/src/gnl/get_next_line.c @@ -0,0 +1,128 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maix +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/23 17:38:21 by maix #+# #+# */ +/* Updated: 2023/12/11 19:10:26 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/buffered_str/buf_str.h" +#include "me/gnl/gnl.h" +#include "me/mem/mem_alloc.h" +#include "me/mem/mem_move.h" +#include "me/string/str_len.h" +#include "me/types.h" +#include +#include +#include + +static t_static_buffer *get_next_line_buffer(t_file fd) +{ + t_usize index; + static t_static_buffer bufs[BUFFER_LENGTH] = {0}; + + index = 0; + while (index < BUFFER_LENGTH && (bufs[index].fd != fd && bufs[index].used)) + index++; + if (index == BUFFER_LENGTH) + return (NULL); + bufs[index].fd = fd; + bufs[index].used = true; + return (&bufs[index]); +} + +static bool copy_next_line_into_buffer(t_file fd, t_buffer_str *out, + char *temp_buffer, t_usize amount) +{ + char *buf; + char *newline; + bool got_newline; + t_usize other_part_len; + + buf = get_next_line_buffer(fd)->buf; + newline = buf; + while (*newline != '\n' && *newline && newline < buf + amount) + newline++; + got_newline = *newline == '\n'; + other_part_len = amount - (t_usize)(newline - buf + !got_newline); + if (amount < (t_usize)(newline - buf + !got_newline)) + other_part_len = 0; + mem_move(temp_buffer, buf, (t_usize)(newline - buf + got_newline)); + buf[(t_usize)(newline - buf)] = 0; + temp_buffer[(t_usize)(newline - buf + got_newline)] = 0; + mem_move(buf, newline + 1, other_part_len); + push_str_buffer(out, temp_buffer); + buf[amount - (t_usize)(newline - buf + got_newline)] = 0; + return (got_newline); +} + +static bool read_and_copy(t_file fd, t_buffer_str *out, char *tmp, + t_copy_flags *flags) +{ + t_isize amount; + t_static_buffer *buf; + + buf = get_next_line_buffer(fd); + amount = read(fd, &buf->buf, BUFFER_SIZE); + flags->error = amount < 0; + if (flags->error) + return (true); + buf->init = true; + if ((t_isize)amount < (t_isize)BUFFER_SIZE) + { + copy_next_line_into_buffer(fd, out, tmp, (t_usize)amount); + flags->empty_read = (amount == 0 && out->len == 0); + return (true); + } + buf->buf[(t_usize)amount] = 0; + return (copy_next_line_into_buffer(fd, out, tmp, (t_usize)amount)); +} + +static bool handle_leftovers(t_file fd, char *temp_buffer, t_buffer_str *buf) +{ + t_static_buffer *static_buffer; + + static_buffer = get_next_line_buffer(fd); + if (static_buffer->init) + { + if (copy_next_line_into_buffer(fd, buf, temp_buffer, + str_len(static_buffer->buf))) + { + free(temp_buffer); + return (true); + } + } + return (false); +} + +t_buffer_str get_next_line(t_file fd, bool *error) +{ + t_buffer_str buf; + char *temp_buffer; + t_copy_flags flags; + + *error = false; + if (fd < 0 || BUFFER_SIZE <= 0) + return (*error = true, (t_buffer_str){0}); + flags = (t_copy_flags){ + .error = false, + .empty_read = false, + }; + temp_buffer = mem_alloc(sizeof(char) * (BUFFER_SIZE + 2)); + buf = alloc_new_buffer(32); + if (handle_leftovers(fd, temp_buffer, &buf)) + return (buf); + while (!read_and_copy(fd, &buf, temp_buffer, &flags) && !flags.empty_read) + ; + free(temp_buffer); + if (flags.error || flags.empty_read) + { + free(buf.buf); + return (*error = true, (t_buffer_str){0}); + } + return (buf); +} diff --git a/src/hash/hash_signed.c b/src/hash/hash_signed.c new file mode 100644 index 0000000..28fd330 --- /dev/null +++ b/src/hash/hash_signed.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* hash_signed.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 17:26:06 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 17:27:12 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/hash/hasher.h" +#include "me/types.h" + +void hasher_write_i8(t_hasher *hasher, t_i8 n) +{ + hasher_write_bytes(hasher, (t_u8 *)&n, 1); +} + +void hasher_write_i16(t_hasher *hasher, t_i16 n) +{ + hasher_write_bytes(hasher, (t_u8 *)&n, 2); +} + +void hasher_write_i32(t_hasher *hasher, t_i32 n) +{ + hasher_write_bytes(hasher, (t_u8 *)&n, 1); +} + +void hasher_write_i64(t_hasher *hasher, t_i64 n) +{ + hasher_write_bytes(hasher, (t_u8 *)&n, 1); +} + +void hasher_write_isize(t_hasher *hasher, t_isize n) +{ + hasher_write_bytes(hasher, (t_u8 *)&n, sizeof(t_isize)); +} diff --git a/src/hash/hash_unsigned.c b/src/hash/hash_unsigned.c new file mode 100644 index 0000000..2e4ed86 --- /dev/null +++ b/src/hash/hash_unsigned.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* hash_unsigned.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 17:25:23 by maiboyer #+# #+# */ +/* Updated: 2023/12/27 16:37:58 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/hash/hasher.h" +#include "me/types.h" + +void hasher_write_u8(t_hasher *hasher, t_u8 byte) +{ + hasher->hash_bytes(hasher->hasher, &byte, 1); +} + +void hasher_write_u16(t_hasher *hasher, t_u16 byte) +{ + hasher->hash_bytes(hasher->hasher, (t_u8 *)&byte, 2); +} + +void hasher_write_u32(t_hasher *hasher, t_u32 byte) +{ + hasher->hash_bytes(hasher->hasher, (t_u8 *)&byte, 4); +} + +void hasher_write_u64(t_hasher *hasher, t_u64 byte) +{ + hasher->hash_bytes(hasher->hasher, (t_u8 *)&byte, 8); +} diff --git a/src/hash/hasher.c b/src/hash/hasher.c new file mode 100644 index 0000000..8e544e1 --- /dev/null +++ b/src/hash/hasher.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* hasher.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 15:58:12 by maiboyer #+# #+# */ +/* Updated: 2023/12/27 16:44:25 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/hash/hasher.h" +#include "me/types.h" + +t_u64 hasher_finish(t_hasher *hasher) +{ + return (hasher->finish(hasher->hasher)); +} + +t_u64 hasher_reset_and_finish(t_hasher *hasher) +{ + return (hasher->reset_and_finish(hasher->hasher)); +} + +void hasher_write_bytes(t_hasher *hasher, t_u8 *bytes, t_usize count) +{ + hasher->hash_bytes(hasher->hasher, bytes, count); +} diff --git a/src/hash/sip/sip13.c b/src/hash/sip/sip13.c new file mode 100644 index 0000000..d9ab515 --- /dev/null +++ b/src/hash/sip/sip13.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sip13.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/10 19:32:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/27 16:48:13 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/hash/sip.h" +#include "me/hash/sip/sip_utils.h" +#include "me/mem/mem_alloc.h" + +t_hasher hasher_sip13_new(void) +{ + t_hasher out; + t_sip13 *inner; + + inner = mem_alloc(sizeof(t_sip13)); + inner->state = create_state_with_key(0, 0); + inner->k0 = 0; + inner->k1 = 0; + out.hasher = inner; + out.hash_bytes = (t_hash_bytes)sip13_write_bytes; + out.finish = (t_hasher_finish)sip13_finish; + out.reset_and_finish = (t_hasher_reset_and_finish)sip13_reset_and_finish; + return (out); +} diff --git a/src/hash/sip/sip_utils.c b/src/hash/sip/sip_utils.c new file mode 100644 index 0000000..aed4a33 --- /dev/null +++ b/src/hash/sip/sip_utils.c @@ -0,0 +1,106 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sip_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maix +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/10 20:02:12 by maix #+# #+# */ +/* Updated: 2023/12/11 19:09:32 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/hash/sip.h" +#include "me/hash/sip/sip_utils.h" +#include "me/num/u64.h" +#include "me/num/usize.h" +#include +#include + +static t_usize me_min(t_usize lhs, t_usize rhs) +{ + if (lhs < rhs) + return (lhs); + return (rhs); +} + +static t_usize handle_remaining(t_sip13 *self, t_u8 *msg, t_usize count, + bool *exit_early) +{ + t_usize needed; + + needed = 0; + *exit_early = false; + if (self->ntail != 0) + { + needed = 8 - self->ntail; + self->tail |= u64_from_bytes(msg, me_min(count, needed)) << (8 \ + * self->ntail); + if (count < needed) + { + self->ntail += count; + *exit_early = true; + return (needed); + } + else + { + self->state.v3 ^= self->tail; + compress(&self->state); + self->state.v0 ^= self->tail; + self->ntail = 0; + } + } + return (needed); +} + +t_usize read_u8_to_u64(t_u8 p[]) +{ + return (((t_u64)((p)[0])) | ((t_u64)((p)[1]) << 8) | \ + ((t_u64)((p)[2]) << 16) | ((t_u64)((p)[3]) << 24) | \ + ((t_u64)((p)[4]) << 32) | ((t_u64)((p)[5]) << 40) | \ + ((t_u64)((p)[6]) << 48) | ((t_u64)((p)[7]) << 56)); +} + +void sip13_write_bytes(t_sip13 *self, t_u8 *msg, t_usize count) +{ + bool exit_early; + t_usize needed; + t_usize left; + t_usize i; + t_u64 mi; + + self->length += count; + needed = handle_remaining(self, msg, count, &exit_early); + if (exit_early) + return ; + count = count - needed; + left = count & 0x7; + i = needed; + while (i < count - left) + { + mi = read_u8_to_u64(msg + i); + self->state.v3 ^= mi; + compress(&self->state); + self->state.v0 ^= mi; + i += 8; + } + self->tail = u64_from_7bytes(msg, i, left); + self->ntail = left; +} + +t_u64 sip13_finish(t_sip13 *self) +{ + t_sip_state state; + t_u64 b; + + state = self->state; + b = (((t_u64)self->length & 0xff) << 56) | self->tail; + state.v3 ^= b; + compress(&state); + state.v0 ^= b; + state.v2 ^= 0xff; + compress(&state); + compress(&state); + free(self); + return (state.v0 ^ state.v1 ^ state.v2 ^ state.v3); +} diff --git a/src/hash/sip/sip_utils2.c b/src/hash/sip/sip_utils2.c new file mode 100644 index 0000000..4575821 --- /dev/null +++ b/src/hash/sip/sip_utils2.c @@ -0,0 +1,68 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sip_utils2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 14:29:03 by maiboyer #+# #+# */ +/* Updated: 2023/12/27 16:52:17 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/hash/sip/sip_utils.h" +#include "me/num/usize.h" + +void compress(t_sip_state *state) +{ + state->v0 = state->v0 + state->v1; + state->v1 = usize_rotate_left(state->v1, 13); + state->v1 ^= state->v0; + state->v0 = usize_rotate_left(state->v0, 32); + state->v2 = state->v2 + state->v3; + state->v3 = usize_rotate_left(state->v3, 16); + state->v3 ^= state->v2; + state->v0 = state->v0 + state->v3; + state->v3 = usize_rotate_left(state->v3, 21); + state->v3 ^= state->v0; + state->v2 = state->v2 + state->v1; + state->v1 = usize_rotate_left(state->v1, 17); + state->v1 ^= state->v2; + state->v2 = usize_rotate_left(state->v2, 32); +} + +t_sip_state create_state_with_key(t_u64 k0, t_u64 k1) +{ + t_sip_state state; + + state = (t_sip_state){.v0 = 0, .v1 = 0, .v2 = 0, .v3 = 0}; + state.v0 = k0 ^ 0x736f6d6570736575; + state.v1 = k1 ^ 0x646f72616e646f6d; + state.v2 = k0 ^ 0x6c7967656e657261; + state.v3 = k1 ^ 0x7465646279746573; + return (state); +} + +t_u64 sip13_reset_and_finish(t_sip13 *self) +{ + t_sip_state state; + t_u64 b; + t_u64 ret; + + state = self->state; + b = (((t_u64)self->length & 0xff) << 56) | self->tail; + state.v3 ^= b; + compress(&state); + state.v0 ^= b; + state.v2 ^= 0xff; + compress(&state); + compress(&state); + ret = (state.v0 ^ state.v1 ^ state.v2 ^ state.v3); + self->length = 0; + self->state.v0 = self->k0 ^ 0x736f6d6570736575; + self->state.v1 = self->k1 ^ 0x646f72616e646f6d; + self->state.v2 = self->k0 ^ 0x6c7967656e657261; + self->state.v3 = self->k1 ^ 0x7465646279746573; + self->ntail = 0; + return (ret); +} diff --git a/src/img/qoi/qoi_decode.c b/src/img/qoi/qoi_decode.c new file mode 100644 index 0000000..971f8bc --- /dev/null +++ b/src/img/qoi/qoi_decode.c @@ -0,0 +1,73 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* qoi_decode.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/25 22:31:40 by maiboyer #+# #+# */ +/* Updated: 2023/12/25 22:35:44 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/img/qoi.h" +#include "me/img/qoi/qoi_utils.h" +#include "me/img/qoi/qoi_decode.h" + +static inline void *qoi_decode_inner(t_decode_vals *vals, const t_u8 *bytes, + t_i32 channels, t_i32 size) +{ + t_u8 *pixels; + + pixels = (t_u8 *)mem_alloc(vals->px_len); + if (!pixels) + return (NULL); + mem_set_zero(vals->index, sizeof(vals->index)); + vals->px.v = 0; + vals->px.rgba.a = 255; + vals->chunks_len = size - (t_i32) sizeof(t_u8[8]); + vals->px_pos = 0; + while (vals->px_pos < vals->px_len) + { + if (vals->run > 0) + vals->run--; + else if (vals->p < vals->chunks_len) + qoi_decode_inner_inner(vals, bytes); + pixels[vals->px_pos + 0] = vals->px.rgba.r; + pixels[vals->px_pos + 1] = vals->px.rgba.g; + pixels[vals->px_pos + 2] = vals->px.rgba.b; + if (channels == 4) + pixels[vals->px_pos + 3] = vals->px.rgba.a; + vals->px_pos += channels; + } + return (pixels); +} + +void *qoi_decode(const void *data, t_i32 size, t_qoi_desc *desc, + t_i32 channels) +{ + const t_u8 *bytes; + t_decode_vals vals; + + vals.p = 0; + vals.run = 0; + if (data == NULL || desc == NULL || (channels != 0 && channels != 3 + && channels != 4) || size < QOI_HEADER_SIZE + + (t_i32) sizeof(t_u8[8])) + return (NULL); + bytes = (const t_u8 *)data; + vals.header_magic = qoi_read_32(bytes, &vals.p); + desc->width = qoi_read_32(bytes, &vals.p); + desc->height = qoi_read_32(bytes, &vals.p); + desc->channels = bytes[vals.p++]; + desc->colorspace = bytes[vals.p++]; + if (desc->width == 0 || desc->height == 0 || desc->channels < 3 + || desc->channels > 4 || desc->colorspace > 1 + || vals.header_magic != QOI_MAGIC || desc->height >= QOI_PIXELS_MAX + / desc->width) + return (NULL); + if (channels == 0) + channels = desc->channels; + vals.px_len = desc->width * desc->height * channels; + return (qoi_decode_inner(&vals, bytes, channels, size)); +} diff --git a/src/img/qoi/qoi_encode.c b/src/img/qoi/qoi_encode.c new file mode 100644 index 0000000..137bc96 --- /dev/null +++ b/src/img/qoi/qoi_encode.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* qoi_encode.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/25 22:38:42 by maiboyer #+# #+# */ +/* Updated: 2023/12/25 22:56:06 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/img/qoi.h" +#include "me/img/qoi/qoi_encode.h" +#include "me/img/qoi/qoi_utils.h" + +void *qoi_encode(const void *data, const t_qoi_desc *desc, t_i32 *out_len) +{ + t_encode_vals vals; + + if (data == NULL || out_len == NULL || desc == NULL || desc->width == 0 + || desc->height == 0 || desc->channels < 3 || desc->channels > 4 + || desc->colorspace > 1 || desc->height >= QOI_PIXELS_MAX / desc->width) + return (NULL); + vals = (t_encode_vals){0}; + vals.max_size = desc->width * desc->height * (desc->channels + 1) + + QOI_HEADER_SIZE + sizeof(t_u8[8]); + return (qoi_encode_inner(&vals, desc, (const t_u8 *)data, out_len)); +} diff --git a/src/img/qoi/qoi_fs.c b/src/img/qoi/qoi_fs.c new file mode 100644 index 0000000..9be0545 --- /dev/null +++ b/src/img/qoi/qoi_fs.c @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* qoi_fs.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/24 19:06:05 by maiboyer #+# #+# */ +/* Updated: 2023/12/24 19:18:01 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/fs/close.h" +#include "me/fs/open.h" +#include "me/fs/read_to_vec.h" +#include "me/fs/write.h" +#include "me/img/qoi.h" +#include + +t_i32 qoi_write(t_const_str filename, const void *data, + const t_qoi_desc *desc) +{ + t_file f; + void *encoded; + t_i32 size; + + if (me_open(filename, false, true, &f)) + return (0); + encoded = qoi_encode(data, desc, &size); + if (!encoded) + return (me_close(f, NULL), 0); + if (me_write(f, encoded, size)) + return (me_close(f, NULL), 0); + me_close(f, NULL); + free(encoded); + return (size); +} + +void *qoi_read(t_const_str filename, t_qoi_desc *desc, t_i32 channels) +{ + t_vec_u8 out; + void *pixels; + + if (read_to_vec(filename, &out)) + return (NULL); + pixels = qoi_decode(out.buffer, out.len, desc, channels); + vec_u8_free(out); + return (pixels); +} diff --git a/src/img/qoi/qoi_utils.c b/src/img/qoi/qoi_utils.c new file mode 100644 index 0000000..7eb494b --- /dev/null +++ b/src/img/qoi/qoi_utils.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* qoi_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/24 19:02:03 by maiboyer #+# #+# */ +/* Updated: 2023/12/24 19:02:42 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/img/qoi/qoi_utils.h" +#include "me/types.h" + +void qoi_write_32(t_u8 *bytes, t_i32 *p, t_u32 v) +{ + bytes[(*p)++] = (0xff000000 & v) >> 24; + bytes[(*p)++] = (0x00ff0000 & v) >> 16; + bytes[(*p)++] = (0x0000ff00 & v) >> 8; + bytes[(*p)++] = (0x000000ff & v); +} + +t_u32 qoi_read_32(const t_u8 *bytes, t_i32 *p) +{ + t_u32 a; + t_u32 b; + t_u32 c; + t_u32 d; + + a = bytes[(*p)++]; + b = bytes[(*p)++]; + c = bytes[(*p)++]; + d = bytes[(*p)++]; + return (a << 24 | b << 16 | c << 8 | d); +} diff --git a/src/list/list_add_back.c b/src/list/list_add_back.c new file mode 100644 index 0000000..0c8bea4 --- /dev/null +++ b/src/list/list_add_back.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_add_back.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 20:38:45 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 15:02:42 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/list/list_add_back.h" +#include "me/list/list_get_last.h" + +void list_add_back(t_list **list, t_list *new) +{ + if (*list) + list_get_last(*list)->next = new; + else + *list = new; +} diff --git a/src/list/list_add_front.c b/src/list/list_add_front.c new file mode 100644 index 0000000..5b1244f --- /dev/null +++ b/src/list/list_add_front.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_add_front.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 20:15:23 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 15:02:50 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/list/list_add_front.h" +#include "me/list/list_alloc_node.h" + +void list_add_front(t_list **lst, t_list *new) +{ + new->next = *lst; + *lst = new; +} diff --git a/src/list/list_alloc_node.c b/src/list/list_alloc_node.c new file mode 100644 index 0000000..7e93cdc --- /dev/null +++ b/src/list/list_alloc_node.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_alloc_node.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 19:57:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 18:13:05 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/list/list_alloc_node.h" +#include "me/mem/mem_alloc.h" + +t_list *list_alloc_node(void *content) +{ + t_list *out; + + out = mem_alloc(sizeof(t_list) * 1); + if (out == NULL) + return (NULL); + out->content = content; + out->next = NULL; + return (out); +} diff --git a/src/list/list_free_all.c b/src/list/list_free_all.c new file mode 100644 index 0000000..af358b9 --- /dev/null +++ b/src/list/list_free_all.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_free_all.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 21:35:20 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 15:03:34 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/list/list_free_all.h" +#include + +void list_free_all(t_list **lst, void (*del)(void *)) +{ + t_list *tmp; + + if (lst == NULL || del == NULL) + return ; + while (*lst) + { + del((*lst)->content); + tmp = *lst; + *lst = (*lst)->next; + free(tmp); + } + *lst = NULL; +} diff --git a/src/list/list_free_one.c b/src/list/list_free_one.c new file mode 100644 index 0000000..df44299 --- /dev/null +++ b/src/list/list_free_one.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_free_one.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 21:30:20 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 15:03:42 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/list/list_free_one.h" +#include + +void list_free_one(t_list *lst, void (*del)(void *)) +{ + if (lst == NULL || del == NULL) + return ; + del(lst->content); + free(lst); +} diff --git a/src/list/list_get_last.c b/src/list/list_get_last.c new file mode 100644 index 0000000..789796c --- /dev/null +++ b/src/list/list_get_last.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_get_last.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 20:37:08 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 15:03:49 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/list/list_get_last.h" + +t_list *list_get_last(t_list *list) +{ + t_list *head; + + head = list; + if (head == NULL) + return (NULL); + while (head->next) + head = head->next; + return (head); +} diff --git a/src/list/list_iter.c b/src/list/list_iter.c new file mode 100644 index 0000000..e1d6c6e --- /dev/null +++ b/src/list/list_iter.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_iter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 21:39:05 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 15:03:55 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/list/list_iter.h" + +void list_iter(t_list *list, void (*f)(void *)) +{ + while (list) + { + f(list->content); + list = list->next; + } +} diff --git a/src/list/list_map.c b/src/list/list_map.c new file mode 100644 index 0000000..0be2735 --- /dev/null +++ b/src/list/list_map.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_map.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 21:40:24 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 15:05:20 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/list/list_alloc_node.h" +#include "me/list/list_free_all.h" +#include "me/list/list_map.h" +#include + +t_list *list_map(t_list *lst, void *(*f)(void *), void (*del)(void *)) +{ + void *data; + t_list new; + t_list *cursor; + + new = (t_list){.next = NULL, .content = NULL}; + cursor = &new; + while (lst) + { + data = f(lst->content); + cursor->next = list_alloc_node(data); + if (cursor->next == NULL) + { + del(data); + list_free_all(&new.next, del); + return (NULL); + } + cursor = cursor->next; + lst = lst->next; + } + return (new.next); +} diff --git a/src/list/list_size.c b/src/list/list_size.c new file mode 100644 index 0000000..58efc4a --- /dev/null +++ b/src/list/list_size.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_size.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 20:23:19 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 15:05:00 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/list/list_size.h" + +t_usize list_size(t_list *lst) +{ + t_list *head; + t_usize idx; + + head = lst; + idx = 0; + while (head) + { + head = head->next; + idx++; + } + return (idx); +} diff --git a/src/mem/mem_alloc.c b/src/mem/mem_alloc.c new file mode 100644 index 0000000..a52c40a --- /dev/null +++ b/src/mem/mem_alloc.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_alloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/06 14:47:49 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 18:14:11 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc.h" +#include + +void *mem_alloc(t_usize size) +{ + void *out; + size_t i; + + i = 0; + out = malloc(size); + while (out && i < size) + ((t_u8 *)out)[i++] = 0; + return (out); +} diff --git a/src/mem/mem_alloc_array.c b/src/mem/mem_alloc_array.c new file mode 100644 index 0000000..b17ce7f --- /dev/null +++ b/src/mem/mem_alloc_array.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_alloc_array.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 15:53:21 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 18:14:47 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc.h" +#include "me/mem/mem_alloc_array.h" +#include + +void *mem_alloc_array(t_usize item_count, t_usize item_size) +{ + t_usize multiplied; + + multiplied = item_count * item_size; + if (multiplied == 0 || multiplied / item_count != item_size) + return (NULL); + return (mem_alloc(multiplied)); +} diff --git a/src/mem/mem_compare.c b/src/mem/mem_compare.c new file mode 100644 index 0000000..76d9d5b --- /dev/null +++ b/src/mem/mem_compare.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_compare.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:16:02 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 15:00:58 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_compare.h" + +t_i32 mem_compare(const void *lhs, const void *rhs, t_usize count) +{ + t_usize i; + t_u8 *lhs_; + t_u8 *rhs_; + + i = 0; + lhs_ = (t_u8 *)lhs; + rhs_ = (t_u8 *)rhs; + while (i < count) + { + if (lhs_[i] - rhs_[i]) + return ((t_i32)(lhs_[i] - rhs_[i])); + i++; + } + return (0); +} diff --git a/src/mem/mem_copy.c b/src/mem/mem_copy.c new file mode 100644 index 0000000..bbd7298 --- /dev/null +++ b/src/mem/mem_copy.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_copy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:16:02 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 15:01:08 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_copy.h" + +void *mem_copy(void *destination, const void *source, t_usize count) +{ + t_usize i; + t_u8 *dst; + t_u8 *src; + + i = 0; + dst = (t_u8 *)destination; + src = (t_u8 *)source; + if (dst == src) + return (destination); + while (i < count) + { + dst[i] = src[i]; + i++; + } + return (destination); +} diff --git a/src/mem/mem_find.c b/src/mem/mem_find.c new file mode 100644 index 0000000..e38303e --- /dev/null +++ b/src/mem/mem_find.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_find.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:16:02 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 15:01:36 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_find.h" + +void *mem_find(void *buf, t_u8 find, t_usize count) +{ + t_usize i; + t_u8 *buf_bytes; + + i = 0; + buf_bytes = (t_u8 *)buf; + while (i < count) + { + if (buf_bytes[i] == find) + return ((void *)&buf_bytes[i]); + i++; + } + return (NULL); +} diff --git a/src/mem/mem_find_bytes.c b/src/mem/mem_find_bytes.c new file mode 100644 index 0000000..59eb784 --- /dev/null +++ b/src/mem/mem_find_bytes.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_find_bytes.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:16:02 by maiboyer #+# #+# */ +/* Updated: 2024/01/06 17:14:23 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_find_bytes.h" +#include "stdio.h" + +void *mem_find_bytes(void *buf, t_u8 *find, t_usize find_len, t_usize count) +{ + t_usize i; + t_usize j; + t_u8 *buf_bytes; + + i = 0; + printf("inside\n"); + buf_bytes = (t_u8 *)buf; + while (i < count) + { + j = 0; + while (j < find_len && i + j < count) + { + if (buf_bytes[i + j] != find[j]) + break ; + j++; + } + if (j == find_len) + return ((void *)&buf_bytes[i]); + i++; + } + return (NULL); +} diff --git a/src/mem/mem_move.c b/src/mem/mem_move.c new file mode 100644 index 0000000..03ebde3 --- /dev/null +++ b/src/mem/mem_move.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_move.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:16:02 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 15:01:43 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_move.h" + +void *mem_move(void *destination, const void *source, t_usize count) +{ + t_usize i; + t_u8 *dst; + t_u8 *src; + + i = 0; + dst = (t_u8 *)destination; + src = (t_u8 *)source; + if (dst < src) + { + while (i < count) + { + dst[i] = src[i]; + i++; + } + } + else if (dst > src) + { + i = count; + while (i > 0) + { + i--; + dst[i] = src[i]; + } + } + return (destination); +} diff --git a/src/mem/mem_set.c b/src/mem/mem_set.c new file mode 100644 index 0000000..106c736 --- /dev/null +++ b/src/mem/mem_set.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_set.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:16:02 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 18:15:22 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_set.h" + +void mem_set(void *buf, t_u8 fill_by, t_usize count) +{ + t_usize i; + t_u8 *buf_bytes; + + i = 0; + buf_bytes = (t_u8 *)buf; + while (i < count) + { + buf_bytes[i] = fill_by; + i++; + } +} diff --git a/src/mem/mem_set_zero.c b/src/mem/mem_set_zero.c new file mode 100644 index 0000000..b12e07d --- /dev/null +++ b/src/mem/mem_set_zero.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_set_zero.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:58:11 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 15:01:57 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_set_zero.h" + +void mem_set_zero(void *buf, t_usize count) +{ + t_u8 *buffer; + t_usize index; + + index = 0; + buffer = (t_u8 *)buf; + while (index < count) + { + buffer[index] = 0; + index++; + } +} diff --git a/src/num/u16/rotate.c b/src/num/u16/rotate.c new file mode 100644 index 0000000..6d1a3f8 --- /dev/null +++ b/src/num/u16/rotate.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rotate.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 14:08:55 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 14:13:32 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/num/u16.h" + +t_u16 u16_rotate_left(t_u16 value, t_usize by) +{ + by &= sizeof(value) * 8 - 1; + if (by == 0) + return (value); + return ((value << by) | (value >> (sizeof(value) * 8 - by))); +} + +t_u16 u16_rotate_right(t_u16 value, t_usize by) +{ + by &= sizeof(value) * 8 - 1; + if (by == 0) + return (value); + return ((value >> by) | (value << (sizeof(value) * 8 - by))); +} diff --git a/src/num/u32/rotate.c b/src/num/u32/rotate.c new file mode 100644 index 0000000..2a7e5c2 --- /dev/null +++ b/src/num/u32/rotate.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rotate.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 14:08:55 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 14:13:32 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/num/u32.h" + +t_u32 u32_rotate_left(t_u32 value, t_usize by) +{ + by &= sizeof(value) * 8 - 1; + if (by == 0) + return (value); + return ((value << by) | (value >> (sizeof(value) * 8 - by))); +} + +t_u32 u32_rotate_right(t_u32 value, t_usize by) +{ + by &= sizeof(value) * 8 - 1; + if (by == 0) + return (value); + return ((value >> by) | (value << (sizeof(value) * 8 - by))); +} diff --git a/src/num/u64/from_bytes.c b/src/num/u64/from_bytes.c new file mode 100644 index 0000000..70b162f --- /dev/null +++ b/src/num/u64/from_bytes.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* from_bytes.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 14:30:56 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 19:09:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/num/u64.h" + +t_u64 u64_from_7bytes(t_u8 *msg, t_usize start, t_usize len) +{ + t_usize i; + t_usize out; + t_u64 temp_val; + + i = 0; + out = 0; + if (i + 3 < len) + { + temp_val = (t_u64)(msg[start + i]); + temp_val |= (t_u64)(msg[start + i + 1]) << (8 * 1); + temp_val |= (t_u64)(msg[start + i + 2]) << (8 * 2); + temp_val |= (t_u64)(msg[start + i + 3]) << (8 * 3); + out = temp_val; + i += 4; + } + if (i + 1 < len) + { + temp_val = ((t_u64)(msg[start + i + 1]) << (8 * 1)) | (t_u64)(msg[start \ + + i]); + out |= temp_val << (i * 8); + i += 2; + } + if (i++ < len) + out |= (t_u64)(msg[start + i - 1]) << ((i - 1) * 8); + return (out); +} + +t_u64 u64_from_bytes(t_u8 *bytes, t_usize len) +{ + t_u64 out; + t_usize i; + + i = 0; + out = 0; + while (i < len && i < 8) + { + out |= ((t_u64)(bytes[i])) << (8 * i); + i++; + } + return (out); +} diff --git a/src/num/u64/rotate.c b/src/num/u64/rotate.c new file mode 100644 index 0000000..561d553 --- /dev/null +++ b/src/num/u64/rotate.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rotate.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 14:08:55 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 14:24:16 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/num/u64.h" + +t_u64 u64_rotate_left(t_u64 value, t_usize by) +{ + by &= sizeof(value) * 8 - 1; + if (by == 0) + return (value); + return ((value << by) | (value >> (sizeof(value) * 8 - by))); +} + +t_u64 u64_rotate_right(t_u64 value, t_usize by) +{ + by &= sizeof(value) * 8 - 1; + if (by == 0) + return (value); + return ((value >> by) | (value << (sizeof(value) * 8 - by))); +} diff --git a/src/num/u8/rotate.c b/src/num/u8/rotate.c new file mode 100644 index 0000000..45f0fd8 --- /dev/null +++ b/src/num/u8/rotate.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rotate.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 14:08:55 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 14:13:32 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/num/u8.h" + +t_u8 u8_rotate_left(t_u8 value, t_usize by) +{ + by &= sizeof(value) * 8 - 1; + if (by == 0) + return (value); + return ((value << by) | (value >> (sizeof(value) * 8 - by))); +} + +t_u8 u8_rotate_right(t_u8 value, t_usize by) +{ + by &= sizeof(value) * 8 - 1; + if (by == 0) + return (value); + return ((value >> by) | (value << (sizeof(value) * 8 - by))); +} diff --git a/src/num/usize/rotate.c b/src/num/usize/rotate.c new file mode 100644 index 0000000..7ad365d --- /dev/null +++ b/src/num/usize/rotate.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rotate.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 14:08:55 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 14:13:32 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/num/usize.h" + +t_usize usize_rotate_left(t_usize value, t_usize by) +{ + by &= sizeof(value) * 8 - 1; + if (by == 0) + return (value); + return ((value << by) | (value >> (sizeof(value) * 8 - by))); +} + +t_usize usize_rotate_right(t_usize value, t_usize by) +{ + by &= sizeof(value) * 8 - 1; + if (by == 0) + return (value); + return ((value >> by) | (value << (sizeof(value) * 8 - by))); +} diff --git a/src/printf/formatter/char.c b/src/printf/formatter/char.c new file mode 100644 index 0000000..dcbe742 --- /dev/null +++ b/src/printf/formatter/char.c @@ -0,0 +1,70 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* char.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/18 18:12:11 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 19:18:48 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_set.h" +#include "me/printf/formatter/utils.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#include "me/string/str_substring.h" +#include + +void printf_c(t_printf_arg data, t_printf_func f) +{ + char value[2]; + t_str start_num; + + value[0] = *(char *)data.argument; + value[1] = 0; + start_num = &value[0]; + pad_and_stuff( + (t_pad_and_stuff_args){\ +.fill_zero = 0, .fill = 0, .len = 1, .pretty = "", .pretty_len = 0, \ +.str = start_num, .allow_zero_fill = false, .sign = NULL, \ +.sign_len = 0, }, data, f); +} + +t_usize printf_s_inner(t_str *value, t_printf_arg *data, t_printf_func *f) +{ + t_usize len; + + if (*value == NULL) + { + if (data->flags & (PRECISION) && data->extra.precision < 6) + *value = ""; + else + *value = "(null)"; + } + len = str_len(*value); + if (data->flags & (PRECISION) && len > data->extra.precision) + len = data->extra.precision; + if (data->flags & (PRECISION) && len < data->extra.precision) + data->extra.precision = len; + (void)(f); + return (len); +} + +void printf_s(t_printf_arg data, t_printf_func f) +{ + t_str value; + t_str start_num; + t_usize len; + + value = (t_str)data.argument; + len = printf_s_inner(&value, &data, &f); + start_num = str_substring(value, 0, len); + pad_and_stuff( + (t_pad_and_stuff_args){\ +.fill_zero = 0, .fill = 0, .len = len, .pretty = "", .pretty_len = 0, \ +.str = start_num, .allow_zero_fill = false, .sign = NULL, \ +.sign_len = 0, }, data, f); + free(start_num); +} diff --git a/src/printf/formatter/decimal.c b/src/printf/formatter/decimal.c new file mode 100644 index 0000000..b99b5e3 --- /dev/null +++ b/src/printf/formatter/decimal.c @@ -0,0 +1,89 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* decimal.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maix +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/18 01:44:35 by maix #+# #+# */ +/* Updated: 2023/12/11 19:19:27 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_set.h" +#include "me/printf/formatter/utils.h" +#include "me/printf/printf.h" +#include "me/string/str_clone.h" +#include "me/string/str_len.h" +#include +#include +#define INT_INLINE_BUF 21 + +static void itoa_inner(t_u64 nb, t_str out) +{ + t_u64 modulus; + bool need_print; + char c; + t_usize idx; + + need_print = false; + modulus = 1000000000000000000; + idx = 0; + while (modulus) + { + c = (char)(nb / modulus) + '0'; + if (c != '0' || need_print) + { + out[idx++] = c; + need_print = true; + } + nb = nb % modulus; + modulus /= 10; + } +} + +static t_str itoa_64(t_i64 nb) +{ + char out[INT_INLINE_BUF]; + t_u64 n; + + n = (t_u64)nb; + mem_set(out, 0, INT_INLINE_BUF); + if (nb < 0) + itoa_inner(-n, out); + else if (nb == 0) + out[0] = '0'; + else + itoa_inner(n, out); + return (str_clone(out)); +} + +void printf_d(t_printf_arg data, t_printf_func f) +{ + t_u64 value; + t_str start_num; + t_str sign; + t_str void_write; + + value = *(t_i64 *)data.argument; + if ((t_u64)value & ((t_u64)1 << 31)) + sign = "-"; + else + sign = "+"; + if (!(data.flags & SIGN) && !((t_u64)value & ((t_u64)1 << 31))) + sign = ""; + if (!(data.flags & SIGN) && data.extra.space_align + && !((t_u64)value & ((t_u64)1 << 31))) + sign = " "; + data.flags |= SIGN; + start_num = itoa_64(value); + handle_weird_precision_stuff(&data, (t_prec_strs){.out = &start_num, + .free_out = true, .pretty = &void_write}, value); + pad_and_stuff( + (t_pad_and_stuff_args){\ +.fill_zero = 0, .fill = 0, .sign = sign, .pretty = NULL, .len = \ +str_len(start_num), .pretty_len = 0, .str = start_num, .allow_zero_fill \ + = true, .sign_len = str_len(sign), }, data, f); + free(start_num); +} diff --git a/src/printf/formatter/hex.c b/src/printf/formatter/hex.c new file mode 100644 index 0000000..9e91150 --- /dev/null +++ b/src/printf/formatter/hex.c @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* hex.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/16 18:16:16 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 19:19:03 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_set.h" +#include "me/printf/formatter/utils.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#define HEX_INLINE_BUF 17 + +static void fill_hex(t_str out_buf, t_u64 num, t_const_str base) +{ + t_usize i; + + mem_set(out_buf, 0, HEX_INLINE_BUF); + out_buf[HEX_INLINE_BUF - 1] = 0; + i = 0; + while (i < HEX_INLINE_BUF - 1) + { + out_buf[HEX_INLINE_BUF - i - 2] = base[(num >> (4 * i) & 15)]; + i++; + } +} + +static void printf_x_common(t_printf_arg data, t_printf_func f, + t_const_str pretty, t_const_str base) +{ + t_u64 value; + char inline_buffer[HEX_INLINE_BUF]; + t_str start_num; + + value = *(t_u64 *)data.argument; + inline_buffer[0] = '0'; + inline_buffer[1] = '\0'; + if (value) + fill_hex(inline_buffer, value, base); + start_num = &inline_buffer[0]; + while (start_num[1] != '\0' && start_num[0] == '0') + start_num++; + if (value == 0) + data.extra.pretty = false; + handle_weird_precision_stuff(&data, (t_prec_strs){.out = &start_num, + .free_out = false, .pretty = (t_str *)&pretty}, value); + pad_and_stuff( + (t_pad_and_stuff_args){\ +.fill_zero = 0, .fill = 0, .len = str_len(start_num), .pretty = (t_str)pretty, \ +.pretty_len = 2, .str = start_num, .allow_zero_fill = true, .sign = NULL, \ +.sign_len = 0, }, data, f); +} + +void printf_x_up(t_printf_arg data, t_printf_func f) +{ + printf_x_common(data, f, "0X", "0123456789ABCDEF"); +} + +void printf_x_low(t_printf_arg data, t_printf_func f) +{ + printf_x_common(data, f, "0x", "0123456789abcdef"); +} diff --git a/src/printf/formatter/oct.c b/src/printf/formatter/oct.c new file mode 100644 index 0000000..a6befc2 --- /dev/null +++ b/src/printf/formatter/oct.c @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* oct.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maix +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/18 01:19:18 by maix #+# #+# */ +/* Updated: 2023/12/11 19:17:23 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_set.h" +#include "me/printf/formatter/utils.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#include +#define OCT_INLINE_BUF 23 + +static void fill_oct(t_str out_buf, t_u64 num, t_str base) +{ + t_usize i; + + mem_set(out_buf, 0, OCT_INLINE_BUF); + out_buf[OCT_INLINE_BUF - 1] = 0; + i = 0; + while (i < OCT_INLINE_BUF - 1) + { + out_buf[OCT_INLINE_BUF - i - 2] = base[(num >> (3 * i) & 7)]; + i++; + } +} + +void printf_o(t_printf_arg data, t_printf_func f) +{ + t_u64 value; + char inline_buffer[OCT_INLINE_BUF]; + t_str start_num; + t_str pretty; + + value = *(t_u64 *)data.argument; + inline_buffer[0] = '0'; + inline_buffer[1] = '\0'; + pretty = "0o"; + if (value) + fill_oct(inline_buffer, value, "01234567"); + start_num = &inline_buffer[0]; + while (start_num[1] != '\0' && start_num[0] == '0') + start_num++; + if (!value && data.extra.precision == 0 && (data.flags & PRECISION)) + { + start_num = ""; + pretty = ""; + } + pad_and_stuff( + (t_pad_and_stuff_args){\ +.fill_zero = 0, .fill = 0, .len = str_len(start_num), .pretty = pretty, \ +.pretty_len = str_len(pretty), .str = start_num, .allow_zero_fill = true, \ +.sign = NULL, .sign_len = 0, }, data, f); +} diff --git a/src/printf/formatter/ptr.c b/src/printf/formatter/ptr.c new file mode 100644 index 0000000..9f169eb --- /dev/null +++ b/src/printf/formatter/ptr.c @@ -0,0 +1,74 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ptr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/16 18:16:16 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 19:20:42 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_set.h" +#include "me/printf/formatter/utils.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#define PTR_INLINE_BUF 17 + +static void fill_hex(t_str out_buf, t_u64 num, t_str base) +{ + t_usize i; + + mem_set(out_buf, 0, PTR_INLINE_BUF); + out_buf[PTR_INLINE_BUF - 1] = 0; + i = 0; + while (i < PTR_INLINE_BUF - 1) + { + out_buf[PTR_INLINE_BUF - i - 2] = base[(num >> (4 * i) & 15)]; + i++; + } +} + +static void set_values_for_nil(t_usize value, t_str *out, t_printf_arg *data, + t_printf_func f) +{ + (void)(f); + if (value) + { + data->flags &= ~PRECISION; + data->extra.precision = 0; + data->extra.pretty = true; + } + else + { + *out = "(nil)"; + data->extra.precision = 0; + data->extra.pretty = false; + data->flags &= ~(ZERO_ALIGN | PRECISION); + data->flags |= ALIGN; + } +} + +void printf_p(t_printf_arg data, t_printf_func f) +{ + t_u64 value; + char inline_buffer[PTR_INLINE_BUF + 1]; + t_str start_num; + + value = (t_u64)data.argument; + inline_buffer[0] = '0'; + inline_buffer[1] = '\0'; + if (value) + fill_hex(inline_buffer, value, "0123456789abcdef"); + start_num = &inline_buffer[0]; + inline_buffer[PTR_INLINE_BUF] = 0; + while (start_num[1] != '\0' && start_num[0] == '0') + start_num++; + set_values_for_nil(value, &start_num, &data, f); + pad_and_stuff( + (t_pad_and_stuff_args){\ +.fill_zero = 0, .fill = 0, .len = str_len(start_num), .pretty = "0x", \ +.pretty_len = 2, .str = start_num, .allow_zero_fill = value != 0, \ +.sign = NULL, .sign_len = 0, }, data, f); +} diff --git a/src/printf/formatter/unsigned_decimal.c b/src/printf/formatter/unsigned_decimal.c new file mode 100644 index 0000000..5d44f4e --- /dev/null +++ b/src/printf/formatter/unsigned_decimal.c @@ -0,0 +1,76 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* unsigned_decimal.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maix +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/18 01:44:35 by maix #+# #+# */ +/* Updated: 2023/12/11 19:19:59 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_set.h" +#include "me/printf/formatter/utils.h" +#include "me/printf/printf.h" +#include "me/string/str_clone.h" +#include "me/string/str_len.h" +#include +#include +#define UINT_INLINE_BUF 21 + +static void itoa_inner(t_u64 nb, t_str out) +{ + t_u64 modulus; + bool need_print; + char c; + t_usize idx; + + need_print = false; + modulus = 10000000000000000000u; + idx = 0; + while (modulus) + { + c = (char)(nb / modulus) + '0'; + if (c != '0' || need_print) + { + out[idx++] = c; + need_print = true; + } + nb = nb % modulus; + modulus /= 10; + } +} + +static t_str itoa_u64(t_u64 nb) +{ + char out[UINT_INLINE_BUF]; + t_u64 n; + + n = (t_u64)nb; + mem_set(out, 0, UINT_INLINE_BUF); + if (nb == 0) + out[0] = '0'; + else + itoa_inner(n, out); + return (str_clone(out)); +} + +void printf_u(t_printf_arg data, t_printf_func f) +{ + t_u64 value; + t_str start_num; + t_str void_write; + + value = *(t_u64 *)data.argument; + start_num = itoa_u64(value); + handle_weird_precision_stuff(&data, (t_prec_strs){.out = &start_num, + .free_out = true, .pretty = &void_write}, value); + pad_and_stuff( + (t_pad_and_stuff_args){\ +.fill_zero = 0, .fill = 0, .len = str_len(start_num), \ +.pretty = NULL, .pretty_len = 0, .str = start_num, \ +.allow_zero_fill = true, .sign = NULL, .sign_len = 0, }, data, f); + free(start_num); +} diff --git a/src/printf/formatter/utils.c b/src/printf/formatter/utils.c new file mode 100644 index 0000000..f8dd664 --- /dev/null +++ b/src/printf/formatter/utils.c @@ -0,0 +1,136 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/16 17:57:04 by maiboyer #+# #+# */ +/* Updated: 2023/12/01 21:20:07 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/buffered_str/buf_str.h" +#include "me/convert/atoi.h" +#include "me/printf/formatter/utils.h" +#include "me/printf/matchers/matchers.h" +#include "me/printf/printf.h" +#include "me/string/str_find_chr.h" +#include "me/string/str_substring.h" +#include "me/types.h" +#include +#include + +bool handle_atoi_stuff(t_const_str fmt, t_usize *c_idx, t_usize *nxt, + t_printf_arg *c_arg) +{ + t_i32 atoi_res; + + atoi_res = me_atoi(&fmt[*c_idx]); + if (atoi_res < 0) + { + *c_idx = *nxt; + *nxt = (t_usize)(str_find_chr(fmt + *nxt + 1, '%') - fmt); + return (false); + } + advance_atoi(fmt, c_idx); + c_arg->extra.align = (t_u64)atoi_res; + handle_prec_and_align(fmt, c_idx, c_arg); + atoi_res = atoi(&fmt[*c_idx]); + if (atoi_res < 0) + { + *c_idx = *nxt; + *nxt = (t_usize)(str_find_chr(fmt + *nxt + 1, '%') - fmt); + return (false); + } + advance_atoi(fmt, c_idx); + c_arg->extra.precision = (t_u64)atoi_res; + return (true); +} + +static void set_flags_if_needed(t_const_str fmt, t_usize *c_idx, + t_printf_arg *c_arg) +{ + if (fmt[*c_idx] == ' ') + { + (*c_idx)++; + c_arg->extra.space_align = true; + } + if (fmt[*c_idx] == '#') + { + (*c_idx)++; + c_arg->extra.pretty = true; + } + if (fmt[*c_idx] == '+') + { + (*c_idx)++; + c_arg->flags |= SIGN; + } + if (fmt[*c_idx] == '-') + { + (*c_idx)++; + c_arg->extra.left_align = true; + } + if (fmt[*c_idx] == '0') + { + (*c_idx)++; + c_arg->flags |= ZERO_ALIGN; + } +} + +bool set_params(t_const_str fmt, t_usize *c_idx, t_usize *nxt, + t_printf_arg *c_arg) +{ + t_usize b_idx; + + b_idx = ~0; + while (b_idx != *c_idx) + { + b_idx = *c_idx; + set_flags_if_needed(fmt, c_idx, c_arg); + } + return (handle_atoi_stuff(fmt, c_idx, nxt, c_arg)); +} + +t_printf_arg print_substr(t_usize *c_idx, t_usize *nxt, t_const_str fmt, + t_pad_inner_args extra) +{ + t_str truc; + + truc = str_substring(fmt, *c_idx, *nxt - *c_idx); + extra.f(truc, *nxt - *c_idx, extra.p_args); + free(truc); + *c_idx = *nxt + 1; + return ((t_printf_arg){ + .p_args = extra.p_args, + .argument = extra.arguments, + }); +} + +void pad_inner(t_const_str fmt, t_usize *c_idx, t_usize *nxt, + t_pad_inner_args extra) +{ + t_printf_arg c_arg; + t_matcher *matcher; + + c_arg = print_substr(c_idx, nxt, fmt, extra); + if (fmt[*c_idx] == '%') + { + (*c_idx)++; + extra.f("%", 1, extra.p_args); + } + else + { + if (!set_params(fmt, c_idx, nxt, &c_arg)) + return (ret_reset(c_idx, nxt, fmt)); + matcher = find_matcher(fmt, extra.matchers, c_idx); + if (matcher == NULL) + return (ret_reset(c_idx, nxt, fmt)); + call_matcher(matcher, c_arg, *extra.arguments, extra.f); + } + *nxt = (t_usize)(str_find_chr(fmt + *c_idx, '%')); + if (*nxt == 0) + return (*nxt = extra.fmt_len, (void)extra.f(fmt + *c_idx, extra.fmt_len + - *c_idx, extra.p_args)); + *nxt = *nxt - (t_usize)fmt; +} diff --git a/src/printf/formatter/utils2.c b/src/printf/formatter/utils2.c new file mode 100644 index 0000000..1537211 --- /dev/null +++ b/src/printf/formatter/utils2.c @@ -0,0 +1,96 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utils2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/16 18:00:07 by maiboyer #+# #+# */ +/* Updated: 2023/12/01 21:48:22 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/buffered_str/buf_str.h" +#include "me/char/isdigit.h" +#include "me/printf/formatter/utils.h" +#include "me/printf/printf.h" +#include "me/types.h" + +void set_var_for_pad_and_stuff(t_pad_and_stuff_args *a, t_printf_arg *d) +{ + t_isize fill_zero; + t_isize fill; + + fill_zero = 0; + fill = 0; + if (d->flags & ZERO_ALIGN && a->len < d->extra.align) + fill_zero = d->extra.align - a->len - a->fill_zero - 2 * d->extra.pretty + - a->sign_len; + if (d->flags & PRECISION && a->len < d->extra.precision) + fill_zero = d->extra.precision - a->len; + if (d->flags & (ALIGN) && a->len < d->extra.align) + fill = d->extra.align - a->len - a->fill_zero - 2 * d->extra.pretty + - a->sign_len - fill_zero; + if (fill_zero < 0) + fill_zero = 0; + if (fill < 0) + fill = 0; + if (fill_zero < 0) + fill_zero = 0; + if (fill < 0) + fill = 0; + a->fill_zero = fill_zero; + a->fill = fill; +} + +void print_with_func(t_pad_and_stuff_args *a, t_printf_arg *d, + t_printf_func f, t_const_str t) +{ + f(t, 1, d->p_args); + if (t[0] == ' ' && t[1] == 0 && a->fill) + a->fill--; + if (t[0] == '0' && t[1] == 0 && a->fill_zero) + a->fill_zero--; +} + +void pad_and_stuff(t_pad_and_stuff_args a, t_printf_arg d, t_printf_func f) +{ + set_var_for_pad_and_stuff(&a, &d); + if (!(d.extra.left_align || d.extra.precision) && d.flags & (ZERO_ALIGN)) + { + print_sign_if_needed(a, d, f); + while (a.allow_zero_fill && a.fill_zero > 0) + print_with_func(&a, &d, f, "0"); + } + else + { + while (!(d.extra.left_align) && d.flags & (ALIGN) && a.fill > 0) + print_with_func(&a, &d, f, " "); + print_sign_if_needed(a, d, f); + if (d.extra.pretty) + f(a.pretty, a.pretty_len, d.p_args); + } + while (a.allow_zero_fill && a.fill_zero > 0) + print_with_func(&a, &d, f, "0"); + f(a.str, a.len, d.p_args); + while (d.extra.left_align && a.fill > 0) + print_with_func(&a, &d, f, " "); +} + +void handle_prec_and_align(t_const_str fmt, t_usize *c_idx, + t_printf_arg *c_arg) +{ + if (c_arg->extra.align && !(c_arg->flags & ZERO_ALIGN)) + c_arg->flags |= ALIGN; + if (fmt[*c_idx] == '.') + { + (*c_idx)++; + c_arg->flags |= PRECISION; + } +} + +void advance_atoi(t_const_str fmt, t_usize *idx) +{ + while (me_isdigit(fmt[*idx])) + (*idx)++; +} diff --git a/src/printf/formatter/utils3.c b/src/printf/formatter/utils3.c new file mode 100644 index 0000000..1b03f1e --- /dev/null +++ b/src/printf/formatter/utils3.c @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utils3.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/16 18:06:15 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 19:21:38 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/printf/formatter/utils.h" +#include "me/printf/matchers/matchers.h" +#include "me/string/str_find_chr.h" +#include "me/string/str_len.h" +#include "me/types.h" +#include + +void ret_reset(t_usize *c_idx, t_usize *nxt, t_const_str fmt) +{ + *c_idx = *nxt; + *nxt = (t_usize)(str_find_chr(fmt + *nxt + 1, '%') - fmt); +} + +void me_printf_str_inner(t_const_str fmt, t_printf_func f, + va_list *arguments, void *p_args) +{ + t_usize c_idx; + t_usize nxt; + t_usize fmt_len; + + c_idx = 0; + fmt_len = str_len(fmt); + nxt = (t_usize)(str_find_chr(fmt, '%')); + if (nxt == 0) + return (f(fmt, fmt_len, p_args)); + nxt = nxt - (t_usize)fmt; + while (nxt < fmt_len) + { + pad_inner(fmt, &c_idx, &nxt, \ + ((t_pad_inner_args){\ + .p_args = p_args, .fmt_len = fmt_len, .f = f, \ + .arguments = arguments, .matchers = get_matchers(), })); + } +} + +void print_sign_if_needed(t_pad_and_stuff_args a, t_printf_arg d, + t_printf_func f) +{ + if (d.flags & (SIGN) && a.sign != NULL) + f(a.sign, a.sign_len, d.p_args); +} diff --git a/src/printf/formatter/utils_numbers.c b/src/printf/formatter/utils_numbers.c new file mode 100644 index 0000000..58e5095 --- /dev/null +++ b/src/printf/formatter/utils_numbers.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utils_numbers.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/01 21:05:47 by maiboyer #+# #+# */ +/* Updated: 2023/12/01 21:49:51 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/printf/formatter/utils.h" +#include "me/printf/printf.h" +#include + +void handle_weird_precision_stuff(t_printf_arg *data, t_prec_strs strs, + t_usize value) +{ + if (!value && data->extra.precision == 0 && (data->flags & PRECISION)) + { + data->flags &= (~ZERO_ALIGN); + data->flags |= ALIGN; + if (strs.free_out) + *strs.out = (free(*strs.out), (t_str)mem_alloc_array(1, 1)); + else + *strs.out = ""; + *strs.pretty = ""; + } + else if ((data->flags & PRECISION) && !data->extra.left_align) + { + data->flags &= (~ZERO_ALIGN); + data->flags |= ALIGN; + } +} diff --git a/src/printf/matchers.c b/src/printf/matchers.c new file mode 100644 index 0000000..d122dac --- /dev/null +++ b/src/printf/matchers.c @@ -0,0 +1,120 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* matchers.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/16 18:07:40 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 19:11:51 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_compare.h" +#include "me/printf/formatter/formatter.h" +#include "me/printf/matchers/matchers.h" +#include "me/printf/printf.h" +#include +#include +#include + +t_matcher_list *get_matchers(void) +{ + static t_matcher_list printf_matchers = (t_matcher_list){ + .data = { + {.matcher = "o", .matcher_len = 1, .arg_type = U64, .f = &printf_o}, + {.matcher = "c", .matcher_len = 1, .arg_type = CHAR, .f = &printf_c}, + {.matcher = "s", .matcher_len = 1, .arg_type = STR, .f = &printf_s}, + {.matcher = "p", .matcher_len = 1, .arg_type = VOID_PTR, .f = &printf_p}, + {.matcher = "d", .matcher_len = 1, .arg_type = I32, .f = &printf_d}, + {.matcher = "i", .matcher_len = 1, .arg_type = I32, .f = &printf_d}, + {.matcher = "u", .matcher_len = 1, .arg_type = U32, .f = &printf_u}, + {.matcher = "x", .matcher_len = 1, .arg_type = U32, .f = &printf_x_low}, + {.matcher = "X", .matcher_len = 1, .arg_type = U32, .f = &printf_x_up}, + }, + .next = NULL, + }; + + return (&printf_matchers); +} + +bool insert_matcher(t_matcher matcher) +{ + t_matcher_list *matchers; + t_usize i; + + matchers = get_matchers(); + while (matchers) + { + i = 0; + while (i < PRINTF_BUFFER_CHUNK) + { + if (matchers->data[i].f == NULL) + { + (*matchers).data[i] = matcher; + return (true); + } + } + matchers->next = malloc(sizeof(t_matcher_list) * 1); + } + return (false); +} + +t_matcher *find_matcher(t_const_str fmt, t_matcher_list *matchers, + t_usize *c_idx) +{ + t_usize matcher_index; + t_matcher *matcher; + + while (matchers) + { + matcher_index = 0; + while (matcher_index < PRINTF_BUFFER_CHUNK) + { + matcher = &matchers->data[matcher_index]; + if (matcher->f) + { + if (!mem_compare(&fmt[*c_idx], matcher->matcher, + matcher->matcher_len)) + { + *c_idx += matcher->matcher_len; + return (matcher); + } + } + matcher_index++; + } + matchers = matchers->next; + } + return (NULL); +} + +// FIGURE OUT HOW TO MAKE I64 WORK +void call_matcher(t_matcher *matcher, t_printf_arg matcher_arguments, + va_list args, t_printf_func f) +{ + t_matcher_tmp_val vals; + + matcher_arguments.argument = NULL; + if (matcher->arg_type == CHAR) + { + vals.chr_val = (char)va_arg(args, int); + matcher_arguments.argument = (void *)&vals.chr_val; + } + if (matcher->arg_type == U32) + vals.u64_val = va_arg(args, t_u32); + if (matcher->arg_type == U64) + vals.u64_val = va_arg(args, t_u64); + if (matcher->arg_type == I64) + vals.i64_val = va_arg(args, t_i64); + if (matcher->arg_type == I32) + vals.i64_val = va_arg(args, t_i32); + if (matcher->arg_type == I32 || matcher->arg_type == I64) + matcher_arguments.argument = (void *)&vals.i64_val; + if (matcher->arg_type == U32 || matcher->arg_type == U64) + matcher_arguments.argument = (void *)&vals.u64_val; + if (matcher->arg_type == VOID_PTR) + matcher_arguments.argument = (void *)va_arg(args, void *); + if (matcher->arg_type == STR) + matcher_arguments.argument = (void *)va_arg(args, t_str); + matcher->f(matcher_arguments, f); +} diff --git a/src/printf/printf.c b/src/printf/printf.c new file mode 100644 index 0000000..935b3cd --- /dev/null +++ b/src/printf/printf.c @@ -0,0 +1,120 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* printf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/11 17:50:56 by maiboyer #+# #+# */ +/* Updated: 2024/02/09 14:58:10 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/buffered_str/buf_str.h" +#include "me/fs/write.h" +#include "me/printf/formatter/formatter.h" +#include "me/printf/formatter/utils.h" +#include "me/printf/matchers/matchers.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#include "me/types.h" +#include +#include +#include +#include + +// p_args is an t_buffer_str; +static void me_printf_add_to_string(t_const_str to_write, t_usize to_write_len, + void *p_args) +{ + t_buffer_str *out_buf; + + out_buf = (t_buffer_str *)p_args; + (void)(to_write_len); + push_str_buffer(out_buf, to_write); +} + +t_str me_printf_str(t_const_str fmt, va_list *arguments) +{ + t_buffer_str out; + + out = alloc_new_buffer(str_len(fmt)); + if (out.buf == NULL) + { + return (NULL); + } + me_printf_str_inner(fmt, &me_printf_add_to_string, arguments, (void *)&out); + return (out.buf); +} + +void me_printf_write(t_const_str to_write, t_usize to_write_len, + void *p_args) +{ + t_fprintf_arg *arg; + + arg = (t_fprintf_arg *)p_args; + me_write(arg->fd, (t_u8 *)to_write, to_write_len); + arg->total_print += to_write_len; +} + +t_usize me_printf(t_const_str fmt, ...) +{ + va_list args; + t_fprintf_arg passthru; + + passthru = (t_fprintf_arg){ + .fd = 1, + .total_print = 0, + }; + va_start(args, fmt); + me_printf_str_inner(fmt, &me_printf_write, &args, (void *)&passthru); + va_end(args); + return (passthru.total_print); +} + +t_usize me_eprintf(t_const_str fmt, ...) +{ + va_list args; + t_fprintf_arg passthru; + + passthru = (t_fprintf_arg){ + .fd = 2, + .total_print = 0, + }; + va_start(args, fmt); + me_printf_str_inner(fmt, &me_printf_write, &args, (void *)&passthru); + va_end(args); + return (passthru.total_print); +} + +/* +t_usize me_printf(t_const_str fmt, ...) +{ + va_list args; + t_str str; + t_usize len; + + va_start(args, fmt); + str = me_printf_str(fmt, &args); + va_end(args); + len = str_len(str); + write(1, str, len); + free(str); + return (len); +} + +t_usize me_eprintf(t_const_str fmt, ...) +{ + va_list args; + t_str str; + t_usize len; + + va_start(args, fmt); + str = me_printf_str(fmt, &args); + va_end(args); + len = str_len(str); + write(2, str, len); + free(str); + return (len); +} +*/ diff --git a/src/printf/vprintf.c b/src/printf/vprintf.c new file mode 100644 index 0000000..6cf3bc0 --- /dev/null +++ b/src/printf/vprintf.c @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vprintf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/09 14:57:28 by maiboyer #+# #+# */ +/* Updated: 2024/02/09 15:00:39 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/buffered_str/buf_str.h" +#include "me/fs/write.h" +#include "me/printf/formatter/formatter.h" +#include "me/printf/formatter/utils.h" +#include "me/printf/matchers/matchers.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#include "me/types.h" +#include +#include +#include +#include + +void me_printf_write(t_const_str to_write, t_usize to_write_len, + void *p_args); + +t_usize me_vprintf(t_const_str fmt, va_list *args) +{ + t_fprintf_arg passthru; + + passthru = (t_fprintf_arg){ + .fd = 1, + .total_print = 0, + }; + me_printf_str_inner(fmt, &me_printf_write, args, (void *)&passthru); + return (passthru.total_print); +} + +t_usize me_veprintf(t_const_str fmt, va_list *args) +{ + t_fprintf_arg passthru; + + passthru = (t_fprintf_arg){ + .fd = 2, + .total_print = 0, + }; + me_printf_str_inner(fmt, &me_printf_write, args, (void *)&passthru); + return (passthru.total_print); +} diff --git a/src/string/str_clone.c b/src/string/str_clone.c new file mode 100644 index 0000000..70fd942 --- /dev/null +++ b/src/string/str_clone.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_clone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 16:05:48 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 18:15:57 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc.h" +#include "me/string/str_clone.h" +#include "me/string/str_l_copy.h" +#include "me/string/str_len.h" +#include + +t_str str_clone(t_const_str source) +{ + t_str res; + t_usize len; + + len = str_len(source) + 1; + res = mem_alloc(sizeof(unsigned char) * len); + if (res == NULL) + return (NULL); + str_l_copy(res, source, len); + return (res); +} diff --git a/src/string/str_find_chr.c b/src/string/str_find_chr.c new file mode 100644 index 0000000..565ead1 --- /dev/null +++ b/src/string/str_find_chr.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_find_chr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 17:29:13 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 14:47:43 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/string/str_find_chr.h" + +char *str_find_chr(t_const_str str, char chr) +{ + t_usize index; + + index = 0; + while (str[index]) + { + if (str[index] == chr) + return ((char *)&str[index]); + index++; + } + if (str[index] == chr) + return ((char *)&str[index]); + return (NULL); +} diff --git a/src/string/str_find_rev_chr.c b/src/string/str_find_rev_chr.c new file mode 100644 index 0000000..1930873 --- /dev/null +++ b/src/string/str_find_rev_chr.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_find_rev_chr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 17:29:13 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 14:47:47 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/string/str_find_rev_chr.h" +#include "me/string/str_len.h" + +char *str_find_rev_chr(t_const_str str, char chr) +{ + t_usize index; + + index = str_len((t_str)str); + while (index >= 0) + { + if (str[index] == chr) + return ((char *)&str[index]); + index--; + } + return (NULL); +} diff --git a/src/string/str_find_str.c b/src/string/str_find_str.c new file mode 100644 index 0000000..4afa489 --- /dev/null +++ b/src/string/str_find_str.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_find_str.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/10 11:11:01 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:53:53 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/string/str_n_find_str.h" + +const char *str_find_str(t_const_str str, t_const_str to_find) +{ + t_str needle; + t_str haystack; + + if (*to_find == '\0') + return (str); + while (*str) + { + haystack = (t_str)str; + needle = (t_str)to_find; + while (*haystack && *haystack == *needle) + { + haystack++; + needle++; + } + if (*needle == '\0') + return (str); + str++; + } + return (NULL); +} + +/*R + +#include +#include + +int main(int argc, char *argv[]) { + if (argc != 3) + return (2); + printf("HAYSTACK = '%s'\n", argv[1]); + printf(" NEEDLE = '%s'\n", argv[2]); + + printf("libc: %p : '%s'\n", str_find_str(argv[1], argv[2]), + str_find_str(argv[1], + argv[2])); + printf(" ft : %p : '%s'\n", + str_find_str(argv[1], argv[2]), + str_find_str(argv[1], argv[2])); +} +R*/ diff --git a/src/string/str_iter.c b/src/string/str_iter.c new file mode 100644 index 0000000..dc9abb0 --- /dev/null +++ b/src/string/str_iter.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_iter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 18:26:00 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 14:49:47 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/string/str_iter.h" + +void str_iter(t_str s, void (*f)(t_usize, char *)) +{ + t_usize idx; + + if (s == NULL) + return ; + idx = 0; + while (s[idx]) + { + f(idx, &s[idx]); + idx++; + } +} diff --git a/src/string/str_join.c b/src/string/str_join.c new file mode 100644 index 0000000..5aacfd9 --- /dev/null +++ b/src/string/str_join.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_join.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 23:02:58 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 18:16:19 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc.h" +#include "me/string/str_join.h" +#include "me/string/str_l_cat.h" +#include "me/string/str_l_copy.h" +#include "me/string/str_len.h" +#include + +t_str str_join(t_const_str s1, t_const_str s2) +{ + t_str out; + t_usize buf_size; + + if (s1 == NULL || s2 == NULL) + return (NULL); + buf_size = str_len(s1) + str_len(s2) + 1; + out = mem_alloc(sizeof(char) * buf_size); + if (out == NULL) + return (NULL); + str_l_copy(out, s1, buf_size); + str_l_cat(out, s2, buf_size); + return (out); +} diff --git a/src/string/str_l_cat.c b/src/string/str_l_cat.c new file mode 100644 index 0000000..a606722 --- /dev/null +++ b/src/string/str_l_cat.c @@ -0,0 +1,108 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_l_cat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/09 18:01:09 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 14:51:34 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/*R + +//CFLAGS="-lbsd" + +#include +#include +#include + +R*/ + +#include "me/string/str_l_cat.h" +#include "me/string/str_len.h" + +t_usize str_l_cat(t_str dest, t_const_str src, t_usize buffer_size) +{ + t_usize dest_len; + t_usize src_len; + t_usize i; + + if (buffer_size == 0 && (dest == NULL || src == NULL)) + return (0); + dest_len = str_len(dest); + src_len = str_len(src); + if (dest_len >= buffer_size) + return (buffer_size + src_len); + i = 0; + while (src[i] && dest_len + i < buffer_size - 1) + { + dest[dest_len + i] = src[i]; + i++; + } + dest[dest_len + i] = '\0'; + return (dest_len + src_len); +} + +/*R + +#include +#include +#include + +#define SIZE 64 + +int main(void) { + char* dest_ft; + char* dest_libc; + char* to_cat = "banane"; + unsigned int res_libc = 0; + unsigned int res_ft = 0; + int i; + + for (int v = 0; v <= SIZE; v++) + for (int j = 0; j < SIZE; j++) + { + dest_ft = malloc(SIZE); + dest_libc = malloc(SIZE); + i = 0; + while (i < SIZE) + { + dest_ft[i] = 'X'; + dest_libc[i] = 'X'; + i++; + } + dest_ft[SIZE - j - 1] = 0; + dest_libc[SIZE - j - 1] = 0; + res_libc = str_l_cat(dest_libc, to_cat, SIZE - v); + res_ft = str_l_cat(dest_ft, to_cat, SIZE - v); + int k; + k = 0; + while (k < SIZE && dest_libc[k] == dest_ft[k]) + k++; + + if (strcmp(dest_ft, dest_libc) != 0 || res_ft != res_libc + || k != SIZE) + { + printf("----------v[%d]-j[%d]---------\n", v, j); + printf("libc: [%d]\t'%s'\n", res_libc ,dest_libc); + printf(" ft : [%d]\t'%s'\n", res_ft, dest_ft); + printf("[ERROR] byte %d is different: LIBC[%X] != FT[%X]\n\n", + i, + dest_libc[j], dest_ft[j]); + printf(" ft :"); + for (int x = 0; x < SIZE; x++) + printf("%02X", dest_ft[x]); + printf("\n"); + printf("libc:"); + for (int x = 0; x < SIZE; x++) + printf("%02X", dest_libc[x]); + printf("\n"); + printf("\n"); + } + free(dest_libc); + free(dest_ft); + } +} +R*/ diff --git a/src/string/str_l_copy.c b/src/string/str_l_copy.c new file mode 100644 index 0000000..e4bb6bc --- /dev/null +++ b/src/string/str_l_copy.c @@ -0,0 +1,104 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_l_copy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/09 18:01:09 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 14:51:55 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/*R + +//CFLAGS="-lbsd" + +#include +#include +#include + +R*/ + +#include "me/string/str_l_copy.h" +#include "me/string/str_len.h" + +t_usize str_l_copy(t_str dest, t_const_str src, t_usize buffer_size) +{ + t_usize src_len; + t_usize i; + + src_len = str_len(src); + i = 0; + if (buffer_size == 0) + return (src_len); + while (src[i] && i < buffer_size - 1) + { + dest[i] = src[i]; + i++; + } + dest[i] = '\0'; + return (src_len); +} + +/*R + +#include +#include +#include + +#define SIZE 64 + +int main(void) { + char* dest_ft; + char* dest_libc; + char* to_cat = "banane"; + unsigned int res_libc = 0; + unsigned int res_ft = 0; + int i; + + for (int v = 0; v <= SIZE; v++) + for (int j = 0; j < SIZE; j++) + { + dest_ft = malloc(SIZE); + dest_libc = malloc(SIZE); + i = 0; + while (i < SIZE) + { + dest_ft[i] = 'X'; + dest_libc[i] = 'X'; + i++; + } + dest_ft[SIZE - j - 1] = 0; + dest_libc[SIZE - j - 1] = 0; + res_libc = str_l_cat(dest_libc, to_cat, SIZE - v); + res_ft = str_l_cat(dest_ft, to_cat, SIZE - v); + int k; + k = 0; + while (k < SIZE && dest_libc[k] == dest_ft[k]) + k++; + + if (strcmp(dest_ft, dest_libc) != 0 || res_ft != res_libc + || k != SIZE) + { + printf("----------v[%d]-j[%d]---------\n", v, j); + printf("libc: [%d]\t'%s'\n", res_libc ,dest_libc); + printf(" ft : [%d]\t'%s'\n", res_ft, dest_ft); + printf("[ERROR] byte %d is different: LIBC[%X] != FT[%X]\n\n", + i, + dest_libc[j], dest_ft[j]); + printf(" ft :"); + for (int x = 0; x < SIZE; x++) + printf("%02X", dest_ft[x]); + printf("\n"); + printf("libc:"); + for (int x = 0; x < SIZE; x++) + printf("%02X", dest_libc[x]); + printf("\n"); + printf("\n"); + } + free(dest_libc); + free(dest_ft); + } +} +R*/ diff --git a/src/string/str_len.c b/src/string/str_len.c new file mode 100644 index 0000000..1feb088 --- /dev/null +++ b/src/string/str_len.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_len.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 17:07:41 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 14:52:13 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/string/str_len.h" + +t_usize str_len(t_const_str str) +{ + t_usize out; + + out = 0; + while (str[out]) + out++; + return (out); +} diff --git a/src/string/str_map.c b/src/string/str_map.c new file mode 100644 index 0000000..2e65a86 --- /dev/null +++ b/src/string/str_map.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_map.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 18:26:00 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 14:52:34 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/string/str_clone.h" +#include "me/string/str_map.h" + +t_str str_map(t_const_str s, char (*f)(t_usize, char)) +{ + t_str out; + t_usize idx; + + if (f == NULL || s == NULL) + return (NULL); + out = str_clone((t_str)s); + if (out == NULL) + return (NULL); + idx = 0; + while (s[idx]) + { + out[idx] = f(idx, s[idx]); + idx++; + } + return (out); +} diff --git a/src/string/str_n_compare.c b/src/string/str_n_compare.c new file mode 100644 index 0000000..5c39ac5 --- /dev/null +++ b/src/string/str_n_compare.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_n_compare.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 18:53:47 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 14:52:48 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/string/str_n_compare.h" + +// PLEASE FIX THIS FUNCTION IF NEEDED ! +t_i32 str_n_compare(t_const_str lhs, t_const_str rhs, t_usize n) +{ + t_usize index; + + index = 0; + if (n == 0) + return (0); + while (lhs[index] && rhs[index] && lhs[index] == rhs[index] && index < n + - 1) + index++; + return ((t_i32)(t_u8)lhs[index] - (t_i32)(t_u8)rhs[index]); +} diff --git a/src/string/str_n_find_str.c b/src/string/str_n_find_str.c new file mode 100644 index 0000000..321af97 --- /dev/null +++ b/src/string/str_n_find_str.c @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_n_find_str.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/10 11:11:01 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:53:21 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/string/str_len.h" +#include "me/string/str_n_find_str.h" + +static t_str local_get_end_of_search(t_usize len, t_str str) +{ + t_usize out_len; + + out_len = str_len(str); + if (len > out_len) + len = out_len; + return (str + len); +} + +const char *str_n_find_str(t_const_str str, t_const_str to_find, t_usize len) +{ + t_str needle; + t_str haystack; + t_str end_of_search; + + if ((str == NULL || to_find == NULL) && len == 0) + return (NULL); + if (*to_find == '\0') + return ((t_str)str); + end_of_search = local_get_end_of_search(len, (t_str)str); + while (*str) + { + haystack = (t_str)str; + needle = (t_str)to_find; + while (haystack < end_of_search && *haystack && *haystack == *needle) + { + haystack++; + needle++; + } + if (*needle == '\0') + return (str); + str++; + } + return (NULL); +} diff --git a/src/string/str_split.c b/src/string/str_split.c new file mode 100644 index 0000000..d533e60 --- /dev/null +++ b/src/string/str_split.c @@ -0,0 +1,93 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/17 15:56:59 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:52:08 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc.h" +#include "me/mem/mem_alloc_array.h" +#include "me/string/str_l_copy.h" +#include "me/string/str_split.h" +#include + +static t_usize local_count_words(t_const_str str, char chr); +static t_str *local_split_inner(t_const_str str, char chr, t_str *out); +static t_str *local_split_freeall(t_str **to_free); + +static t_usize local_count_words(t_const_str str, char chr) +{ + t_usize i; + t_usize out; + + out = 0; + i = 0; + while (str[i]) + { + while (str[i] && str[i] == chr) + i++; + if (str[i] == 0) + return (out); + out++; + while (str[i] && str[i] != chr) + i++; + } + return (out); +} + +static t_str *local_split_freeall(t_str **to_free) +{ + while (*to_free) + free(*(to_free++)); + return (NULL); +} + +static t_str *local_split_inner(t_const_str str, char chr, t_str *out) +{ + t_usize str_i; + t_usize sub_i; + t_usize ptr_i; + + str_i = 0; + ptr_i = 0; + while (str[str_i]) + { + while (str[str_i] && str[str_i] == chr) + str_i++; + if (str[str_i] == 0) + break ; + sub_i = 0; + while (str[str_i + sub_i] && str[str_i + sub_i] != chr) + sub_i++; + out[ptr_i] = mem_alloc(sizeof(char) * (sub_i + 1)); + if (out[ptr_i] == NULL) + return (local_split_freeall(&out)); + str_l_copy(out[ptr_i++], (t_str)(str + str_i), sub_i + 1); + str_i += sub_i; + } + out[ptr_i] = NULL; + return (out); +} + +t_str *str_split(t_const_str str, char chr) +{ + t_usize ptr_len; + t_str *out; + + if (str == NULL || *str == 0) + { + out = mem_alloc(sizeof(t_str) * 1); + *out = NULL; + return (out); + } + ptr_len = local_count_words(str, chr); + out = mem_alloc_array(sizeof(t_str), (ptr_len + 1)); + if (out == NULL) + return (NULL); + return (local_split_inner(str, chr, out)); +} diff --git a/src/string/str_substring.c b/src/string/str_substring.c new file mode 100644 index 0000000..790bd31 --- /dev/null +++ b/src/string/str_substring.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_substring.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 22:42:55 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:52:34 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc.h" +#include "me/string/str_l_copy.h" +#include "me/string/str_len.h" +#include "me/string/str_substring.h" +#include + +t_str str_substring(t_const_str str, t_usize start, t_usize len) +{ + t_usize len_str; + t_usize len_str_substring; + t_str out; + + if (str == NULL) + return (NULL); + len_str = str_len(str); + if (start >= len_str) + return (mem_alloc(1)); + len_str_substring = len_str - start + 1; + if (len_str_substring > len) + len_str_substring = len + 1; + out = mem_alloc(sizeof(char) * len_str_substring); + if (out == NULL) + return (NULL); + str_l_copy(out, &str[start], len_str_substring); + return (out); +} diff --git a/src/string/str_trim.c b/src/string/str_trim.c new file mode 100644 index 0000000..9320f10 --- /dev/null +++ b/src/string/str_trim.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_trim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 23:43:42 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 18:16:31 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc.h" +#include "me/string/str_find_chr.h" +#include "me/string/str_l_copy.h" +#include "me/string/str_len.h" +#include "me/string/str_trim.h" +#include + +t_str str_trim(t_const_str str, t_const_str charset) +{ + t_usize start_idx; + t_usize end_idx; + t_usize buf_size; + t_str out; + + if (str == NULL || charset == NULL) + return (0); + start_idx = 0; + while (str[start_idx] && str_find_chr((t_str)charset, + str[start_idx]) != NULL) + start_idx++; + end_idx = str_len((t_str)str); + while (end_idx > 0 && str_find_chr((t_str)charset, str[end_idx]) != NULL) + end_idx--; + buf_size = end_idx - start_idx + 2; + if (end_idx == 0) + buf_size = 1; + out = mem_alloc(sizeof(char) * buf_size); + if (out == NULL) + return (NULL); + str_l_copy(out, &((t_str)str)[start_idx], buf_size); + return (out); +} diff --git a/src/vec/vec_buf_str.c b/src/vec/vec_buf_str.c new file mode 100644 index 0000000..281433a --- /dev/null +++ b/src/vec/vec_buf_str.c @@ -0,0 +1,115 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_buf_str.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/05 18:46:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 17:54:11 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_copy.h" +#include "me/mem/mem_set_zero.h" +#include "me/types.h" +#include "me/vec/vec_buf_str.h" +#include + +t_vec_buf_str vec_buf_str_new(t_usize capacity, + t_free_buf_str_item free_function) +{ + t_vec_buf_str out; + + out = (t_vec_buf_str){0}; + out.free_func = free_function; + out.buffer = mem_alloc_array(capacity, sizeof(t_buffer_str)); + if (out.buffer) + out.capacity = capacity; + return (out); +} + +/// Return true in case of an error +t_error vec_buf_str_push(t_vec_buf_str *vec, t_buffer_str element) +{ + t_buffer_str *temp_buffer; + size_t new_capacity; + + if (vec == NULL) + return (ERROR); + if (vec->len + 1 > vec->capacity) + { + new_capacity = (vec->capacity * 3) / 2 + 1; + while (vec->len + 1 > new_capacity) + new_capacity = (new_capacity * 3) / 2 + 1; + temp_buffer = mem_alloc_array(new_capacity, sizeof(t_buffer_str)); + if (temp_buffer == NULL) + return (ERROR); + mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_buffer_str)); + free(vec->buffer); + vec->buffer = temp_buffer; + vec->capacity = new_capacity; + } + vec->buffer[vec->len] = element; + vec->len += 1; + return (NO_ERROR); +} + +/// Return true in case of an error +t_error vec_buf_str_reserve(t_vec_buf_str *vec, t_usize wanted_capacity) +{ + t_buffer_str *temp_buffer; + size_t new_capacity; + + if (vec == NULL) + return (ERROR); + if (wanted_capacity > vec->capacity) + { + new_capacity = (vec->capacity * 3) / 2 + 1; + while (wanted_capacity > new_capacity) + new_capacity = (new_capacity * 3) / 2 + 1; + temp_buffer = mem_alloc_array(new_capacity, sizeof(t_buffer_str)); + if (temp_buffer == NULL) + return (ERROR); + mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_buffer_str)); + free(vec->buffer); + vec->buffer = temp_buffer; + vec->capacity = new_capacity; + } + return (NO_ERROR); +} + +/// Return true if the vector is empty +/// This function is safe to call with value being NULL +t_error vec_buf_str_pop(t_vec_buf_str *vec, t_buffer_str *value) +{ + t_buffer_str temp_value; + t_buffer_str *ptr; + + if (vec == NULL) + return (ERROR); + ptr = value; + if (vec->len == 0) + return (ERROR); + if (value == NULL) + ptr = &temp_value; + vec->len--; + *ptr = vec->buffer[vec->len]; + mem_set_zero(&vec->buffer[vec->len], sizeof(t_buffer_str)); + return (NO_ERROR); +} + +/// This function is safe to call with `free_elem` being NULL +void vec_buf_str_free(t_vec_buf_str vec) +{ + if (vec.free_func) + { + while (vec.len) + { + vec.free_func(vec.buffer[vec.len - 1]); + vec.len--; + } + } + free(vec.buffer); +} diff --git a/src/vec/vec_buf_str_functions2.c b/src/vec/vec_buf_str_functions2.c new file mode 100644 index 0000000..6bee346 --- /dev/null +++ b/src/vec/vec_buf_str_functions2.c @@ -0,0 +1,110 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_buf_str.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_copy.h" +#include "me/mem/mem_set_zero.h" +#include "me/types.h" +#include "me/vec/vec_buf_str.h" +#include + +t_error vec_buf_str_find(t_vec_buf_str *vec, bool (*fn)(const t_buffer_str *), + t_usize *index) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || index == NULL) + return (ERROR); + idx = 0; + while (idx < vec->len) + { + if (fn(&vec->buffer[idx])) + { + *index = idx; + return (NO_ERROR); + } + idx++; + } + return (ERROR); +} + +t_error vec_buf_str_find_starting(t_vec_buf_str *vec, + bool (*fn)(const t_buffer_str *), t_usize starting_index, + t_usize *index) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || index == NULL) + return (ERROR); + idx = starting_index; + while (idx < vec->len) + { + if (fn(&vec->buffer[idx])) + { + *index = idx; + return (NO_ERROR); + } + idx++; + } + return (ERROR); +} + +t_error vec_buf_str_all(t_vec_buf_str *vec, bool (*fn)(const t_buffer_str *), + bool *result) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || result == NULL) + return (ERROR); + idx = 0; + *result = true; + while (*result && idx < vec->len) + { + if (!fn(&vec->buffer[idx])) + *result = false; + idx++; + } + return (ERROR); +} + +t_error vec_buf_str_any(t_vec_buf_str *vec, bool (*fn)(const t_buffer_str *), + bool *result) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || result == NULL) + return (ERROR); + idx = 0; + *result = false; + while (*result && idx < vec->len) + { + if (fn(&vec->buffer[idx])) + *result = true; + idx++; + } + return (ERROR); +} + +void vec_buf_str_iter(t_vec_buf_str *vec, void (*fn)(t_usize index, + t_buffer_str *value, void *state), void *state) +{ + t_usize idx; + + if (vec == NULL || fn == NULL) + return ; + idx = 0; + while (idx < vec->len) + { + fn(idx, &vec->buffer[idx], state); + idx++; + } +} diff --git a/src/vec/vec_buf_str_functions3.c b/src/vec/vec_buf_str_functions3.c new file mode 100644 index 0000000..2b51c44 --- /dev/null +++ b/src/vec/vec_buf_str_functions3.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_buf_str.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_copy.h" +#include "me/mem/mem_set_zero.h" +#include "me/types.h" +#include "me/vec/vec_buf_str.h" +#include + +t_error vec_buf_str_push_front(t_vec_buf_str *vec, t_buffer_str element) +{ + t_usize i; + + if (vec->len == 0) + return (vec_buf_str_push(vec, element)); + i = vec->len - 1; + if (vec->capacity < vec->len + 1 && vec_buf_str_reserve(vec, 3 * vec->len + / 2 + 1)) + return (ERROR); + while (i > 0) + { + vec->buffer[i + 1] = vec->buffer[i]; + i--; + } + vec->buffer[1] = vec->buffer[0]; + vec->buffer[0] = element; + vec->len++; + return (NO_ERROR); +} + +t_error vec_buf_str_pop_front(t_vec_buf_str *vec, t_buffer_str *value) +{ + t_usize i; + + if (vec->len <= 1) + return (vec_buf_str_pop(vec, value)); + i = 0; + *value = vec->buffer[0]; + vec->len--; + while (i < vec->len) + { + vec->buffer[i] = vec->buffer[i + 1]; + i++; + } + mem_set_zero(&vec->buffer[i], sizeof(*vec->buffer)); + return (NO_ERROR); +} + +void vec_buf_str_reverse(t_vec_buf_str *vec) +{ + t_buffer_str temporary; + t_usize i; + + i = 0; + while (i < vec->len / 2) + { + temporary = vec->buffer[vec->len - 1 - i]; + vec->buffer[vec->len - 1 - i] = vec->buffer[i]; + vec->buffer[i] = temporary; + i++; + } +} diff --git a/src/vec/vec_i64.c b/src/vec/vec_i64.c new file mode 100644 index 0000000..1a24dec --- /dev/null +++ b/src/vec/vec_i64.c @@ -0,0 +1,114 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/05 18:46:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 17:54:11 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_copy.h" +#include "me/mem/mem_set_zero.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" +#include + +t_vec_i64 vec_i64_new(t_usize capacity, t_free_i64_item free_function) +{ + t_vec_i64 out; + + out = (t_vec_i64){0}; + out.free_func = free_function; + out.buffer = mem_alloc_array(capacity, sizeof(t_i64)); + if (out.buffer) + out.capacity = capacity; + return (out); +} + +/// Return true in case of an error +t_error vec_i64_push(t_vec_i64 *vec, t_i64 element) +{ + t_i64 *temp_buffer; + size_t new_capacity; + + if (vec == NULL) + return (ERROR); + if (vec->len + 1 > vec->capacity) + { + new_capacity = (vec->capacity * 3) / 2 + 1; + while (vec->len + 1 > new_capacity) + new_capacity = (new_capacity * 3) / 2 + 1; + temp_buffer = mem_alloc_array(new_capacity, sizeof(t_i64)); + if (temp_buffer == NULL) + return (ERROR); + mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_i64)); + free(vec->buffer); + vec->buffer = temp_buffer; + vec->capacity = new_capacity; + } + vec->buffer[vec->len] = element; + vec->len += 1; + return (NO_ERROR); +} + +/// Return true in case of an error +t_error vec_i64_reserve(t_vec_i64 *vec, t_usize wanted_capacity) +{ + t_i64 *temp_buffer; + size_t new_capacity; + + if (vec == NULL) + return (ERROR); + if (wanted_capacity > vec->capacity) + { + new_capacity = (vec->capacity * 3) / 2 + 1; + while (wanted_capacity > new_capacity) + new_capacity = (new_capacity * 3) / 2 + 1; + temp_buffer = mem_alloc_array(new_capacity, sizeof(t_i64)); + if (temp_buffer == NULL) + return (ERROR); + mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_i64)); + free(vec->buffer); + vec->buffer = temp_buffer; + vec->capacity = new_capacity; + } + return (NO_ERROR); +} + +/// Return true if the vector is empty +/// This function is safe to call with value being NULL +t_error vec_i64_pop(t_vec_i64 *vec, t_i64 *value) +{ + t_i64 temp_value; + t_i64 *ptr; + + if (vec == NULL) + return (ERROR); + ptr = value; + if (vec->len == 0) + return (ERROR); + if (value == NULL) + ptr = &temp_value; + vec->len--; + *ptr = vec->buffer[vec->len]; + mem_set_zero(&vec->buffer[vec->len], sizeof(t_i64)); + return (NO_ERROR); +} + +/// This function is safe to call with `free_elem` being NULL +void vec_i64_free(t_vec_i64 vec) +{ + if (vec.free_func) + { + while (vec.len) + { + vec.free_func(vec.buffer[vec.len - 1]); + vec.len--; + } + } + free(vec.buffer); +} diff --git a/src/vec/vec_i64_bool.c b/src/vec/vec_i64_bool.c new file mode 100644 index 0000000..af10724 --- /dev/null +++ b/src/vec/vec_i64_bool.c @@ -0,0 +1,115 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_i64_bool.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/05 18:46:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 17:54:11 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_copy.h" +#include "me/mem/mem_set_zero.h" +#include "me/types.h" +#include "me/vec/vec_i64_bool.h" +#include + +t_vec_i64_bool vec_i64_bool_new(t_usize capacity, + t_free_i64_bool_item free_function) +{ + t_vec_i64_bool out; + + out = (t_vec_i64_bool){0}; + out.free_func = free_function; + out.buffer = mem_alloc_array(capacity, sizeof(t_i64_bool)); + if (out.buffer) + out.capacity = capacity; + return (out); +} + +/// Return true in case of an error +t_error vec_i64_bool_push(t_vec_i64_bool *vec, t_i64_bool element) +{ + t_i64_bool *temp_buffer; + size_t new_capacity; + + if (vec == NULL) + return (ERROR); + if (vec->len + 1 > vec->capacity) + { + new_capacity = (vec->capacity * 3) / 2 + 1; + while (vec->len + 1 > new_capacity) + new_capacity = (new_capacity * 3) / 2 + 1; + temp_buffer = mem_alloc_array(new_capacity, sizeof(t_i64_bool)); + if (temp_buffer == NULL) + return (ERROR); + mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_i64_bool)); + free(vec->buffer); + vec->buffer = temp_buffer; + vec->capacity = new_capacity; + } + vec->buffer[vec->len] = element; + vec->len += 1; + return (NO_ERROR); +} + +/// Return true in case of an error +t_error vec_i64_bool_reserve(t_vec_i64_bool *vec, t_usize wanted_capacity) +{ + t_i64_bool *temp_buffer; + size_t new_capacity; + + if (vec == NULL) + return (ERROR); + if (wanted_capacity > vec->capacity) + { + new_capacity = (vec->capacity * 3) / 2 + 1; + while (wanted_capacity > new_capacity) + new_capacity = (new_capacity * 3) / 2 + 1; + temp_buffer = mem_alloc_array(new_capacity, sizeof(t_i64_bool)); + if (temp_buffer == NULL) + return (ERROR); + mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_i64_bool)); + free(vec->buffer); + vec->buffer = temp_buffer; + vec->capacity = new_capacity; + } + return (NO_ERROR); +} + +/// Return true if the vector is empty +/// This function is safe to call with value being NULL +t_error vec_i64_bool_pop(t_vec_i64_bool *vec, t_i64_bool *value) +{ + t_i64_bool temp_value; + t_i64_bool *ptr; + + if (vec == NULL) + return (ERROR); + ptr = value; + if (vec->len == 0) + return (ERROR); + if (value == NULL) + ptr = &temp_value; + vec->len--; + *ptr = vec->buffer[vec->len]; + mem_set_zero(&vec->buffer[vec->len], sizeof(t_i64_bool)); + return (NO_ERROR); +} + +/// This function is safe to call with `free_elem` being NULL +void vec_i64_bool_free(t_vec_i64_bool vec) +{ + if (vec.free_func) + { + while (vec.len) + { + vec.free_func(vec.buffer[vec.len - 1]); + vec.len--; + } + } + free(vec.buffer); +} diff --git a/src/vec/vec_i64_bool_functions2.c b/src/vec/vec_i64_bool_functions2.c new file mode 100644 index 0000000..01927c2 --- /dev/null +++ b/src/vec/vec_i64_bool_functions2.c @@ -0,0 +1,109 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_i64_bool.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_copy.h" +#include "me/mem/mem_set_zero.h" +#include "me/types.h" +#include "me/vec/vec_i64_bool.h" +#include + +t_error vec_i64_bool_find(t_vec_i64_bool *vec, bool (*fn)(const t_i64_bool *), + t_usize *index) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || index == NULL) + return (ERROR); + idx = 0; + while (idx < vec->len) + { + if (fn(&vec->buffer[idx])) + { + *index = idx; + return (NO_ERROR); + } + idx++; + } + return (ERROR); +} + +t_error vec_i64_bool_find_starting(t_vec_i64_bool *vec, + bool (*fn)(const t_i64_bool *), t_usize starting_index, t_usize *index) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || index == NULL) + return (ERROR); + idx = starting_index; + while (idx < vec->len) + { + if (fn(&vec->buffer[idx])) + { + *index = idx; + return (NO_ERROR); + } + idx++; + } + return (ERROR); +} + +t_error vec_i64_bool_all(t_vec_i64_bool *vec, bool (*fn)(const t_i64_bool *), + bool *result) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || result == NULL) + return (ERROR); + idx = 0; + *result = true; + while (*result && idx < vec->len) + { + if (!fn(&vec->buffer[idx])) + *result = false; + idx++; + } + return (ERROR); +} + +t_error vec_i64_bool_any(t_vec_i64_bool *vec, bool (*fn)(const t_i64_bool *), + bool *result) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || result == NULL) + return (ERROR); + idx = 0; + *result = false; + while (*result && idx < vec->len) + { + if (fn(&vec->buffer[idx])) + *result = true; + idx++; + } + return (ERROR); +} + +void vec_i64_bool_iter(t_vec_i64_bool *vec, void (*fn)(t_usize index, + t_i64_bool *value, void *state), void *state) +{ + t_usize idx; + + if (vec == NULL || fn == NULL) + return ; + idx = 0; + while (idx < vec->len) + { + fn(idx, &vec->buffer[idx], state); + idx++; + } +} diff --git a/src/vec/vec_i64_bool_functions3.c b/src/vec/vec_i64_bool_functions3.c new file mode 100644 index 0000000..ee82947 --- /dev/null +++ b/src/vec/vec_i64_bool_functions3.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_i64_bool.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_copy.h" +#include "me/mem/mem_set_zero.h" +#include "me/types.h" +#include "me/vec/vec_i64_bool.h" +#include + +t_error vec_i64_bool_push_front(t_vec_i64_bool *vec, t_i64_bool element) +{ + t_usize i; + + if (vec->len == 0) + return (vec_i64_bool_push(vec, element)); + i = vec->len - 1; + if (vec->capacity < vec->len + 1 && vec_i64_bool_reserve(vec, 3 * vec->len + / 2 + 1)) + return (ERROR); + while (i > 0) + { + vec->buffer[i + 1] = vec->buffer[i]; + i--; + } + vec->buffer[1] = vec->buffer[0]; + vec->buffer[0] = element; + vec->len++; + return (NO_ERROR); +} + +t_error vec_i64_bool_pop_front(t_vec_i64_bool *vec, t_i64_bool *value) +{ + t_usize i; + + if (vec->len <= 1) + return (vec_i64_bool_pop(vec, value)); + i = 0; + *value = vec->buffer[0]; + vec->len--; + while (i < vec->len) + { + vec->buffer[i] = vec->buffer[i + 1]; + i++; + } + mem_set_zero(&vec->buffer[i], sizeof(*vec->buffer)); + return (NO_ERROR); +} + +void vec_i64_bool_reverse(t_vec_i64_bool *vec) +{ + t_i64_bool temporary; + t_usize i; + + i = 0; + while (i < vec->len / 2) + { + temporary = vec->buffer[vec->len - 1 - i]; + vec->buffer[vec->len - 1 - i] = vec->buffer[i]; + vec->buffer[i] = temporary; + i++; + } +} diff --git a/src/vec/vec_i64_bool_sort.c b/src/vec/vec_i64_bool_sort.c new file mode 100644 index 0000000..edbdfc1 --- /dev/null +++ b/src/vec/vec_i64_bool_sort.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* best_move.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/29 20:04:33 by maiboyer #+# #+# */ +/* Updated: 2024/01/31 14:25:00 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/types.h" +#include "me/vec/vec_i64_bool.h" + +void vec_i64_bool_sort(t_vec_i64_bool *v, + t_vec_i64_bool_sort_fn is_sorted_fn) +{ + t_usize sorted_part; + t_usize i; + t_i64_bool tmp; + + if (v == NULL) + return ; + sorted_part = v->len; + while (sorted_part > 0) + { + i = 0; + while (i < sorted_part - 1) + { + if (!is_sorted_fn(&v->buffer[i], &v->buffer[i + 1])) + { + tmp = v->buffer[i]; + v->buffer[i] = v->buffer[i + 1]; + v->buffer[i + 1] = tmp; + } + i++; + } + sorted_part--; + } +} diff --git a/src/vec/vec_i64_functions2.c b/src/vec/vec_i64_functions2.c new file mode 100644 index 0000000..d7180bc --- /dev/null +++ b/src/vec/vec_i64_functions2.c @@ -0,0 +1,106 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_copy.h" +#include "me/mem/mem_set_zero.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" +#include + +t_error vec_i64_find(t_vec_i64 *vec, bool (*fn)(const t_i64 *), t_usize *index) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || index == NULL) + return (ERROR); + idx = 0; + while (idx < vec->len) + { + if (fn(&vec->buffer[idx])) + { + *index = idx; + return (NO_ERROR); + } + idx++; + } + return (ERROR); +} + +t_error vec_i64_find_starting(t_vec_i64 *vec, bool (*fn)(const t_i64 *), + t_usize starting_index, t_usize *index) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || index == NULL) + return (ERROR); + idx = starting_index; + while (idx < vec->len) + { + if (fn(&vec->buffer[idx])) + { + *index = idx; + return (NO_ERROR); + } + idx++; + } + return (ERROR); +} + +t_error vec_i64_all(t_vec_i64 *vec, bool (*fn)(const t_i64 *), bool *result) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || result == NULL) + return (ERROR); + idx = 0; + *result = true; + while (*result && idx < vec->len) + { + if (!fn(&vec->buffer[idx])) + *result = false; + idx++; + } + return (ERROR); +} + +t_error vec_i64_any(t_vec_i64 *vec, bool (*fn)(const t_i64 *), bool *result) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || result == NULL) + return (ERROR); + idx = 0; + *result = false; + while (*result && idx < vec->len) + { + if (fn(&vec->buffer[idx])) + *result = true; + idx++; + } + return (ERROR); +} + +void vec_i64_iter(t_vec_i64 *vec, void (*fn)(t_usize index, t_i64 *value, + void *state), void *state) +{ + t_usize idx; + + if (vec == NULL || fn == NULL) + return ; + idx = 0; + while (idx < vec->len) + { + fn(idx, &vec->buffer[idx], state); + idx++; + } +} diff --git a/src/vec/vec_i64_functions3.c b/src/vec/vec_i64_functions3.c new file mode 100644 index 0000000..653eee8 --- /dev/null +++ b/src/vec/vec_i64_functions3.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_copy.h" +#include "me/mem/mem_set_zero.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" +#include + +t_error vec_i64_push_front(t_vec_i64 *vec, t_i64 element) +{ + t_usize i; + + if (vec->len == 0) + return (vec_i64_push(vec, element)); + i = vec->len - 1; + if (vec->capacity < vec->len + 1 && vec_i64_reserve(vec, 3 * vec->len / 2 + + 1)) + return (ERROR); + while (i > 0) + { + vec->buffer[i + 1] = vec->buffer[i]; + i--; + } + vec->buffer[1] = vec->buffer[0]; + vec->buffer[0] = element; + vec->len++; + return (NO_ERROR); +} + +t_error vec_i64_pop_front(t_vec_i64 *vec, t_i64 *value) +{ + t_usize i; + + if (vec->len <= 1) + return (vec_i64_pop(vec, value)); + i = 0; + *value = vec->buffer[0]; + vec->len--; + while (i < vec->len) + { + vec->buffer[i] = vec->buffer[i + 1]; + i++; + } + mem_set_zero(&vec->buffer[i], sizeof(*vec->buffer)); + return (NO_ERROR); +} + +void vec_i64_reverse(t_vec_i64 *vec) +{ + t_i64 temporary; + t_usize i; + + i = 0; + while (i < vec->len / 2) + { + temporary = vec->buffer[vec->len - 1 - i]; + vec->buffer[vec->len - 1 - i] = vec->buffer[i]; + vec->buffer[i] = temporary; + i++; + } +} diff --git a/src/vec/vec_i64_sort.c b/src/vec/vec_i64_sort.c new file mode 100644 index 0000000..d32b66b --- /dev/null +++ b/src/vec/vec_i64_sort.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* best_move.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/29 20:04:33 by maiboyer #+# #+# */ +/* Updated: 2024/01/31 14:25:00 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/types.h" +#include "me/vec/vec_i64.h" + +void vec_i64_sort(t_vec_i64 *v, t_vec_i64_sort_fn is_sorted_fn) +{ + t_usize sorted_part; + t_usize i; + t_i64 tmp; + + if (v == NULL) + return ; + sorted_part = v->len; + while (sorted_part > 0) + { + i = 0; + while (i < sorted_part - 1) + { + if (!is_sorted_fn(&v->buffer[i], &v->buffer[i + 1])) + { + tmp = v->buffer[i]; + v->buffer[i] = v->buffer[i + 1]; + v->buffer[i + 1] = tmp; + } + i++; + } + sorted_part--; + } +} diff --git a/src/vec/vec_u8.c b/src/vec/vec_u8.c new file mode 100644 index 0000000..74ff9a0 --- /dev/null +++ b/src/vec/vec_u8.c @@ -0,0 +1,114 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_u8.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/05 18:46:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 17:54:11 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_copy.h" +#include "me/mem/mem_set_zero.h" +#include "me/types.h" +#include "me/vec/vec_u8.h" +#include + +t_vec_u8 vec_u8_new(t_usize capacity, t_free_u8_item free_function) +{ + t_vec_u8 out; + + out = (t_vec_u8){0}; + out.free_func = free_function; + out.buffer = mem_alloc_array(capacity, sizeof(t_u8)); + if (out.buffer) + out.capacity = capacity; + return (out); +} + +/// Return true in case of an error +t_error vec_u8_push(t_vec_u8 *vec, t_u8 element) +{ + t_u8 *temp_buffer; + size_t new_capacity; + + if (vec == NULL) + return (ERROR); + if (vec->len + 1 > vec->capacity) + { + new_capacity = (vec->capacity * 3) / 2 + 1; + while (vec->len + 1 > new_capacity) + new_capacity = (new_capacity * 3) / 2 + 1; + temp_buffer = mem_alloc_array(new_capacity, sizeof(t_u8)); + if (temp_buffer == NULL) + return (ERROR); + mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_u8)); + free(vec->buffer); + vec->buffer = temp_buffer; + vec->capacity = new_capacity; + } + vec->buffer[vec->len] = element; + vec->len += 1; + return (NO_ERROR); +} + +/// Return true in case of an error +t_error vec_u8_reserve(t_vec_u8 *vec, t_usize wanted_capacity) +{ + t_u8 *temp_buffer; + size_t new_capacity; + + if (vec == NULL) + return (ERROR); + if (wanted_capacity > vec->capacity) + { + new_capacity = (vec->capacity * 3) / 2 + 1; + while (wanted_capacity > new_capacity) + new_capacity = (new_capacity * 3) / 2 + 1; + temp_buffer = mem_alloc_array(new_capacity, sizeof(t_u8)); + if (temp_buffer == NULL) + return (ERROR); + mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_u8)); + free(vec->buffer); + vec->buffer = temp_buffer; + vec->capacity = new_capacity; + } + return (NO_ERROR); +} + +/// Return true if the vector is empty +/// This function is safe to call with value being NULL +t_error vec_u8_pop(t_vec_u8 *vec, t_u8 *value) +{ + t_u8 temp_value; + t_u8 *ptr; + + if (vec == NULL) + return (ERROR); + ptr = value; + if (vec->len == 0) + return (ERROR); + if (value == NULL) + ptr = &temp_value; + vec->len--; + *ptr = vec->buffer[vec->len]; + mem_set_zero(&vec->buffer[vec->len], sizeof(t_u8)); + return (NO_ERROR); +} + +/// This function is safe to call with `free_elem` being NULL +void vec_u8_free(t_vec_u8 vec) +{ + if (vec.free_func) + { + while (vec.len) + { + vec.free_func(vec.buffer[vec.len - 1]); + vec.len--; + } + } + free(vec.buffer); +} diff --git a/src/vec/vec_u8_functions2.c b/src/vec/vec_u8_functions2.c new file mode 100644 index 0000000..3358623 --- /dev/null +++ b/src/vec/vec_u8_functions2.c @@ -0,0 +1,106 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_u8.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_copy.h" +#include "me/mem/mem_set_zero.h" +#include "me/types.h" +#include "me/vec/vec_u8.h" +#include + +t_error vec_u8_find(t_vec_u8 *vec, bool (*fn)(const t_u8 *), t_usize *index) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || index == NULL) + return (ERROR); + idx = 0; + while (idx < vec->len) + { + if (fn(&vec->buffer[idx])) + { + *index = idx; + return (NO_ERROR); + } + idx++; + } + return (ERROR); +} + +t_error vec_u8_find_starting(t_vec_u8 *vec, bool (*fn)(const t_u8 *), + t_usize starting_index, t_usize *index) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || index == NULL) + return (ERROR); + idx = starting_index; + while (idx < vec->len) + { + if (fn(&vec->buffer[idx])) + { + *index = idx; + return (NO_ERROR); + } + idx++; + } + return (ERROR); +} + +t_error vec_u8_all(t_vec_u8 *vec, bool (*fn)(const t_u8 *), bool *result) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || result == NULL) + return (ERROR); + idx = 0; + *result = true; + while (*result && idx < vec->len) + { + if (!fn(&vec->buffer[idx])) + *result = false; + idx++; + } + return (ERROR); +} + +t_error vec_u8_any(t_vec_u8 *vec, bool (*fn)(const t_u8 *), bool *result) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || result == NULL) + return (ERROR); + idx = 0; + *result = false; + while (*result && idx < vec->len) + { + if (fn(&vec->buffer[idx])) + *result = true; + idx++; + } + return (ERROR); +} + +void vec_u8_iter(t_vec_u8 *vec, void (*fn)(t_usize index, t_u8 *value, + void *state), void *state) +{ + t_usize idx; + + if (vec == NULL || fn == NULL) + return ; + idx = 0; + while (idx < vec->len) + { + fn(idx, &vec->buffer[idx], state); + idx++; + } +} diff --git a/src/vec/vec_u8_functions3.c b/src/vec/vec_u8_functions3.c new file mode 100644 index 0000000..3167ba2 --- /dev/null +++ b/src/vec/vec_u8_functions3.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_u8.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_copy.h" +#include "me/mem/mem_set_zero.h" +#include "me/types.h" +#include "me/vec/vec_u8.h" +#include + +t_error vec_u8_push_front(t_vec_u8 *vec, t_u8 element) +{ + t_usize i; + + if (vec->len == 0) + return (vec_u8_push(vec, element)); + i = vec->len - 1; + if (vec->capacity < vec->len + 1 && vec_u8_reserve(vec, 3 * vec->len / 2 + + 1)) + return (ERROR); + while (i > 0) + { + vec->buffer[i + 1] = vec->buffer[i]; + i--; + } + vec->buffer[1] = vec->buffer[0]; + vec->buffer[0] = element; + vec->len++; + return (NO_ERROR); +} + +t_error vec_u8_pop_front(t_vec_u8 *vec, t_u8 *value) +{ + t_usize i; + + if (vec->len <= 1) + return (vec_u8_pop(vec, value)); + i = 0; + *value = vec->buffer[0]; + vec->len--; + while (i < vec->len) + { + vec->buffer[i] = vec->buffer[i + 1]; + i++; + } + mem_set_zero(&vec->buffer[i], sizeof(*vec->buffer)); + return (NO_ERROR); +} + +void vec_u8_reverse(t_vec_u8 *vec) +{ + t_u8 temporary; + t_usize i; + + i = 0; + while (i < vec->len / 2) + { + temporary = vec->buffer[vec->len - 1 - i]; + vec->buffer[vec->len - 1 - i] = vec->buffer[i]; + vec->buffer[i] = temporary; + i++; + } +} From 980dff66be851136958a806116f9f192377abda3 Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Thu, 15 Feb 2024 18:02:41 +0100 Subject: [PATCH 28/29] fixed headers --- include/me/convert/str_to_numbers.h | 2 +- include/me/vec/vec_buf_str.h | 4 ++-- include/me/vec/vec_i64.h | 4 ++-- include/me/vec/vec_i64_bool.h | 4 ++-- include/me/vec/vec_u8.h | 4 ++-- src/convert/str_to_i16.c | 4 ++-- src/convert/str_to_i16_utils.c | 2 +- src/convert/str_to_i32.c | 4 ++-- src/convert/str_to_i32_utils.c | 2 +- src/convert/str_to_i64.c | 2 +- src/convert/str_to_i64_utils.c | 2 +- src/convert/str_to_i8.c | 4 ++-- src/convert/str_to_i8_utils.c | 4 ++-- src/convert/str_to_u16.c | 4 ++-- src/convert/str_to_u16_utils.c | 4 ++-- src/convert/str_to_u32.c | 4 ++-- src/convert/str_to_u32_utils.c | 4 ++-- src/convert/str_to_u64.c | 4 ++-- src/convert/str_to_u64_utils.c | 4 ++-- src/convert/str_to_u8.c | 4 ++-- src/convert/str_to_u8_utils.c | 4 ++-- src/vec/vec_buf_str.c | 4 ++-- src/vec/vec_buf_str_functions2.c | 4 ++-- src/vec/vec_buf_str_functions3.c | 4 ++-- src/vec/vec_i64.c | 4 ++-- src/vec/vec_i64_bool.c | 4 ++-- src/vec/vec_i64_bool_functions2.c | 4 ++-- src/vec/vec_i64_bool_functions3.c | 4 ++-- src/vec/vec_i64_bool_sort.c | 4 ++-- src/vec/vec_i64_functions2.c | 4 ++-- src/vec/vec_i64_functions3.c | 4 ++-- src/vec/vec_i64_sort.c | 4 ++-- src/vec/vec_u8.c | 4 ++-- src/vec/vec_u8_functions2.c | 4 ++-- src/vec/vec_u8_functions3.c | 4 ++-- 35 files changed, 65 insertions(+), 65 deletions(-) diff --git a/include/me/convert/str_to_numbers.h b/include/me/convert/str_to_numbers.h index 9f90ddf..a953680 100644 --- a/include/me/convert/str_to_numbers.h +++ b/include/me/convert/str_to_numbers.h @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/01 21:08:13 by maiboyer #+# #+# */ -/* Updated: 2024/02/02 00:22:50 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 18:02:28 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/include/me/vec/vec_buf_str.h b/include/me/vec/vec_buf_str.h index 083cb24..afd4058 100644 --- a/include/me/vec/vec_buf_str.h +++ b/include/me/vec/vec_buf_str.h @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* vec_buf_str.h :+: :+: :+: */ +/* vec_buf_str.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/04 18:46:53 by maiboyer #+# #+# */ -/* Updated: 2023/12/09 17:53:00 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 18:02:08 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/include/me/vec/vec_i64.h b/include/me/vec/vec_i64.h index 8a8f200..6b4c417 100644 --- a/include/me/vec/vec_i64.h +++ b/include/me/vec/vec_i64.h @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* vec_i64.h :+: :+: :+: */ +/* vec_i64.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/04 18:46:53 by maiboyer #+# #+# */ -/* Updated: 2023/12/09 17:53:00 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 18:02:14 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/include/me/vec/vec_i64_bool.h b/include/me/vec/vec_i64_bool.h index 42f456a..e64bf16 100644 --- a/include/me/vec/vec_i64_bool.h +++ b/include/me/vec/vec_i64_bool.h @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* vec_i64_bool.h :+: :+: :+: */ +/* vec_i64_bool.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/04 18:46:53 by maiboyer #+# #+# */ -/* Updated: 2023/12/09 17:53:00 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 18:02:10 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/include/me/vec/vec_u8.h b/include/me/vec/vec_u8.h index fe8ec03..e0a8c23 100644 --- a/include/me/vec/vec_u8.h +++ b/include/me/vec/vec_u8.h @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* vec_u8.h :+: :+: :+: */ +/* vec_u8.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/04 18:46:53 by maiboyer #+# #+# */ -/* Updated: 2023/12/09 17:53:00 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 18:02:19 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/convert/str_to_i16.c b/src/convert/str_to_i16.c index b1d2bc8..c120781 100644 --- a/src/convert/str_to_i16.c +++ b/src/convert/str_to_i16.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* str_to_i64.c :+: :+: :+: */ +/* str_to_i16.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ -/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 18:01:39 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/convert/str_to_i16_utils.c b/src/convert/str_to_i16_utils.c index 4ecbbe2..915d55a 100644 --- a/src/convert/str_to_i16_utils.c +++ b/src/convert/str_to_i16_utils.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ -/* Updated: 2024/02/08 18:55:47 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 18:01:36 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/convert/str_to_i32.c b/src/convert/str_to_i32.c index 4956029..34cf1d9 100644 --- a/src/convert/str_to_i32.c +++ b/src/convert/str_to_i32.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* str_to_i64.c :+: :+: :+: */ +/* str_to_i32.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ -/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 18:01:43 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/convert/str_to_i32_utils.c b/src/convert/str_to_i32_utils.c index 5b6f5fd..75221dc 100644 --- a/src/convert/str_to_i32_utils.c +++ b/src/convert/str_to_i32_utils.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ -/* Updated: 2024/02/08 18:56:06 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 18:01:44 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/convert/str_to_i64.c b/src/convert/str_to_i64.c index 56a48cc..6a2a51e 100644 --- a/src/convert/str_to_i64.c +++ b/src/convert/str_to_i64.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ -/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 18:01:46 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/convert/str_to_i64_utils.c b/src/convert/str_to_i64_utils.c index 1dd268d..4c0f81d 100644 --- a/src/convert/str_to_i64_utils.c +++ b/src/convert/str_to_i64_utils.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ -/* Updated: 2024/02/08 18:55:37 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 18:01:45 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/convert/str_to_i8.c b/src/convert/str_to_i8.c index 80f91a0..d0bee76 100644 --- a/src/convert/str_to_i8.c +++ b/src/convert/str_to_i8.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* str_to_i64.c :+: :+: :+: */ +/* str_to_i8.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ -/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 18:01:35 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/convert/str_to_i8_utils.c b/src/convert/str_to_i8_utils.c index 2073dd6..210a975 100644 --- a/src/convert/str_to_i8_utils.c +++ b/src/convert/str_to_i8_utils.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* str_to_i64.c :+: :+: :+: */ +/* str_to_i8_utils.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ -/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 18:01:34 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/convert/str_to_u16.c b/src/convert/str_to_u16.c index 7c5bbaa..af63a8e 100644 --- a/src/convert/str_to_u16.c +++ b/src/convert/str_to_u16.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* str_to_i64.c :+: :+: :+: */ +/* str_to_u16.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ -/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 18:01:49 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/convert/str_to_u16_utils.c b/src/convert/str_to_u16_utils.c index 85fd7b2..4803db1 100644 --- a/src/convert/str_to_u16_utils.c +++ b/src/convert/str_to_u16_utils.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* str_to_i64.c :+: :+: :+: */ +/* str_to_u16_utils.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ -/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 18:01:48 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/convert/str_to_u32.c b/src/convert/str_to_u32.c index 1b625d1..f68794d 100644 --- a/src/convert/str_to_u32.c +++ b/src/convert/str_to_u32.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* str_to_i64.c :+: :+: :+: */ +/* str_to_u32.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ -/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 18:01:50 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/convert/str_to_u32_utils.c b/src/convert/str_to_u32_utils.c index 3a929d5..33001bf 100644 --- a/src/convert/str_to_u32_utils.c +++ b/src/convert/str_to_u32_utils.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* str_to_i64.c :+: :+: :+: */ +/* str_to_u32_utils.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ -/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 18:01:49 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/convert/str_to_u64.c b/src/convert/str_to_u64.c index b744be8..fcd95c4 100644 --- a/src/convert/str_to_u64.c +++ b/src/convert/str_to_u64.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* str_to_i64.c :+: :+: :+: */ +/* str_to_u64.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ -/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 18:01:52 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/convert/str_to_u64_utils.c b/src/convert/str_to_u64_utils.c index f2bb77f..f04b99a 100644 --- a/src/convert/str_to_u64_utils.c +++ b/src/convert/str_to_u64_utils.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* str_to_i64.c :+: :+: :+: */ +/* str_to_u64_utils.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ -/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 18:01:51 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/convert/str_to_u8.c b/src/convert/str_to_u8.c index 8fac9fa..57413fc 100644 --- a/src/convert/str_to_u8.c +++ b/src/convert/str_to_u8.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* str_to_i64.c :+: :+: :+: */ +/* str_to_u8.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ -/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 18:01:47 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/convert/str_to_u8_utils.c b/src/convert/str_to_u8_utils.c index 14a4a04..f66e0cd 100644 --- a/src/convert/str_to_u8_utils.c +++ b/src/convert/str_to_u8_utils.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* str_to_i64.c :+: :+: :+: */ +/* str_to_u8_utils.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ -/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 18:01:47 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/vec/vec_buf_str.c b/src/vec/vec_buf_str.c index 281433a..0c423f0 100644 --- a/src/vec/vec_buf_str.c +++ b/src/vec/vec_buf_str.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* vec_buf_str.c :+: :+: :+: */ +/* vec_buf_str.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/05 18:46:28 by maiboyer #+# #+# */ -/* Updated: 2023/12/09 17:54:11 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 18:00:33 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/vec/vec_buf_str_functions2.c b/src/vec/vec_buf_str_functions2.c index 6bee346..b43d83b 100644 --- a/src/vec/vec_buf_str_functions2.c +++ b/src/vec/vec_buf_str_functions2.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* vec_buf_str.c :+: :+: :+: */ +/* vec_buf_str_functions2.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */ -/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 18:00:22 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/vec/vec_buf_str_functions3.c b/src/vec/vec_buf_str_functions3.c index 2b51c44..c4b7d87 100644 --- a/src/vec/vec_buf_str_functions3.c +++ b/src/vec/vec_buf_str_functions3.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* vec_buf_str.c :+: :+: :+: */ +/* vec_buf_str_functions3.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */ -/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 18:00:26 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/vec/vec_i64.c b/src/vec/vec_i64.c index 1a24dec..ee0272d 100644 --- a/src/vec/vec_i64.c +++ b/src/vec/vec_i64.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* vec_i64.c :+: :+: :+: */ +/* vec_i64.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/05 18:46:28 by maiboyer #+# #+# */ -/* Updated: 2023/12/09 17:54:11 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 18:01:08 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/vec/vec_i64_bool.c b/src/vec/vec_i64_bool.c index af10724..4f405f2 100644 --- a/src/vec/vec_i64_bool.c +++ b/src/vec/vec_i64_bool.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* vec_i64_bool.c :+: :+: :+: */ +/* vec_i64_bool.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/05 18:46:28 by maiboyer #+# #+# */ -/* Updated: 2023/12/09 17:54:11 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 18:00:52 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/vec/vec_i64_bool_functions2.c b/src/vec/vec_i64_bool_functions2.c index 01927c2..b5d59f6 100644 --- a/src/vec/vec_i64_bool_functions2.c +++ b/src/vec/vec_i64_bool_functions2.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* vec_i64_bool.c :+: :+: :+: */ +/* vec_i64_bool_functions2.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */ -/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 18:00:38 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/vec/vec_i64_bool_functions3.c b/src/vec/vec_i64_bool_functions3.c index ee82947..32f2007 100644 --- a/src/vec/vec_i64_bool_functions3.c +++ b/src/vec/vec_i64_bool_functions3.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* vec_i64_bool.c :+: :+: :+: */ +/* vec_i64_bool_functions3.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */ -/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 18:00:41 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/vec/vec_i64_bool_sort.c b/src/vec/vec_i64_bool_sort.c index edbdfc1..e20a733 100644 --- a/src/vec/vec_i64_bool_sort.c +++ b/src/vec/vec_i64_bool_sort.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* best_move.c :+: :+: :+: */ +/* vec_i64_bool_sort.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/29 20:04:33 by maiboyer #+# #+# */ -/* Updated: 2024/01/31 14:25:00 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 18:00:47 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/vec/vec_i64_functions2.c b/src/vec/vec_i64_functions2.c index d7180bc..db278db 100644 --- a/src/vec/vec_i64_functions2.c +++ b/src/vec/vec_i64_functions2.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* vec_i64.c :+: :+: :+: */ +/* vec_i64_functions2.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */ -/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 18:00:57 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/vec/vec_i64_functions3.c b/src/vec/vec_i64_functions3.c index 653eee8..3bcb369 100644 --- a/src/vec/vec_i64_functions3.c +++ b/src/vec/vec_i64_functions3.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* vec_i64.c :+: :+: :+: */ +/* vec_i64_functions3.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */ -/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 18:01:02 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/vec/vec_i64_sort.c b/src/vec/vec_i64_sort.c index d32b66b..43c86df 100644 --- a/src/vec/vec_i64_sort.c +++ b/src/vec/vec_i64_sort.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* best_move.c :+: :+: :+: */ +/* vec_i64_sort.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/29 20:04:33 by maiboyer #+# #+# */ -/* Updated: 2024/01/31 14:25:00 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 18:01:04 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/vec/vec_u8.c b/src/vec/vec_u8.c index 74ff9a0..0ca886d 100644 --- a/src/vec/vec_u8.c +++ b/src/vec/vec_u8.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* vec_u8.c :+: :+: :+: */ +/* vec_u8.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/05 18:46:28 by maiboyer #+# #+# */ -/* Updated: 2023/12/09 17:54:11 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 18:01:21 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/vec/vec_u8_functions2.c b/src/vec/vec_u8_functions2.c index 3358623..bc4b22b 100644 --- a/src/vec/vec_u8_functions2.c +++ b/src/vec/vec_u8_functions2.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* vec_u8.c :+: :+: :+: */ +/* vec_u8_functions2.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */ -/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 18:01:12 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/vec/vec_u8_functions3.c b/src/vec/vec_u8_functions3.c index 3167ba2..b71f31b 100644 --- a/src/vec/vec_u8_functions3.c +++ b/src/vec/vec_u8_functions3.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* vec_u8.c :+: :+: :+: */ +/* vec_u8_functions3.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */ -/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 18:01:17 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ From aa9db0ed4aa718e0c10ced41d557af01cb137aa8 Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Thu, 15 Feb 2024 19:10:12 +0100 Subject: [PATCH 29/29] fixed bonus issue --- src/app/main_bonus.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/app/main_bonus.c b/src/app/main_bonus.c index 4de3a02..5d112a9 100644 --- a/src/app/main_bonus.c +++ b/src/app/main_bonus.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/08 18:59:33 by maiboyer #+# #+# */ -/* Updated: 2024/02/15 17:47:33 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 19:09:47 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -81,9 +81,10 @@ int main_checker(t_i32 argc, t_str argv[]) bool err; (void)(argc--, argv++); - s.buf = ""; err = false; state = parses_arguments(argc, argv); + if (state.stack_a.len == 0) + return (free_state(state), 0); while (!err) { s = get_next_line(0, &err); @@ -94,7 +95,7 @@ int main_checker(t_i32 argc, t_str argv[]) if (s.buf[0] == '\0') continue ; if (handle_operation(s, &state)) - return (me_printf("KO\n"), str_free(s), free_state(state), 1); + return (me_printf("Error\n"), str_free(s), free_state(state), 1); if (s.buf != NULL) str_free(s); }