Skip to content

Commit 982b6c6

Browse files
author
Sein Lee
authored
Merge branch 'master' into feature/array_list
2 parents a17a5dd + 34c2813 commit 982b6c6

File tree

13 files changed

+123
-220
lines changed

13 files changed

+123
-220
lines changed

ArrayList/ArrayList.h

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ class ArrayList
1717

1818
explicit ArrayList(const std::initializer_list<T> args) noexcept;
1919

20-
ArrayList(const ArrayList& src);
21-
ArrayList(ArrayList&& src) noexcept;
22-
2320
~ArrayList() noexcept;
2421

2522
void add(const T& value);
@@ -34,6 +31,7 @@ class ArrayList
3431

3532
ArrayList& operator=(const ArrayList& rhs);
3633
ArrayList& operator=(ArrayList&& rhs) noexcept;
34+
3735
T& operator[](const size_t index);
3836

3937
void show(const std::string& name) const;
@@ -62,37 +60,13 @@ ArrayList<T>::ArrayList(const std::initializer_list<T> args) noexcept
6260
__init(args);
6361
}
6462

65-
template<typename T>
66-
ArrayList<T>::ArrayList(const ArrayList& src)
67-
{
68-
const size_t size = src.__size;
69-
70-
if (size)
71-
{
72-
const size_t memSize = (sizeof(T) * size);
73-
74-
__malloc(src.__size);
75-
memcpy(__pMemory, src.__pMemory, memSize);
76-
}
77-
}
78-
79-
template<typename T>
80-
ArrayList<T>::ArrayList(ArrayList&& src) noexcept
81-
{
82-
std::swap(__pMemory, src.__pMemory);
83-
84-
__size = src.__size;
85-
__capacity = src.__capacity;
86-
}
87-
8863
template <typename T>
8964
ArrayList<T>::~ArrayList() noexcept
9065
{
9166
__size = 0ULL;
9267
__capacity = 0ULL;
9368

94-
if (__pMemory)
95-
__free();
69+
__free();
9670
}
9771

9872
template <typename T>
@@ -241,8 +215,11 @@ void ArrayList<T>::show(const std::string& name) const
241215
template<typename T>
242216
void ArrayList<T>::__free()
243217
{
244-
delete[] __pMemory;
245-
__pMemory = nullptr;
218+
if (__pMemory)
219+
{
220+
delete[] __pMemory;
221+
__pMemory = nullptr;
222+
}
246223
}
247224

248225
template<typename T>

ArrayList/ArrayList.vcxproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
</PropertyGroup>
2020
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
2121
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
22-
<ConfigurationType>StaticLibrary</ConfigurationType>
22+
<ConfigurationType>Application</ConfigurationType>
2323
<UseDebugLibraries>true</UseDebugLibraries>
2424
<PlatformToolset>v142</PlatformToolset>
2525
<CharacterSet>Unicode</CharacterSet>
2626
</PropertyGroup>
2727
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
28-
<ConfigurationType>StaticLibrary</ConfigurationType>
28+
<ConfigurationType>Application</ConfigurationType>
2929
<UseDebugLibraries>false</UseDebugLibraries>
3030
<PlatformToolset>v142</PlatformToolset>
3131
<WholeProgramOptimization>true</WholeProgramOptimization>
@@ -84,7 +84,7 @@
8484
</Link>
8585
</ItemDefinitionGroup>
8686
<ItemGroup>
87-
<ClCompile Include="dummy.cpp" />
87+
<ClCompile Include="main.cpp" />
8888
</ItemGroup>
8989
<ItemGroup>
9090
<ClInclude Include="ArrayList.h" />

ArrayList/ArrayList.vcxproj.filters

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
<ClInclude Include="ArrayList.h" />
55
</ItemGroup>
66
<ItemGroup>
7-
<ClCompile Include="dummy.cpp" />
7+
<ClCompile Include="main.cpp" />
88
</ItemGroup>
99
</Project>

ArrayList/dummy.cpp

Whitespace-only changes.

