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
|
#include "Hangar.h"
#include "RocketStage.h"
void Hangar::free() {
delete[] rockets;
delete[] stages;
}
void Hangar::copyFrom(const Hangar& other) {
rocketsCount = other.rocketsCount;
rocketsAlloc = other.rocketsAlloc;
rockets = new Rocket[rocketsAlloc];
for (unsigned i = 0; i < rocketsCount; ++i)
rockets[i] = other.rockets[i];
}
void Hangar::resizeRockets() {
rocketsAlloc = rocketsAlloc == 0 ? 8 : rocketsAlloc * 2;
Rocket* rocketsResized = new Rocket[rocketsAlloc];
for (unsigned i = 0; i < rocketsCount; ++i)
rocketsResized[i] = rockets[i];
delete[] rockets;
rockets = rocketsResized;
}
void Hangar::resizeStages() {
stagesAlloc = stagesAlloc == 0 ? 8 : stagesAlloc * 2;
RocketStage* stagesResized = new RocketStage[stagesAlloc];
for (unsigned i = 0; i < stagesCount; ++i)
stagesResized[i] = stages[i];
delete[] stages;
stages = stagesResized;
}
Hangar::Hangar() {
rockets = nullptr;
stages = nullptr;
rocketsCount = rocketsAlloc = stagesCount = stagesAlloc = 0;
}
Hangar::~Hangar() {
free();
}
Hangar::Hangar(const Hangar& other) {
copyFrom(other);
}
Hangar& Hangar::operator=(const Hangar& other) {
if (this != &other) {
free();
copyFrom(other);
}
return *this;
}
Hangar::Hangar(Hangar&& other) {
rockets = other.rockets;
rocketsCount = other.rocketsCount;
rocketsAlloc = other.rocketsAlloc;
stages = other.stages;
stagesCount = other.stagesCount;
stagesAlloc = other.stagesAlloc;
other.rockets = nullptr;
other.rocketsCount = other.rocketsAlloc = 0;
other.stages = nullptr;
other.stagesCount = other.stagesAlloc = 0;
}
Hangar& Hangar::operator=(Hangar&& other) {
if (this != &other) {
free();
rockets = other.rockets;
rocketsCount = other.rocketsCount;
rocketsAlloc = other.rocketsAlloc;
stages = other.stages;
stagesCount = other.stagesCount;
stagesAlloc = other.stagesAlloc;
other.rockets = nullptr;
other.rocketsCount = other.rocketsAlloc = 0;
other.stages = nullptr;
other.stagesCount = other.stagesAlloc = 0;
}
return *this;
}
unsigned Hangar::AddStage(const RocketStage& stage) {
if (stagesCount >= stagesAlloc)
resizeStages();
stages[stagesCount] = stage;
return stagesCount++;
}
RocketStage Hangar::RemoveStage(unsigned stageIndex) {
if (stageIndex >= stagesCount)
return RocketStage();
RocketStage rs = stages[stageIndex];
stagesCount--;
for (unsigned i = stageIndex; i < stagesCount; ++i)
stages[i] = stages[i + 1];
return rs;
}
unsigned Hangar::AddRocket(const Rocket& rocket) {
if (rocketsCount >= rocketsAlloc)
resizeRockets();
rockets[rocketsCount] = rocket;
return rocketsCount++;
}
const Rocket* Hangar::GetRocket(unsigned rocketIndex) const {
if (rocketIndex >= rocketsCount)
return nullptr;
return &rockets[rocketIndex];
}
void Hangar::MoveStageToRocket(unsigned rocketIndex, unsigned stageIndex) {
if (rocketIndex >= rocketsCount || stageIndex >= stagesCount)
return;
rockets[rocketIndex].AddStage(RemoveStage(stageIndex));
}
void Hangar::Export(std::ofstream& outFile) const {
outFile.write((const char*)&rocketsCount, sizeof(rocketsCount));
outFile.write((const char*)&rocketsAlloc, sizeof(rocketsAlloc));
for (unsigned i = 0; i < rocketsCount; ++i)
rockets[i].Export(outFile);
outFile.write((const char*)&stagesCount, sizeof(stagesCount));
outFile.write((const char*)&stagesAlloc, sizeof(stagesAlloc));
for (unsigned i = 0; i < stagesCount; ++i)
outFile.write((const char*)&stages[i], sizeof(stages[i]));
}
void Hangar::Import(std::ifstream& inFile) {
free();
inFile.read((char*)&rocketsCount, sizeof(rocketsCount));
inFile.read((char*)&rocketsAlloc, sizeof(rocketsAlloc));
rockets = new Rocket[rocketsAlloc];
for (unsigned i = 0; i < rocketsCount; ++i)
inFile.read((char*)&rockets[i], sizeof(rockets[i]));
inFile.read((char*)&stagesCount, sizeof(stagesCount));
inFile.read((char*)&stagesAlloc, sizeof(stagesAlloc));
stages = new RocketStage[stagesAlloc];
for (unsigned i = 0; i < stagesCount; ++i)
inFile.read((char*)&stages[i], sizeof(stages[i]));
}
|