diff --git a/libft/Makefile b/libft/Makefile index 3e8c47af..cf9e06f0 100644 --- a/libft/Makefile +++ b/libft/Makefile @@ -23,6 +23,7 @@ INC_DIR = includes/ OBJ_DIR = objs/ SRC_BASE = \ +rs/rs.c\ btree/btree_apply_by_level.c\ btree/btree_apply_infix.c\ btree/btree_apply_prefix.c\ @@ -138,6 +139,7 @@ printing/ft_putchar.c\ printing/ft_putendl.c\ printing/ft_putnbr.c\ printing/ft_putstr.c\ +printing/hexdump.c\ sstr/ft_sstradd.c\ sstr/ft_sstrcat.c\ sstr/ft_sstrdel.c\ diff --git a/libft/includes/libft.h b/libft/includes/libft.h index 7e2bd5ed..a3008315 100644 --- a/libft/includes/libft.h +++ b/libft/includes/libft.h @@ -13,6 +13,8 @@ #ifndef LIBFT_H # define LIBFT_H +# define FT_TRY(a,b) ((a) ? (a) : (b)) + # include # include # include @@ -24,6 +26,7 @@ # include "error.h" # include "color.h" # include "cliopts.h" +# include "rs.h" # include "lst.h" # include "dlst.h" @@ -86,6 +89,8 @@ void ft_putendl_fd(char const *s, int fd); void ft_putnbr_fd(long n, int fd); void ft_putnbr_hex_fd(long n, int fd); +void hexdump(void *pAddressIn, long lSize); + void *ft_realloc(void *data, int size); #endif diff --git a/libft/includes/rs.h b/libft/includes/rs.h new file mode 100644 index 00000000..c5b716e4 --- /dev/null +++ b/libft/includes/rs.h @@ -0,0 +1,21 @@ +#ifndef LIBFT_RS_H +# define LIBFT_RS_H + +#include +#include + +extern struct s_stats { + int count; + double min; + double max; + double avg; + double m; + double stdev; + double var; +} g_rs; + +void rs_clear(); +void rs_push(double n); +void rs_calcmore(); + +#endif diff --git a/libft/srcs/printing/hexdump.c b/libft/srcs/printing/hexdump.c new file mode 100644 index 00000000..924e2004 --- /dev/null +++ b/libft/srcs/printing/hexdump.c @@ -0,0 +1,57 @@ +#include +#include + +#include "libft.h" + +void hexdump(void *pAddressIn, long lSize) +{ + char szBuf[100]; + long lIndent = 1; + long lOutLen, lIndex, lIndex2, lOutLen2; + long lRelPos; + struct { char *pData; unsigned long lSize; } buf; + unsigned char *pTmp,ucTmp; + unsigned char *pAddress = (unsigned char *)pAddressIn; + + buf.pData = (char *)pAddress; + buf.lSize = lSize; + + while (buf.lSize > 0) + { + pTmp = (unsigned char *)buf.pData; + lOutLen = (int)buf.lSize; + if (lOutLen > 16) + lOutLen = 16; + + // create a 64-character formatted output line: + sprintf(szBuf, " > " + " " + " %08lX", pTmp-pAddress); + lOutLen2 = lOutLen; + + for(lIndex = 1+lIndent, lIndex2 = 53-15+lIndent, lRelPos = 0; + lOutLen2; + lOutLen2--, lIndex += 2, lIndex2++ + ) + { + ucTmp = *pTmp++; + + sprintf(szBuf + lIndex, "%02X ", (unsigned short)ucTmp); + if(!isprint(ucTmp)) ucTmp = '.'; // nonprintable char + szBuf[lIndex2] = ucTmp; + + if (!(++lRelPos & 3)) // extra blank after 4 bytes + { lIndex++; szBuf[lIndex+2] = ' '; } + } + + if (!(lRelPos & 3)) lIndex--; + + szBuf[lIndex ] = '<'; + szBuf[lIndex+1] = ' '; + + printf("%s\n", szBuf); + + buf.pData += lOutLen; + buf.lSize -= lOutLen; + } +} diff --git a/libft/srcs/rs/rs.c b/libft/srcs/rs/rs.c new file mode 100644 index 00000000..3f738785 --- /dev/null +++ b/libft/srcs/rs/rs.c @@ -0,0 +1,56 @@ +#include "libft.h" +#include + +struct s_stats g_rs = {0, 0, 0, 0, 0, 0, 0}; + +void rs_clear() +{ + g_rs.count = 0; + g_rs.min = DBL_MAX; + g_rs.max = -DBL_MAX; +} + +void rs_push(double n) +{ + double delta; + + g_rs.count++; + n < g_rs.min ? g_rs.min = n : (0); + n > g_rs.max ? g_rs.max = n : (0); + if (g_rs.count == 1) + { + g_rs.avg = n; + g_rs.m = 0; + } + else + { + delta = n - g_rs.avg; + g_rs.avg += delta / g_rs.count; + g_rs.m += delta * (n - g_rs.avg); + } +} + +void rs_calcmore() +{ + void *libm; + double (*sqrt)(double); + + if (g_rs.count == 0) + { + g_rs.min = 0; + g_rs.max = 0; + } + if (g_rs.count < 2) + { + g_rs.var = 0; + g_rs.stdev = 0; + return ; + } + g_rs.var = g_rs.m / (g_rs.count - 1); + if ((libm = dlopen("libm.dylib", 0)) == NULL) + printf("%s\n", dlerror()); + else if ((sqrt = dlsym(libm, "sqrt")) == NULL) + printf("%s\n", dlerror()); + else + g_rs.stdev = sqrt(g_rs.var); +}