-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy patharray.h
177 lines (145 loc) · 3.24 KB
/
array.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
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
//
// Created by light on 19-10-31.
//
#ifndef ALG_ARRAY_H
#define ALG_ARRAY_H
#include <cassert>
#include <iostream>
using namespace std;
// 动态数组
template<class T>
class Array {
private:
T *data;
int size;
int capacity;
void resize(int newCapacity) {
T *newData = new T[newCapacity];
for (int i = 0; i < size; i++) {
newData[i] = data[i];
}
data = newData;
capacity = newCapacity;
}
void init(int capacity) {
this->data = new T[capacity];
this->size = 0;
this->capacity = capacity;
}
public:
Array(int capacity) {
init(capacity);
}
Array() {
// this->Array(5); error!!!
// new(this)Array(5);
init(5);
}
~Array() {
delete[]data;
size=0;
capacity=0;
cout << "deconstruct\n";
}
int getSize() {
return size;
}
int getCapacity() {
return capacity;
}
bool isEmpty() {
return size == 0;
}
// add O(n)
void add(int index, T e) {
assert(index >= 0 && index <= size);
if (size == capacity) {
resize(2 * capacity); // 均摊复杂度O(1)
}
for (int i = size - 1; i >= index; i--) {
data[i + 1] = data[i];
}
data[index] = e;
size++;
}
// addfirst O(n)
void addFirst(T e) {
add(0, e);
}
// addlast O(1)
void addLast(T e) {
add(size, e);
}
// get O(1)
T get(int index) {
assert(index >= 0 && index < size);
return data[index];
}
// set O(1)
void set(int index, T e) {
assert(index >= 0 && index < size);
data[index] = e;
}
// contains O(n)
bool contains(T e) {
for (int i = 0; i < size; i++)
if (data[i] == e) return true;
return false;
}
// O(n)
int find(T e) {
for (int i = 0; i < size; ++i) {
if (data[i] == e) {
return i;
}
}
return -1;
}
// O(n)
T remove(int index) {
assert(index >= 0 && index < size);
T ret = data[index];
for (int i = index + 1; i < size; ++i) {
data[i - 1] = data[i];
}
size--;
if (size == capacity / 4 && capacity / 2 != 0) resize(capacity / 2); // 防止复杂度震荡 lazy
return ret;
}
// O(n)
T removeFirst() {
return remove(0);
}
// O(1)
T removeLast() {
return remove(size - 1);
}
// O(n)
void removeElement(T e) {
int index = find(e);
if (index != -1) {
remove(index);
}
}
// O(1)
T getFirst() {
return get(0);
}
// O(1)
T getLast() {
return get(size - 1);
}
// 打印输出
friend ostream &operator<<(ostream &out, Array &array) {
out << "Array: size = " << array.getSize() << ", capacity = " << array.getCapacity() << endl;
out << "[";
for (int i = 0; i < array.size; i++) {
out << array.data[i];
if (i != array.size - 1)
out << ", ";
}
out << "]" << endl;
return out;
}
};
#endif //ALG_ARRAY_H