aboutsummaryrefslogtreecommitdiff
path: root/Vector.hpp
blob: 7d6b40bdd2338a89a64a7dfc1070056fece9c11d (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   Vector.hpp                                         :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: charles <charles.cabergs@gmail.com>        +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/04/24 16:19:04 by charles           #+#    #+#             */
/*   Updated: 2020/04/24 16:19:05 by charles          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#ifndef VECTOR_HPP
# define VECTOR_HPP

# include <stdexcept>

# define FT_VECTOR_GROWTH_FACTOR 1.5

namespace ft
{
	template < typename T, typename Alloc = allocator<T> >
	class vector
	{
    public:
		typedef T                                        value_type;
		typedef Alloc                                    allocator_type;
		typedef typename allocator_type::reference       reference;
		typedef typename allocator_type::const_reference const_reference;
		typedef typename allocator_type::pointer         pointer;
		typedef typename allocator_type::const_pointer   const_pointer;
		typedef ptrdiff_t                                difference_type;
		typedef size_t                                   size_type;

        explicit Vector(const allocator_type& alloc = allocator_type())
            : m_under(new value_type[0]), m_size(0), m_capacity(0)
        {}

        explicit Vector(size_type n,
                        const value_type& val = value_type(),
                        const allocator_type& alloc = allocator_type())
            : m_under(new value_type[n]), m_size(n), m_capacity(n)
        {
            for (size_type i = 0; i < n; i++)
                m_under[i] = val;
        }

        template <typename InputIterator>
        Vector (InputIterator first,
                InputIterator last,
                const allocator_type& alloc = allocator_type())
            : m_under(new value_type[0]), m_size(0), m_capacity(0)
        {
            for (; first != last; first++)
                push_back(*first);
        }

        Vector (const Vector& x)
        {
            *this = x;
        }

        Vector& operator=(const Vector& x)
        {
            if (this == &x)
                return *this;
            delete [] m_under;
            m_capacity = x.m_capacity;
            m_size = x.m_size;
            m_under = new value_type[m_capacity];
            for (size_type i = 0; i < m_size; i++)
                m_under[i] = x.m_under[i];
            return *this;
        }

        ~Vector()
        {
            delete [] m_under;
        }

        size_type size()     const { return m_size;      }
        size_type max_size() const { return UINT_MAX;    }
        size_type capacity() const { return m_capacity;  }
        bool      empty()    const { return m_size == 0; }

        void resize(size_type n, value_type val = value_type())
        {
            if (n == m_size)
                return ;
            if (n > m_size)
            {
                reserve(n);
                for (; m_size < n; m_size++)
                    m_under[m_size] = val;
            }
            else
            {
                value_type* newUnder = new value_type[n];
                m_capacity = n;
                m_size = n;
                for (size_type i = 0; i < m_size; i++)
                    newUnder[i] = m_under[i];
                delete [] m_under;
                m_under = newUnder;
            }
        }

        void reserve(size_type n)
        {
            if (m_capacity < 2)
                m_capacity = 2;
            while (m_capacity < n)
                m_capacity *= FT_VECTOR_GROWTH_FACTOR;
            value_type* newUnder = new value_type[m_capacity];
            for (size_type i = 0; i < m_size; i++)
                newUnder[i] = m_under[i];
            delete [] m_under;
            m_under = newUnder;
        }

        reference       operator[](size_type n)       { return m_under[n]; }
        const_reference operator[](size_type n) const { return m_under[n]; }

        reference at(size_type n)
        {
            if (n >= m_size)
                throw std::out_of_range("vector::_M_range_check");
            return m_under[n];
        }

        const_reference at(size_type n) const
        {
            if (n >= m_size)
                throw std::out_of_range("vector::_M_range_check");
            return m_under[n];
        }

        reference       front()       { return m_under[0];          }
        const_reference front() const { return m_under[0];          }
        reference       back()        { return m_under[m_size - 1]; }
        const_reference back()  const { return m_under[m_size - 1]; }

        template <typename InputIterator>
        void assign(InputIterator first, InputIterator last)
        {
            size_type count;
            InputIterator copy;
            copy = first;
            count = 0;
            for (; copy != last; copy++)
                count++;
            reserve(count);
            for (size_type i = 0; first != last; first++, i++)
                m_under[i] = *first;
        }

        void assign(size_type n, const value_type& val)
        {
            reserve(n);
            for (size_type i = 0; i < n; ++)
                m_under[i] = val;
        }

        void push_back(const value_type& val)
        {
            m_size++;
            reserve(m_size);
            m_under[m_size - 1] = val;
        }

        void pop_back()
        {
            m_size--;
            ~value_type(m_under[m_size]);
            m_under[m_size] = value_type();
        }

        // insert

        // erase

        void swap(Vector& x)
        {
            value_type* tmpUnder = m_under;
            size_type* tmpSize = m_size;
            size_type* tmpCapacity = m_capacity;

            m_under = x.m_under;
            m_size = x.m_size;
            m_capacity = x.m_capacity;

            x.m_under = tmpUnder;
            x.m_size = tmpSize;
            x.m_capacity = tmpCapacity;
        }

        void clear()
        {
            delete [] m_under;
            m_under = new value_type[0];
            m_size = 0;
            m_capacity = 0;
        }

    private:
        value_type* m_under;
        size_type   m_size;
        size_type   m_capacity;
	};
}

template <typename T, typename Alloc>
bool operator==(const Vector<T, Alloc>& lhs, const Vector<T, Alloc>& rhs)
{
    if (lhs.size() != rhs.size())
        return false;
    for (Vector::size_type i = 0; i < lhs.size(); i++)
        if (!(lhs[i] == rhs[i]))
            return false;
    return true;
}

template <typename T, typename Alloc>
bool operator!=(const Vector<T, Alloc>& lhs, const Vector<T, Alloc>& rhs)
{
    return !(lhs == rhs);
}

template <typename T, typename Alloc>
bool operator<const Vector<T, Alloc>& lhs, const Vector<T, Alloc>& rhs)
{
    for (Vector::size_type i = 0; i < lhs.size() && i < rhs.size(); i++)
        if (lhs[i] < rhs[i] && !(rhs[i] < lhs[i]))
            return true;
    return false;
}

template <typename T, typename Alloc>
bool operator<=const Vector<T, Alloc>& lhs, const Vector<T, Alloc>& rhs)
{
    return !(rhs < lhs);
}

template <typename T, typename Alloc>
bool operator>const Vector<T, Alloc>& lhs, const Vector<T, Alloc>& rhs)
{
    return rhs < lhs;
}

template <typename T, typename Alloc>
bool operator>=const Vector<T, Alloc>& lhs, const Vector<T, Alloc>& rhs)
{
    return !(lhs < rhs);
}

template <typename T, typename Alloc>
void swap(Vector<T, Alloc>& x, Vector<T, Alloc>& y)
{
    x.swap(y);
}

#endif