/* SPDX-License-Identifier: Unlicense */ #include "vec.h" #include #include template void Vec::expand(Vec::size_type by) { if (_size + by <= _capacity) { return; } const size_type new_capacity = (_size + by) * 1.5f; T *d{static_cast(calloc(new_capacity, sizeof (T)))}; assert(d); if (_d) { for (size_type i = 0; i < _size; i++) { d[i] = static_cast(_d[i]); } free(_d); } _d = d; _capacity = new_capacity; } template Vec::Vec(const Vec& other) : _size(other._size) , _capacity(other._size) , _d(static_cast(calloc(_capacity, sizeof (T)))) { assert(_d); for (size_type i = 0; i < _size; i++) { _d[i] = other._d[i]; } } template Vec::Vec(const T *data, size_type nmemb) : _size(nmemb) , _capacity(nmemb) , _d(static_cast(calloc(_capacity, sizeof (T)))) { assert(_d); for (size_type i = 0; i < _size; i++) { _d[i] = static_cast(data)[i]; } } template Vec::Vec(const void *data, size_type size) : _size(size / sizeof (T)) , _capacity(size / sizeof (T)) , _d(static_cast(calloc(_capacity, sizeof (T)))) { assert(_d); for (size_type i = 0; i < _size; i++) { _d[i] = static_cast(data)[i]; } } template Vec::~Vec() { if (_d == nullptr) { return; } for (size_type i = 0; i < _size; i++) { _d[i].~T(); } free(_d); _d = nullptr; _capacity = _size = 0; } template Vec &Vec::operator=(const Vec &other) { this->~Vec(); _size = _capacity = 0; _d = nullptr; expand(other._size); for (size_type i = 0; i < other._size; i++) { _d[i] = other._d[i]; } _size = other._size; return *this; } template bool Vec::operator==(const Vec& other) const { if (_size != other._size) { return false; } for (size_type i = 0; i < _size; i++) { if (_d[i] != other._d[i]) { return false; } } return true; } template class Vec; // Used in vecutil.cpp template class Vec; // Used in vec_tests.cpp template class Vec>; // Used in vec_tests.cpp #include "tracetab.h" template class Vec; // Used in tracetab.cpp template class Vec; // Used in tracetab.cpp