不少人吐槽 C++ STL 中的 vector 速度很慢,但实际上它其实已经很快了。
所以我就自己写了一个“更适合 OIER 的 vector”,不知道命名成什么,那就叫 fvector 吧。
Current Version: 0.1.0-beta
特点:
- 需 C++ 11 以上的编译器
- 需 GCC 4.7.1 以上的版本
- 仅实现了大部分常用的函数
- 实现的所有函数性能不劣于原版
vector - 使用和原版
vector一样的allocate方式(根据需要可以自行修改)
洛谷 C++ 14(GCC 9) -O2 情况下(粗略计算):
push_back/emplace_back速率是原来的 倍。reserve + push_back速率是原来的 倍。- 其余函数速率基本不变或几乎不需要用时。
#define NDEBUG
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <new>
#include <type_traits>
#include <utility>
// Helper functions
template <class T>
inline void construct_lr(T* _B, T* _E)
{ while (_B != _E) new (_B) T, ++ _B; }
template <class T>
inline void copy_lr(T* _B, T* _E, T* dest)
{ while (_B != _E) new (dest) T(*_B), ++ _B, ++ dest; }
template <class T>
inline void destruct_lr(T* _B, T* _E)
{ while (_B != _E) _B->~T(), ++ _B; }
template <class T>
class fvector {
public:
using size_type = std::size_t;
fvector() = default;
fvector(const fvector& rhs);
fvector(fvector&& rhs) noexcept;
fvector& operator=(const fvector& rhs);
fvector& operator=(fvector&& rhs) noexcept;
~fvector();
// Element-Access
T& operator[](size_type pos);
const T& operator[](size_type pos) const;
T* data() noexcept;
const T* data() const noexcept;
T& front();
const T& front() const;
T& back();
const T& back() const;
// Iterators
T* begin() noexcept;
const T* begin() const noexcept;
T* end() noexcept;
const T* end() const noexcept;
// Capacity
size_type size() const noexcept;
size_type capacity() const noexcept;
bool empty() const noexcept;
void reserve(size_type new_cap);
void shrink_to_fit();
// Modifiers
void clear() noexcept;
template< class... Args >
void emplace_back(Args&&... args);
void push_back(const T& value);
void push_back(T&& value);
void pop_back();
void resize(size_type count);
static constexpr size_type grow_factor = 2;
private:
T* _data = nullptr;
size_type _size = 0;
size_type _capacity = 0;
};
template <class T>
fvector<T>::fvector(const fvector& rhs)
: _size(rhs._size)
, _capacity(rhs._capacity) {
_data = reinterpret_cast<T*>(std::malloc(sizeof(T) * rhs._capacity));
if (std::is_trivial<T>::value)
std::memcpy(_data, rhs._data, rhs._size * sizeof(T));
else copy_lr(rhs.begin(), rhs.end(), _data);
}
template <class T>
fvector<T>::fvector(fvector&& rhs) noexcept
: _data(rhs._data)
, _size(rhs._size)
, _capacity(rhs._capacity)
{ rhs._data = nullptr, rhs._size = 0, rhs._capacity = 0; }
template <class T>
fvector<T>& fvector<T>::operator=(const fvector& rhs) {
if (this != &rhs) {
// Free existing resources
if (!std::is_trivial<T>::value) destruct_lr(begin(), end());
std::free(_data);
// Copy from rhs
_size = rhs._size;
_capacity = rhs._capacity;
_data = reinterpret_cast<T*>(std::malloc(sizeof(T) * rhs._capacity));
if (std::is_trivial<T>::value)
std::memcpy(_data, rhs._data, rhs._size * sizeof(T));
else copy_lr(rhs.begin(), rhs.end(), _data);
}
return *this;
}
template <class T>
fvector<T>& fvector<T>::operator=(fvector&& rhs) noexcept {
if (this != &rhs) {
// Free existing resources
if (!std::is_trivial<T>::value) destruct_lr(begin(), end());
std::free(_data);
// Move from rhs
_data = rhs._data, _size = rhs._size, _capacity = rhs._capacity;
rhs._data = nullptr, rhs._size = 0, rhs._capacity = 0;
}
return *this;
}
template <class T>
fvector<T>::~fvector() {
if (!std::is_trivial<T>::value) destruct_lr(begin(), end());
std::free(_data);
}
// Element access
template <class T>
T& fvector<T>::operator[](size_type pos) {
assert(pos < _size && "Position is out of lr");
return _data[pos];
}
template <class T>
const T& fvector<T>::operator[](size_type pos) const {
assert(pos < _size && "Position is out of lr");
return _data[pos];
}
template <class T>
T& fvector<T>::front() {
assert(_size > 0 && "Container is empty");
return _data[0];
}
template <class T>
const T& fvector<T>::front() const {
assert(_size > 0 && "Container is empty");
return _data[0];
}
template <class T>
T& fvector<T>::back()
{
assert(_size > 0 && "Container is empty");
return _data[_size - 1];
}
template <class T>
const T& fvector<T>::back() const {
assert(_size > 0 && "Container is empty");
return _data[_size - 1];
}
template <class T>
T* fvector<T>::data() noexcept { return _data; }
template <class T>
const T* fvector<T>::data() const noexcept { return _data; }
// Iterators
template <class T>
T* fvector<T>::begin() noexcept { return _data; }
template <class T>
const T* fvector<T>::begin() const noexcept { return _data; }
template <class T>
T* fvector<T>::end() noexcept { return _data + _size; }
template <class T>
const T* fvector<T>::end() const noexcept { return _data + _size; }
// Capacity
template <class T>
bool fvector<T>::empty() const noexcept { return _size == 0; }
template <class T>
typename fvector<T>::size_type fvector<T>::size() const noexcept
{ return _size; }
template <class T>
void fvector<T>::reserve(size_type new_cap) {
if (new_cap <= _capacity) return;
if (std::is_trivial<T>::value) {
_data = reinterpret_cast<T*>(std::realloc(_data, sizeof(T) * new_cap));
assert(_data != nullptr && "Reallocation failed");
} else {
T* new_data_location =
reinterpret_cast<T*>(std::malloc(sizeof(T) * new_cap));
assert(new_data_location != nullptr && "Allocation failed");
copy_lr(begin(), end(), new_data_location);
destruct_lr(begin(), end());
std::free(_data);
_data = new_data_location;
}
_capacity = new_cap;
}
template <class T>
typename fvector<T>::size_type fvector<T>::capacity() const noexcept
{ return _capacity; }
template <class T>
void fvector<T>::shrink_to_fit() {
if (_size < _capacity) {
if (std::is_trivial<T>::value) {
_data = reinterpret_cast<T*>(std::realloc(_data, sizeof(T) * _size));
assert(_data != nullptr && "Reallocation failed");
}
else {
T* new_data_location =
reinterpret_cast<T*>(std::malloc(sizeof(T) * _size));
assert(new_data_location != nullptr && "Allocation failed");
copy_lr(begin(), end(), new_data_location);
destruct_lr(begin(), end());
std::free(_data);
_data = new_data_location;
}
_capacity = _size;
}
}
// Modifiers
template <class T>
void fvector<T>::clear() noexcept {
if (!std::is_trivial<T>::value) destruct_lr(begin(), end());
_size = 0;
}
template <class T>
void fvector<T>::push_back(const T& value) {
if (_size == _capacity) reserve(_capacity * fvector::grow_factor + 1);
if (std::is_trivial<T>::value) _data[_size] = value;
else new (_data + _size) T(value);
++ _size;
}
template <class T>
void fvector<T>::push_back(T&& value) {
if (_size == _capacity) reserve(_capacity * fvector::grow_factor + 1);
if (std::is_trivial<T>::value) _data[_size] = std::move(value);
else new (_data + _size) T(std::move(value));
++ _size;
}
template <class T>
template< class... Args >
void fvector<T>::emplace_back(Args&&... args) {
// static_assert(!std::is_trivial<T>::value,
// "Use push_back() instead of emplace_back() with trivial types");
if (_size == _capacity) reserve(_capacity * fvector::grow_factor + 1);
new (_data + _size) T(std::forward<Args>(args)...);
++ _size;
}
template <class T>
void fvector<T>::pop_back() {
assert(_size > 0 && "Container is empty");
if (!std::is_trivial<T>::value) _data[_size - 1].~T();
-- _size;
}
template <class T>
void fvector<T>::resize(size_type count) {
if (count == _size) return;
if (count > _capacity) reserve(count);
if (!std::is_trivial<T>::value) {
if (count > _size) construct_lr(_data + _size, _data + count);
else if (count < _size) destruct_lr(_data + count, _data + _size);
}
_size = count;
}
// 以下为测试
#include <vector>
#include <cstdio>
#include <ctime>
#include <cstdint>
using namespace std;
struct i2 {
int x1, x2;
i2() = default;
i2(const int& a, const int& b) : x1(a), x2(b) {}
};
fvector<i2> a, A;
vector<i2> b, B;
clock_t c;
#define testf(...) {c = clock(); __VA_ARGS__ printf("f:%lf ms\n", (clock() - c) * 1000.0 / CLOCKS_PER_SEC);}
#define testv(...) {c = clock(); __VA_ARGS__ printf("v:%lf ms\n", (clock() - c) * 1000.0 / CLOCKS_PER_SEC);}
int main() {
//testf( for (int i = 1; i <= 3e7; ++ i) a.emplace_back(i); )
//testv( for (int i = 1; i <= 3e7; ++ i) b.emplace_back(i); )
testf( a.reserve(1.5e7); )
testv( b.reserve(1.5e7); )
testf( for (int i = 1; i <= 1.5e7; ++ i) a.push_back({i, i}); )
testv( for (int i = 1; i <= 1.5e7; ++ i) b.push_back({i, i}); )
testf( uint64_t x = 0; for (auto p : a) x += p.x1; printf("%llu\n", x); )
testv( uint64_t x = 0; for (auto p : b) x += p.x1; printf("%llu\n", x); )
//testf( for (int i = 1; i <= 2e7; ++ i) ; )
//testv( for (int i = 1; i <= 2e7; ++ i) ; )
}
到目前未知还没有发现 Bug 或者性能劣于原版 vector 的地方,下一步大概就是实现更多的函数。如果发现了可以在下方评论或私信作者。
以上内容是半夜脑子不清醒的状态下写的,可能会胡言乱语,如果发现也可以在下方评论或私信作者。