Repository navigation
Expand file tree
/
Copy pathArray.h
More file actions
185 lines (152 loc) · 2.77 KB
/
Copy pathArray.h
File metadata and controls
185 lines (152 loc) · 2.77 KB
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
#ifndef Array_H
#define Array_H
#include <cstring>
#include <algorithm>
#include <inttypes.h>
#include <stdlib.h>
#include "Utils.h"
template<class T>
class Array {
T *_records;
size_t _size;
size_t _capacity;
size_t _extend;
public:
Array (void):
_records(0), _size(0), _capacity(0), _extend(100)
{
}
Array (size_t c, size_t e = 100):
_records(0), _size(0), _capacity(c), _extend(e)
{
_records = new T[_capacity];
}
~Array (void)
{
if (_records) {
delete[] _records;
_records = 0;
}
}
Array (const Array& a)
{
// ZAMAN_START(Array_Copy);
// LOG("Copying array of size %d %d", a._size, a._capacity);
_size = a._size;
_capacity = a._capacity;
_extend = a._extend;
_records = new T[a._capacity];
std::copy(a._records, a._records + a._size, _records);
// ZAMAN_END(Array_Copy);
}
Array(Array&& a): Array()
{
swap(*this, a);
}
Array& operator= (Array a)
{
swap(*this, a);
return *this;
}
friend void swap(Array& a, Array& b) // nothrow
{
using std::swap;
swap(a._records, b._records);
swap(a._size, b._size);
swap(a._capacity, b._capacity);
swap(a._extend, b._extend);
}
public: // for range based loops
T *begin() const
{
return _records;
}
T *end() const
{
return _records + _size;
}
public:
void realloc (size_t sz)
{
// ZAMAN_START(Realloc);
_capacity = sz + _extend;
if (_size > _capacity) _size = _capacity;
if (!_capacity) { _records = nullptr; return; }
T *tmp = new T[_capacity];
std::copy(_records, _records + _size, tmp);
delete[] _records;
_records = tmp;
// ZAMAN_END(Realloc);
}
void resize (size_t sz)
{
if (sz > _capacity)
realloc(sz);
_size = sz;
}
// Add defualt element or use allocated
void add ()
{
if (_size == _capacity)
realloc(_capacity);
_size++;
}
// add element, realloc if needed
void add (const T &t)
{
if (_size == _capacity)
realloc(_capacity);
_records[_size++] = t;
}
// add array, realloc if needed
void add (const T *t, size_t sz)
{
if (_size + sz > _capacity)
realloc(_capacity + sz);
std::copy(t, t + sz, _records + _size);
_size += sz;
}
size_t size (void) const
{
return _size;
}
size_t capacity (void) const
{
return _capacity;
}
T *data (void)
{
return _records;
}
const T *data (void) const
{
return _records;
}
T &operator[] (size_t i)
{
assert(i < _size);
return _records[i];
}
const T &operator[] (size_t i) const
{
assert(i < _size);
return _records[i];
}
void set_extend (size_t s)
{
_extend = s;
}
void removeFirstK (size_t k)
{
ZAMAN_START(RemoveFirstK);
if (k < _size) {
std::copy(_records + k, _records + _size, _records);
_size -= k;
} else {
_size = 0;
}
ZAMAN_END(RemoveFirstK);
}
};
// typedef Array<uint8_t> ByteArray;
#endif