| 1 |
#ifndef __BTAS_VARRAY_H |
| 2 |
#define __BTAS_VARRAY_H 1 |
| 3 |
|
| 4 |
#include <algorithm> |
| 5 |
#include <cassert> |
| 6 |
#include <btas/serialization.h> |
| 7 |
#include <boost/serialization/split_free.hpp> |
| 8 |
#include <boost/serialization/array.hpp> |
| 9 |
#include <boost/serialization/collection_size_type.hpp> |
| 10 |
#if BOOST_VERSION >= 106400 |
| 11 |
#include <boost/serialization/array_wrapper.hpp> |
| 12 |
#endif |
| 13 |
|
| 14 |
namespace btas { |
| 15 |
|
| 16 |
/// variable size array class without capacity info |
| 17 |
template <typename _T, |
| 18 |
typename _Allocator = std::allocator<_T> > |
| 19 |
class varray : private _Allocator { |
| 20 |
public: |
| 21 |
|
| 22 |
typedef std::allocator_traits<_Allocator> allocator_traits; ///< Allocator traits |
| 23 |
typedef typename allocator_traits::allocator_type allocator_type; ///< Allocator type |
| 24 |
|
| 25 |
typedef typename allocator_traits::value_type value_type; |
| 26 |
typedef value_type& reference; |
| 27 |
typedef const value_type& const_reference; |
| 28 |
typedef typename allocator_traits::pointer pointer; |
| 29 |
typedef typename allocator_traits::const_pointer const_pointer; |
| 30 |
typedef typename allocator_traits::difference_type difference_type; |
| 31 |
typedef typename allocator_traits::size_type size_type; |
| 32 |
|
| 33 |
typedef pointer iterator; |
| 34 |
typedef const_pointer const_iterator; |
| 35 |
typedef iterator reverse_iterator; |
| 36 |
typedef const_iterator const_reverse_iterator; |
| 37 |
|
| 38 |
private: |
| 39 |
|
| 40 |
struct _M_impl { |
| 41 |
pointer _M_start; |
| 42 |
pointer _M_finish; |
| 43 |
|
| 44 |
_M_impl() : _M_start(nullptr), _M_finish(nullptr) { } |
| 45 |
_M_impl(pointer s, pointer f) : _M_start(s), _M_finish(f) { } |
| 46 |
_M_impl(_M_impl&& other) { |
| 47 |
_M_start = other._M_start; |
| 48 |
_M_finish = other._M_finish; |
| 49 |
other._M_start = nullptr; |
| 50 |
other._M_finish = nullptr; |
| 51 |
} |
| 52 |
|
| 53 |
size_type size() const { return _M_finish - _M_start; } |
| 54 |
bool empty() const { return _M_start == _M_finish; } |
| 55 |
|
| 56 |
pointer begin() { return _M_start; } |
| 57 |
pointer end() { return _M_finish; } |
| 58 |
const_pointer cbegin() const { return const_cast<const_pointer>(_M_start); } |
| 59 |
const_pointer cend() const { return const_cast<const_pointer>(_M_finish); } |
| 60 |
|
| 61 |
pointer rbegin() { return _M_finish-1; } |
| 62 |
pointer rend() { return _M_start-1; } |
| 63 |
const_pointer crbegin() const { return const_cast<const_pointer>(_M_finish-1); } |
| 64 |
const_pointer crend() const { return const_cast<const_pointer>(_M_start-1); } |
| 65 |
|
| 66 |
reference front() { return *begin(); } |
| 67 |
reference back() { return *rbegin(); } |
| 68 |
const_reference front() const { return *cbegin(); } |
| 69 |
const_reference back() const { return *crbegin(); } |
| 70 |
|
| 71 |
reference operator[](size_type i) { |
| 72 |
return _M_start[i]; |
| 73 |
} |
| 74 |
const_reference operator[](size_type i) const { |
| 75 |
return const_cast<const_reference>(_M_start[i]); |
| 76 |
} |
| 77 |
reference at(size_type i) { |
| 78 |
assert(i < size()); |
| 79 |
return _M_start[i]; |
| 80 |
} |
| 81 |
const_reference at(size_type i) const { |
| 82 |
assert(i < size()); |
| 83 |
return const_cast<const_reference>(_M_start[i]); |
| 84 |
} |
| 85 |
|
| 86 |
pointer data() { |
| 87 |
return _M_start; |
| 88 |
} |
| 89 |
const_pointer data() const { |
| 90 |
return const_cast<const_pointer>(_M_start); |
| 91 |
} |
| 92 |
|
| 93 |
void swap (_M_impl& other) |
| 94 |
{ |
| 95 |
_M_start = other._M_start; |
| 96 |
_M_finish = other._M_finish; |
| 97 |
other._M_start = nullptr; |
| 98 |
other._M_finish = nullptr; |
| 99 |
} |
| 100 |
}; |
| 101 |
_M_impl data_; |
| 102 |
|
| 103 |
allocator_type& alloc() { return static_cast<allocator_type&>(*this); } |
| 104 |
const allocator_type& alloc() const { return static_cast<const allocator_type&>(*this); } |
| 105 |
|
| 106 |
friend class boost::serialization::access; |
| 107 |
|
| 108 |
public: |
| 109 |
|
| 110 |
varray () : allocator_type() |
| 111 |
{ } |
| 112 |
|
| 113 |
explicit |
| 114 |
varray (const allocator_type& a) : allocator_type(a) |
| 115 |
{ } |
| 116 |
|
| 117 |
~varray () |
| 118 |
{ |
| 119 |
deallocate(); |
| 120 |
} |
| 121 |
|
| 122 |
explicit |
| 123 |
varray (size_type n, const allocator_type& a = allocator_type()) : allocator_type(a) |
| 124 |
{ |
| 125 |
if (n > 0) { // this ensures that if n == 0, pointers are null |
| 126 |
allocate(n); |
| 127 |
construct(n); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
varray (size_type n, const_reference val, |
| 132 |
const allocator_type& a = allocator_type()) : allocator_type(a) |
| 133 |
{ |
| 134 |
if (n > 0) { |
| 135 |
allocate(n); |
| 136 |
construct(n, val); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
template <class InputIterator> |
| 141 |
varray (InputIterator first, InputIterator last) |
| 142 |
{ |
| 143 |
const auto n = std::distance(first, last); |
| 144 |
if (n > 0) { |
| 145 |
allocate(n); |
| 146 |
construct(first, last); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
varray (const varray& x) : allocator_type(x) |
| 151 |
{ |
| 152 |
const auto n = x.size(); |
| 153 |
if (n > 0) { |
| 154 |
allocate(n); |
| 155 |
construct(x.cbegin(), x.cend()); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
varray (const varray& x, const allocator_type& a) : allocator_type(a) |
| 160 |
{ |
| 161 |
const auto n = x.size(); |
| 162 |
if (n > 0) { |
| 163 |
allocate(n); |
| 164 |
construct(x.cbegin(), x.cend()); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
varray (varray&& x) |
| 169 |
: allocator_type(std::move(static_cast<allocator_type&&>(x))), data_(std::move(x.data_)) |
| 170 |
{ |
| 171 |
} |
| 172 |
|
| 173 |
template <typename U, class = typename std::enable_if< std::is_convertible<U, value_type>::value >::type > |
| 174 |
varray (std::initializer_list<U> il) |
| 175 |
{ |
| 176 |
size_type n = il.size(); |
| 177 |
if (n > 0) { |
| 178 |
allocate(n); |
| 179 |
construct(il.begin(), il.end()); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
varray& operator= (const varray& x) { |
| 184 |
const auto n = x.size(); |
| 185 |
if (n != data_.size()) { |
| 186 |
deallocate(); |
| 187 |
if (n > 0) |
| 188 |
allocate(n); |
| 189 |
} |
| 190 |
|
| 191 |
if (n > 0) { |
| 192 |
construct(x.cbegin(), x.cend()); |
| 193 |
} |
| 194 |
return *this; |
| 195 |
} |
| 196 |
|
| 197 |
varray& operator= (varray&& x) |
| 198 |
{ |
| 199 |
swap (x); // if something in this object, it will be destructed by x |
| 200 |
return *this; |
| 201 |
} |
| 202 |
|
| 203 |
template <typename U, class = typename std::enable_if< std::is_convertible<U, value_type>::value >::type > |
| 204 |
varray& operator= (std::initializer_list<U> il) |
| 205 |
{ |
| 206 |
const auto n = il.size(); |
| 207 |
if (n != data_.size()) { |
| 208 |
deallocate(); |
| 209 |
if (n > 0) |
| 210 |
allocate(n); |
| 211 |
} |
| 212 |
|
| 213 |
if (n > 0) { |
| 214 |
construct(il.cbegin(), il.cend()); |
| 215 |
} |
| 216 |
return *this; |
| 217 |
} |
| 218 |
|
| 219 |
iterator begin () noexcept |
| 220 |
{ |
| 221 |
return data_.begin(); |
| 222 |
} |
| 223 |
|
| 224 |
const_iterator begin () const noexcept |
| 225 |
{ |
| 226 |
return cbegin(); |
| 227 |
} |
| 228 |
|
| 229 |
const_iterator cbegin () const noexcept |
| 230 |
{ |
| 231 |
return data_.cbegin(); |
| 232 |
} |
| 233 |
|
| 234 |
iterator end () noexcept |
| 235 |
{ |
| 236 |
return data_.end(); |
| 237 |
} |
| 238 |
|
| 239 |
const_iterator end () const noexcept |
| 240 |
{ |
| 241 |
return cend(); |
| 242 |
} |
| 243 |
|
| 244 |
const_iterator cend () const noexcept |
| 245 |
{ |
| 246 |
return data_.cend(); |
| 247 |
} |
| 248 |
|
| 249 |
reverse_iterator rbegin () noexcept |
| 250 |
{ |
| 251 |
return data_.rbegin(); |
| 252 |
} |
| 253 |
|
| 254 |
const_reverse_iterator rbegin () const noexcept |
| 255 |
{ |
| 256 |
return data_.rbegin(); |
| 257 |
} |
| 258 |
|
| 259 |
reverse_iterator rend () noexcept |
| 260 |
{ |
| 261 |
return data_.rend(); |
| 262 |
} |
| 263 |
|
| 264 |
const_reverse_iterator rend () const noexcept |
| 265 |
{ |
| 266 |
return data_.rend(); |
| 267 |
} |
| 268 |
|
| 269 |
size_type size () const noexcept |
| 270 |
{ return data_.size(); } |
| 271 |
|
| 272 |
void resize (size_type n) |
| 273 |
{ |
| 274 |
if (size() != n) { |
| 275 |
if (!empty()) { |
| 276 |
deallocate(); |
| 277 |
} |
| 278 |
if (n > 0) { |
| 279 |
allocate(n); |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
void resize (size_type n, const value_type& val) |
| 285 |
{ |
| 286 |
resize(n); |
| 287 |
construct(n, val); |
| 288 |
} |
| 289 |
|
| 290 |
bool empty () const noexcept |
| 291 |
{ return data_.empty(); } |
| 292 |
|
| 293 |
reference operator [] (size_type n) |
| 294 |
{ return data_[n]; } |
| 295 |
|
| 296 |
const_reference operator [] (size_type n) const |
| 297 |
{ return data_[n]; } |
| 298 |
|
| 299 |
reference at (size_type n) |
| 300 |
{ return data_.at(n); } |
| 301 |
|
| 302 |
const_reference at (size_type n) const |
| 303 |
{ return data_.at(n); } |
| 304 |
|
| 305 |
reference front () |
| 306 |
{ return data_.front(); } |
| 307 |
|
| 308 |
const_reference front () const |
| 309 |
{ return data_.front(); } |
| 310 |
|
| 311 |
reference back () |
| 312 |
{ return data_.back(); } |
| 313 |
|
| 314 |
const_reference back () const |
| 315 |
{ return data_.back(); } |
| 316 |
|
| 317 |
value_type* data () noexcept |
| 318 |
{ return data_.data(); } |
| 319 |
|
| 320 |
const value_type* data () const noexcept |
| 321 |
{ return data_.data(); } |
| 322 |
|
| 323 |
void swap (varray& x) |
| 324 |
{ data_.swap(x.data_); } |
| 325 |
|
| 326 |
void clear () |
| 327 |
{ |
| 328 |
if (!empty()) { |
| 329 |
deallocate(); |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
private: |
| 334 |
|
| 335 |
void allocate(size_type n) { |
| 336 |
assert(n <= allocator_traits::max_size(alloc())); |
| 337 |
data_._M_start = allocator_traits::allocate(alloc(), n); |
| 338 |
data_._M_finish = data_._M_start + n; |
| 339 |
} |
| 340 |
|
| 341 |
void deallocate() { |
| 342 |
if (!data_.empty()) |
| 343 |
allocator_traits::deallocate(alloc(), data_._M_start, data_.size()); |
| 344 |
data_._M_start = data_._M_finish = nullptr; |
| 345 |
} |
| 346 |
|
| 347 |
void |
| 348 |
construct(size_type n) |
| 349 |
{ |
| 350 |
auto ptr = data_._M_start; |
| 351 |
do |
| 352 |
{ |
| 353 |
allocator_traits::construct(alloc(), ptr); |
| 354 |
++ptr; |
| 355 |
--n; |
| 356 |
} while (n > 0); |
| 357 |
} |
| 358 |
|
| 359 |
void |
| 360 |
construct(size_type n, const_reference x) |
| 361 |
{ |
| 362 |
auto ptr = data_._M_start; |
| 363 |
do |
| 364 |
{ |
| 365 |
allocator_traits::construct(alloc(), ptr, x); |
| 366 |
++ptr; |
| 367 |
--n; |
| 368 |
} while (n > 0); |
| 369 |
} |
| 370 |
|
| 371 |
template <typename InputIterator> |
| 372 |
void |
| 373 |
construct(const InputIterator& begin, const InputIterator& end) { |
| 374 |
auto ptr = data_._M_start; |
| 375 |
for(auto i = begin; i != end; ++i) { |
| 376 |
allocator_traits::construct(alloc(), ptr, *i); |
| 377 |
++ptr; |
| 378 |
} |
| 379 |
} |
| 380 |
}; |
| 381 |
|
| 382 |
template <typename T> |
| 383 |
inline bool operator== (const btas::varray<T>& a, |
| 384 |
const btas::varray<T>& b) { |
| 385 |
return std::equal(a.begin(), a.end(), b.begin()); |
| 386 |
} |
| 387 |
|
| 388 |
template <typename T> |
| 389 |
inline bool operator!= (const btas::varray<T>& a, |
| 390 |
const btas::varray<T>& b) { |
| 391 |
return not (a == b); |
| 392 |
} |
| 393 |
|
| 394 |
} // namespace btas |
| 395 |
|
| 396 |
namespace boost { |
| 397 |
namespace serialization { |
| 398 |
|
| 399 |
/// boost serialization for varray |
| 400 |
template<class Archive, typename T> |
| 401 |
void serialize (Archive& ar, btas::varray<T>& x, const unsigned int version) |
| 402 |
{ |
| 403 |
boost::serialization::split_free(ar, x, version); |
| 404 |
} |
| 405 |
template<class Archive, typename T> |
| 406 |
void save (Archive& ar, const btas::varray<T>& x, const unsigned int version) |
| 407 |
{ |
| 408 |
const boost::serialization::collection_size_type count(x.size()); |
| 409 |
ar << BOOST_SERIALIZATION_NVP(count); |
| 410 |
if (count != 0) |
| 411 |
ar << boost::serialization::make_array(x.data(), count); |
| 412 |
} |
| 413 |
template<class Archive, typename T> |
| 414 |
void load (Archive& ar, btas::varray<T>& x, const unsigned int version) |
| 415 |
{ |
| 416 |
boost::serialization::collection_size_type count; |
| 417 |
ar >> BOOST_SERIALIZATION_NVP(count); |
| 418 |
x.resize(count); |
| 419 |
if (count != 0) |
| 420 |
ar >> boost::serialization::make_array(x.data(), count); |
| 421 |
} |
| 422 |
|
| 423 |
} // namespace serialization |
| 424 |
} // namespace boost |
| 425 |
|
| 426 |
template <typename T> |
| 427 |
inline bool operator== (const btas::varray<T>& a, |
| 428 |
const btas::varray<T>& b) { |
| 429 |
return std::equal(a.begin(), a.end(), b.begin()); |
| 430 |
} |
| 431 |
|
| 432 |
template <typename T> |
| 433 |
inline bool operator!= (const btas::varray<T>& a, |
| 434 |
const btas::varray<T>& b) { |
| 435 |
return not (a == b); |
| 436 |
} |
| 437 |
|
| 438 |
#endif // __BTAS_VARRAY_H |