forked from FluorineDog/2017aut-quest2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab_vector.hpp
159 lines (143 loc) · 3.52 KB
/
lab_vector.hpp
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
#ifndef LAB_VECTOR_HPP_
#define LAB_VECTOR_HPP_
//#define Lab std
//#endif
#include "labafx.hpp"
#include <cstdio>
#include <cstring>
#include <iostream>
namespace Lab {
template <typename T> class vector {
public:
vector();
class iterator {
public:
iterator() {
point = nullptr;
// return *this;
}
iterator(T *pointTmp) {
point = pointTmp;
// return *this;
}
// T &operator*();
// iterator operator+(int n);
// zht does not require the followings, make him happy.
// NOOOOOOOOOOOOO!
// iterator &operator++();
// bool operator==(const iterator &another) const;
// bool operator!=(const iterator &another) const;
// iterator operator++(int);
public:
// add your data and function here
T *point;
// T &operator*() { return point; }
iterator operator+(const int &n) {
// iterator ite = *this;
// ite.t += n;
// return ite;
this->point += n;
return *this;
}
int operator-(const iterator &another) { return point - another.point; }
iterator &operator++() {
point++;
return *this;
}
T &operator*() { return *point; }
bool operator==(const iterator &another) const {
return another.point == point;
}
bool operator!=(const iterator &another) const {
return another.point != point;
}
};
iterator begin();
iterator end();
size_t size() const;
void push_back(const T &item);
void pop_back();
void erase(iterator iter);
void clear();
size_t capacity();
void shrink_to_fit();
void reserve(size_t resv_size);
~vector();
private:
T *data;
int length;
int cap;
void restruction();
// add your data and function here
};
template <typename T> vector<T>::vector() {
data = nullptr;
length = 0;
cap = 0;
}
// template <typename T> T &vector<T>::iterator::operator*() { return *point; }
template <typename T> typename vector<T>::iterator vector<T>::begin() {
return iterator(&data[0]);
}
template <typename T> typename vector<T>::iterator vector<T>::end() {
return iterator(&data[length]);
}
template <typename T> size_t vector<T>::size() const { return size_t(length); }
template <typename T> void vector<T>::push_back(const T &item) {
if (length == cap) {
restruction();
}
data[length++] = item;
}
template <typename T> void vector<T>::pop_back() {
if (!length) {
std::cout << "The vector is empty";
} else {
data[--length] = T();
}
}
template <typename T> void vector<T>::erase(iterator iter) {
int dist = iter - begin();
std::memmove(data + dist, data + dist + 1, (length - dist - 1) * sizeof(T));
data[--length] = T();
}
template <typename T> void vector<T>::clear() {
while (length)
data[--length] = T();
}
template <typename T> size_t vector<T>::capacity() { return size_t(cap); }
template <typename T> void vector<T>::shrink_to_fit() {
data = (T *)realloc(data, (length + 1) * sizeof(T));
cap = length;
}
template <typename T> void vector<T>::reserve(size_t resv_size) {
cap = resv_size;
T *newdata = new T[cap + 1];
if (length) {
memcpy(newdata, data, (cap + 1) * sizeof(T));
delete[] data;
}
data = newdata;
}
template <typename T> void vector<T>::restruction() {
if (cap == 0)
cap = 1;
else
cap *= 2;
T *newdata = new T[cap + 1];
if (length) {
memcpy(newdata, data, sizeof(T) * length);
delete[] data;
}
data = newdata;
}
template <typename T> vector<T>::~vector() {
if (data) {
delete[] data;
data = nullptr;
}
length = 0;
cap = 0;
}
} // namespace Lab
#endif