-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdvector.h
234 lines (209 loc) · 6.83 KB
/
dvector.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright (c) 2014 ipkn.
// Licensed under the MIT license.
#pragma once
#include "dptr.h"
#include <vector>
#include <cassert>
namespace dumpable
{
namespace detail
{
template <bool Cond, typename T>
struct do_shift
{
void operator()(T& v, int shift) const{}
};
template <typename T>
struct do_shift<true, T>
{
void operator()(T& v, int shift) const
{
v |= v >> shift;
}
};
template <typename T>
T find_power_of_2_greater_than(T n)
{
if (n==0)
return 0;
n += (n==0);
n--;
n|=n>>1;
n|=n>>2;
n|=n>>4;
n|=n>>8;
n|=n>>16;
do_shift<(sizeof(T)>4),T>()(n, 32);
n++;
if (n < 8)
return 8;
return n;
}
}
template <typename T>
class dvector : protected dptr<T>
{
public:
typedef T value_type;
typedef dumpable::size_t size_type;
typedef dumpable::ptrdiff_t difference_type;
typedef T& reference;
typedef const T& const_reference;
protected:
void assign(const T* begin, size_type size)
{
if (!size)
{
clear();
return;
}
if (dumpable::detail::dptr_alloc())
{
isPooled_ = true;
size_ = size;
void* buf = dptr<T>::alloc_internal(size * sizeof(T));
std::copy(begin, begin+size, (T*)buf);
}
else
{
isPooled_ = false;
size_ = size;
size_type capacity = detail::find_power_of_2_greater_than(size);
dptr<T>::operator =(new T[capacity]);
std::copy(begin, begin+size, (T*)*this);
}
}
void uninitialized_resize(size_type newSize)
{
assert(!dumpable::detail::dptr_alloc());
size_type oldCapacity = detail::find_power_of_2_greater_than(size());
if (isPooled_)
oldCapacity = size_;
size_type newCapacity = detail::find_power_of_2_greater_than(newSize);
if (oldCapacity != newCapacity)
{
T* newBuffer = newCapacity ? new T[newCapacity] : nullptr;
T* oldBuffer = (T*)*this;
for(size_type i = 0; i < std::min(size_, newCapacity); i ++)
{
new (newBuffer+i) T(std::move(*(oldBuffer+i)));
(oldBuffer+i)->~T();
}
if (!isPooled_)
delete[](oldBuffer);
dptr<T>::operator =(newBuffer);
isPooled_ = false;
}
size_ = newSize;
}
public:
dvector() : size_(0), isPooled_(false) {}
dvector(const std::vector<T>& v)
{
assign(v.data(), v.size());
}
dvector(const dvector<T>& v)
{
assign(v.data(), v.size());
}
template <typename Iter>
dvector(Iter first, Iter last)
{
std::vector<T> v(first, last);
assign(v.data(), v.size());
}
dvector(dvector<T>&& v) noexcept
: dptr<T>(std::move(v)), size_(v.size_), isPooled_(v.isPooled_)
{
v.size_ = 0;
v.isPooled_ = false;
}
~dvector()
{
clear();
}
public:
void clear()
{
T* begin = (T*)*this;
if (!isPooled_ && begin)
{
delete[] begin;
}
dptr<T>::operator =(nullptr);
size_ = 0;
isPooled_ = false;
}
typedef T* iterator;
typedef const T* const_iterator;
T* data() const { return (T*)*this; }
iterator begin() const { return (T*)*this; }
iterator end() const { return begin() + size_; }
const_iterator cbegin() const { return begin(); }
const_iterator cend() const { return end(); }
size_type size() const { return size_; }
bool empty() const { return !size_; }
T& operator[](int index) { return *(begin() + index); }
const T& operator[](int index) const { return *(begin() + index); }
T& at(int index) { return *(begin() + index); }
const T& at(int index) const { return *(begin() + index); }
const T& front() const { return *begin(); }
const T& back() const { return *(end()-1); }
T& front() { return *begin(); }
T& back() { return *(end()-1); }
void reserve(size_type)
{
// do nothing
}
void resize(size_type newSize)
{
size_type oldSize = size();
uninitialized_resize(newSize);
T* buffer = (T*)*this;
for(size_type i = oldSize; i < newSize; i ++)
{
new (buffer+i) T();
}
}
void push_back(const T& value)
{
uninitialized_resize(size()+1);
T* buffer = (T*)*this;
new (buffer+size_-1) T(value);
}
void push_back(T&& value)
{
uninitialized_resize(size()+1);
T* buffer = (T*)*this;
new (buffer+size_-1) T(std::move(value));
}
dvector<T>& operator = (const std::vector<T>& v)
{
clear();
assign(v.data(), v.size());
return *this;
}
dvector<T>& operator = (const dvector<T>& v)
{
if (&v == this)
return *this;
clear();
assign(v.data(), v.size());
return *this;
}
dvector<T>& operator = (dvector<T>&& v) noexcept
{
if (this == &v)
return *this;
size_ = v.size_;
isPooled_ = v.isPooled_;
dptr<T>::operator =(std::move(v));
v.size_ = 0;
v.isPooled_ = false;
return *this;
}
private:
size_type size_;
char isPooled_;
};
}