Website Summary Files Commits Branches @commit @diff @stats
path: root / Week11 / Exercise04 / Vector.hpp
blob: 78dba08f4b1b61f0fbe07483cdf7f24aeca1940d (plain) (blame)
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
#define _VECTOR

template<class T>
class Vector {
	T* objects;
	unsigned objectsCount;
	unsigned objectsAlloc;

	void free();
	void copyFrom(const Vector& other);

	void resize();

public:
	Vector();
	~Vector();
	Vector(const Vector& other);
	Vector& operator=(const Vector& other);

	Vector(Vector&& other);
	Vector& operator=(Vector&& other);

	unsigned Count() const;
	void PushBack(const T& object);

	T& operator[](unsigned index);
	const T& operator[](unsigned index) const;

	Vector& operator+=(const Vector& rhs);
};

template<class T>
Vector<T> operator+(const Vector<T>& lhs, const Vector<T>& rhs);

template<class T>
void Vector<T>::free() {
	delete[] objects;
}

template<class T>
void Vector<T>::copyFrom(const Vector& other) {
	objectsCount = other.objectsCount;
	objectsAlloc = other.objectsAlloc;

	objects = new T[objectsAlloc];
	for (unsigned i = 0; i < objectsCount; ++i)
		objects[i] = other.objects[i];
}

template<class T>
void Vector<T>::resize() {
	objectsAlloc = objectsAlloc == 0 ? 8 : objectsAlloc * 2;
	T* objectsLarger = new T[objectsAlloc];

	for (unsigned i = 0; i < objectsCount; ++i)
		objectsLarger[i] = objects[i];

	delete[] objects;
	objects = objectsLarger;
}

template<class T>
Vector<T>::Vector() : objects(), objectsAlloc(), objectsCount()
{ }

template<class T>
Vector<T>::~Vector() {
	free();
}

template<class T>
Vector<T>::Vector(const Vector& other) {
	copyFrom(other);
}

template<class T>
Vector<T>& Vector<T>::operator=(const Vector& other) {
	if (this != &other) {
		free();
		copyFrom(other);
	}
	return *this;
}

template<class T>
Vector<T>::Vector(Vector&& other) {
	objects = other.objects;
	objectsCount = other.objectsCount;
	objectsAlloc = other.objectsAlloc;

	other.objects = nullptr;
	other.objectsCount = other.objectsAlloc = 0;
}

template<class T>
Vector<T>& Vector<T>::operator=(Vector&& other) {
	if (this != &other) {
		free();

		objects = other.objects;
		objectsCount = other.objectsCount;
		objectsAlloc = other.objectsAlloc;

		other.objects = nullptr;
		other.objectsCount = other.objectsAlloc = 0;
	}
	return *this;
}

template<class T>
unsigned Vector<T>::Count() const {
	return objectsCount;
}

template<class T>
void Vector<T>::PushBack(const T& object) {
	if (objectsCount >= objectsAlloc)
		resize();
	objects[objectsCount++] = object;
}

template<class T>
T& Vector<T>::operator[](unsigned index) {
	return objects[index];
}

template<class T>
const T& Vector<T>::operator[](unsigned index) const {
	return objects[index];
}

template<class T>
Vector<T>& Vector<T>::operator+=(const Vector& rhs) {
	for (unsigned i = 0; i < rhs.objectsCount; ++i)
		PushBack(rhs.objects[i]);
	return *this;
}

template<class T>
Vector<T> operator+(const Vector<T>& lhs, const Vector<T>& rhs) {
	Vector val(lhs);
	val += rhs;
	return val;
}

#endif /* _VECTOR */