ArrayList/main.cpp

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include <vector>
2+
#include "ArrayList.h"
3+
4+
using namespace std;
5+
6+
int main()
7+
{
8+
/*
9+
아래의 코드를 모두 정상적으로 실행할 수 있는 ArrayList를 구현하여라.
10+
ArrayList는 std::vector와 동일한 동작을 수행한다.
11+
12+
구현시 아래의 명세를 모두 만족하시오.
13+
14+
1.
15+
ArrayList의 원소는 임의로 설정 가능하다. (템플릿화 할 것)
16+
17+
2.
18+
ArrayList의 메모리는 항상 단일 블록으로 구성되어 있다. 즉 배열과 같이,
19+
메모리가 쪼개져있지 않고 쭉 이어져 있다.
20+
21+
3.
22+
스마트 포인터를 사용하지 않고, new/delete만을 이용하여 메모리를 관리하여라.
23+
*/
24+
25+
/*
26+
초기 크기 3의 리스트 생성.
27+
이 때 원소의 값은 타입 파라미터의 기본 값으로 설정.
28+
int가 아닌 다른 값(float, double, 심지어 string까지) 모두 기본 값으로 초기화 되어야 한다.
29+
30+
소괄호() 가 아닌 중괄호{} 로 생성자를 호출하는 uniform initializer에 대해 조사하여 글을 정리하여라.
31+
*/
32+
ArrayList<int> list1 { 3 };
33+
34+
// operator[] 를 구현하여 index에 따라 해당 원소를 접근할 수 있도록 하여라.
35+
int& first = list1[0];
36+
first = 10;
37+
38+
// TEST 1
39+
list1.show("list1");
40+
41+
// initializer list를 활용하여 원소 3개를 삽입하며 초기화
42+
// initializer list에 대해 조사하여 글을 정리하여라.
43+
ArrayList<string> list2{ "elem1", "elem2", "elem3" };
44+
45+
// TEST 2
46+
list2.show("list2");
47+
48+
// list의 뒤쪽에 elem4, elem5를 추가
49+
list2.add("elem4");
50+
list2.add("elem5");
51+
52+
// size_t 타입의 element size를 반환. (5가 출력되어야 함)
53+
cout << list2.getSize() << endl;
54+
55+
// 1번째 원소 (elem2)를 삭제한다.
56+
const size_t removeIndex = 1ULL;
57+
list2.remove(removeIndex);
58+
59+
// 4 출력
60+
cout << list2.getSize() << endl;
61+
62+
// elem3 출력
63+
cout << list2.get(1ULL) << endl;
64+
65+
// 1번째 위치에 "elem5" 삽입
66+
const size_t insertIndex = 1ULL;
67+
list2.insert(insertIndex, "elem5");
68+
69+
// "elem5" 출력
70+
cout << list2[1] << endl;
71+
72+
// "elem3" 출력
73+
cout << list2[2] << endl << endl;
74+
75+
// TEST 3
76+
const int list1Size = int(list1.getSize());
77+
ArrayList<int> list3{ list1Size };
78+
{
79+
memcpy(list3.getRaw(), list1.getRaw(), sizeof(int) * list1Size);
80+
list3.show("list3");
81+
}
82+
83+
// TEST 4
84+
ArrayList<int> list4;
85+
{
86+
list4.insert(0ULL, 5);
87+
list4.show("list4");
88+
}
89+
90+
// TEST 5
91+
ArrayList<string> list5 { 6 };
92+
93+
return 0;
94+
}

Client/Client.vcxproj

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,6 @@
7070
<ClCompile Include="main.cpp" />
7171
</ItemGroup>
7272
<ItemGroup>
73-
<ProjectReference Include="..\ArrayList\ArrayList.vcxproj">
74-
<Project>{5f928571-411f-4f81-b1a4-e18f15efdda4}</Project>
75-
</ProjectReference>
76-
<ProjectReference Include="..\StaticCalculator\StaticCalculator.vcxproj">
77-
<Project>{b2be17ee-02ae-4158-b453-12b7e517abf0}</Project>
78-
</ProjectReference>
7973
<ProjectReference Include="..\Var\Var.vcxproj">
8074
<Project>{2d566859-9659-40ea-bb45-a9add4a74ccf}</Project>
8175
</ProjectReference>

Client/main.cpp

Lines changed: 5 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,14 @@
22

33
using namespace std;
44

5-
static int varMain();
6-
static int staticCalculatorMain();
7-
static int arrayListMain();
5+
int varMain();
6+
int staticCalculatorMain();
87

98
int main()
109
{
11-
int (*fp[])() =
12-
{
13-
varMain,
14-
staticCalculatorMain,
15-
arrayListMain
16-
};
17-
18-
return fp[2]();
10+
int (*fp[2])() = { varMain, staticCalculatorMain };
11+
12+
return fp[1]();
1913
}
2014

2115
// --------------------------- Var
@@ -179,99 +173,5 @@ int staticCalculatorMain()
179173
// 크기 7인 배열 생성
180174
int arr[calcResult2]{};
181175

