From 99f67b808c533c2668bfddef2f7fe52d31f7558f Mon Sep 17 00:00:00 2001 From: hugogogo Date: Wed, 1 Jun 2022 15:41:14 +0200 Subject: [PATCH] first implementation, badly, of allocator --- Makefile | 6 ++- headers/ftvector.hpp | 34 +++++++++++++++++ headers/tests.h | 1 + main.cpp | 16 ++++++++ srcs/ftvector.cpp | 90 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 145 insertions(+), 2 deletions(-) create mode 100644 headers/ftvector.hpp create mode 100644 srcs/ftvector.cpp diff --git a/Makefile b/Makefile index 4a2a272..5adac0e 100644 --- a/Makefile +++ b/Makefile @@ -19,11 +19,13 @@ LIBS = INCLUDES = -I$(D_HEADERS) D_SRCS = srcs -SRCS = main.cpp +SRCS = main.cpp \ + ftvector.cpp D_HEADERS = headers HEADERS = colors.h \ - tests.h + tests.h \ + ftvector.hpp D_OBJS = builds OBJS = $(SRCS:%.$(EXT)=$(D_OBJS)/%.o) diff --git a/headers/ftvector.hpp b/headers/ftvector.hpp new file mode 100644 index 0000000..155ad15 --- /dev/null +++ b/headers/ftvector.hpp @@ -0,0 +1,34 @@ +#ifndef FTVECTOR_HPP +# define FTVECTOR_HPP + +# include "colors.h" +# include +# include +# include // std::allocator + +class ftvector { + +public: + + ftvector(); + ftvector( ftvector const & src ); + ~ftvector(); + ftvector & operator=( ftvector const & rhs ); + + unsigned int size() const; + void push_back(const int & element); + +private: + + unsigned int _size; + int * _mem_ptr; + std::allocator _allocator; + +// static std::string const ftvector::_bar; + +}; + +//std::ostream & operator<<(std::ostream & o, ftvector const & rhs); + +#endif + diff --git a/headers/tests.h b/headers/tests.h index 12a747e..c4cd145 100644 --- a/headers/tests.h +++ b/headers/tests.h @@ -2,6 +2,7 @@ # define TESTS_H #include +#include # define TEST(s) \ {\ diff --git a/main.cpp b/main.cpp index 656e66a..8253b47 100644 --- a/main.cpp +++ b/main.cpp @@ -4,9 +4,24 @@ #include "tests.h" #include +#include "ftvector.hpp" int main() { + TEST(vector::vector (constructor)) + { + ftvector myvector; + int myint[] = {12434, -2432, 12, 5345, 23, 0, -4, 387, 8432, -934723, 1}; + int size = sizeof(myint) / sizeof(myint[0]); + + for (int i = 0; i < size; i++) + myvector.push_back (myint[i]); + + std::cout << "myvector stores " << int(myvector.size()) << " numbers.\n"; + } + TESTEND + +/* TEST(vector::vector (constructor)) { // constructors used in the same order as described above: @@ -445,6 +460,7 @@ int main() { myvector.get_allocator().deallocate(p,5); } TESTEND +*/ // execute tests and print them : diff --git a/srcs/ftvector.cpp b/srcs/ftvector.cpp new file mode 100644 index 0000000..a566614 --- /dev/null +++ b/srcs/ftvector.cpp @@ -0,0 +1,90 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ftvector.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: simplonco +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/01 15:39:26 by simplonco #+# #+# */ +/* Updated: 2022/06/01 15:39:57 by simplonco ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ftvector.hpp" + +#define COPLIEN_COLOR B_CYAN + +/********************************************* + * CONSTRUCTORS + *********************************************/ + +ftvector::ftvector() : +_size(0) { +// std::cout << COPLIEN_COLOR "ftvector constructor" RESET "\n"; + _allocator = std::allocator(); + return; +} + +ftvector::ftvector( ftvector const & src ) { +// std::cout << COPLIEN_COLOR "ftvector copy constructor" RESET "\n"; + *this = src; + return; +} + +/********************************************* + * DESTRUCTORS + *********************************************/ + +ftvector::~ftvector() { +// std::cout << COPLIEN_COLOR "ftvector destructor" RESET "\n"; + return; +} + +/********************************************* + * OPERATORS + *********************************************/ + +ftvector & ftvector::operator=( ftvector const & rhs ) { +// Base::operator=(rhs); + if ( this != &rhs ) + { + _size = rhs.size(); + } + return *this; +} + +//std::ostream & operator<<(std::ostream & o, ftvector const & rhs) +//{ +// o << rhs.getFoo(); +// return (o); +//} + +/********************************************* + * ACCESSORS + *********************************************/ + +unsigned int ftvector::size() const {return _size;} + +/********************************************* + * PUBLIC MEMBER FUNCTIONS + *********************************************/ + +void ftvector::push_back(const int & element) { + _mem_ptr = _allocator.allocate(1); + _allocator.construct(_mem_ptr, element); + _size++; +} + +/********************************************* + * NESTED CLASS + *********************************************/ + +//void ftvector::Class::function() {} + +/********************************************* + * STATICS + *********************************************/ + +//std::string const ftvector::_bar = "bar"; + +