-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector.h
160 lines (137 loc) · 2.63 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
148
149
150
151
152
153
154
155
156
157
158
159
#pragma once
#include <iostream>
using namespace std;
class Vector
{
int size = 0; // êîëè÷åñòâî äåéñòâèòåëüíî ïðèñóòñòâóþùèõ ýëåìåíòîâ â êîëëåêöèè
int capacity = 10; // ¸ìêîñòü (âìåñòèòåëüíîñòü, çàïàñ ïàìÿòè)
int* data; // óêàçàòåëü íà äèíàìè÷åñêèé ìàññèâ äàííûõ
public:
Vector() : Vector(10)
{
// cout << "C-TOR WITHOUT PARAMS!\n";
}
Vector(int capacity)
{
if (capacity < 10)
{
capacity = 10;
}
if (capacity > 500000000)
{
capacity = 500000000;
}
this->capacity = capacity;
data = new int[capacity];
// cout << "C-TOR WITH PARAMS!\n";
}
~Vector()
{
// cout << "DESTRUCTOR!\n";
delete[] data;
}
private:
void EnsureCapacity(int count)
{
// åñëè çàïàñ ïàìÿòè äîñòàòî÷åí
if (capacity >= count) return;
// åñëè íåò
int new_capacity = capacity * 3 / 2 + 1;
int* temp = new int[new_capacity];
for (int i = 0; i < capacity; i++)
{
temp[i] = data[i];
}
delete[] data;
data = temp;
capacity = new_capacity;
cout << "NEW CAP: " << capacity << "\n";
}
public:
void PushBack(int value)
{
EnsureCapacity(size + 1); // ïðîâåðêà, õâàòèò ëè ìåñòà äëÿ íîâîãî ýëåìåíòà - äåëàéòå ñàìè ;)
data[size++] = value;
}
Vector operator+(const Vector& another)
{
Vector result;
result.capacity = this->capacity + another.capacity;
result.size = this->size + another.size;
result.data = new int[result.capacity];
int index = 0;
for (; index < this->size; index++)
{
result.data[index] = this->data[index];
}
for (; index < this->size + another.size; index++)
{
result.data[index] = another.data[index - this->size];
}
return result;
}
bool Equals(const Vector& another)const
{
if (size == another.size)
{
int result = 0;
for (int i = 0; i < size; i++)
{
if (data[i] == another.data[i])
{
result++;
}
}
return result == size;
}
return false;
}
void PushFront(int value)
{
// EnsureCapacity(size + 1);
for (int i = size; i > 0; i--)
{
data[i] = data[i - 1];
}
data[0] = value;
size++;
}
Vector(const Vector& original)
{
this->size = original.size;
this->capacity = original.capacity;
this->data = new int[original.capacity];
for (int i = 0; i < size; i++)
{
this->data[i] = original.data[i];
}
}
void Clear()
{
size = 0;
}
bool IsEmpty()
{
return size == 0;
}
void Print()
{
if (IsEmpty())
{
cout << "Vector is empty.\n";
}
else
{
for (int i = 0; i < size; i++)
{
cout << data[i] << " ";
}
cout << endl;
}
}
const int& operator[](unsigned int index) const
{
if (index >= size) throw "Incorrect index!"; // ãåíåðàöèÿ èñêëþ÷åíèÿ
return data[index];
}
};