-
Notifications
You must be signed in to change notification settings - Fork 1
/
stretchy_buf.h
110 lines (87 loc) · 2.05 KB
/
stretchy_buf.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
#ifndef STRETCHY_BUF_H
#define STRETCHY_BUF_H
#include "common.h"
#include <cstdlib>
#include <limits>
#define MAX(a, b) ((a > b) ? a : b)
// A stretchy buffer without C++ magic.
// This is essentially a simple std::vector
template<typename T>
struct StretchyBuf {
// Members
size_t cap, len;
T *data;
StretchyBuf() {
cap = len = 0;
data = nullptr;
}
StretchyBuf(size_t n) : StretchyBuf() { reserve(n); }
// No destructor, no copy and rvalue constructors.
// It works like a C struct in those moves.
private:
void _grow(size_t new_len) {
constexpr size_t size_t_max = std::numeric_limits<size_t>::max();
assert(cap <= (size_t_max - 1) / 2);
size_t new_cap = MAX(MAX(2 * cap, new_len), 256);
assert(new_len <= new_cap);
assert(new_cap <= (size_t_max) / sizeof(T));
size_t new_size = new_cap * sizeof(T);
data = (T *) realloc(data, new_size);
assert(data);
cap = new_cap;
}
public:
void push(T v) {
constexpr size_t size_t_max = std::numeric_limits<size_t>::max();
assert(len < size_t_max);
size_t new_len = len + 1;
if (new_len > cap)
_grow(new_len);
data[len] = v;
len = new_len;
}
T pop() {
assert(!empty());
--len;
return data[len];
}
bool empty() { return len == 0; }
void reserve(size_t n) {
assert(len == 0 && cap == 0);
_grow(n);
cap = n;
}
void reset() {
len = 0;
}
void clear() {
cap = 0;
len = 0;
}
void free() {
if (data != nullptr)
::free(data);
data = nullptr;
len = 0;
cap = 0;
}
void shrink_to_fit() {
assert(len);
data = (T *) realloc(data, len * sizeof(T));
assert(data);
cap = len;
}
T &operator[](size_t i) {
assert(i < len);
return data[i];
}
const T &operator[](size_t i) const {
assert(i < len);
return data[i];
}
inline T *begin() { return this->data; }
inline const T *begin() const { return this->data; }
inline T *end() { return &this->data[len]; }
inline const T *end() const { return &this->data[len]; }
};
#endif