-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.h
272 lines (206 loc) · 3.78 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#ifndef __ARRAY_H__
#define __ARRAY_H__
#include<iostream>
#include<stdlib.h>
#include<assert.h>
using namespace std;
template<class T>
class array
{
public:
array();
array(int size);
array(T * a, int size);
array(array<T> * a);
array(array<T> & a);
virtual ~array();
void set_element(T d, int index);
T get_element(int index);
T& operator[](int i);
void grow(int howmuch);
void shrink(int howmuch);
void read(istream & in);
void print(ostream & out);
void copy_in(T * a, int size);
void copy_in(array<T> * a);
void operator=(array<T> & a);
array * clone();
void sort(int ascending=1);
int get_size(){return _size;}
T* get_data(){return _data;}
template<class H>
friend void operator>>(istream & in, array<H> & a);
template<class H>
friend void operator<<(ostream & out, array<H> & a);
protected:
void allocate_data();
T * _data;
int _size;
};
template<class T>
array<T>::array()
{
_size = 1;
allocate_data();
}
template<class T>
array<T>::array(int size)
{
_size = size;
allocate_data();
}
template<class T>
array<T>::array(T * a, int size)
{
_data = 0;
copy_in(a,size);
}
template<class T>
array<T>::array(array<T> * a)
{
_data = 0;
copy_in(a);
}
template<class T>
array<T>::array(array<T> & a)
{
_data = 0;
copy_in(a._data, a._size);
}
template<class T>
array<T>::~array()
{
delete [] _data;
}
template<class T>
void array<T>::allocate_data()
{
_data = new T[_size];
assert(_data);
}
template<class T>
T& array<T>::operator[](int i)
{
if (i >= _size)
grow(i - _size + 1);
assert(i >= 0);
return _data[i];
}
template<class T>
void array<T>::set_element(T d, int index)
{
if (index >= _size)
grow(index - _size + 1);
assert(index >= 0);
_data[index] = d;
}
template<class T>
T array<T>::get_element(int index)
{
assert(index >= 0 && index < _size);
return _data[index];
}
template<class T>
void array<T>::grow(int howmuch)
{
T * new_data = new T[_size + howmuch];
assert(new_data);
int i;
for ( i = 0; i < _size && i < _size + howmuch; i++)
new_data[i] = _data[i];
_size += howmuch;
delete [] _data;
_data = new_data;
}
template<class T>
void array<T>::shrink(int howmuch)
{
grow(-howmuch);
}
template<class T>
void array<T>::read(istream & in)
{
int num,i;
in>>num;
grow(num - _size);
for (i = 0; i < _size; i++)
in>>_data[i];
}
template<class T>
void operator>>(istream & in, array<T> & a)
{
int num, i;
in>>num;
a.grow(num - a._size);
for (i = 0; i < a._size; i++)
in>>a._data[i];
}
template<class T>
void array<T>::print(ostream & out)
{
int i;
out<<_size;
for (i = 0; i < _size; i++)
out<<" "<<_data[i];
out<<endl;
}
template<class T>
void operator<<(ostream & out, array<T> & a)
{
int i;
out<<a._size;
for (i = 0; i < a._size; i++)
out<<" "<<a._data[i];
out<<endl;
}
template<class T>
void array<T>::copy_in(T * a, int size)
{
int i;
if (_data)
delete [] _data;
_size = size;
allocate_data();
for (i = 0; i < _size; i++)
_data[i] = a[i];
}
template<class T>
void array<T>::copy_in(array<T> * a)
{
copy_in(a->_data, a->_size);
}
template<class T>
void array<T>::operator=(array<T> & a)
{
copy_in(a._data, a._size);
}
template<class T>
array<T> * array<T>::clone()
{
array<T> * r = new array<T>(this);
assert(r);
return r;
}
template<class T>
void array<T>::sort(int ascending)
{
int i,j;
T temp;
int need_swap;
for (i = 0; i < _size - 1; i++)
for (j = i + 1; j < _size; j++)
{
need_swap = 0;
if (ascending && _data[i] > _data[j])
need_swap = 1;
if (!ascending && _data[i] > _data[j])
need_swap = 1;
if (need_swap)
{
temp = _data[i];
_data[i] = _data[j];
_data[j] = temp;
}
}
}
#endif