182-
return 0;
183-
}
184-
185-
// --------------------------- ArrayList
186-
187-
#include "../ArrayList/ArrayList.h"
188-
189-
int arrayListMain()
190-
{
191-
/*
192-
아래의 코드를 모두 정상적으로 실행할 수 있는 ArrayList를 구현하여라.
193-
ArrayList는 std::vector와 동일한 동작을 수행한다.
194-
195-
구현시 아래의 명세를 모두 만족하시오.
196-
197-
1.
198-
ArrayList의 원소는 임의로 설정 가능하다. (템플릿화 할 것)
199-
200-
2.
201-
ArrayList의 메모리는 항상 단일 블록으로 구성되어 있다. 즉 배열과 같이,
202-
메모리가 쪼개져있지 않고 쭉 이어져 있다.
203-
204-
3.
205-
스마트 포인터를 사용하지 않고, new/delete만을 이용하여 메모리를 관리하여라.
206-
*/
207-
208-
/*
209-
초기 크기 3의 리스트 생성.
210-
이 때 원소의 값은 타입 파라미터의 기본 값으로 설정.
211-
int가 아닌 다른 값(float, double, 심지어 string까지) 모두 기본 값으로 초기화 되어야 한다.
212-
213-
소괄호() 가 아닌 중괄호{} 로 생성자를 호출하는 uniform initializer에 대해 조사하여 글을 정리하여라.
214-
*/
215-
ArrayList<int> list1{ 3 };
216-
217-
// operator[] 를 구현하여 index에 따라 해당 원소를 접근할 수 있도록 하여라.
218-
int& first = list1[0];
219-
first = 10;
220-
221-
// TEST 1
222-
list1.show("list1");
223-
224-
// initializer list를 활용하여 원소 3개를 삽입하며 초기화
225-
// initializer list에 대해 조사하여 글을 정리하여라.
226-
ArrayList<string> list2{ "elem1", "elem2", "elem3" };
227-
228-
// TEST 2
229-
list2.show("list2");
230-
231-
// list의 뒤쪽에 elem4, elem5를 추가
232-
list2.add("elem4");
233-
list2.add("elem5");
234-
235-
// size_t 타입의 element size를 반환. (5가 출력되어야 함)
236-
cout << list2.getSize() << endl;
237-
238-
// 1번째 원소 (elem2)를 삭제한다.
239-
const size_t removeIndex = 1ULL;
240-
list2.remove(removeIndex);
241-
242-
// 4 출력
243-
cout << list2.getSize() << endl;
244-
245-
// elem3 출력
246-
cout << list2.get(1ULL) << endl;
247-
248-
// 1번째 위치에 "elem5" 삽입
249-
const size_t insertIndex = 1ULL;
250-
list2.insert(insertIndex, "elem5");
251-
252-
// "elem5" 출력
253-
cout << list2[1] << endl;
254-
255-
// "elem3" 출력
256-
cout << list2[2] << endl << endl;
257-
258-
// TEST 3
259-
const int list1Size = int(list1.getSize());
260-
ArrayList<int> list3{ list1Size };
261-
{
262-
memcpy(list3.getRaw(), list1.getRaw(), sizeof(int) * list1Size);
263-
list3.show("list3");
264-
}
265-
266-
// TEST 4
267-
ArrayList<int> list4;
268-
{
269-
list4.insert(0ULL, 5);
270-
list4.show("list4");
271-
}
272-
273-
// TEST 5
274-
ArrayList<string> list5{ 6 };
275-
276176
return 0;
277177
}

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# CPlusStudy
22

3-
👀 <a href="md/initializer_list.md">initializer_list</a> <br>
4-
👀 <a href="md/uniform_initialization.md">uniform_initialization</a> <br>
5-
👀 <a href="md/rtti.md">rtti</a> <br>
6-
👀 <a href="md/constexpr.md">constexpr</a> <br>
7-
👀 <a href="md/static_assert.md">static_assert</a> <br>
8-
👀 <a href="md/rvalue_reference.md">rvalue reference & forwarding reference</a>
3+
👀 <a href="./md/initializer_list.md">initializer_list</a> <br>
4+
👀 <a href="./md/uniform_initialization.md">uniform_initialization</a> <br>
5+
👀 <a href="./md/rtti.md">rtti</a> <br>
6+
👀 <a href="./md/constexpr.md">constexpr</a> <br>
7+
👀 <a href="./md/static_assert.md">static_assert</a> <br>
8+
👀 <a href="./md/rvalue_reference.md">rvalue reference & forwarding reference</a> <br>
9+
👀 <a href="./md/volatile.md">volatile</a>

StaticCalculator/StaticCalculator.vcxproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,6 @@
147147
<ClInclude Include="OperationType.h" />
148148
<ClInclude Include="StaticCalculator.h" />
149149
</ItemGroup>
150-
<ItemGroup>
151-
<ClCompile Include="dummy.cpp" />
152-
</ItemGroup>
153150
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
154151
<ImportGroup Label="ExtensionTargets">
155152
</ImportGroup>
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<Filter Include="리소스 파일">
5+
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
6+
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
7+
</Filter>
8+
</ItemGroup>
39
<ItemGroup>
410
<ClInclude Include="Number.h" />
511
<ClInclude Include="OperationType.h" />
612
<ClInclude Include="StaticCalculator.h" />
713
</ItemGroup>
8-
<ItemGroup>
9-
<ClCompile Include="dummy.cpp" />
10-
</ItemGroup>
1114
</Project>

0 commit comments

Comments
 (0)