aboutsummaryrefslogtreecommitdiff
path: root/src/Vector.hpp
blob: 0f21995666805ca99259e81a2974f6d2d67323a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#ifndef VECTOR_HPP
# define VECTOR_HPP

# include <stddef.h>

# define GROWTH_FACTOR 1.5

typedef size_t size_type;


namespace ft
{
	template < class T, class Alloc = allocator<T> >
	class vector
	{
		typedef T value_type;
		typedef T& reference;

	public:
		// capacity
		size_type size() const;
		{
			return size;
		}
		void resize (size_type n, value_type val = value_type())
		{
			if (n < size)
			{
				for (int i = n; i < size; i++)
					~T(under[i]);
				size = n;
				return;
			}
			if (n > capacity)
				grow();
			size_type prev_n;
			for (prev_n = n; prev_n < size; prev_n++)
				under[n] = val;
			size = n;
		}
		size_type capacity() const
		{
			return capacity;
		}
		bool empty() const
		{
			return size == 0;
		}
		void reserve (size_type n)
		{
			if (n > capacity)
				grow_to(n);
		}

		// element access
		reference operator[] (size_type n)
		{
			return under[n];
		}
		reference at (size_type n)
		{
			if (n >= size)
				raise std::out_of_range;
			return under[n];
		}
		reference front()
		{
			return under[0];
		}
		reference back()
		{
			return under[size - 1];
		}

		// modifiers
		template <class InputIterator>
		void assign (InputIterator first, InputIterator last);
		void assign (size_type n, const value_type& val);
		void push_back (const value_type& val)
		{
			if (size >= capacity)
				grow();
			under[size] = val;
			size++;
		}
		void pop_back()
		{
			~T(back());
			size--;
		}
		iterator insert (iterator position, const value_type& val)
		{
			
		}
		void insert (iterator position, size_type n, const value_type& val);
		template <class InputIterator>
		void insert (iterator position, InputIterator first, InputIterator last);

		


	private:
		T *under;
		size_type size;
		size_type capacity;

		void grow()
		{
			capacity *= GROWTH_FACTOR;
			grow_to(capacity);
		}

		void grow_to(size_type new_capacity)
		{
			T *under_copy = new T[capacity];
			for (int i = 0; i < capacity; i++)
				under_copy[i] = under[i];
			delete [] under;
			capacity = new_capacity;
			under = new T[capacity];
			for (int i = 0; i < capacity; i++)
				under[i] = under_copy[i];
			delete [] under_copy;
		}
	};
}

#endif