2013-09-15 10:53:53 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <cstddef>
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
|
|
#include <boost/noncopyable.hpp>
|
2013-12-08 02:29:40 +00:00
|
|
|
|
#include <boost/iterator_adaptors.hpp>
|
2013-09-15 10:53:53 +00:00
|
|
|
|
|
2015-09-29 19:19:54 +00:00
|
|
|
|
#include <common/likely.h>
|
|
|
|
|
#include <common/strong_typedef.h>
|
2013-09-15 10:53:53 +00:00
|
|
|
|
|
2015-08-16 13:00:22 +00:00
|
|
|
|
#include <DB/Common/Allocator.h>
|
2015-10-05 01:35:28 +00:00
|
|
|
|
#include <DB/Common/Exception.h>
|
2013-09-15 10:53:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/** Динамический массив для POD-типов.
|
|
|
|
|
* Предназначен для небольшого количества больших массивов (а не большого количества маленьких).
|
|
|
|
|
* А точнее - для использования в ColumnVector.
|
|
|
|
|
* Отличается от std::vector тем, что не инициализирует элементы.
|
2013-12-08 02:29:40 +00:00
|
|
|
|
*
|
|
|
|
|
* Сделан некопируемым, чтобы не было случайных копий. Скопировать данные можно с помощью метода assign.
|
|
|
|
|
*
|
2013-09-15 10:53:53 +00:00
|
|
|
|
* Поддерживается только часть интерфейса std::vector.
|
|
|
|
|
*
|
2013-12-08 02:29:40 +00:00
|
|
|
|
* Конструктор по-умолчанию создаёт пустой объект, который не выделяет память.
|
2016-04-14 21:26:06 +00:00
|
|
|
|
* Затем выделяется память минимум в INITIAL_SIZE байт.
|
2013-12-08 02:29:40 +00:00
|
|
|
|
*
|
2013-09-15 10:53:53 +00:00
|
|
|
|
* Если вставлять элементы push_back-ом, не делая reserve, то PODArray примерно в 2.5 раза быстрее std::vector.
|
2016-04-14 21:26:06 +00:00
|
|
|
|
*
|
|
|
|
|
* Шаблонный параметр pad_right - всегда выделять в конце массива столько неиспользуемых байт.
|
|
|
|
|
* Может использоваться для того, чтобы делать оптимистичное чтение, запись, копирование невыровненными SIMD-инструкциями.
|
2013-09-15 10:53:53 +00:00
|
|
|
|
*/
|
2016-04-15 00:33:21 +00:00
|
|
|
|
template <typename T, size_t INITIAL_SIZE = 4096, typename TAllocator = Allocator<false>, size_t pad_right_ = 0>
|
2015-11-15 03:09:40 +00:00
|
|
|
|
class PODArray : private boost::noncopyable, private TAllocator /// empty base optimization
|
2013-09-15 10:53:53 +00:00
|
|
|
|
{
|
|
|
|
|
private:
|
2016-04-14 21:26:06 +00:00
|
|
|
|
/// Округление padding-а вверх до целого количества элементов, чтобы упростить арифметику.
|
|
|
|
|
static constexpr size_t pad_right = (pad_right_ + sizeof(T) - 1) / sizeof(T) * sizeof(T);
|
|
|
|
|
|
|
|
|
|
char * c_start = nullptr;
|
|
|
|
|
char * c_end = nullptr;
|
|
|
|
|
char * c_end_of_storage = nullptr; /// Не включает в себя pad_right.
|
2013-09-15 10:53:53 +00:00
|
|
|
|
|
|
|
|
|
T * t_start() { return reinterpret_cast<T *>(c_start); }
|
|
|
|
|
T * t_end() { return reinterpret_cast<T *>(c_end); }
|
|
|
|
|
T * t_end_of_storage() { return reinterpret_cast<T *>(c_end_of_storage); }
|
2014-07-06 04:22:12 +00:00
|
|
|
|
|
2013-09-15 10:53:53 +00:00
|
|
|
|
const T * t_start() const { return reinterpret_cast<const T *>(c_start); }
|
|
|
|
|
const T * t_end() const { return reinterpret_cast<const T *>(c_end); }
|
|
|
|
|
const T * t_end_of_storage() const { return reinterpret_cast<const T *>(c_end_of_storage); }
|
|
|
|
|
|
2016-04-14 21:26:06 +00:00
|
|
|
|
/// Количество памяти, занимаемое num_elements элементов.
|
|
|
|
|
static size_t byte_size(size_t num_elements) { return num_elements * sizeof(T); }
|
|
|
|
|
|
|
|
|
|
/// Минимальное количество памяти, которое нужно выделить для num_elements элементов, включая padding.
|
|
|
|
|
static size_t minimum_memory_for_elements(size_t num_elements) { return byte_size(num_elements) + pad_right; }
|
2013-09-15 10:53:53 +00:00
|
|
|
|
|
|
|
|
|
static size_t round_up_to_power_of_two(size_t n)
|
|
|
|
|
{
|
|
|
|
|
--n;
|
|
|
|
|
n |= n >> 1;
|
|
|
|
|
n |= n >> 2;
|
|
|
|
|
n |= n >> 4;
|
|
|
|
|
n |= n >> 8;
|
|
|
|
|
n |= n >> 16;
|
|
|
|
|
n |= n >> 32;
|
|
|
|
|
++n;
|
|
|
|
|
|
|
|
|
|
return n;
|
|
|
|
|
}
|
2014-07-06 04:22:12 +00:00
|
|
|
|
|
2016-04-14 21:26:06 +00:00
|
|
|
|
void alloc_for_num_elements(size_t num_elements)
|
2013-09-15 10:53:53 +00:00
|
|
|
|
{
|
2016-04-14 21:26:06 +00:00
|
|
|
|
alloc(round_up_to_power_of_two(minimum_memory_for_elements(num_elements)));
|
|
|
|
|
}
|
2014-05-03 22:57:43 +00:00
|
|
|
|
|
2016-04-14 21:26:06 +00:00
|
|
|
|
void alloc(size_t bytes)
|
|
|
|
|
{
|
|
|
|
|
c_start = c_end = reinterpret_cast<char *>(TAllocator::alloc(bytes));
|
|
|
|
|
c_end_of_storage = c_start + bytes - pad_right;
|
2013-09-15 10:53:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void dealloc()
|
|
|
|
|
{
|
2014-04-08 07:31:51 +00:00
|
|
|
|
if (c_start == nullptr)
|
2013-12-08 02:29:40 +00:00
|
|
|
|
return;
|
2014-07-06 04:22:12 +00:00
|
|
|
|
|
2016-04-14 21:26:06 +00:00
|
|
|
|
TAllocator::free(c_start, allocated_size());
|
2013-09-15 10:53:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-14 21:26:06 +00:00
|
|
|
|
void realloc(size_t bytes)
|
2013-09-15 10:53:53 +00:00
|
|
|
|
{
|
2014-04-08 07:31:51 +00:00
|
|
|
|
if (c_start == nullptr)
|
2013-12-08 02:29:40 +00:00
|
|
|
|
{
|
2016-04-14 21:26:06 +00:00
|
|
|
|
alloc(bytes);
|
2013-12-08 02:29:40 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2014-07-06 04:22:12 +00:00
|
|
|
|
|
2013-09-15 10:53:53 +00:00
|
|
|
|
ptrdiff_t end_diff = c_end - c_start;
|
|
|
|
|
|
2016-04-14 21:26:06 +00:00
|
|
|
|
c_start = reinterpret_cast<char *>(TAllocator::realloc(c_start, allocated_size(), bytes));
|
2014-04-02 18:38:17 +00:00
|
|
|
|
|
2013-09-15 10:53:53 +00:00
|
|
|
|
c_end = c_start + end_diff;
|
2016-04-14 21:26:06 +00:00
|
|
|
|
c_end_of_storage = c_start + bytes - pad_right;
|
2013-09-15 10:53:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-08 14:53:00 +00:00
|
|
|
|
bool isInitialized() const
|
|
|
|
|
{
|
|
|
|
|
return (c_start != nullptr) && (c_end != nullptr) && (c_end_of_storage != nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-07 22:49:38 +00:00
|
|
|
|
bool isAllocatedFromStack() const
|
|
|
|
|
{
|
|
|
|
|
constexpr size_t stack_threshold = TAllocator::getStackThreshold();
|
|
|
|
|
return (stack_threshold > 0) && (allocated_size() <= stack_threshold);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-15 10:53:53 +00:00
|
|
|
|
public:
|
2016-05-28 10:35:44 +00:00
|
|
|
|
using value_type = T;
|
2013-09-15 10:53:53 +00:00
|
|
|
|
|
2016-04-14 21:26:06 +00:00
|
|
|
|
size_t allocated_size() const { return c_end_of_storage - c_start + pad_right; }
|
2013-09-15 10:53:53 +00:00
|
|
|
|
|
|
|
|
|
/// Просто typedef нельзя, так как возникает неоднозначность для конструкторов и функций assign.
|
2013-12-08 02:29:40 +00:00
|
|
|
|
struct iterator : public boost::iterator_adaptor<iterator, T*>
|
2013-09-15 10:53:53 +00:00
|
|
|
|
{
|
|
|
|
|
iterator() {}
|
2014-12-19 18:33:30 +00:00
|
|
|
|
iterator(T * ptr_) : iterator::iterator_adaptor_(ptr_) {}
|
2013-09-15 10:53:53 +00:00
|
|
|
|
};
|
|
|
|
|
|
2013-12-08 02:29:40 +00:00
|
|
|
|
struct const_iterator : public boost::iterator_adaptor<const_iterator, const T*>
|
2013-09-15 10:53:53 +00:00
|
|
|
|
{
|
|
|
|
|
const_iterator() {}
|
2013-12-08 02:29:40 +00:00
|
|
|
|
const_iterator(const T * ptr_) : const_iterator::iterator_adaptor_(ptr_) {}
|
2013-09-15 10:53:53 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2016-04-14 21:26:06 +00:00
|
|
|
|
PODArray() {}
|
|
|
|
|
|
2016-07-07 16:56:49 +00:00
|
|
|
|
PODArray(size_t n)
|
2016-04-14 21:26:06 +00:00
|
|
|
|
{
|
|
|
|
|
alloc_for_num_elements(n);
|
|
|
|
|
c_end += byte_size(n);
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-07 16:56:49 +00:00
|
|
|
|
PODArray(size_t n, const T & x)
|
2016-04-14 21:26:06 +00:00
|
|
|
|
{
|
|
|
|
|
alloc_for_num_elements(n);
|
|
|
|
|
assign(n, x);
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-07 16:56:49 +00:00
|
|
|
|
PODArray(const_iterator from_begin, const_iterator from_end)
|
2016-04-14 21:26:06 +00:00
|
|
|
|
{
|
|
|
|
|
alloc_for_num_elements(from_end - from_begin);
|
|
|
|
|
insert(from_begin, from_end);
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-07 16:56:49 +00:00
|
|
|
|
~PODArray()
|
2016-04-14 21:26:06 +00:00
|
|
|
|
{
|
|
|
|
|
dealloc();
|
|
|
|
|
}
|
2013-09-15 10:53:53 +00:00
|
|
|
|
|
2015-11-15 05:52:41 +00:00
|
|
|
|
PODArray(PODArray && other)
|
|
|
|
|
{
|
2016-07-08 16:08:32 +00:00
|
|
|
|
this->swap(other);
|
2015-11-15 05:52:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-26 14:51:28 +00:00
|
|
|
|
PODArray & operator=(PODArray && other)
|
|
|
|
|
{
|
2016-07-08 16:08:32 +00:00
|
|
|
|
this->swap(other);
|
2015-02-26 14:51:28 +00:00
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-28 12:32:43 +00:00
|
|
|
|
T * data() { return t_start(); }
|
|
|
|
|
const T * data() const { return t_start(); }
|
2013-12-08 02:29:40 +00:00
|
|
|
|
|
2016-07-07 16:56:49 +00:00
|
|
|
|
size_t size() const { return t_end() - t_start(); }
|
|
|
|
|
bool empty() const { return t_end() == t_start(); }
|
|
|
|
|
size_t capacity() const { return t_end_of_storage() - t_start(); }
|
2013-09-15 10:53:53 +00:00
|
|
|
|
|
|
|
|
|
T & operator[] (size_t n) { return t_start()[n]; }
|
|
|
|
|
const T & operator[] (size_t n) const { return t_start()[n]; }
|
|
|
|
|
|
|
|
|
|
T & front() { return t_start()[0]; }
|
|
|
|
|
T & back() { return t_end()[-1]; }
|
|
|
|
|
const T & front() const { return t_start()[0]; }
|
|
|
|
|
const T & back() const { return t_end()[-1]; }
|
|
|
|
|
|
|
|
|
|
iterator begin() { return t_start(); }
|
|
|
|
|
iterator end() { return t_end(); }
|
|
|
|
|
const_iterator begin() const { return t_start(); }
|
|
|
|
|
const_iterator end() const { return t_end(); }
|
2015-10-15 19:41:32 +00:00
|
|
|
|
const_iterator cbegin() const { return t_start(); }
|
|
|
|
|
const_iterator cend() const { return t_end(); }
|
2013-09-15 10:53:53 +00:00
|
|
|
|
|
|
|
|
|
void reserve(size_t n)
|
|
|
|
|
{
|
|
|
|
|
if (n > capacity())
|
2016-04-14 21:26:06 +00:00
|
|
|
|
realloc(round_up_to_power_of_two(minimum_memory_for_elements(n)));
|
2013-09-15 10:53:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void reserve()
|
|
|
|
|
{
|
2013-12-08 02:29:40 +00:00
|
|
|
|
if (size() == 0)
|
2016-04-14 21:26:06 +00:00
|
|
|
|
realloc(std::max(INITIAL_SIZE, minimum_memory_for_elements(1)));
|
2013-12-08 02:29:40 +00:00
|
|
|
|
else
|
2016-04-14 21:26:06 +00:00
|
|
|
|
realloc(allocated_size() * 2);
|
2013-09-15 10:53:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void resize(size_t n)
|
|
|
|
|
{
|
|
|
|
|
reserve(n);
|
2014-08-28 11:59:41 +00:00
|
|
|
|
resize_assume_reserved(n);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void resize_assume_reserved(const size_t n)
|
|
|
|
|
{
|
2013-09-15 10:53:53 +00:00
|
|
|
|
c_end = c_start + byte_size(n);
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-03 10:01:04 +00:00
|
|
|
|
/// Как resize, но обнуляет новые элементы.
|
|
|
|
|
void resize_fill(size_t n)
|
|
|
|
|
{
|
|
|
|
|
size_t old_size = size();
|
|
|
|
|
if (n > old_size)
|
|
|
|
|
{
|
|
|
|
|
reserve(n);
|
|
|
|
|
memset(c_end, 0, n - old_size);
|
|
|
|
|
}
|
|
|
|
|
c_end = c_start + byte_size(n);
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-28 16:23:07 +00:00
|
|
|
|
void resize_fill(size_t n, const T & value)
|
|
|
|
|
{
|
|
|
|
|
size_t old_size = size();
|
|
|
|
|
if (n > old_size)
|
|
|
|
|
{
|
|
|
|
|
reserve(n);
|
2015-03-23 13:26:13 +00:00
|
|
|
|
std::fill(t_end(), t_end() + n - old_size, value);
|
2015-01-28 16:23:07 +00:00
|
|
|
|
}
|
|
|
|
|
c_end = c_start + byte_size(n);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-15 10:53:53 +00:00
|
|
|
|
void clear()
|
|
|
|
|
{
|
|
|
|
|
c_end = c_start;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void push_back(const T & x)
|
|
|
|
|
{
|
|
|
|
|
if (unlikely(c_end == c_end_of_storage))
|
|
|
|
|
reserve();
|
|
|
|
|
|
|
|
|
|
*t_end() = x;
|
|
|
|
|
c_end += byte_size(1);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-15 05:52:41 +00:00
|
|
|
|
template <typename... Args>
|
|
|
|
|
void emplace_back(Args &&... args)
|
|
|
|
|
{
|
|
|
|
|
if (unlikely(c_end == c_end_of_storage))
|
|
|
|
|
reserve();
|
|
|
|
|
|
|
|
|
|
new (t_end()) T(std::forward<Args>(args)...);
|
|
|
|
|
c_end += byte_size(1);
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-16 16:39:39 +00:00
|
|
|
|
void pop_back()
|
|
|
|
|
{
|
|
|
|
|
c_end -= byte_size(1);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-15 10:53:53 +00:00
|
|
|
|
/// Не вставляйте в массив кусок самого себя. Потому что при ресайзе, итераторы на самого себя могут инвалидироваться.
|
2013-12-08 02:29:40 +00:00
|
|
|
|
template <typename It1, typename It2>
|
|
|
|
|
void insert(It1 from_begin, It2 from_end)
|
2013-09-15 10:53:53 +00:00
|
|
|
|
{
|
|
|
|
|
size_t required_capacity = size() + (from_end - from_begin);
|
|
|
|
|
if (required_capacity > capacity())
|
|
|
|
|
reserve(round_up_to_power_of_two(required_capacity));
|
|
|
|
|
|
2014-08-17 07:39:28 +00:00
|
|
|
|
insert_assume_reserved(from_begin, from_end);
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-16 16:39:39 +00:00
|
|
|
|
template <typename It1, typename It2>
|
|
|
|
|
void insert(iterator it, It1 from_begin, It2 from_end)
|
|
|
|
|
{
|
|
|
|
|
size_t required_capacity = size() + (from_end - from_begin);
|
|
|
|
|
if (required_capacity > capacity())
|
|
|
|
|
reserve(round_up_to_power_of_two(required_capacity));
|
|
|
|
|
|
|
|
|
|
size_t bytes_to_copy = byte_size(from_end - from_begin);
|
|
|
|
|
size_t bytes_to_move = (end() - it) * sizeof(T);
|
|
|
|
|
|
|
|
|
|
if (unlikely(bytes_to_move))
|
|
|
|
|
memcpy(c_end + bytes_to_copy - bytes_to_move, c_end - bytes_to_move, bytes_to_move);
|
|
|
|
|
|
|
|
|
|
memcpy(c_end - bytes_to_move, reinterpret_cast<const void *>(&*from_begin), bytes_to_copy);
|
|
|
|
|
c_end += bytes_to_copy;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-17 07:39:28 +00:00
|
|
|
|
template <typename It1, typename It2>
|
|
|
|
|
void insert_assume_reserved(It1 from_begin, It2 from_end)
|
|
|
|
|
{
|
2013-09-15 10:53:53 +00:00
|
|
|
|
size_t bytes_to_copy = byte_size(from_end - from_begin);
|
|
|
|
|
memcpy(c_end, reinterpret_cast<const void *>(&*from_begin), bytes_to_copy);
|
|
|
|
|
c_end += bytes_to_copy;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-15 07:05:01 +00:00
|
|
|
|
void swap(PODArray & rhs)
|
2013-09-15 10:53:53 +00:00
|
|
|
|
{
|
2016-07-08 14:53:00 +00:00
|
|
|
|
/// Swap two PODArray objects, arr1 and arr2, that satisfy the following conditions:
|
|
|
|
|
/// - The elements of arr1 are stored on stack.
|
|
|
|
|
/// - The elements of arr2 are stored on heap.
|
|
|
|
|
auto swap_stack_heap = [](PODArray & arr1, PODArray & arr2)
|
2016-07-07 16:56:49 +00:00
|
|
|
|
{
|
2016-07-08 14:53:00 +00:00
|
|
|
|
size_t stack_size = arr1.size();
|
|
|
|
|
size_t stack_allocated = arr1.allocated_size();
|
2016-07-07 22:49:38 +00:00
|
|
|
|
|
2016-07-08 14:53:00 +00:00
|
|
|
|
size_t heap_size = arr2.size();
|
|
|
|
|
size_t heap_allocated = arr2.allocated_size();
|
2016-07-07 22:49:38 +00:00
|
|
|
|
|
2016-07-08 14:53:00 +00:00
|
|
|
|
/// Keep track of the stack content we have to copy.
|
|
|
|
|
char * stack_c_start = arr1.c_start;
|
2016-07-07 22:49:38 +00:00
|
|
|
|
|
2016-07-08 14:53:00 +00:00
|
|
|
|
/// arr1 takes ownership of the heap memory of arr2.
|
|
|
|
|
arr1.c_start = arr2.c_start;
|
|
|
|
|
arr1.c_end_of_storage = arr1.c_start + heap_allocated - arr1.pad_right;
|
|
|
|
|
arr1.c_end = arr1.c_start + byte_size(heap_size);
|
2016-07-07 22:49:38 +00:00
|
|
|
|
|
2016-07-08 14:53:00 +00:00
|
|
|
|
/// Allocate stack space for arr2.
|
|
|
|
|
arr2.alloc(stack_allocated);
|
|
|
|
|
/// Copy the stack content.
|
|
|
|
|
memcpy(arr2.c_start, stack_c_start, byte_size(stack_size));
|
|
|
|
|
arr2.c_end = arr2.c_start + byte_size(stack_size);
|
2016-07-07 22:49:38 +00:00
|
|
|
|
};
|
|
|
|
|
|
2016-07-08 16:08:32 +00:00
|
|
|
|
auto do_move = [](PODArray & src, PODArray & dest)
|
|
|
|
|
{
|
|
|
|
|
if (src.isAllocatedFromStack())
|
|
|
|
|
{
|
|
|
|
|
dest.dealloc();
|
|
|
|
|
dest.alloc(src.allocated_size());
|
|
|
|
|
memcpy(dest.c_start, src.c_start, byte_size(src.size()));
|
|
|
|
|
dest.c_end = dest.c_start + (src.c_end - src.c_start);
|
|
|
|
|
|
|
|
|
|
src.c_start = nullptr;
|
|
|
|
|
src.c_end = nullptr;
|
|
|
|
|
src.c_end_of_storage = nullptr;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
std::swap(dest.c_start, src.c_start);
|
|
|
|
|
std::swap(dest.c_end, src.c_end);
|
|
|
|
|
std::swap(dest.c_end_of_storage, src.c_end_of_storage);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-07-08 14:53:00 +00:00
|
|
|
|
if (!isInitialized() && !rhs.isInitialized())
|
|
|
|
|
return;
|
|
|
|
|
else if (!isInitialized() && rhs.isInitialized())
|
|
|
|
|
{
|
2016-07-08 16:08:32 +00:00
|
|
|
|
do_move(rhs, *this);
|
2016-07-08 14:53:00 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else if (isInitialized() && !rhs.isInitialized())
|
|
|
|
|
{
|
2016-07-08 16:08:32 +00:00
|
|
|
|
do_move(*this, rhs);
|
2016-07-08 14:53:00 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-07 22:49:38 +00:00
|
|
|
|
if (isAllocatedFromStack() && rhs.isAllocatedFromStack())
|
|
|
|
|
{
|
|
|
|
|
size_t min_size = std::min(size(), rhs.size());
|
|
|
|
|
size_t max_size = std::max(size(), rhs.size());
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < min_size; ++i)
|
|
|
|
|
std::swap(this->operator[](i), rhs[i]);
|
|
|
|
|
|
|
|
|
|
if (size() == max_size)
|
|
|
|
|
{
|
|
|
|
|
for (size_t i = min_size; i < max_size; ++i)
|
|
|
|
|
rhs[i] = this->operator[](i);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for (size_t i = min_size; i < max_size; ++i)
|
|
|
|
|
this->operator[](i) = rhs[i];
|
|
|
|
|
}
|
2016-07-07 16:59:01 +00:00
|
|
|
|
|
2016-07-07 16:56:49 +00:00
|
|
|
|
size_t lhs_size = size();
|
2016-07-07 22:49:38 +00:00
|
|
|
|
size_t lhs_allocated = allocated_size();
|
2016-07-07 16:56:49 +00:00
|
|
|
|
|
2016-07-07 22:49:38 +00:00
|
|
|
|
size_t rhs_size = rhs.size();
|
|
|
|
|
size_t rhs_allocated = rhs.allocated_size();
|
2016-07-07 16:56:49 +00:00
|
|
|
|
|
2016-07-07 22:49:38 +00:00
|
|
|
|
c_end_of_storage = c_start + rhs_allocated - pad_right;
|
|
|
|
|
rhs.c_end_of_storage = rhs.c_start + lhs_allocated - pad_right;
|
2016-07-07 16:56:49 +00:00
|
|
|
|
|
2016-07-07 22:49:38 +00:00
|
|
|
|
c_end = c_start + byte_size(rhs_size);
|
|
|
|
|
rhs.c_end = rhs.c_start + byte_size(lhs_size);
|
2016-07-07 16:56:49 +00:00
|
|
|
|
}
|
2016-07-07 22:49:38 +00:00
|
|
|
|
else if (isAllocatedFromStack() && !rhs.isAllocatedFromStack())
|
|
|
|
|
swap_stack_heap(*this, rhs);
|
|
|
|
|
else if (!isAllocatedFromStack() && rhs.isAllocatedFromStack())
|
|
|
|
|
swap_stack_heap(rhs, *this);
|
2016-07-07 16:56:49 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
std::swap(c_start, rhs.c_start);
|
|
|
|
|
std::swap(c_end, rhs.c_end);
|
|
|
|
|
std::swap(c_end_of_storage, rhs.c_end_of_storage);
|
|
|
|
|
}
|
2013-09-15 10:53:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void assign(size_t n, const T & x)
|
|
|
|
|
{
|
|
|
|
|
resize(n);
|
|
|
|
|
std::fill(begin(), end(), x);
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-08 02:29:40 +00:00
|
|
|
|
template <typename It1, typename It2>
|
|
|
|
|
void assign(It1 from_begin, It2 from_end)
|
2013-09-15 10:53:53 +00:00
|
|
|
|
{
|
|
|
|
|
size_t required_capacity = from_end - from_begin;
|
|
|
|
|
if (required_capacity > capacity())
|
|
|
|
|
reserve(round_up_to_power_of_two(required_capacity));
|
|
|
|
|
|
|
|
|
|
size_t bytes_to_copy = byte_size(required_capacity);
|
2013-12-08 02:29:40 +00:00
|
|
|
|
memcpy(c_start, reinterpret_cast<const void *>(&*from_begin), bytes_to_copy);
|
2013-09-15 10:53:53 +00:00
|
|
|
|
c_end = c_start + bytes_to_copy;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-15 07:05:01 +00:00
|
|
|
|
void assign(const PODArray & from)
|
2013-09-15 10:53:53 +00:00
|
|
|
|
{
|
|
|
|
|
assign(from.begin(), from.end());
|
|
|
|
|
}
|
2013-12-08 02:29:40 +00:00
|
|
|
|
|
|
|
|
|
|
2015-11-15 07:05:01 +00:00
|
|
|
|
bool operator== (const PODArray & other) const
|
2013-12-08 02:29:40 +00:00
|
|
|
|
{
|
|
|
|
|
if (size() != other.size())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
const_iterator this_it = begin();
|
|
|
|
|
const_iterator that_it = other.begin();
|
|
|
|
|
|
|
|
|
|
while (this_it != end())
|
|
|
|
|
{
|
|
|
|
|
if (*this_it != *that_it)
|
|
|
|
|
return false;
|
2014-07-06 04:22:12 +00:00
|
|
|
|
|
2013-12-08 02:29:40 +00:00
|
|
|
|
++this_it;
|
|
|
|
|
++that_it;
|
|
|
|
|
}
|
2014-07-06 04:22:12 +00:00
|
|
|
|
|
2013-12-08 02:29:40 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-15 07:05:01 +00:00
|
|
|
|
bool operator!= (const PODArray & other) const
|
2013-12-08 02:29:40 +00:00
|
|
|
|
{
|
|
|
|
|
return !operator==(other);
|
|
|
|
|
}
|
2013-09-15 10:53:53 +00:00
|
|
|
|
};
|
|
|
|
|
|
2016-07-08 15:10:47 +00:00
|
|
|
|
template <typename T, size_t INITIAL_SIZE, typename TAllocator, size_t pad_right_>
|
2016-07-08 15:30:57 +00:00
|
|
|
|
void swap(PODArray<T, INITIAL_SIZE, TAllocator, pad_right_> & lhs, PODArray<T, INITIAL_SIZE, TAllocator, pad_right_> & rhs)
|
2016-07-08 15:10:47 +00:00
|
|
|
|
{
|
|
|
|
|
lhs.swap(rhs);
|
|
|
|
|
}
|
2013-09-15 10:53:53 +00:00
|
|
|
|
|
2016-04-14 21:26:06 +00:00
|
|
|
|
/** Для столбцов. Padding-а хватает, чтобы читать и писать xmm-регистр по адресу последнего элемента. */
|
2016-04-15 00:33:21 +00:00
|
|
|
|
template <typename T, size_t INITIAL_SIZE = 4096, typename TAllocator = Allocator<false>>
|
|
|
|
|
using PaddedPODArray = PODArray<T, INITIAL_SIZE, TAllocator, 15>;
|
2016-04-14 21:26:06 +00:00
|
|
|
|
|
2013-09-15 10:53:53 +00:00
|
|
|
|
}
|