2018-10-10 12:53:07 +03:00
|
|
|
#pragma once
|
|
|
|
|
2019-04-20 14:34:37 +03:00
|
|
|
#include <AK/Assertions.h>
|
|
|
|
#include <AK/StdLibExtras.h>
|
2019-06-29 20:14:03 +03:00
|
|
|
#include <AK/Traits.h>
|
2019-04-20 14:34:37 +03:00
|
|
|
#include <AK/kmalloc.h>
|
2019-06-28 21:58:41 +03:00
|
|
|
|
|
|
|
// NOTE: We can't include <initializer_list> during the toolchain bootstrap,
|
|
|
|
// since it's part of libstdc++, and libstdc++ depends on LibC.
|
|
|
|
// For this reason, we don't support Vector(initializer_list) in LibC.
|
|
|
|
#ifndef SERENITY_LIBC_BUILD
|
2019-06-29 20:14:03 +03:00
|
|
|
# include <initializer_list>
|
2019-06-28 21:58:41 +03:00
|
|
|
#endif
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2019-06-14 07:43:56 +03:00
|
|
|
#ifndef __serenity__
|
2019-06-29 20:14:03 +03:00
|
|
|
# include <new>
|
2019-06-14 07:43:56 +03:00
|
|
|
#endif
|
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
namespace AK {
|
|
|
|
|
2019-06-29 20:14:03 +03:00
|
|
|
template<typename T, int inline_capacity>
|
|
|
|
class Vector;
|
2019-06-27 13:04:27 +03:00
|
|
|
|
|
|
|
template<typename VectorType, typename ElementType>
|
|
|
|
class VectorIterator {
|
|
|
|
public:
|
2019-08-04 20:21:08 +03:00
|
|
|
bool operator!=(const VectorIterator& other) const { return m_index != other.m_index; }
|
|
|
|
bool operator==(const VectorIterator& other) const { return m_index == other.m_index; }
|
|
|
|
bool operator<(const VectorIterator& other) const { return m_index < other.m_index; }
|
|
|
|
bool operator>(const VectorIterator& other) const { return m_index > other.m_index; }
|
|
|
|
bool operator>=(const VectorIterator& other) const { return m_index >= other.m_index; }
|
2019-06-27 13:04:27 +03:00
|
|
|
VectorIterator& operator++()
|
|
|
|
{
|
|
|
|
++m_index;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
VectorIterator& operator--()
|
|
|
|
{
|
|
|
|
--m_index;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
VectorIterator operator-(int value) { return { m_vector, m_index - value }; }
|
|
|
|
VectorIterator operator+(int value) { return { m_vector, m_index + value }; }
|
|
|
|
VectorIterator& operator=(const VectorIterator& other)
|
|
|
|
{
|
|
|
|
m_index = other.m_index;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
ElementType& operator*() { return m_vector[m_index]; }
|
|
|
|
int operator-(const VectorIterator& other) { return m_index - other.m_index; }
|
|
|
|
|
2019-08-04 20:21:08 +03:00
|
|
|
bool is_end() const { return m_index == m_vector.size(); }
|
2019-08-04 21:17:55 +03:00
|
|
|
int index() const { return m_index; }
|
2019-08-04 20:21:08 +03:00
|
|
|
|
2019-06-27 13:04:27 +03:00
|
|
|
private:
|
|
|
|
friend VectorType;
|
|
|
|
VectorIterator(VectorType& vector, int index)
|
|
|
|
: m_vector(vector)
|
|
|
|
, m_index(index)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
VectorType& m_vector;
|
|
|
|
int m_index { 0 };
|
|
|
|
};
|
|
|
|
|
2019-08-07 12:53:21 +03:00
|
|
|
template<typename T>
|
|
|
|
class TypedTransfer {
|
|
|
|
public:
|
|
|
|
static void move(T* destination, T* source, size_t count)
|
|
|
|
{
|
|
|
|
if constexpr (Traits<T>::is_trivial()) {
|
|
|
|
memmove(destination, source, count * sizeof(T));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < count; ++i)
|
|
|
|
new (&destination[i]) T(AK::move(source[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void copy(T* destination, const T* source, size_t count)
|
|
|
|
{
|
|
|
|
if constexpr (Traits<T>::is_trivial()) {
|
|
|
|
memmove(destination, source, count * sizeof(T));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < count; ++i)
|
|
|
|
new (&destination[i]) T(source[i]);
|
|
|
|
}
|
2019-08-07 16:05:10 +03:00
|
|
|
|
|
|
|
static bool compare(const T* a, const T* b, size_t count)
|
|
|
|
{
|
|
|
|
if constexpr (Traits<T>::is_trivial())
|
|
|
|
return !memcmp(a, b, count * sizeof(T));
|
|
|
|
|
|
|
|
for (size_t i = 0; i < count; ++i) {
|
|
|
|
if (a[i] != b[i])
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2019-08-07 12:53:21 +03:00
|
|
|
};
|
|
|
|
|
2019-04-20 14:34:37 +03:00
|
|
|
template<typename T, int inline_capacity = 0>
|
|
|
|
class Vector {
|
2018-10-10 12:53:07 +03:00
|
|
|
public:
|
2019-04-20 14:34:37 +03:00
|
|
|
Vector()
|
|
|
|
: m_capacity(inline_capacity)
|
2019-03-17 17:53:03 +03:00
|
|
|
{
|
|
|
|
}
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2019-04-20 14:34:37 +03:00
|
|
|
~Vector()
|
2018-10-13 02:17:36 +03:00
|
|
|
{
|
2019-04-20 14:34:37 +03:00
|
|
|
clear();
|
2018-10-13 02:17:36 +03:00
|
|
|
}
|
|
|
|
|
2019-06-28 21:58:41 +03:00
|
|
|
#ifndef SERENITY_LIBC_BUILD
|
2019-06-28 21:20:19 +03:00
|
|
|
Vector(std::initializer_list<T> list)
|
|
|
|
{
|
|
|
|
ensure_capacity(list.size());
|
|
|
|
for (auto& item : list)
|
|
|
|
unchecked_append(item);
|
|
|
|
}
|
2019-06-28 21:58:41 +03:00
|
|
|
#endif
|
2019-06-28 21:20:19 +03:00
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
Vector(Vector&& other)
|
2019-04-20 14:34:37 +03:00
|
|
|
: m_size(other.m_size)
|
|
|
|
, m_capacity(other.m_capacity)
|
|
|
|
, m_outline_buffer(other.m_outline_buffer)
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2019-04-20 14:34:37 +03:00
|
|
|
if constexpr (inline_capacity > 0) {
|
|
|
|
if (!m_outline_buffer) {
|
|
|
|
for (int i = 0; i < m_size; ++i) {
|
|
|
|
new (&inline_buffer()[i]) T(move(other.inline_buffer()[i]));
|
|
|
|
other.inline_buffer()[i].~T();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
other.m_outline_buffer = nullptr;
|
|
|
|
other.m_size = 0;
|
|
|
|
other.reset_capacity();
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2018-11-05 12:23:00 +03:00
|
|
|
Vector(const Vector& other)
|
|
|
|
{
|
2019-01-20 00:53:05 +03:00
|
|
|
ensure_capacity(other.size());
|
2019-08-07 16:25:34 +03:00
|
|
|
TypedTransfer<T>::copy(data(), other.data(), other.size());
|
|
|
|
m_size = other.size();
|
2018-11-05 12:23:00 +03:00
|
|
|
}
|
|
|
|
|
2019-04-20 14:34:37 +03:00
|
|
|
// FIXME: What about assigning from a vector with lower inline capacity?
|
2018-10-10 12:53:07 +03:00
|
|
|
Vector& operator=(Vector&& other)
|
|
|
|
{
|
2019-04-20 14:34:37 +03:00
|
|
|
if (this != &other) {
|
|
|
|
clear();
|
|
|
|
m_size = other.m_size;
|
|
|
|
m_capacity = other.m_capacity;
|
|
|
|
m_outline_buffer = other.m_outline_buffer;
|
|
|
|
if constexpr (inline_capacity > 0) {
|
|
|
|
if (!m_outline_buffer) {
|
|
|
|
for (int i = 0; i < m_size; ++i) {
|
|
|
|
new (&inline_buffer()[i]) T(move(other.inline_buffer()[i]));
|
|
|
|
other.inline_buffer()[i].~T();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
other.m_outline_buffer = nullptr;
|
|
|
|
other.m_size = 0;
|
|
|
|
other.reset_capacity();
|
|
|
|
}
|
2018-10-10 12:53:07 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
2019-04-20 14:34:37 +03:00
|
|
|
clear_with_capacity();
|
|
|
|
if (m_outline_buffer) {
|
|
|
|
kfree(m_outline_buffer);
|
|
|
|
m_outline_buffer = nullptr;
|
|
|
|
}
|
|
|
|
reset_capacity();
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2019-01-12 09:22:25 +03:00
|
|
|
void clear_with_capacity()
|
|
|
|
{
|
2019-04-20 14:34:37 +03:00
|
|
|
for (int i = 0; i < m_size; ++i)
|
|
|
|
data()[i].~T();
|
|
|
|
m_size = 0;
|
2019-01-12 09:22:25 +03:00
|
|
|
}
|
|
|
|
|
2019-07-11 15:05:22 +03:00
|
|
|
bool operator==(const Vector& other) const
|
|
|
|
{
|
|
|
|
if (m_size != other.m_size)
|
|
|
|
return false;
|
2019-08-07 16:05:10 +03:00
|
|
|
return TypedTransfer<T>::compare(data(), other.data(), size());
|
2019-07-11 15:05:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const Vector& other) const
|
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
2018-11-03 02:31:42 +03:00
|
|
|
bool contains_slow(const T& value) const
|
|
|
|
{
|
2019-03-16 15:24:39 +03:00
|
|
|
for (int i = 0; i < size(); ++i) {
|
2019-07-11 15:06:28 +03:00
|
|
|
if (at(i) == value)
|
2018-11-03 02:31:42 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-20 15:13:40 +03:00
|
|
|
// NOTE: Vector::is_null() exists for the benefit of String::copy().
|
|
|
|
bool is_null() const { return is_empty(); }
|
2018-12-21 04:10:45 +03:00
|
|
|
bool is_empty() const { return size() == 0; }
|
2019-04-20 14:34:37 +03:00
|
|
|
int size() const { return m_size; }
|
|
|
|
int capacity() const { return m_capacity; }
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2019-04-20 14:34:37 +03:00
|
|
|
T* data()
|
|
|
|
{
|
|
|
|
if constexpr (inline_capacity > 0)
|
|
|
|
return m_outline_buffer ? m_outline_buffer : inline_buffer();
|
|
|
|
return m_outline_buffer;
|
|
|
|
}
|
|
|
|
const T* data() const
|
|
|
|
{
|
|
|
|
if constexpr (inline_capacity > 0)
|
|
|
|
return m_outline_buffer ? m_outline_buffer : inline_buffer();
|
|
|
|
return m_outline_buffer;
|
|
|
|
}
|
2018-10-28 00:42:20 +03:00
|
|
|
|
2019-05-28 12:53:16 +03:00
|
|
|
const T& at(int i) const
|
|
|
|
{
|
|
|
|
ASSERT(i >= 0 && i < m_size);
|
|
|
|
return data()[i];
|
|
|
|
}
|
|
|
|
T& at(int i)
|
|
|
|
{
|
|
|
|
ASSERT(i >= 0 && i < m_size);
|
|
|
|
return data()[i];
|
|
|
|
}
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2019-03-16 15:24:39 +03:00
|
|
|
const T& operator[](int i) const { return at(i); }
|
|
|
|
T& operator[](int i) { return at(i); }
|
2018-10-10 12:53:07 +03:00
|
|
|
|
|
|
|
const T& first() const { return at(0); }
|
|
|
|
T& first() { return at(0); }
|
|
|
|
|
|
|
|
const T& last() const { return at(size() - 1); }
|
|
|
|
T& last() { return at(size() - 1); }
|
|
|
|
|
2019-01-20 00:53:05 +03:00
|
|
|
T take_last()
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2018-12-21 04:10:45 +03:00
|
|
|
ASSERT(!is_empty());
|
2018-10-16 13:10:01 +03:00
|
|
|
T value = move(last());
|
2018-10-10 12:53:07 +03:00
|
|
|
last().~T();
|
2019-04-20 14:34:37 +03:00
|
|
|
--m_size;
|
2018-10-10 12:53:07 +03:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2019-01-14 04:49:30 +03:00
|
|
|
T take_first()
|
|
|
|
{
|
|
|
|
ASSERT(!is_empty());
|
|
|
|
T value = move(first());
|
|
|
|
remove(0);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2019-03-16 15:24:39 +03:00
|
|
|
void remove(int index)
|
2018-10-13 02:17:36 +03:00
|
|
|
{
|
2019-04-20 14:34:37 +03:00
|
|
|
ASSERT(index < m_size);
|
2019-08-12 11:35:05 +03:00
|
|
|
|
|
|
|
if constexpr (Traits<T>::is_trivial()) {
|
|
|
|
TypedTransfer<T>::copy(slot(index), slot(index + 1), m_size - index - 1);
|
|
|
|
} else {
|
|
|
|
at(index).~T();
|
|
|
|
for (int i = index + 1; i < m_size; ++i) {
|
|
|
|
new (slot(i - 1)) T(move(at(i)));
|
|
|
|
at(i).~T();
|
|
|
|
}
|
2019-04-20 14:34:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
--m_size;
|
2018-10-13 02:17:36 +03:00
|
|
|
}
|
|
|
|
|
2019-03-07 17:52:11 +03:00
|
|
|
void insert(int index, T&& value)
|
|
|
|
{
|
|
|
|
ASSERT(index <= size());
|
|
|
|
if (index == size())
|
|
|
|
return append(move(value));
|
2019-04-20 14:34:37 +03:00
|
|
|
grow_capacity(size() + 1);
|
|
|
|
++m_size;
|
2019-03-07 17:52:11 +03:00
|
|
|
for (int i = size() - 1; i > index; --i) {
|
2019-04-20 14:34:37 +03:00
|
|
|
new (slot(i)) T(move(at(i - 1)));
|
|
|
|
at(i - 1).~T();
|
2019-03-07 17:52:11 +03:00
|
|
|
}
|
2019-04-20 14:34:37 +03:00
|
|
|
new (slot(index)) T(move(value));
|
2019-03-07 17:52:11 +03:00
|
|
|
}
|
|
|
|
|
2019-05-17 05:59:56 +03:00
|
|
|
void insert(int index, const T& value)
|
|
|
|
{
|
2019-07-04 14:54:37 +03:00
|
|
|
insert(index, T(value));
|
2019-05-17 05:59:56 +03:00
|
|
|
}
|
|
|
|
|
2019-07-04 15:20:48 +03:00
|
|
|
template<typename C>
|
|
|
|
void insert_before_matching(T&& value, C callback)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < size(); ++i) {
|
|
|
|
if (callback(at(i))) {
|
|
|
|
insert(i, move(value));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
append(move(value));
|
|
|
|
}
|
|
|
|
|
2019-04-20 14:34:37 +03:00
|
|
|
Vector& operator=(const Vector& other)
|
2018-11-13 03:36:31 +03:00
|
|
|
{
|
|
|
|
if (this != &other) {
|
|
|
|
clear();
|
2019-01-20 00:53:05 +03:00
|
|
|
ensure_capacity(other.size());
|
2019-08-07 16:25:34 +03:00
|
|
|
TypedTransfer<T>::copy(data(), other.data(), other.size());
|
|
|
|
m_size = other.size();
|
2018-11-13 03:36:31 +03:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-04-20 14:34:37 +03:00
|
|
|
void append(Vector&& other)
|
2018-10-28 12:26:07 +03:00
|
|
|
{
|
2019-04-20 14:34:37 +03:00
|
|
|
if (is_empty()) {
|
|
|
|
*this = move(other);
|
2019-02-07 11:08:59 +03:00
|
|
|
return;
|
|
|
|
}
|
2019-08-07 16:25:34 +03:00
|
|
|
auto other_size = other.size();
|
2019-04-20 14:34:37 +03:00
|
|
|
Vector tmp = move(other);
|
2019-08-07 16:25:34 +03:00
|
|
|
grow_capacity(size() + other_size);
|
|
|
|
TypedTransfer<T>::move(data() + m_size, tmp.data(), other_size);
|
|
|
|
m_size += other_size;
|
2018-10-28 12:26:07 +03:00
|
|
|
}
|
|
|
|
|
2019-07-04 14:54:37 +03:00
|
|
|
void append(const Vector& other)
|
|
|
|
{
|
|
|
|
grow_capacity(size() + other.size());
|
2019-08-07 16:25:34 +03:00
|
|
|
TypedTransfer<T>::copy(data() + m_size, other.data(), other.size());
|
|
|
|
m_size += other.m_size;
|
2019-07-04 14:54:37 +03:00
|
|
|
}
|
|
|
|
|
2019-03-18 22:50:39 +03:00
|
|
|
template<typename Callback>
|
|
|
|
void remove_first_matching(Callback callback)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < size(); ++i) {
|
|
|
|
if (callback(at(i))) {
|
|
|
|
remove(i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-05 12:23:00 +03:00
|
|
|
void unchecked_append(T&& value)
|
2018-10-28 12:26:07 +03:00
|
|
|
{
|
2018-12-27 00:02:24 +03:00
|
|
|
ASSERT((size() + 1) <= capacity());
|
2019-04-20 14:34:37 +03:00
|
|
|
new (slot(m_size)) T(move(value));
|
|
|
|
++m_size;
|
2018-10-28 12:26:07 +03:00
|
|
|
}
|
|
|
|
|
2018-11-05 12:23:00 +03:00
|
|
|
void unchecked_append(const T& value)
|
|
|
|
{
|
2019-07-04 14:54:37 +03:00
|
|
|
unchecked_append(T(value));
|
2018-11-05 12:23:00 +03:00
|
|
|
}
|
|
|
|
|
2019-08-01 16:35:45 +03:00
|
|
|
template<class... Args>
|
|
|
|
void empend(Args&&... args)
|
|
|
|
{
|
|
|
|
grow_capacity(m_size + 1);
|
|
|
|
new (slot(m_size)) T(forward<Args>(args)...);
|
|
|
|
++m_size;
|
|
|
|
}
|
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
void append(T&& value)
|
|
|
|
{
|
2019-04-20 14:34:37 +03:00
|
|
|
grow_capacity(size() + 1);
|
|
|
|
new (slot(m_size)) T(move(value));
|
|
|
|
++m_size;
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void append(const T& value)
|
|
|
|
{
|
2019-07-04 14:54:37 +03:00
|
|
|
append(T(value));
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2019-04-16 04:47:24 +03:00
|
|
|
void prepend(const T& value)
|
|
|
|
{
|
2019-04-20 14:34:37 +03:00
|
|
|
grow_capacity(size() + 1);
|
2019-04-16 04:47:24 +03:00
|
|
|
for (int i = size(); i > 0; --i) {
|
2019-04-20 14:34:37 +03:00
|
|
|
new (slot(i)) T(move(at(i - 1)));
|
2019-04-16 04:47:24 +03:00
|
|
|
at(i - 1).~T();
|
|
|
|
}
|
2019-04-20 14:34:37 +03:00
|
|
|
new (slot(0)) T(value);
|
|
|
|
++m_size;
|
2019-04-16 04:47:24 +03:00
|
|
|
}
|
|
|
|
|
2019-07-20 17:10:52 +03:00
|
|
|
void prepend(Vector&& other)
|
|
|
|
{
|
|
|
|
if (other.is_empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (is_empty()) {
|
|
|
|
*this = move(other);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto other_size = other.size();
|
|
|
|
grow_capacity(size() + other_size);
|
|
|
|
|
2019-07-21 13:51:54 +03:00
|
|
|
for (int i = size() + other_size - 1; i >= other.size(); --i) {
|
2019-07-20 17:10:52 +03:00
|
|
|
new (slot(i)) T(move(at(i - other_size)));
|
|
|
|
at(i - other_size).~T();
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector tmp = move(other);
|
2019-08-07 12:53:21 +03:00
|
|
|
TypedTransfer<T>::move(slot(0), tmp.data(), tmp.size());
|
2019-07-20 17:10:52 +03:00
|
|
|
m_size += other_size;
|
|
|
|
}
|
|
|
|
|
2019-03-16 15:24:39 +03:00
|
|
|
void append(const T* values, int count)
|
2018-11-16 19:56:18 +03:00
|
|
|
{
|
2019-03-17 17:53:03 +03:00
|
|
|
if (!count)
|
|
|
|
return;
|
2019-04-20 14:34:37 +03:00
|
|
|
grow_capacity(size() + count);
|
2019-08-07 12:53:21 +03:00
|
|
|
TypedTransfer<T>::copy(slot(m_size), values, count);
|
2019-04-20 14:34:37 +03:00
|
|
|
m_size += count;
|
2018-11-16 19:56:18 +03:00
|
|
|
}
|
|
|
|
|
2019-04-20 14:34:37 +03:00
|
|
|
void grow_capacity(int needed_capacity)
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2019-04-20 14:34:37 +03:00
|
|
|
if (m_capacity >= needed_capacity)
|
2018-10-10 12:53:07 +03:00
|
|
|
return;
|
2019-04-20 14:34:37 +03:00
|
|
|
ensure_capacity(padded_capacity(needed_capacity));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ensure_capacity(int needed_capacity)
|
|
|
|
{
|
|
|
|
if (m_capacity >= needed_capacity)
|
|
|
|
return;
|
|
|
|
int new_capacity = needed_capacity;
|
|
|
|
auto* new_buffer = (T*)kmalloc(new_capacity * sizeof(T));
|
2019-08-07 16:35:23 +03:00
|
|
|
|
|
|
|
if constexpr (Traits<T>::is_trivial()) {
|
|
|
|
TypedTransfer<T>::copy(new_buffer, data(), m_size);
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < m_size; ++i) {
|
|
|
|
new (&new_buffer[i]) T(move(at(i)));
|
|
|
|
at(i).~T();
|
|
|
|
}
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
2019-04-20 14:34:37 +03:00
|
|
|
if (m_outline_buffer)
|
|
|
|
kfree(m_outline_buffer);
|
|
|
|
m_outline_buffer = new_buffer;
|
|
|
|
m_capacity = new_capacity;
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2019-07-24 10:33:26 +03:00
|
|
|
void shrink(int new_size)
|
2018-11-01 15:39:28 +03:00
|
|
|
{
|
2019-07-24 10:33:26 +03:00
|
|
|
ASSERT(new_size <= size());
|
2019-03-07 18:49:04 +03:00
|
|
|
if (new_size == size())
|
2018-11-01 15:39:28 +03:00
|
|
|
return;
|
2019-03-07 18:49:04 +03:00
|
|
|
|
|
|
|
if (!new_size) {
|
|
|
|
clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-24 10:33:26 +03:00
|
|
|
for (int i = new_size; i < size(); ++i)
|
|
|
|
at(i).~T();
|
|
|
|
m_size = new_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void resize(int new_size)
|
|
|
|
{
|
|
|
|
if (new_size <= size())
|
|
|
|
return shrink(new_size);
|
|
|
|
|
|
|
|
ensure_capacity(new_size);
|
|
|
|
for (int i = size(); i < new_size; ++i)
|
|
|
|
new (slot(i)) T;
|
2019-04-20 14:34:37 +03:00
|
|
|
m_size = new_size;
|
2018-11-01 15:39:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-27 13:04:27 +03:00
|
|
|
using Iterator = VectorIterator<Vector, T>;
|
2018-10-10 12:53:07 +03:00
|
|
|
Iterator begin() { return Iterator(*this, 0); }
|
|
|
|
Iterator end() { return Iterator(*this, size()); }
|
|
|
|
|
2019-06-27 15:50:22 +03:00
|
|
|
using ConstIterator = VectorIterator<const Vector, const T>;
|
2018-10-13 02:17:36 +03:00
|
|
|
ConstIterator begin() const { return ConstIterator(*this, 0); }
|
|
|
|
ConstIterator end() const { return ConstIterator(*this, size()); }
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2019-08-04 20:21:08 +03:00
|
|
|
template<typename Finder>
|
|
|
|
ConstIterator find(Finder finder) const
|
|
|
|
{
|
|
|
|
for (int i = 0; i < m_size; ++i) {
|
|
|
|
if (finder(at(i)))
|
|
|
|
return ConstIterator(*this, i);
|
|
|
|
}
|
|
|
|
return end();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Finder>
|
|
|
|
Iterator find(Finder finder)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < m_size; ++i) {
|
|
|
|
if (finder(at(i)))
|
|
|
|
return Iterator(*this, i);
|
|
|
|
}
|
|
|
|
return end();
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstIterator find(const T& value) const
|
|
|
|
{
|
|
|
|
return find([&](auto& other) { return value == other; });
|
|
|
|
}
|
|
|
|
|
|
|
|
Iterator find(const T& value)
|
|
|
|
{
|
|
|
|
return find([&](auto& other) { return value == other; });
|
|
|
|
}
|
|
|
|
|
2019-03-16 15:24:39 +03:00
|
|
|
private:
|
2019-04-20 14:34:37 +03:00
|
|
|
void reset_capacity()
|
|
|
|
{
|
|
|
|
m_capacity = inline_capacity;
|
|
|
|
}
|
|
|
|
|
2019-03-16 15:24:39 +03:00
|
|
|
static int padded_capacity(int capacity)
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2019-03-16 15:24:39 +03:00
|
|
|
return max(int(4), capacity + (capacity / 4) + 4);
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2019-04-20 14:34:37 +03:00
|
|
|
T* slot(int i) { return &data()[i]; }
|
|
|
|
const T* slot(int i) const { return &data()[i]; }
|
|
|
|
|
2019-05-28 12:53:16 +03:00
|
|
|
T* inline_buffer()
|
|
|
|
{
|
|
|
|
static_assert(inline_capacity > 0);
|
|
|
|
return reinterpret_cast<T*>(m_inline_buffer_storage);
|
|
|
|
}
|
|
|
|
const T* inline_buffer() const
|
|
|
|
{
|
|
|
|
static_assert(inline_capacity > 0);
|
|
|
|
return reinterpret_cast<const T*>(m_inline_buffer_storage);
|
|
|
|
}
|
2019-04-20 14:34:37 +03:00
|
|
|
|
|
|
|
int m_size { 0 };
|
|
|
|
int m_capacity { 0 };
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
alignas(T) u8 m_inline_buffer_storage[sizeof(T) * inline_capacity];
|
2019-04-20 14:34:37 +03:00
|
|
|
T* m_outline_buffer { nullptr };
|
2018-10-10 12:53:07 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::Vector;
|