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>
|
|
|
|
#include <AK/kmalloc.h>
|
2018-10-10 12:53:07 +03:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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-03-16 15:24:39 +03:00
|
|
|
for (int i = 0; i < other.size(); ++i)
|
2018-11-05 12:23:00 +03:00
|
|
|
unchecked_append(other[i]);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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) {
|
2018-11-03 02:31:42 +03:00
|
|
|
if (at(i) == value)
|
|
|
|
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-04-20 14:34:37 +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);
|
|
|
|
at(index).~T();
|
|
|
|
for (int i = index + 1; i < m_size; ++i) {
|
|
|
|
new (slot(i - 1)) T(move(at(i)));
|
|
|
|
at(i).~T();
|
|
|
|
}
|
|
|
|
|
|
|
|
--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)
|
|
|
|
{
|
|
|
|
ASSERT(index <= size());
|
|
|
|
if (index == size())
|
|
|
|
return append(value);
|
|
|
|
grow_capacity(size() + 1);
|
|
|
|
++m_size;
|
|
|
|
for (int i = size() - 1; i > index; --i) {
|
|
|
|
new (slot(i)) T(move(at(i - 1)));
|
|
|
|
at(i - 1).~T();
|
|
|
|
}
|
|
|
|
new (slot(index)) T(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());
|
2018-11-13 03:36:31 +03:00
|
|
|
for (const auto& v : other)
|
|
|
|
unchecked_append(v);
|
|
|
|
}
|
|
|
|
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-04-20 14:34:37 +03:00
|
|
|
Vector tmp = move(other);
|
|
|
|
grow_capacity(size() + tmp.size());
|
|
|
|
for (auto&& v : tmp)
|
2018-11-05 12:23:00 +03:00
|
|
|
unchecked_append(move(v));
|
2018-10-28 12:26:07 +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-04-20 14:34:37 +03:00
|
|
|
new (slot(m_size)) T(value);
|
|
|
|
++m_size;
|
2018-11-05 12:23:00 +03:00
|
|
|
}
|
|
|
|
|
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-04-20 14:34:37 +03:00
|
|
|
grow_capacity(size() + 1);
|
|
|
|
new (slot(m_size)) T(value);
|
|
|
|
++m_size;
|
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-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-03-16 15:24:39 +03:00
|
|
|
for (int i = 0; i < count; ++i)
|
2019-04-20 14:34:37 +03:00
|
|
|
new (slot(m_size + i)) T(values[i]);
|
|
|
|
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));
|
|
|
|
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-03-16 15:24:39 +03:00
|
|
|
void resize(int new_size)
|
2018-11-01 15:39:28 +03:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_size > size()) {
|
|
|
|
ensure_capacity(new_size);
|
2019-03-16 15:24:39 +03:00
|
|
|
for (int i = size(); i < new_size; ++i)
|
2019-04-20 14:34:37 +03:00
|
|
|
new (slot(i)) T;
|
2019-03-07 18:49:04 +03:00
|
|
|
} else {
|
|
|
|
for (int i = new_size; i < size(); ++i)
|
2019-04-20 14:34:37 +03:00
|
|
|
at(i).~T();
|
2019-03-07 18:49:04 +03:00
|
|
|
}
|
2019-04-20 14:34:37 +03:00
|
|
|
m_size = new_size;
|
2018-11-01 15:39:28 +03:00
|
|
|
}
|
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
class Iterator {
|
|
|
|
public:
|
|
|
|
bool operator!=(const Iterator& other) { return m_index != other.m_index; }
|
2019-03-09 18:13:08 +03:00
|
|
|
bool operator==(const Iterator& other) { return m_index == other.m_index; }
|
|
|
|
bool operator<(const Iterator& other) { return m_index < other.m_index; }
|
2018-10-10 12:53:07 +03:00
|
|
|
Iterator& operator++() { ++m_index; return *this; }
|
2019-03-09 18:13:08 +03:00
|
|
|
Iterator operator-(int value) { return { m_vector, m_index - value }; }
|
|
|
|
Iterator operator+(int value) { return { m_vector, m_index + value }; }
|
2018-10-10 12:53:07 +03:00
|
|
|
T& operator*() { return m_vector[m_index]; }
|
2019-05-19 02:53:51 +03:00
|
|
|
int operator-(const Iterator& other) { return m_index - other.m_index; }
|
2018-10-10 12:53:07 +03:00
|
|
|
private:
|
|
|
|
friend class Vector;
|
2019-03-16 15:24:39 +03:00
|
|
|
Iterator(Vector& vector, int index) : m_vector(vector), m_index(index) { }
|
2018-10-10 12:53:07 +03:00
|
|
|
Vector& m_vector;
|
2019-03-16 15:24:39 +03:00
|
|
|
int m_index { 0 };
|
2018-10-10 12:53:07 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
Iterator begin() { return Iterator(*this, 0); }
|
|
|
|
Iterator end() { return Iterator(*this, size()); }
|
|
|
|
|
|
|
|
class ConstIterator {
|
|
|
|
public:
|
|
|
|
bool operator!=(const ConstIterator& other) { return m_index != other.m_index; }
|
2019-03-09 18:13:08 +03:00
|
|
|
bool operator==(const ConstIterator& other) { return m_index == other.m_index; }
|
|
|
|
bool operator<(const ConstIterator& other) { return m_index < other.m_index; }
|
2018-10-10 12:53:07 +03:00
|
|
|
ConstIterator& operator++() { ++m_index; return *this; }
|
2019-03-09 18:13:08 +03:00
|
|
|
ConstIterator operator-(int value) { return { m_vector, m_index - value }; }
|
|
|
|
ConstIterator operator+(int value) { return { m_vector, m_index + value }; }
|
2018-10-10 12:53:07 +03:00
|
|
|
const T& operator*() const { return m_vector[m_index]; }
|
2019-05-19 02:53:51 +03:00
|
|
|
int operator-(const ConstIterator& other) { return m_index - other.m_index; }
|
2018-10-10 12:53:07 +03:00
|
|
|
private:
|
|
|
|
friend class Vector;
|
2019-03-16 15:24:39 +03:00
|
|
|
ConstIterator(const Vector& vector, const int index) : m_vector(vector), m_index(index) { }
|
2018-10-10 12:53:07 +03:00
|
|
|
const Vector& m_vector;
|
2019-03-16 15:24:39 +03:00
|
|
|
int m_index { 0 };
|
2018-10-10 12:53:07 +03:00
|
|
|
};
|
|
|
|
|
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-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]; }
|
|
|
|
|
|
|
|
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); }
|
|
|
|
|
|
|
|
int m_size { 0 };
|
|
|
|
int m_capacity { 0 };
|
|
|
|
|
|
|
|
alignas(T) byte m_inline_buffer_storage[sizeof(T) * inline_capacity];
|
|
|
|
T* m_outline_buffer { nullptr };
|
2018-10-10 12:53:07 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::Vector;
|