-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleVector.h
145 lines (115 loc) · 2.46 KB
/
SimpleVector.h
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
#pragma once
#include <iostream>
#include <algorithm>
constexpr int INCREASE_CAPACITY = 5;
template<typename T>
class SimpleVector
{
public :
SimpleVector();
SimpleVector(int capacity);
SimpleVector(const SimpleVector& other);
T getData(int num) { return data[num]; }
private :
void resize(int capacity);
public:
void push_back(const T& value);
void pop_back();
int size();
int capacity();
void sortData();
~SimpleVector();
private:
T* data;
int currentSize;
int currentCapacity;
};
template<typename T>
inline SimpleVector<T>::SimpleVector()
{
currentSize = 0;
currentCapacity = 10;
data = new T[currentCapacity];
std::cout << "기본 생성자 생성" << std::endl;
}
template<typename T>
inline SimpleVector<T>::SimpleVector(int capacity)
{
currentSize = 0;
currentCapacity = capacity;
data = new T[currentCapacity];
std::cout << "생성자 생성 크기 : " << capacity << std::endl;
}
template<typename T>
inline SimpleVector<T>::SimpleVector(const SimpleVector& other)
{
currentSize = other.currentSize;
currentCapacity = other.currentSize + INCREASE_CAPACITY;
data = new T[currentCapacity];
for (int i = 0; i < currentSize; i++)
{
data[i] = other.data[i];
}
std::cout << "생성자 복사" << std::endl;
}
template<typename T>
inline void SimpleVector<T>::resize(int capacity)
{
if (capacity <= currentCapacity)
{
std::cout << "Resize return" << std::endl;
return;
}
T* resizeData = new T[capacity];
for (int i = 0; i < currentSize; i++)
{
resizeData[i] = data[i];
}
delete[] data;
currentCapacity = capacity;
data = resizeData;
}
template<typename T>
inline void SimpleVector<T>::push_back(const T& value)
{
if (currentSize == currentCapacity)
{
resize(currentCapacity + INCREASE_CAPACITY);
}
data[currentSize] = value;
std::cout << "Push Value : " << data[currentSize] << std::endl;
currentSize++;
}
template<typename T>
inline void SimpleVector<T>::pop_back()
{
if (currentSize < 0)
{
currentSize = 0;
return;
}
std::cout << "Pop Value : " << data[currentSize] << std::endl;
currentSize--;
}
template<typename T>
inline int SimpleVector<T>::size()
{
return currentSize;
}
template<typename T>
inline int SimpleVector<T>::capacity()
{
return currentCapacity;
}
template<typename T>
inline void SimpleVector<T>::sortData()
{
std::sort(data, data + currentSize);
}
template<typename T>
inline SimpleVector<T>::~SimpleVector()
{
if (data != NULL)
delete[] data;
std::cout << "소멸자 호출" << std::endl;
}