-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamic_array.cpp
100 lines (73 loc) · 2.16 KB
/
dynamic_array.cpp
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
#include "assignment/dynamic_array.hpp"
#include <algorithm> // copy, fill
#include <stdexcept> // invalid_argument (НЕЛЬЗЯ ИСПОЛЬЗОВАТЬ)
namespace assignment {
DynamicArray::DynamicArray(int capacity) {
// выбрасываем ошибку, если указана неположительная емкость массива
if (capacity <= 0) {
throw std::invalid_argument("capacity is not positive");
}
// Write your code here ...
}
DynamicArray::~DynamicArray() {
// Write your code here ...
}
void DynamicArray::Add(int value) {
// Write your code here ...
}
bool DynamicArray::Insert(int index, int value) {
// Write your code here ...
return false;
}
bool DynamicArray::Set(int index, int new_value) {
// Write your code here ...
return false;
}
std::optional<int> DynamicArray::Remove(int index) {
// Write your code here ...
return std::nullopt;
}
void DynamicArray::Clear() {
// Write your code here ...
}
std::optional<int> DynamicArray::Get(int index) const {
// Write your code here ...
return std::nullopt;
}
std::optional<int> DynamicArray::IndexOf(int value) const {
// Write your code here ...
return std::nullopt;
}
bool DynamicArray::Contains(int value) const {
return false;
}
bool DynamicArray::IsEmpty() const {
return false;
}
int DynamicArray::size() const {
return 0;
}
int DynamicArray::capacity() const {
return 0;
}
bool DynamicArray::Resize(int new_capacity) {
// Write your code here ...
return false;
}
// ДЛЯ ТЕСТИРОВАНИЯ
DynamicArray::DynamicArray(const std::vector<int>& values, int capacity) {
size_ = static_cast<int>(values.size());
capacity_ = capacity;
data_ = new int[capacity]{};
std::copy(values.data(), values.data() + size_, data_);
}
std::vector<int> DynamicArray::toVector(std::optional<int> size) const {
if (capacity_ == 0 || data_ == nullptr) {
return {};
}
if (size.has_value()) {
return {data_, data_ + size.value()};
}
return {data_, data_ + capacity_};
}
} // namespace assignment