Browse Source

save 22-12-6

master
nicolas-arnaud 2 years ago
parent
commit
1e583e7c8a
  1. BIN
      .cache/clangd/index/ft_container.hpp.AE6A49ED28E963FF.idx
  2. BIN
      .cache/clangd/index/iterator.hpp.B6BE3296FFA2AF9F.idx
  3. BIN
      .cache/clangd/index/tester.cpp.133FD4700F7AEE0A.idx
  4. BIN
      .cache/clangd/index/vector.hpp.8C3201A34AC2A736.idx
  5. 14
      .gitignore
  6. 3
      Makefile
  7. 20
      includes/ft_container.hpp
  8. 6
      includes/iterator.hpp
  9. 40
      includes/utils.hpp
  10. 103
      includes/vector.hpp
  11. 80
      tester.cpp

BIN
.cache/clangd/index/ft_container.hpp.AE6A49ED28E963FF.idx

Binary file not shown.

BIN
.cache/clangd/index/iterator.hpp.B6BE3296FFA2AF9F.idx

Binary file not shown.

BIN
.cache/clangd/index/tester.cpp.133FD4700F7AEE0A.idx

Binary file not shown.

BIN
.cache/clangd/index/vector.hpp.8C3201A34AC2A736.idx

Binary file not shown.

14
.gitignore

