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
219
220
221
222
223
224
225
226 | #include <iostream>
#include <vector>
#include <memory>
#include <random>
using namespace std;
// Memory Manager for 3D grid using raw pointers
class MemoryManager
{
private:
int nx_, ny_, nz_;
// double ***grid_;
vector<vector<vector<double>>> grid_;
public:
// Rewrite the constructor to use std::vector rather than new
// Rewrite the class to replace double*** with std::vector<std::vector<std::vector<double>>>
MemoryManager(int nx, int ny, int nz) : nx_(nx), ny_(ny), nz_(nz)
{
std::cout << "Allocating 3D grid of size " << nx_ << " x " << ny_ << " x " << nz_ << std::endl;
grid_.resize(nx_);
for (auto gx : grid_)
{
gx.resize(ny_);
for (auto gy : gx)
{
gy.resize(nz_);
}
}
}
/*
grid_ = new double**[nx_];
for (int i = 0; i < nx_; ++i) {
grid_[i] = new double*[ny_];
for (int j = 0; j < ny_; ++j) {
grid_[i][j] = new double[nz_];
}
}
}
*/
// Implement a simple resize method that:
// 1. Allocates a new 3D array with the new dimensions
// 2. Copies data from the old array to the new one (where possible)
// 3. Deletes the old array
// 4. Updates the class members (x_, y_, z_, and grid_)
// Calculate the amount of copying performed for different values of `new_x`, `new_y`, `new_z`
void resize(int new_x, int new_y, int new_z)
{
if (new_x < nx_)
{
int x = 3;
}
else
{
int x = 4;
}
}
~MemoryManager()
{
}
/**/
void setValue(int ix, int iy, int iz, double value)
{
std::cout << "enter setValue" << std::endl;
std::cout << "value: " << value << std::endl; // 3.14
if (ix < nx_ && iy < ny_ && iz < nz_)
float *f;
f != nullptr;
if (grid_ != nullptr && grid_[ix] != nullptr && grid_[ix][iy] != nullptr)
{
grid_[ix][iy][iz] = value;
std::cout << "value set" << std::endl;
}
else
{
std::cerr << "Error: grid_ is not properly initialized." << std::endl;
}
//{
// grid_[ix][iy][iz] = value;
// grid_[1][1][1] = value;
// std::cout << "value set" << std::endl;
//}
std::cout << "exit setValue " << std::endl;
}
double
getValue(int ix, int iy, int iz) const
{
if (ix < nx_ && iy < ny_ && iz < nz_)
{
return grid_[ix][iy][iz];
}
return 0.0; // Handle out of bounds
}
};
// Particle class for particle system simulation
class Particle
{
public:
Particle(double x, double y, double z) : x_(x), y_(y), z_(z) {}
~Particle()
{
std::cout << "Destroying Particle at (" << x_ << ", " << y_ << ", " << z_ << ")" << std::endl;
}
void move(double dx, double dy, double dz)
{
x_ += dx;
y_ += dy;
z_ += dz;
}
void print() const
{
std::cout << "Particle at (" << x_ << ", " << y_ << ", " << z_ << ")" << std::endl;
}
private:
double x_, y_, z_;
};
// Particle system using std::shared_ptr
class ParticleSystem
{
public:
void addParticle(double x, double y, double z)
{
particles_.push_back(std::make_shared<Particle>(x, y, z));
}
void moveParticles()
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> dis(-0.1, 0.1);
for (auto &particle : particles_)
{
particle->move(dis(gen), dis(gen), dis(gen));
}
}
void printParticles() const
{
for (const auto &particle : particles_)
{
particle->print();
}
}
private:
std::vector<std::shared_ptr<Particle>> particles_;
};
// Grid class for unique ownership demonstration
class Grid
{
public:
Grid(int size) : size_(size)
{
std::cout << "Creating Grid of size " << size_ << std::endl;
}
~Grid()
{
std::cout << "Destroying Grid of size " << size_ << std::endl;
}
int getSize() const { return size_; }
private:
int size_;
};
// Simulation class using std::unique_ptr for Grid ownership
class Simulation
{
public:
Simulation(int gridSize) : grid_(std::make_unique<Grid>(gridSize)) {}
void run()
{
std::cout << "Running simulation with grid size " << grid_->getSize() << std::endl;
}
private:
std::unique_ptr<Grid> grid_;
};
int main()
{
Grid g(10);
return 0;
// Demonstrate MemoryManager with raw pointers
{
MemoryManager mm(4, 4, 4);
mm.setValue(2, 2, 2, 3.14);
return 0;
std::cout << "Value at (2, 2, 2): " << mm.getValue(2, 2, 2) << std::endl;
}
std::cout << "\n";
// Demonstrate ParticleSystem with std::shared_ptr
{
ParticleSystem ps;
ps.addParticle(0, 0, 0);
ps.addParticle(1, 1, 1);
std::cout << "Initial particle positions:" << std::endl;
ps.printParticles();
ps.moveParticles();
std::cout << "Particle positions after movement:" << std::endl;
ps.printParticles();
}
std::cout << "\n";
// Demonstrate Simulation with std::unique_ptr
{
Simulation sim(10);
sim.run();
}
return 0;
}
|