-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraining5_3.cpp
More file actions
333 lines (291 loc) · 9.69 KB
/
training5_3.cpp
File metadata and controls
333 lines (291 loc) · 9.69 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// C++ 스타일의 캐스팅 (static_cast 등등)
// 4가지 캐스팅 존재
// static_cast : 우리가 흔히 생각하는,
// 언어적 차원에서 지원하는 일반적인 타입 변환
// const_cast : 객체의
// 상수성(const) 를 없애는 타입 변환.쉽게 말해 const int 가 int 로 바뀐다.
// dynamic_cast : 파생 클래스 사이에서의 다운 캐스팅
// reinterpret_cast : 위험을 감수하고 하는 캐스팅으로 서로 관련이 없는 포인터들 사이의 캐스팅 등
// 디폴트 인자 (default argument)
// N 차원 배열의 제작
/*
a[2][3][4] 같은 것이 있을 떄
a.[](2) -> x
x.[](3) -> y
y.[](4) -> (int)가 나와야한다.x와 y는 int 일 수 없다->wrapper클래스 Int를 적용한다.
*/
#include <iostream>
namespace MyArray
{
class Array;
class Int; //미리 선언을 해야 friend Int등으로 사용할 수 있다.
class Array
{
friend Int;
const int dim; // 몇 차원 배열 인지
int *size; // size[0] * size[1] * ... * size[dim - 1] 짜리 배열이다.
struct Address
{
int level;
// 맨 마지막 레벨(dim - 1 레벨) 은 데이터 배열을 가리키고, 그 위 상위
// 레벨에서는 다음 Address 배열을 가리킨다.
void *next;
};
Address *top;
public:
class Iterator
{
int *location; //가리키고있는 인덱스 [1][2][3]을 가리킬 때 location = {1,2,3}
Array *arr; //타깃으로 하는 Array
friend Int;
public:
Iterator(Array *arr, int *loc = NULL) : arr(arr)
{
location = new int[arr->dim];
for (int i = 0; i != arr->dim; i++)
location[i] = (loc != NULL ? loc[i] : 0);
}
Iterator(const Iterator &itr) : arr(itr.arr)
{
location = new int[arr->dim];
for (int i = 0; i != arr->dim; i++)
location[i] = itr.location[i];
}
~Iterator() { delete[] location; }
// 다음 원소를 가리키게 된다.
Iterator &operator++()
{
if (location[0] >= arr->size[0])
return (*this);
bool carry = false; // 받아 올림이 있는지
int i = arr->dim - 1;
do
{
// 어차피 다시 돌아온다는 것은 carry 가 true
// 라는 의미 이므로 ++ 을 해야 한다.
location[i]++;
if (location[i] >= arr->size[i] && i >= 1)
{
// i 가 0 일 경우 0 으로 만들지 않는다 (이러면 begin 과 중복됨)
location[i] -= arr->size[i];
carry = true;
i--;
}
else
carry = false;
} while (i >= 0 && carry);
return (*this);
}
Iterator &operator=(const Iterator &itr)
{
arr = itr.arr;
location = new int[itr.arr->dim];
for (int i = 0; i != arr->dim; i++)
location[i] = itr.location[i];
return (*this);
}
Iterator operator++(int)
{
Iterator itr(*this);
++(*this);
return itr;
}
bool operator!=(const Iterator &itr)
{
if (itr.arr->dim != arr->dim)
return true;
for (int i = 0; i != arr->dim; i++)
{
if (itr.location[i] != location[i])
return true;
}
return false;
}
Int operator*();
};
friend Iterator;
Array(int dim, int *array_size) : dim(dim)
{
size = new int[dim];
for (int i = 0; i < dim; i++)
size[i] = array_size[i];
top = new Address;
top->level = 0;
initialize_address(top);
}
Array(const Array &arr) : dim(arr.dim)
{
size = new int[dim];
for (int i = 0; i < dim; i++)
size[i] = arr.size[i];
top = new Address;
top->level = 0;
initialize_address(top);
// 내용물 복사
copy_address(top, arr.top);
}
void copy_address(Address *dst, Address *src)
{
if (dst->level == dim - 1)
{
for (int i = 0; i < size[dst->level]; ++i)
static_cast<int *>(dst->next)[i] = static_cast<int *>(src->next)[i];
return;
}
for (int i = 0; i != size[dst->level]; i++)
{
Address *new_dst = static_cast<Address *>(dst->next) + i;
Address *new_src = static_cast<Address *>(src->next) + i;
copy_address(new_dst, new_src);
}
}
// address 를 초기화 하는 함수이다. 재귀 호출로 구성되어 있다.
void initialize_address(Address *current)
{
if (!current)
return;
if (current->level == dim - 1)
{
current->next = new int[size[current->level]];
return;
}
current->next = new Address[size[current->level]];
for (int i = 0; i != size[current->level]; i++)
{
(static_cast<Address *>(current->next) + i)->level = current->level + 1;
initialize_address(static_cast<Address *>(current->next) + i);
}
}
void delete_address(Address *current)
{
if (!current)
return;
for (int i = 0; current->level < dim - 1 && i < size[current->level]; i++)
{
delete_address(static_cast<Address *>(current->next) + i);
}
if (current->level == dim - 1)
{
delete[] static_cast<int *>(current->next);
}
delete[] static_cast<Address *>(current->next);
}
Int operator[](const int index);
~Array()
{
delete_address(top);
delete[] size;
}
Iterator begin()
{
int *arr = new int[dim];
for (int i = 0; i != dim; i++)
arr[i] = 0;
Iterator temp(this, arr);
delete[] arr;
return temp;
}
Iterator end()
{
int *arr = new int[dim];
arr[0] = size[0];
for (int i = 1; i < dim; i++)
arr[i] = 0;
Iterator temp(this, arr);
delete[] arr;
return temp;
}
};
class Int
{
void *data;
int level;
Array *array;
public:
Int(int index, int _level = 0, void *_data = NULL, Array *_array = NULL)
: level(_level), data(_data), array(_array)
{
if (_level < 1 || index >= array->size[_level - 1])
{
data = NULL;
return;
}
if (level == array->dim)
{
// 이제 data 에 우리의 int 자료형을 저장하도록 해야 한다.
data = static_cast<void *>((
static_cast<int *>(static_cast<Array::Address *>(data)->next) + index));
}
else
{
// 그렇지 않을 경우 data 에 그냥 다음 addr 을 넣어준다.
data = static_cast<void *>(static_cast<Array::Address *>(
static_cast<Array::Address *>(data)->next) +
index);
}
};
Int(const Int &i) : data(i.data), level(i.level), array(i.array) {}
operator int()
{
if (data)
return *static_cast<int *>(data);
return 0;
}
Int &operator=(const int &a)
{
if (data)
*static_cast<int *>(data) = a;
return *this;
}
Int operator[](const int index)
{
if (!data)
return 0;
return Int(index, level + 1, data, array);
}
};
Int Array::operator[](const int index)
{
return Int(index, 1, static_cast<void *>(top), this);
}
Int Array::Iterator::operator*()
{
Int start = arr->operator[](location[0]);
for (int i = 1; i <= arr->dim - 1; i++)
{
start = start.operator[](location[i]);
}
return start;
}
} // namespace MyArray
int main()
{
int size[] = {2, 3, 4};
MyArray::Array arr(3, size);
MyArray::Array::Iterator itr = arr.begin();
for (int i = 0; itr != arr.end(); itr++, i++)
(*itr) = i;
for (itr = arr.begin(); itr != arr.end(); itr++)
std::cout << *itr << std::endl;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
for (int k = 0; k < 4; k++)
{
arr[i][j][k] = (i + 1) * (j + 1) * (k + 1) + arr[i][j][k];
}
}
}
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
for (int k = 0; k < 4; k++)
{
std::cout << i << " " << j << " " << k << " " << arr[i][j][k]
<< std::endl;
}
}
}
}