forked from HIT-SCIR/ltp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyVector.h
94 lines (76 loc) · 1.52 KB
/
MyVector.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
#ifndef _MY_VECTOR_
#define _MY_VECTOR_
#pragma once
#include <exception>
#include <iostream>
using namespace std;
template <typename Ty>
class MyVector {
public:
Ty *m_data; // The last one is not used!
public:
int m_capacity;
int m_size;
public:
MyVector() : m_data(0), m_capacity(0), m_size(0) {
}
~MyVector() {
if (m_data) {
delete [] m_data;
}
}
bool empty() {
return m_size == 0;
}
int capacity() const {
return m_capacity;
}
int size() const {
return m_size;
}
void clear() {
m_size = 0;
}
int resize(int _size) {
if (_size < 0) {
cerr << "MyVector::resize() err: new size is: " << _size << endl;
return -1;
}
if (_size <= m_capacity) {
m_size = _size;
}
else { // _size > m_capacity
int new_capacity = 2 * _size;
try {
if (m_data) delete [] m_data;
m_capacity = 0;
m_size = 0;
m_data = 0;
m_data = new Ty[new_capacity + 1]; // The last one is not used!
} catch (const exception &e) {
cerr << "MyVector::resize( " << new_capacity + 1 << " ) exception" << endl;
cerr << "element size: " << sizeof(Ty) << endl;
cerr << e.what() << endl;
return -1;
}
m_capacity = new_capacity;
m_size = _size;
}
return 0;
}
Ty *begin() {
return m_data;
}
const Ty *begin() const {
return m_data;
}
Ty &operator[](int pos) {
if (pos < 0 || pos >= size()) return m_data[size()];
return m_data[pos];
}
const Ty &operator[](int pos) const {
if (pos < 0 || pos >= size()) return m_data[size()];
return m_data[pos];
}
};
#endif