-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.h
147 lines (123 loc) · 3.33 KB
/
vector.h
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
#ifndef VECTOR_H
#define VECTOR_H
#pragma once
#include <initializer_list>
#include <iterator>
#include <memory>
template <typename T, typename A = std::allocator<T>>
class new_vector
{
A alloc;
int sz;
T* elem;
int space;
public:
new_vector();
explicit new_vector(int s);
new_vector(std::initializer_list<T> lst);
new_vector(const new_vector& a);
new_vector& operator=(const new_vector& a);
new_vector(new_vector&& a);
new_vector operator=(new_vector&& a);
~new_vector() { delete[] elem; }
T& operator[ ](int i) { return elem[i]; }
int capacity() const { return space; }
int size() const { return sz; }
void reserve(int newalloc);
void resize(int newsize, const T& val = T())
{
reserve(newsize);
for (int i = sz; i < newsize; ++i)
alloc.construct(&elem[i], val);
for (int i = sz; i < newsize; ++i)
alloc.destroy(&elem[i]);
sz = newsize;
}
void push_back(const T& d);
};
template <typename T, typename A>
new_vector<T,A>::new_vector():
sz{0}, elem{nullptr}, space{0} {}
template <typename T, typename A>
new_vector<T,A>::new_vector(int s):
sz{s}, space{s},
elem{new T[s]}
{
for(int i=0; i<s; ++i)
elem[i]=0;
}
template <typename T, typename A>
new_vector<T,A>::new_vector(std::initializer_list<T> lst):
sz{ static_cast<int>(lst.size())},
elem{new T[lst.size()]},
space{sz}
{
std::copy(lst.begin(), lst.end(), elem);
}
template <typename T, typename A>
new_vector<T,A>::new_vector(const new_vector& a):
sz{a.sz},
elem{new T[a.sz]},
space{sz}
{
std::copy(a.elem, a.elem + a.sz, elem);
}
template <typename T, typename A>
new_vector<T,A>& new_vector<T,A>::operator=(const new_vector& a)
{
if (this == &a)
return *this;
if (a.sz <= space)
{
std::copy(a.elem, a.elem + a.sz, elem);
sz = a.sz;
return *this;
}
T* p = new T[a.size()];
std::copy(a.elem, a.elem + a.sz, p);
delete[] elem;
elem = p;
space = sz = a.sz;
return *this;
}
template <typename T, typename A>
new_vector<T,A>::new_vector(new_vector&& a):
sz{a.sz}, elem{a.elem}, space{sz}
{
a.sz = 0;
a.elem = nullptr;
}
template <typename T, typename A>
new_vector<T,A> new_vector<T,A>::operator=(new_vector&& a)
{
delete[] elem;
elem = a.elem;
a.elem = nullptr;
sz = a.sz;
a.sz = 0;
return *this;
}
template<typename T, typename A>
void new_vector<T,A>::reserve(int newalloc)
{
if (newalloc <= space) return;
T* p = alloc.allocate(newalloc);
for (int i = 0; i < sz; ++i)
alloc.construct(&p[i], elem[i]);
for (int i = 0; i < sz; ++i)
alloc.destroy(&elem[i]);
alloc.deallocate(elem, space);
elem = p;
space = newalloc;
}
template<typename T, typename A>
void new_vector<T,A>::push_back(const T& d)
{
if (!space)
reserve(8);
else if (sz == space)
reserve(2*space);
elem[sz] = d;
++sz;
}
#endif // VECTOR_H