@ -0,0 +1,14 @@
*
!README.md
!LICENCE
!.gitignore
!.clang-format
!compile_commands.json
!Session.vim
!includes/
!includes/*.hpp
!Makefile
!tester.cpp

3
Makefile

@ -1,3 +1,4 @@
CXX = c++
CXXFLAGS= -std=c++98 -Werror -Wextra -Wall -Iincludes CXXFLAGS= -std=c++98 -Werror -Wextra -Wall -Iincludes
all: tester all: tester
@ -11,6 +12,6 @@ clean:
fclean: clean fclean: clean
rm -rf tester rm -rf tester
re: fclean re re: fclean all
.PHONY: all clean fclean re .PHONY: all clean fclean re

20
includes/ft_container.hpp

@ -1,21 +1,3 @@
#pragma once #pragma once
#include <iostream>
#include "vector.hpp" #include "vector.hpp"
#include "iterator.hpp"
#include "iostream"
template <T> is_integer(T input) {}
template<bool B, class T = void> struct enable_if {};
template<class T> struct enable_if<true, T> { typedef T type; };
template <class T>
void debug_vector(ft::vector<T> vec) {
ft::ra_iterator<T> i = vec.begin();
while (i < vec.end()) {
std::cout << *i << " ";
}
std::cout << "\n";
}

6
includes/iterator.hpp

@ -53,7 +53,7 @@ public:
return (*this); return (*this);
} }
T base(void) const { return current; } pointer base(void) const { return current; }
reference operator*(void) const { return *current; } reference operator*(void) const { return *current; }
pointer operator->(void) const { return current; } pointer operator->(void) const { return current; }
reference operator[](const difference_type n) const { return *(current + n); } reference operator[](const difference_type n) const { return *(current + n); }
@ -189,8 +189,8 @@ public:
}; };
template < class It > std::size_t distance(It first, It last) { template < class It > std::size_t distance(It first, It last) {
size_t ret = 0; size_t ret = 1;
while (first != last && ret++) while (first != last && ++ret)
first++; first++;
return ret; return ret;
} }

40
includes/utils.hpp

@ -0,0 +1,40 @@
#pragma once
namespace ft {
// is_integral:
template < class T, T val > struct integral_constant {
typedef integral_constant< T, val > type;
typedef T value_type;
static const T value = val;
operator T() const;
};
typedef integral_constant< bool, true > true_type;
typedef integral_constant< bool, false > false_type;
template < class T > struct is_integral : public false_type {};
template < class T > struct is_integral< const T > : public false_type {};
template <> struct is_integral< bool > : public true_type {};
template <> struct is_integral< double > : public true_type {};
template <> struct is_integral< char > : public true_type {};
template <> struct is_integral< short > : public true_type {};
template <> struct is_integral< int > : public true_type {};
template <> struct is_integral< long > : public true_type {};
template <> struct is_integral< long long > : public true_type {};
template <> struct is_integral< unsigned char > : public true_type {};
template <> struct is_integral< unsigned short > : public true_type {};
template <> struct is_integral< unsigned int > : public true_type {};
template <> struct is_integral< unsigned long > : public true_type {};
template <> struct is_integral< unsigned long long > : public true_type {};
// enable_if:
template < bool B, class T = void > struct enable_if {};
template < class T > struct enable_if< true, T > {
typedef T type;
};
} // namespace ft

103
includes/vector.hpp

@ -1,6 +1,8 @@
#pragma once #pragma once
#include "ft_conatainer.hpp"
#include "iterator.hpp" #include "iterator.hpp"
#include "utils.hpp"
#include <iostream>
#include <memory> #include <memory>
#include <stdexcept> #include <stdexcept>
@ -31,7 +33,8 @@ public:
explicit vector(const Allocator &alloc = allocator_type()) explicit vector(const Allocator &alloc = allocator_type())
: _alloc(alloc), _begin(NULL), _size(0), _capacity(0){}; : _alloc(alloc), _begin(NULL), _size(0), _capacity(0){};
explicit vector(size_type count, cst_reference value = T(), const Allocator &alloc = allocator_type()) explicit vector(size_type count, cst_reference value = T(),
const Allocator &alloc = allocator_type())
: _alloc(alloc), _size(count), _capacity(count) { : _alloc(alloc), _size(count), _capacity(count) {
_begin = _alloc.allocate(count); _begin = _alloc.allocate(count);
for (size_type i = 0; i < count; i++) for (size_type i = 0; i < count; i++)
@ -39,7 +42,8 @@ public:
} }
template < class It > template < class It >
vector(It first, It last, const Allocator &alloc = allocator_type(), typename enable_if<!is_integral<It>, 1>::type is_it = 0 ) vector(It first, It last, const Allocator &alloc = allocator_type(),
typename ft::enable_if< !is_integral< It >::value, bool >::type = 0)
: _alloc(alloc) { : _alloc(alloc) {
_size = distance(first, last); _size = distance(first, last);
_capacity = _size; _capacity = _size;
@ -75,58 +79,73 @@ public:
void assign(size_type count, cst_reference value) { void assign(size_type count, cst_reference value) {
resize(count); resize(count);
while (count > 0){ do {
_alloc.destroy(this[count]); _alloc.destroy(_begin + count);
_alloc.contruct(this[count], value); _alloc.construct(_begin + count, value);
count--; } while (count-- > 0);
}
} }
template < class It > void assign(It first, It last) { template < class It >
void
assign(It first, It last,
typename ft::enable_if< !is_integral< It >::value, bool >::type = 0) {
size_type i = ft::distance(first, last); size_type i = ft::distance(first, last);
resize(i); resize(i);
while (i > 0) { do {
_alloc.destroy(this[i]); _alloc.destroy(_begin + i);
_alloc.contruct(this[i], first[i - 1]); _alloc.construct(_begin + i, first[i]);
i--; } while (i-- > 0);
}
} }
allocator_type get_allocator(void) const { return _alloc; } allocator_type get_allocator(void) const { return _alloc; }
// ACCESS: // ACCESS:
reference at(size_type pos) { reference at(size_type pos) {
if (pos > _size) throw (std::out_of_range("vector: Out of range access")); if (pos >= _size)
throw(std::out_of_range("vector: Out of range access"));
return *(_begin + pos); return *(_begin + pos);
} }
cst_reference at(size_type pos) const { cst_reference at(size_type pos) const {
if (pos > _size) throw (std::out_of_range("vector: Out of range access")); if (pos >= _size)
throw(std::out_of_range("vector: Out of range access"));
return *(_begin + pos); return *(_begin + pos);
} }
reference operator[](size_type pos) {return *(_begin + pos);} reference operator[](size_type pos) { return _begin[pos]; }
cst_reference operator[](size_type pos) const {return *(_begin + pos);} cst_reference operator[](size_type pos) const { return _begin[pos]; }
reference front(void) {return this[0];} reference front(void) { return _begin[0]; }
cst_reference front(void) const {return this[0];} cst_reference front(void) const { return _begin[0]; }
reference back(void) {return this[_size - 1];} reference back(void) { return _begin[_size - 1]; }
cst_reference back(void) const {return this[_size - 1];} cst_reference back(void) const { return _begin[_size - 1]; }
pointer data(void) {return _begin;} pointer data(void) { return _begin; }
cst_pointer data(void) const {return _begin;} cst_pointer data(void) const { return _begin; }
// ITERATORS: // ITERATORS:
ra_iterator< T > begin(void) {return ra_iterator<T>(front());} ra_iterator< T > begin(void) { return ra_iterator< T >(_begin); }
ra_iterator< const T > begin(void) const {return ra_iterator<const T>(front());} ra_iterator< const T > begin(void) const {
return ra_iterator< const T >(_begin);
}
ra_iterator< T > end(void) {return ra_iterator<T>(this[_size]);} ra_iterator< T > end(void) { return ra_iterator< T >(_begin + _size); }
ra_iterator< const T > end(void) const {return ra_iterator<const T>(this[_size]);} ra_iterator< const T > end(void) const {
return ra_iterator< const T >(_begin + _size);
}
rev_iterator< iterator > rbegin(void) {return rev_iterator<pointer>(end());} rev_iterator< iterator > rbegin(void) {
rev_iterator< cst_iterator > rbegin(void) const {return rev_iterator<cst_pointer>(end());} return rev_iterator< pointer >(end());
}
rev_iterator< cst_iterator > rbegin(void) const {
return rev_iterator< cst_pointer >(end());
}
rev_iterator< iterator > rend(void) {return rev_iterator<pointer>(begin());} rev_iterator< iterator > rend(void) {
rev_iterator< cst_iterator > rend(void) const {return rev_iterator<cst_pointer>(begin());} return rev_iterator< pointer >(begin());
}
rev_iterator< cst_iterator > rend(void) const {
return rev_iterator< cst_pointer >(begin());
}
// CAPACITY: // CAPACITY:
bool empty(void) const { return _size == 0; } bool empty(void) const { return _size == 0; }
@ -157,7 +176,8 @@ public:
iterator insert(iterator pos, cst_reference value) { iterator insert(iterator pos, cst_reference value) {
if (_size == _capacity) if (_size == _capacity)
resize(_size + 1); resize(_size + 1);
iterator it = end() + 1;; iterator it = end() + 1;
;
while (--it >= pos) while (--it >= pos)
*it = *(it - 1); *it = *(it - 1);
*it = value; *it = value;
@ -199,7 +219,7 @@ public:
return _begin; return _begin;
} }
iterator erase(iterator first, iterator last) { iterator erase(iterator first, iterator last) {
while (last++ < end()){ while (last++ < end()) {
_alloc.destroy(first); _alloc.destroy(first);
*(first++) = *last; *(first++) = *last;
} }
@ -222,19 +242,28 @@ public:
size_type i = count; size_type i = count;
if (count < _size) { if (count < _size) {
while (i < _size && --_size) while (i < _size && --_size)
_alloc.destroy(this[i]); _alloc.destroy(_begin + i);
} else { } else {
if (count > _capacity) if (count > _capacity)
reserve(count); reserve(count);
while (i >= _size) while (i >= _size)
_alloc.construct(_begin[--i], value); _alloc.construct(_begin + --i, value);
_size = count;
} }
} }
void swap(vector &other) { void swap(vector &other) {
vector<T> tmp = other; vector< T > tmp = other;
other = this; other = this;
this = tmp; this = tmp;
} }
void print(void) {
ft::ra_iterator< T > i = begin();
while (i < end()) {
std::cout << *i << " ";
i++;
}
std::cout << "\n";
}
}; };
} // namespace ft } // namespace ft

80
tester.cpp

@ -1,16 +1,72 @@
#include "ft_container.hpp" #include "ft_container.hpp"
int main(void) { // debugs:
ft::vector<int> test1 = ft::vector<int>(); //
ft::vector<int> test2 = ft::vector<int>(10);
ft::vector<int> test3 = ft::vector<int>(10, 4); void test_vectors(void) {
ft::vector<int> test4 = ft::vector<int>(test3.begin(), test3.begin() + 4);
ft::vector<int> test5 = ft::vector<int>(test3); ft::vector< int > test1 = ft::vector< int >();
ft::vector< int > test2 = ft::vector< int >(10);
debug_vector(test1); ft::vector< int > test3 = ft::vector< int >(10, 4);
debug_vector(test2); ft::vector< int > test4 = ft::vector< int >(test3.begin(), test3.begin() + 4);
debug_vector(test3); ft::vector< int > test5 = ft::vector< int >(test3);
debug_vector(test4);
debug_vector(test5); std::cout << "1) vector<int>() | ";
test1.print();
std::cout << "2) vector<int>(10) | ";
test2.print();
std::cout << "3) vector<int>(10, 4) | ";
test3.print();
std::cout << "4) vector<int>(test3.begin(), test3.begin() + 4) | ";
test4.print();
std::cout << "5.a) vector<int>(test3) | ";
test5.print();
ft::vector< int >::reference test5_begin = *test5.begin();
std::cout << "5.b) test5_begin = *test5.begin() | " << test5_begin
<< std::endl;
test5 = test4;
std::cout << "\ttest5 = test4 | ";
test5.print();
std::cout << "\ttest5_begin | " << test5_begin << std::endl;
std::cout << "6.a) test5.assign(2,2) | ";
test5.assign(2, 2);
test5.print();
std::cout << "6.b) test5.assign(test3.begin(), test3.begin() + 4) | ";
test5.assign(test3.begin(), test3.begin() + 4);
test5.print();
std::cout << "7.a) test5.at(0) | " << test5.at(0) << std::endl;
std::cout << "7.b) test5.at(4) | " << test5.at(4) << std::endl;
std::cout << "7.c) test5.at(5) | ";
try {
std::cout << test5.at(5) << std::endl;
} catch (std::exception &e) {
std::cout << e.what() << std::endl;
}
std::cout << "8.a) test5[4] = 1; | " << (test5[4] = 1) << std::endl;
std::cout << "8.b) test5[0] | " << test5[0] << std::endl;
std::cout << "8.c) test5[4] | " << test5[4] << std::endl;
std::cout << "9.a) test5.front() | " << test5.front() << std::endl;
std::cout << "9.b) test5.back() | " << test5.back() << std::endl;
std::cout << "10.a) *test5.data() | " << *test5.data() << std::endl;
std::cout << "11.a) test5.empty() | " << test5.empty() << std::endl;
std::cout << "11.b) test5.size() | " << test5.size() << std::endl;
std::cout << "11.c) test5.max_size() | " << test5.max_size() << std::endl;
std::cout << "11.d) test5.capacity() | " << test5.capacity() << std::endl;
test5.resize(0);
std::cout << "12.a) test5.resize(0) : "<< std::endl;
std::cout << "12.b) test5.empty() | " << test5.empty() << std::endl;
std::cout << "12.c) test5.size() | " << test5.size() << std::endl;
std::cout << "12.d) test5.max_size() | " << test5.max_size() << std::endl;
std::cout << "12.e) test5.capacity() | " << test5.capacity() << std::endl;
} }
int main(void) { test_vectors(); }

Loading…
Cancel
Save