Repository navigation
Expand file tree
/
Copy pathCircularArray.h
More file actions
173 lines (145 loc) · 2.89 KB
/
Copy pathCircularArray.h
File metadata and controls
173 lines (145 loc) · 2.89 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
#ifndef CircularArray_H
#define CircularArray_H
#include <cstring>
#include <inttypes.h>
#include <stdlib.h>
#include "Utils.h"
#if 0
template<class T>
class CircularArray {
T* _records;
size_t _size;
size_t _start;
size_t _extend;
size_t _capacity;
public:
CircularArray():
_records(0), _size(0), _start(0), _capacity(0), _extend(0)
{
}
CircularArray (size_t cap):
_size(0), _start(0), _capacity(cap)
{
_records = new T[_capacity];
_extend = _capacity; // by default, double ...
}
~CircularArray (void)
{
if (_records) {
delete[] _records;
_records = 0;
}
}
CircularArray (const CircularArray& a)
{
// ZAMAN_START(CArray_Copy);
// LOG("Copying circular array of size %d %d", a._size, a._capacity);
_size = a._size;
_capacity = a._capacity;
_extend = a._extend;
_start = a._start;
_records = new T[a._capacity];
std::copy(a._records, a._records + a._size, _records);
// ZAMAN_END(CArray_Copy);
}
CircularArray(CircularArray&& a): CircularArray()
{
swap(*this, a);
}
CircularArray& operator= (CircularArray a)
{
swap(*this, a);
return *this;
}
friend void swap(CircularArray& a, CircularArray& b) // nothrow
{
using std::swap;
swap(a._start, b._start);
swap(a._records, b._records);
swap(a._size, b._size);
swap(a._capacity, b._capacity);
swap(a._extend, b._extend);
}
public:
void realloc (size_t sz)
{
size_t newcap = sz + _extend;
T *tmp = new T[newcap];
size_t p1 = std::min(_capacity - _start, _size);
size_t p2 = _size - p1;
std::copy(_records + _start, _records + _start + p1, tmp);
std::copy(_records, _records + p2, tmp + p1);
_start = 0;
_capacity = newcap;
delete[] _records;
_records = tmp;
}
void resize (size_t sz)
{
while (sz > _capacity)
realloc(sz);
_size = sz;
}
// add element, realloc if needed
void add (const T &t)
{
if (_size == _capacity)
realloc(_capacity);
_records[(_start + _size) % _capacity] = t;
_size++;
}
// Add defualt element or use allocated
void add ()
{
if (_size == _capacity)
realloc(_capacity);
_size++;
}
// add array, realloc if needed
void add (const T *t, size_t sz)
{
// can be faster!
for (size_t i = 0; i < sz; i++)
add(t[i]);
}
T &operator[] (size_t i)
{
assert(i < _size);
return _records[(_start + i) % _capacity];
}
const T &operator[] (size_t i) const
{
assert(i < _size);
return _records[(_start + i) % _capacity];
}
size_t size (void) const
{
return _size;
}
void remove_first_n (size_t k)
{
assert (k <= _size);
_size -= k;
_start = (_start + k) % _capacity;
//fprintf(stderr,"Resized %d\n",_size);
}
T *head (void)
{
return _records + _start;
}
T *increase (T *x)
{
if (x + 1 == _records + _capacity)
return _records;
else return x + 1;
}
/// for memory checking
size_t capacity (void) const {
return _capacity;
}
T *data (void) {
return _records;
}
};
#endif
#endif