diff --git a/Makefile b/Makefile index db9cec5..ccb02b8 100644 --- a/Makefile +++ b/Makefile @@ -32,7 +32,7 @@ RESET = "\e[0m" NAME = containers -CC = clang++ +CC = g++ EXT = cpp CFLAGS = -Wall -Wextra -Werror $(INCLUDES) @@ -80,8 +80,8 @@ endif all: $(NAME) stl: CFLAGS += -D STL -stl: all -ft: all +stl: re +ft: re $(D_OBJS)/%.o: %.$(EXT) | $(D_OBJS) @echo $(CYAN)"compilation (objects.o) :"$(RESET) diff --git a/headers/is_integral.hpp b/headers/is_integral.hpp index e6cb782..8a70806 100644 --- a/headers/is_integral.hpp +++ b/headers/is_integral.hpp @@ -3,18 +3,6 @@ namespace ft { -//struct true_type { -// typedef bool value_type; -// typedef true_type type; -// static const bool value = true; -//}; -// -//struct false_type { -// typedef T value_type; -// typedef false_type type; -// static const bool value = false; -//}; - template struct is_integral { typedef char yes[1]; @@ -39,51 +27,6 @@ template struct is_integral static yes& test(unsigned long int); static yes& test(unsigned long long int); -/* - static yes& test(const bool); - static yes& test(const char); - static yes& test(const wchar_t); - static yes& test(const signed char); - static yes& test(const short int); - static yes& test(const int); - static yes& test(const long int); - static yes& test(const long long int); - static yes& test(const unsigned char); - static yes& test(const unsigned short int); - static yes& test(const unsigned int); - static yes& test(const unsigned long int); - static yes& test(const unsigned long long int); - - static yes& test(volatile bool); - static yes& test(volatile char); - static yes& test(volatile wchar_t); - static yes& test(volatile signed char); - static yes& test(volatile short int); - static yes& test(volatile int); - static yes& test(volatile long int); - static yes& test(volatile long long int); - static yes& test(volatile unsigned char); - static yes& test(volatile unsigned short int); - static yes& test(volatile unsigned int); - static yes& test(volatile unsigned long int); - static yes& test(volatile unsigned long long int); - - static yes& test(const volatile bool); - static yes& test(const volatile char); - static yes& test(const volatile wchar_t); - static yes& test(const volatile signed char); - static yes& test(const volatile short int); - static yes& test(const volatile int); - static yes& test(const volatile long int); - static yes& test(const volatile long long int); - static yes& test(const volatile unsigned char); - static yes& test(const volatile unsigned short int); - static yes& test(const volatile unsigned int); - static yes& test(const volatile unsigned long int); - static yes& test(const volatile unsigned long long int); -*/ - - // non-template function with direct matching are always considered first // then the function template with direct matching are considered // https://stackoverflow.com/questions/12877546/how-do-i-avoid-implicit-conversions-on @@ -95,19 +38,6 @@ template struct is_integral } // namespace ft // "template <>" introduce a total specialization of a template : -// -// template -// class A -// { -// // body for the general case -// }; -// -// template <> -// class A -// { -// // body that only applies for T = bool -// }; -// // https://stackoverflow.com/questions/6288812/what-is-the-meaning-of-template-with-empty-angle-brackets-in-c // SFINAE : https://jguegant.github.io/blogs/tech/sfinae-introduction.html diff --git a/headers/vector.hpp b/headers/vector.hpp index 53ed939..e4b3540 100644 --- a/headers/vector.hpp +++ b/headers/vector.hpp @@ -99,17 +99,11 @@ public: template typename enable_if< !is_integral::value,void >::type assign(InputIterator first, InputIterator last); - // template - // void assign(InputIterator first, InputIterator last - // typename enable_if< !is_integral::value, bool >::type == true); -// template -// typename enable_if< is_integral::value,void >::type -// assign(size_type n, const value_type& val); void assign(size_type n, const value_type& val); // push_back --------------------------------- void push_back(const value_type & val); // pop_back ---------------------------------- - //void pop_back(); + void pop_back(); // insert ------------------------------------ //iterator insert(iterator position, const value_type& val); //void insert(iterator position, size_type n, const value_type& val); diff --git a/templates/vector.tpp b/templates/vector.tpp index fe48257..69c756b 100644 --- a/templates/vector.tpp +++ b/templates/vector.tpp @@ -155,7 +155,10 @@ VT_TPL void VT:: ******************/ // operator[] -------------------------------- VT_TPL typename VT::reference VT:: - operator[](size_type n) { return _mem_ptr[n]; } + operator[](size_type n) +{ + return _mem_ptr[n]; +} //const_reference operator[](size_type n) const; // at ---------------------------------------- //reference at(size_type n); @@ -175,35 +178,25 @@ VT_TPL template typename enable_if< !is_integral::value,void >::type VT:: assign( InputIterator first, InputIterator last) { -// TMP -std::cout << B_RED "inside assign(first, last) " RESET; -// TMP END InputIterator tmp = first; - int range; + int range = 0; clear(); - for (range = 1; tmp != last; range++) - tmp++; + while (tmp++ != last) + range++; _size += range; if (_size >= _capacity) reserve(_size); while (first != last) { - _allocator.construct(&_mem_ptr[_size], first); + _allocator.construct(&_mem_ptr[_size], *first); first++; } } - -// VT_TPL typename enable_if< isinteger::value,std::string >::type VT:: -//VT_TPL template -//typename enable_if< is_integral::value,void >::type VT:: VT_TPL void VT:: assign( size_type n, const T & val ) { -// TMP -std::cout << B_RED "inside assign(n, val) " RESET; -// TMP END if (n > _allocator.max_size()) throw std::length_error("assign: n > max_size"); @@ -237,7 +230,12 @@ VT_TPL void VT:: _size++; } // pop_back ---------------------------------- -//void pop_back(); +VT_TPL void VT:: + pop_back() +{ + _allocator.destroy(end() - 1); + _size--; +} // insert ------------------------------------ //iterator insert(iterator position, const value_type& val); //void insert(iterator position, size_type n, const value_type& val); diff --git a/tests/main.cpp b/tests/main.cpp index 4b5d322..bed6c7a 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -298,15 +298,16 @@ int main() { TEST(vector::assign) { + // title TITLE(cplusplus.com reference :) - std::vector first; - std::vector second; - std::vector third; + ft::vector first; + ft::vector second; + ft::vector third; first.assign (7,100); // 7 ints with a value of 100 - std::vector::iterator it; + ft::vector::iterator it; it=first.begin()+1; second.assign (it,first.end()-1); // the 5 central values of first @@ -318,6 +319,8 @@ int main() { std::cout << "Size of second: " << int (second.size()) << '\n'; std::cout << "Size of third: " << int (third.size()) << '\n'; + + // title TITLE(capacity tests of assignation :) ft::vector myvector; @@ -339,17 +342,50 @@ int main() { std::cout << "[" << i << "] " << myvector[i] << " - "; std::cout << "\nsize : " << size << " , capacity : " << myvector.capacity() << "\n"; - // std::cout << "\nassign 7268\n"; - // myvector.assign(7268, 12); - // size = myvector.size(); - // for (int i = 0; i < size; i++) - // std::cout << "[" << i << "] " << myvector[i] << " - "; - // std::cout << "\nsize : " << size << " , capacity : " << myvector.capacity() << "\n"; + std::cout << "\nassign 7268\n"; + myvector.assign(7268, 12); + size = myvector.size(); + for (int i = 0; i < size; i++) + std::cout << "[" << i << "] " << myvector[i] << " - "; + std::cout << "\nsize : " << size << " , capacity : " << myvector.capacity() << "\n"; + + + // title + TITLE(tests of iterators :) + + ft::vector int_vector_1; + ft::vector int_vector_2; + ft::vector int_vector_3; + ft::vector it_vector; + + std::cout << "\nassign 1\n"; + int_vector_1.assign(1, 12); + it_vector.assign(int_vector_1.begin(), int_vector_1.end()); + size = it_vector.size(); + for (int i = 0; i < size; i++) + std::cout << "[" << i << "] " << it_vector[i] << " - "; + std::cout << "\nsize : " << size << " , capacity : " << it_vector.capacity() << "\n"; + + std::cout << "\nassign 1\n"; + int_vector_2.assign(1, 6); + it_vector.assign(int_vector_2.begin(), int_vector_2.end() - 1); + size = it_vector.size(); + for (int i = 0; i < size; i++) + std::cout << "[" << i << "] " << it_vector[i] << " - "; + std::cout << "\nsize : " << size << " , capacity : " << it_vector.capacity() << "\n"; + + std::cout << "\nassign 1\n"; + int_vector_3.assign(266, 1); + it_vector.assign(int_vector_3.begin() + 13, int_vector_3.end() - 172); + size = it_vector.size(); + for (int i = 0; i < size; i++) + std::cout << "[" << i << "] " << it_vector[i] << " - "; + std::cout << "\nsize : " << size << " , capacity : " << it_vector.capacity() << "\n"; + } TESTEND -/* TEST(vector::push_back) { ft::vector myvector; @@ -369,17 +405,15 @@ int main() { std::cout << " -> size : " << myvector.size() << " , capacity :" << myvector.capacity() << "\n"; // second test - // for (int i = 0; i < 72363; i++) - // { - // myvector.push_back(9); - // std::cout << "[" << i - // << ":" << myvector.capacity() << "] "; - // } - // std::cout << " -> size : " << myvector.size() << " , capacity :" << myvector.capacity() << "\n"; + for (int i = 0; i < 72363; i++) + { + myvector.push_back(9); + std::cout << "[" << i + << ":" << myvector.capacity() << "] "; + } + std::cout << " -> size : " << myvector.size() << " , capacity :" << myvector.capacity() << "\n"; } TESTEND -*/ - /* TEST(vector::pop_back) diff --git a/tests/test.sh b/tests/test.sh new file mode 100644 index 0000000..f180752 --- /dev/null +++ b/tests/test.sh @@ -0,0 +1,10 @@ +#!/bin/bash +# cd $(dirname $0) + +make stl > /dev/null +./containers > tests/output_stl.log + +make ft > /dev/null +./containers > tests/output_ft.log + +diff --context=0 --color=always tests/output_stl.log tests/output_ft.log