-
Notifications
You must be signed in to change notification settings - Fork 9
/
buf.h
86 lines (68 loc) · 1.81 KB
/
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
#ifndef BUF_H
#define BUF_H
#include <stdlib.h>
#include <limits>
#include "common.h"
#define MAX(a, b) ((a > b) ? a : b)
template<typename T>
struct Buf {
// Members
size_t cap, len;
T *data;
// Typedefs
typedef T* iterator;
typedef const T* const_iterator;
Buf() {
cap = len = 0;
data = nullptr;
}
Buf(size_t n) : Buf() {
reserve(n);
}
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), 16);
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) {
size_t new_len = len + 1;
if(new_len > cap) _grow(new_len);
data[len] = v;
len = new_len;
}
void reserve(size_t n) {
assert(len == 0 && cap == 0);
_grow(n);
cap = n;
}
void free() {
if(data != nullptr) ::free(data);
len = 0;
cap = 0;
}
Buf<T> deep_copy() {
Buf<T> copy(cap);
memcpy(copy.data, data, len * sizeof(T));
copy.len = len;
return copy;
}
void clear() {
len = 0;
}
const T& back() const { assert(len >= 1); return data[len-1]; }
T& operator[](size_t i) { return data[i]; }
const T& operator[](size_t i) const { return data[i]; }
inline iterator begin() { return this->data; }
inline const_iterator begin() const { return this->data; }
inline iterator end() { return &this->data[len]; }
inline const_iterator end() const { return &this->data[len]; }
};
#endif