-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointer_vector.h
More file actions
189 lines (160 loc) · 4.63 KB
/
pointer_vector.h
File metadata and controls
189 lines (160 loc) · 4.63 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
186
187
188
189
#pragma once
#include <cstdint>
#include <algorithm>
//Vector type with manual memory management that can be converted to and from a void*.
template <class T>
class pointer_vector
{
uint8_t* data_;
void reallocate_copy_destroy(size_t n_bytes_to_allocate)
{
//reallocate
uint8_t* new_data = reinterpret_cast<uint8_t*>(new uint8_t[n_bytes_to_allocate]);
//copy
*reinterpret_cast<uint8_t**>(new_data) = new_data + n_bytes_to_allocate;
*reinterpret_cast<uint8_t**>(new_data + sizeof(uint8_t*)) = new_data + (*reinterpret_cast<uint8_t**>(data_ + sizeof(uint8_t*)) - data_);
std::copy(data_ + 2 * sizeof(uint8_t*), *reinterpret_cast<uint8_t**>(data_ + sizeof(uint8_t*)), new_data + 2 * sizeof(uint8_t*));
//destroy
delete[] data_;
data_ = new_data;
}
size_t allocated_size() const
{
return (*reinterpret_cast<uint8_t**>(data_) - data_ - 2 * sizeof(uint8_t*)) / sizeof(T);
}
public:
void reserve(size_t size)
{
if (allocated_size() < size)
reallocate_copy_destroy(2 * sizeof(uint8_t*) + size * sizeof(T));
}
void resize(size_t size)
{
reserve(size);
auto iter = end();
*reinterpret_cast<uint8_t**>(data_ + sizeof(uint8_t*)) = (data_ + 2 * sizeof(uint8_t*) + size * sizeof(T));
for (; iter < end(); ++iter)
new (iter) T();
for (--iter; iter >= end(); --iter)
iter->~T();
}
template<class... _Args>
void resize(size_t size, _Args&&... args)
{
reserve(size);
auto iter = end();
*reinterpret_cast<uint8_t**>(data_ + sizeof(uint8_t*)) = (data_ + 2 * sizeof(uint8_t*) + size * sizeof(T));
for (; iter < end(); ++iter)
new (iter) T(args...);
for (--iter; iter >= end(); --iter)
iter->~T();
}
template<class... _Args>
void push_back(_Args&&... args)
{
if (end() == *reinterpret_cast<T**>(data_))
reallocate_copy_destroy(2 * size_t(*reinterpret_cast<uint8_t**>(data_) - data_ - sizeof(uint8_t*)));
new (*reinterpret_cast<T**>(data_ + sizeof(uint8_t*))) T(args...);
++*reinterpret_cast<T**>(data_ + sizeof(uint8_t*));
}
void pop_back()
{
--*reinterpret_cast<T**>(data_ + sizeof(uint8_t*));
end()->~T();
}
void clear()
{
for (auto& ref : *this)
ref.~T();
*reinterpret_cast<uint8_t**>(data_ + sizeof(uint8_t*)) = data_ + 2 * sizeof(uint8_t*);
}
size_t size() const
{
return end() - begin();
}
bool empty() const
{
return *reinterpret_cast<uint8_t**>(data_ + sizeof(uint8_t*)) == data_ + 2 * sizeof(uint8_t*);
}
T* begin()
{
return reinterpret_cast<T*>(data_ + 2 * sizeof(uint8_t*));
}
T* end()
{
return *reinterpret_cast<T**>(data_ + sizeof(uint8_t*));
}
T& back()
{
return *(end() - 1);
}
T& operator[](size_t idx)
{
return *(begin() + idx);
}
pointer_vector() {}
~pointer_vector() {}
template<class T2>
pointer_vector(const pointer_vector<T2>& rhs)
{
this->initialize_this(rhs.size());
auto& to_iter = *reinterpret_cast<T**>(data_ + sizeof(uint8_t*));
for (auto& ref : rhs)
new (to_iter++) T(ref);
}
template<class T2>
pointer_vector(const pointer_vector<T2>& rhs, float growth_factor)
{
this->initialize_this(size_t(std::ceil(rhs.size()*growth_factor)));
auto& to_iter = *reinterpret_cast<T**>(data_ + sizeof(uint8_t*));
for (auto& ref : rhs)
new (to_iter++) T(ref);
}
pointer_vector(void* pv)
{
data_ = reinterpret_cast<uint8_t*>(pv);
}
operator void*()
{
return reinterpret_cast<void*>(data_);
}
void initialize_this()
{
data_ = new uint8_t[2 * sizeof(uint8_t*) + sizeof(T)];
*reinterpret_cast<uint8_t**>(data_) = data_ + 2 * sizeof(uint8_t*) + sizeof(T); //End of allocated storage pointer
*reinterpret_cast<uint8_t**>(data_ + sizeof(uint8_t*)) = data_ + 2 * sizeof(uint8_t*); //End of used storage pointer
}
void initialize_this(size_t size)
{
data_ = new uint8_t[2 * sizeof(uint8_t*) + (++size) * sizeof(T)];
*reinterpret_cast<uint8_t**>(data_) = data_ + 2 * sizeof(uint8_t*) + size * sizeof(T); //End of allocated storage pointer
*reinterpret_cast<uint8_t**>(data_ + sizeof(uint8_t*)) = data_ + 2 * sizeof(uint8_t*); //End of used storage pointer
}
void delete_this()
{
for (auto& ref : *this)
ref.~T();
delete[] data_;
}
//const
const T* begin() const
{
return reinterpret_cast<const T*>(data_ + 2 * sizeof(uint8_t*));
}
const T* end() const
{
return *reinterpret_cast<const T**>(data_ + sizeof(uint8_t*));
}
const T& back() const
{
return *(end() - 1);
}
const T& operator[](size_t idx) const
{
return *(begin() + idx);
}
operator const void*() const
{
return reinterpret_cast<const void*>(data_);
}
};