Repository navigation
Expand file tree
/
Copy patharray.h
More file actions
218 lines (176 loc) · 5.89 KB
/
Copy patharray.h
File metadata and controls
218 lines (176 loc) · 5.89 KB
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
#pragma once
#include <iostream>
#include <stdexcept>
#include <cassert> // Для использования assert в отладочной версии
template<typename T>
class Array {
private:
T* data;
int capacity;
int currentSize;
void clear() {
for (int i = 0; i < currentSize; ++i) {
data[i].~T();
}
currentSize = 0;
}
void reallocate() {
capacity = static_cast<int>(capacity * 1.6);
T* newData = new T[capacity];
for (int i = 0; i < currentSize; ++i) {
newData[i] = std::move(data[i]);
}
delete[] data;
data = newData;
}
public:
Array(int cap = 8) : capacity(cap), currentSize(0) {
data = new T[capacity];
}
~Array() {
delete[] data;
}
// Конструктор копирования
Array(const Array& other) : capacity(other.capacity), currentSize(other.currentSize) {
data = new T[capacity];
for (int i = 0; i < currentSize; ++i) {
data[i] = other.data[i]; // Копируем элементы
}
}
// Оператор присваивания
Array& operator=(const Array& other) {
if (this != &other) {
delete[] data; // Освобождаем текущую память
capacity = other.capacity;
currentSize = other.currentSize;
data = new T[capacity];
for (int i = 0; i < currentSize; ++i) {
data[i] = other.data[i]; // Копируем элементы
}
}
return *this;
}
// Оператор перемещения
Array(Array&& other) noexcept : data(other.data), capacity(other.capacity), currentSize(other.currentSize) {
other.data = nullptr;
other.capacity = 0;
other.currentSize = 0;
}
// Оператор перемещения присваивания
Array& operator=(Array&& other) noexcept {
if (this != &other) {
delete[] data;
data = other.data;
capacity = other.capacity;
currentSize = other.currentSize;
other.data = nullptr;
other.capacity = 0;
other.currentSize = 0;
}
return *this;
}
// Вставка элемента в конец
int insert(const T& value) {
if (currentSize == capacity) {
reallocate();
}
data[currentSize++] = value;
return currentSize - 1;
}
// Вставка элемента в заданную позицию
int insert(int index, const T& value) {
if (index < 0 || index > currentSize) {
throw std::out_of_range("Index out of bounds");
}
if (currentSize == capacity) {
reallocate();
}
for (int i = currentSize; i > index; --i) {
data[i] = std::move(data[i - 1]); // Сдвигаем элементы вправо
}
data[index] = value;
++currentSize;
return index;
}
// Удаление элемента по индексу
void remove(int index) {
if (index < 0 || index >= currentSize) {
throw std::out_of_range("Index out of bounds");
}
for (int i = index; i < currentSize - 1; ++i) {
data[i] = std::move(data[i + 1]); // Сдвигаем элементы влево
}
--currentSize;
}
// Получение элемента по индексу
const T& operator[](int index) const {
if (index < 0 || index >= currentSize) {
throw std::out_of_range("Index out of bounds");
}
return data[index];
}
T& operator[](int index) {
if (index < 0 || index >= currentSize) {
throw std::out_of_range("Index out of bounds");
}
return data[index];
}
// Получение текущего размера
int size() const {
return currentSize;
}
class Iterator {
private:
T* ptr;
Array<T>& array;
public:
Iterator(T* p, Array<T>& arr) : ptr(p), array(arr) {}
T& get() const { return *ptr; }
void set(const T& value) { *ptr = value; }
void next() { if (hasNext()) ++ptr; }
bool hasNext() const { return ptr < (array.data + array.currentSize); }
};
Iterator iterator() {
return Iterator(data, *this);
}
class ConstIterator {
private:
const T* ptr;
const Array<T>& array;
public:
ConstIterator(const T* p, const Array<T>& arr) : ptr(p), array(arr) {}
const T& get() const { return *ptr; }
void next() { if(hasNext()) ++ptr; }
bool hasNext() const { return ptr < (array.data + array.currentSize); }
};
ConstIterator const_iterator() const {
return ConstIterator(data, *this); // Константный итератор для прямого обхода
}
class ReverseIterator {
private:
T* ptr;
Array<T>& array;
public:
ReverseIterator(T* p, Array<T>& arr) : ptr(p), array(arr) {}
T& get() const { return *ptr; }
void set(const T& value) { *ptr = value; }
void next() { if(hasNext()) --ptr; }
bool hasNext() const { return ptr >= (array.data); }
};
ReverseIterator reverseIterator() {
return ReverseIterator(data + currentSize - 1, *this);
}
class ConstReverseIterator {
private:
const T* ptr;
const Array<T>& array;
public:
ConstReverseIterator(const T* p, const Array<T>& arr) : ptr(p), array(arr) {}
const T& get() const { return *ptr; }
void next() { if(hasNext()) --ptr; }
bool hasNext() const { return ptr >= (array.data); }
};
ConstReverseIterator const_reverseIterator() const {
return ConstReverseIterator(data + currentSize - 1, *this);
}
};