Skip to content

Commit 6f2ad5a

Browse files
committed
Update virtual machine.
1 parent f8984b9 commit 6f2ad5a

File tree

6 files changed

+393
-65
lines changed

6 files changed

+393
-65
lines changed

Algorithms/Dictionary.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ namespace Algorithm
200200
Node* node;;
201201

202202
Stack<Node*> stack;
203-
stack.push(mRoot);
203+
stack.Push(mRoot);
204204

205205
int currentPos = 0;
206206
while (stack.count() != 0)
@@ -209,11 +209,11 @@ namespace Algorithm
209209

210210
if (node->mLeft != nullptr)
211211
{
212-
stack.push(node->mLeft);
212+
stack.Push(node->mLeft);
213213
}
214214
if (node->mRight != nullptr)
215215
{
216-
stack.push(node->mRight);
216+
stack.Push(node->mRight);
217217
}
218218

219219
keyArray[currentPos] = node->mKey;

Algorithms/Stack.h

+22-22
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ namespace Algorithm {
66
class Stack
77
{
88
private:
9-
static const int DEFAULTCAPACITY;
9+
static const int DEFAULTCapacity;
1010
public:
1111
Stack();
1212
~Stack();
1313

14-
void push(const T& value);
15-
T pop();
16-
void clear();
14+
void Push(const T& value);
15+
T Pop();
16+
void Clear();
1717

18-
int count()const;
19-
int capacity()const;
18+
int Count()const;
19+
int Capacity()const;
2020

2121
private:
22-
bool initialize(int size);
23-
bool reallocation();
24-
bool isCapacityEnough()const;
22+
bool Initialize(int size);
23+
bool Reallocation();
24+
bool IsCapacityEnough()const;
2525

2626
T* mStack;
2727
int mCount;
@@ -31,7 +31,7 @@ namespace Algorithm {
3131
//-------------------define
3232

3333
template<typename T>
34-
const int Stack<T>::DEFAULTCAPACITY = 1;
34+
const int Stack<T>::DEFAULTCapacity = 1;
3535

3636
template<typename T>
3737
inline Stack<T>::Stack()
@@ -41,21 +41,21 @@ namespace Algorithm {
4141
mCapacity = 0;
4242
mStack = nullptr;
4343

44-
initialize(DEFAULTCAPACITY);
44+
Initialize(DEFAULTCapacity);
4545
}
4646

4747
template<typename T>
4848
inline Stack<T>::~Stack()
4949
{
50-
clear();
50+
Clear();
5151
}
5252

5353
template<typename T>
54-
inline void Stack<T>::push(const T & value)
54+
inline void Stack<T>::Push(const T & value)
5555
{
56-
if (isCapacityEnough() == false)
56+
if (IsCapacityEnough() == false)
5757
{
58-
reallocation();
58+
Reallocation();
5959
}
6060

6161
mStack[mCount] = value;
@@ -65,7 +65,7 @@ namespace Algorithm {
6565
}
6666

6767
template<typename T>
68-
inline T Stack<T>::pop()
68+
inline T Stack<T>::Pop()
6969
{
7070
if (mCount <= 0)
7171
{
@@ -80,7 +80,7 @@ namespace Algorithm {
8080
}
8181

8282
template<typename T>
83-
inline void Stack<T>::clear()
83+
inline void Stack<T>::Clear()
8484
{
8585
if (mStack != nullptr)
8686
{
@@ -96,19 +96,19 @@ namespace Algorithm {
9696
}
9797

9898
template<typename T>
99-
inline int Stack<T>::count() const
99+
inline int Stack<T>::Count() const
100100
{
101101
return mCount;
102102
}
103103

104104
template<typename T>
105-
inline int Stack<T>::capacity() const
105+
inline int Stack<T>::Capacity() const
106106
{
107107
return mCapacity;
108108
}
109109

110110
template<typename T>
111-
inline bool Stack<T>::initialize(int size)
111+
inline bool Stack<T>::Initialize(int size)
112112
{
113113
if (size <= 0)
114114
{
@@ -125,7 +125,7 @@ namespace Algorithm {
125125
}
126126

127127
template<typename T>
128-
inline bool Stack<T>::reallocation()
128+
inline bool Stack<T>::Reallocation()
129129
{
130130
if (mCapacity == 0)
131131
{
@@ -149,7 +149,7 @@ namespace Algorithm {
149149
}
150150

151151
template<typename T>
152-
inline bool Stack<T>::isCapacityEnough() const
152+
inline bool Stack<T>::IsCapacityEnough() const
153153
{
154154
return mCount < mCapacity;
155155
}

Algorithms/String.h

+29-10
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,19 @@ namespace Algorithm
2525
friend bool operator==(const StringBase<T>& lhs, const StringBase<T>& rhs);
2626

2727
StringBase();
28-
StringBase(const StringBase& value);
29-
StringBase(const T* value, unsigned int size);
30-
StringBase(const T* value);
28+
StringBase(const StringBase& value);
29+
StringBase(const T* value, unsigned int size);
30+
StringBase(const T* value);
31+
StringBase(T value);
3132
~StringBase();
3233

33-
T operator[](int index) const;
34-
T operator[](unsigned int index) const;
34+
T& operator[](int index);
35+
const T& operator[](int index) const;
36+
T& operator[](unsigned int index);
37+
const T& operator[](unsigned int index) const;
3538
StringBase<T>& operator=(const StringBase<T>& rhs);
3639
StringBase<T>& operator=(const T* rhs);
37-
//bool operator==(const T* rhs)const;
38-
bool operator==(const StringBase<T>& rhs)const;
39-
//bool operator!=(const T* rhs)const;
40+
bool operator==(const StringBase<T>& rhs)const;
4041
bool operator!=(const StringBase<T>& rhs)const;
4142

4243
void Add(T value);
@@ -83,19 +84,37 @@ namespace Algorithm
8384
}
8485
}
8586

87+
template<typename T>
88+
inline StringBase<T>::StringBase(T value)
89+
{
90+
m_string.Add(value);
91+
}
92+
8693
template<typename T>
8794
Algorithm::StringBase<T>::~StringBase()
8895
{
8996
}
9097

9198
template<typename T>
92-
T Algorithm::StringBase<T>::operator[](int index)const
99+
inline T& StringBase<T>::operator[](int index)
100+
{
101+
return const_cast<T&>(const_cast<const StringBase<T>*>(this)[index]);
102+
}
103+
104+
template<typename T>
105+
inline const T& Algorithm::StringBase<T>::operator[](int index)const
93106
{
94107
return m_string[index];
95108
}
96109

97110
template<typename T>
98-
inline T StringBase<T>::operator[](unsigned int index)const
111+
inline T& StringBase<T>::operator[](unsigned int index)
112+
{
113+
return const_cast<T&>(const_cast<const StringBase<T>*>(this)[index]);
114+
}
115+
116+
template<typename T>
117+
inline const T& StringBase<T>::operator[](unsigned int index)const
99118
{
100119
return m_string[index];
101120
}

Algorithms/Vector.h

+10-3
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ namespace Algorithm
2929

3030
bool IsExist(const T& element)const;
3131

32-
T& operator[](int index)const;
32+
T& operator[](int index);
33+
const T& operator[](int index)const;
3334
Vector<T>& operator=(const Vector& rhs);
3435

3536
//For Algorithm
36-
bool Swap(const int lhs, const int rhs);
37+
//bool Swap(const int lhs, const int rhs);
3738
T* Begin();
3839
const T* Begin()const;
3940
T* End();
@@ -179,7 +180,13 @@ namespace Algorithm
179180
}
180181

181182
template<typename T>
182-
inline T& Vector<T>::operator[](int index) const
183+
inline T& Vector<T>::operator[](int index)
184+
{
185+
return const_cast<T&>(const_cast<const Vector<T>*>(this)[index]);
186+
}
187+
188+
template<typename T>
189+
inline const T& Vector<T>::operator[](int index) const
183190
{
184191
assert(index >= 0 && index < m_count);
185192
return m_content[index];

0 commit comments

Comments
 (0)