Changeset View
Changeset View
Standalone View
Standalone View
src/prevector.h
// Copyright (c) 2015-2016 The Bitcoin Core developers | // Copyright (c) 2015-2016 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#ifndef _BITCOIN_PREVECTOR_H_ | #ifndef _BITCOIN_PREVECTOR_H_ | ||||
#define _BITCOIN_PREVECTOR_H_ | #define _BITCOIN_PREVECTOR_H_ | ||||
#include <cassert> | #include <cassert> | ||||
#include <cstdint> | #include <cstdint> | ||||
#include <cstdlib> | #include <cstdlib> | ||||
#include <cstring> | #include <cstring> | ||||
#include <iterator> | #include <iterator> | ||||
#include <type_traits> | |||||
#pragma pack(push, 1) | #pragma pack(push, 1) | ||||
/** | /** | ||||
* Implements a drop-in replacement for std::vector<T> which stores up to N | * Implements a drop-in replacement for std::vector<T> which stores up to N | ||||
* elements directly (without heap allocation). The types Size and Diff are used | * elements directly (without heap allocation). The types Size and Diff are used | ||||
* to store element counts, and can be any unsigned + signed type. | * to store element counts, and can be any unsigned + signed type. | ||||
* | * | ||||
* Storage layout is either: | * Storage layout is either: | ||||
▲ Show 20 Lines • Show All 449 Lines • ▼ Show 20 Lines | void insert(iterator pos, InputIterator first, InputIterator last) { | ||||
} | } | ||||
} | } | ||||
iterator erase(iterator pos) { return erase(pos, pos + 1); } | iterator erase(iterator pos) { return erase(pos, pos + 1); } | ||||
iterator erase(iterator first, iterator last) { | iterator erase(iterator first, iterator last) { | ||||
iterator p = first; | iterator p = first; | ||||
char *endp = (char *)&(*end()); | char *endp = (char *)&(*end()); | ||||
if (!std::is_trivially_destructible<T>::value) { | |||||
while (p != last) { | while (p != last) { | ||||
(*p).~T(); | (*p).~T(); | ||||
_size--; | _size--; | ||||
++p; | ++p; | ||||
} | } | ||||
} else { | |||||
_size -= last - p; | |||||
} | |||||
memmove(&(*first), &(*last), endp - ((char *)(&(*last)))); | memmove(&(*first), &(*last), endp - ((char *)(&(*last)))); | ||||
return first; | return first; | ||||
} | } | ||||
void push_back(const T &value) { | void push_back(const T &value) { | ||||
size_type new_size = size() + 1; | size_type new_size = size() + 1; | ||||
if (capacity() < new_size) { | if (capacity() < new_size) { | ||||
change_capacity(new_size + (new_size >> 1)); | change_capacity(new_size + (new_size >> 1)); | ||||
Show All 13 Lines | public: | ||||
const T &back() const { return *item_ptr(size() - 1); } | const T &back() const { return *item_ptr(size() - 1); } | ||||
void swap(prevector<N, T, Size, Diff> &other) { | void swap(prevector<N, T, Size, Diff> &other) { | ||||
std::swap(_union, other._union); | std::swap(_union, other._union); | ||||
std::swap(_size, other._size); | std::swap(_size, other._size); | ||||
} | } | ||||
~prevector() { | ~prevector() { | ||||
if (!std::is_trivially_destructible<T>::value) { | |||||
clear(); | clear(); | ||||
} | |||||
if (!is_direct()) { | if (!is_direct()) { | ||||
free(_union.indirect); | free(_union.indirect); | ||||
_union.indirect = nullptr; | _union.indirect = nullptr; | ||||
} | } | ||||
} | } | ||||
bool operator==(const prevector<N, T, Size, Diff> &other) const { | bool operator==(const prevector<N, T, Size, Diff> &other) const { | ||||
if (other.size() != size()) { | if (other.size() != size()) { | ||||
▲ Show 20 Lines • Show All 57 Lines • Show Last 20 Lines |