2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2018-10-10 11:53:07 +02:00
|
|
|
#pragma once
|
|
|
|
|
2019-04-20 13:34:37 +02:00
|
|
|
#include <AK/Assertions.h>
|
2020-12-23 11:35:20 -07:00
|
|
|
#include <AK/Find.h>
|
2020-02-14 21:41:10 +01:00
|
|
|
#include <AK/Forward.h>
|
2020-09-06 21:14:08 +02:00
|
|
|
#include <AK/Iterator.h>
|
2020-02-25 14:49:47 +01:00
|
|
|
#include <AK/Optional.h>
|
2020-07-25 16:00:26 +02:00
|
|
|
#include <AK/Span.h>
|
2019-04-20 13:34:37 +02:00
|
|
|
#include <AK/StdLibExtras.h>
|
2019-06-29 19:14:03 +02:00
|
|
|
#include <AK/Traits.h>
|
2020-09-09 13:41:58 +02:00
|
|
|
#include <AK/TypedTransfer.h>
|
2019-04-20 13:34:37 +02:00
|
|
|
#include <AK/kmalloc.h>
|
2019-06-28 20:58:41 +02:00
|
|
|
|
2020-05-20 15:52:20 +02: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
|
2020-05-15 21:23:51 -06:00
|
|
|
# include <initializer_list>
|
2019-06-14 06:43:56 +02:00
|
|
|
#endif
|
|
|
|
|
2020-05-20 15:52:20 +02:00
|
|
|
#ifndef __serenity__
|
|
|
|
# include <new>
|
|
|
|
#endif
|
|
|
|
|
2018-10-10 11:53:07 +02:00
|
|
|
namespace AK {
|
|
|
|
|
2020-02-25 14:49:47 +01:00
|
|
|
template<typename T, size_t inline_capacity>
|
2019-04-20 13:34:37 +02:00
|
|
|
class Vector {
|
2018-10-10 11:53:07 +02:00
|
|
|
public:
|
2020-11-15 09:45:53 -07:00
|
|
|
using value_type = T;
|
|
|
|
|
2019-04-20 13:34:37 +02:00
|
|
|
Vector()
|
|
|
|
: m_capacity(inline_capacity)
|
2019-03-17 15:53:03 +01:00
|
|
|
{
|
|
|
|
}
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2019-04-20 13:34:37 +02:00
|
|
|
~Vector()
|
2018-10-13 01:17:36 +02:00
|
|
|
{
|
2019-04-20 13:34:37 +02:00
|
|
|
clear();
|
2018-10-13 01:17:36 +02:00
|
|
|
}
|
|
|
|
|
2020-05-20 15:52:20 +02:00
|
|
|
#ifndef SERENITY_LIBC_BUILD
|
2019-06-28 20:20:19 +02:00
|
|
|
Vector(std::initializer_list<T> list)
|
|
|
|
{
|
|
|
|
ensure_capacity(list.size());
|
|
|
|
for (auto& item : list)
|
|
|
|
unchecked_append(item);
|
|
|
|
}
|
2020-05-20 15:52:20 +02:00
|
|
|
#endif
|
2019-06-28 20:20:19 +02:00
|
|
|
|
2018-10-10 11:53:07 +02:00
|
|
|
Vector(Vector&& other)
|
2019-04-20 13:34:37 +02:00
|
|
|
: m_size(other.m_size)
|
|
|
|
, m_capacity(other.m_capacity)
|
|
|
|
, m_outline_buffer(other.m_outline_buffer)
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2019-04-20 13:34:37 +02:00
|
|
|
if constexpr (inline_capacity > 0) {
|
|
|
|
if (!m_outline_buffer) {
|
2020-02-25 14:49:47 +01:00
|
|
|
for (size_t i = 0; i < m_size; ++i) {
|
2019-04-20 13:34:37 +02:00
|
|
|
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 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2018-11-05 10:23:00 +01:00
|
|
|
Vector(const Vector& other)
|
|
|
|
{
|
2019-01-19 22:53:05 +01:00
|
|
|
ensure_capacity(other.size());
|
2019-08-07 15:25:34 +02:00
|
|
|
TypedTransfer<T>::copy(data(), other.data(), other.size());
|
|
|
|
m_size = other.size();
|
2018-11-05 10:23:00 +01:00
|
|
|
}
|
|
|
|
|
2020-02-25 14:49:47 +01:00
|
|
|
template<size_t other_inline_capacity>
|
2020-01-04 10:57:30 +01:00
|
|
|
Vector(const Vector<T, other_inline_capacity>& other)
|
|
|
|
{
|
|
|
|
ensure_capacity(other.size());
|
|
|
|
TypedTransfer<T>::copy(data(), other.data(), other.size());
|
|
|
|
m_size = other.size();
|
|
|
|
}
|
|
|
|
|
2020-07-25 16:00:26 +02:00
|
|
|
Span<T> span() { return { data(), size() }; }
|
|
|
|
Span<const T> span() const { return { data(), size() }; }
|
|
|
|
|
2019-04-20 13:34:37 +02:00
|
|
|
// FIXME: What about assigning from a vector with lower inline capacity?
|
2018-10-10 11:53:07 +02:00
|
|
|
Vector& operator=(Vector&& other)
|
|
|
|
{
|
2019-04-20 13:34:37 +02: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) {
|
2020-02-25 14:49:47 +01:00
|
|
|
for (size_t i = 0; i < m_size; ++i) {
|
2019-04-20 13:34:37 +02:00
|
|
|
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 11:53:07 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
2019-04-20 13:34:37 +02:00
|
|
|
clear_with_capacity();
|
|
|
|
if (m_outline_buffer) {
|
|
|
|
kfree(m_outline_buffer);
|
|
|
|
m_outline_buffer = nullptr;
|
|
|
|
}
|
|
|
|
reset_capacity();
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2019-01-12 07:22:25 +01:00
|
|
|
void clear_with_capacity()
|
|
|
|
{
|
2020-02-25 14:49:47 +01:00
|
|
|
for (size_t i = 0; i < m_size; ++i)
|
2019-04-20 13:34:37 +02:00
|
|
|
data()[i].~T();
|
|
|
|
m_size = 0;
|
2019-01-12 07:22:25 +01:00
|
|
|
}
|
|
|
|
|
2020-11-15 09:45:53 -07:00
|
|
|
template<typename V>
|
|
|
|
bool operator==(const V& other) const
|
2019-07-11 14:05:22 +02:00
|
|
|
{
|
2020-11-15 09:45:53 -07:00
|
|
|
if (m_size != other.size())
|
2019-07-11 14:05:22 +02:00
|
|
|
return false;
|
2019-08-07 15:05:10 +02:00
|
|
|
return TypedTransfer<T>::compare(data(), other.data(), size());
|
2019-07-11 14:05:22 +02:00
|
|
|
}
|
|
|
|
|
2020-08-15 18:38:24 +02:00
|
|
|
operator Span<T>() { return span(); }
|
|
|
|
operator Span<const T>() const { return span(); }
|
|
|
|
|
2018-11-03 00:31:42 +01:00
|
|
|
bool contains_slow(const T& value) const
|
|
|
|
{
|
2020-02-25 14:49:47 +01:00
|
|
|
for (size_t i = 0; i < size(); ++i) {
|
2020-09-06 22:49:59 +03:00
|
|
|
if (Traits<T>::equals(at(i), value))
|
2018-11-03 00:31:42 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-20 14:13:40 +02:00
|
|
|
// NOTE: Vector::is_null() exists for the benefit of String::copy().
|
2020-01-25 12:14:59 +01:00
|
|
|
bool is_null() const { return false; }
|
2018-12-21 02:10:45 +01:00
|
|
|
bool is_empty() const { return size() == 0; }
|
2020-04-27 18:52:40 +04:30
|
|
|
ALWAYS_INLINE size_t size() const { return m_size; }
|
2020-02-25 14:49:47 +01:00
|
|
|
size_t capacity() const { return m_capacity; }
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2019-04-20 13:34:37 +02: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-27 23:42:20 +02:00
|
|
|
|
2020-04-27 18:52:40 +04:30
|
|
|
ALWAYS_INLINE const T& at(size_t i) const
|
2019-05-28 11:53:16 +02:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(i < m_size);
|
2019-05-28 11:53:16 +02:00
|
|
|
return data()[i];
|
|
|
|
}
|
2020-04-27 18:52:40 +04:30
|
|
|
ALWAYS_INLINE T& at(size_t i)
|
2019-05-28 11:53:16 +02:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(i < m_size);
|
2019-05-28 11:53:16 +02:00
|
|
|
return data()[i];
|
|
|
|
}
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2020-04-27 18:52:40 +04:30
|
|
|
ALWAYS_INLINE const T& operator[](size_t i) const { return at(i); }
|
|
|
|
ALWAYS_INLINE T& operator[](size_t i) { return at(i); }
|
2018-10-10 11:53:07 +02: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-19 22:53:05 +01:00
|
|
|
T take_last()
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(!is_empty());
|
2018-10-16 12:10:01 +02:00
|
|
|
T value = move(last());
|
2018-10-10 11:53:07 +02:00
|
|
|
last().~T();
|
2019-04-20 13:34:37 +02:00
|
|
|
--m_size;
|
2018-10-10 11:53:07 +02:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2019-01-14 02:49:30 +01:00
|
|
|
T take_first()
|
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(!is_empty());
|
2019-01-14 02:49:30 +01:00
|
|
|
T value = move(first());
|
|
|
|
remove(0);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-02-25 14:49:47 +01:00
|
|
|
T take(size_t index)
|
2019-11-07 20:38:24 +01:00
|
|
|
{
|
|
|
|
T value = move(at(index));
|
|
|
|
remove(index);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-07-02 11:51:46 +03:00
|
|
|
T unstable_take(size_t index)
|
2020-01-15 19:24:25 +01:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(index < m_size);
|
2020-01-15 19:24:25 +01:00
|
|
|
swap(at(index), at(m_size - 1));
|
2020-07-02 11:51:46 +03:00
|
|
|
return take_last();
|
2020-01-15 19:24:25 +01:00
|
|
|
}
|
|
|
|
|
2020-02-25 14:49:47 +01:00
|
|
|
void remove(size_t index)
|
2018-10-13 01:17:36 +02:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(index < m_size);
|
2019-08-12 10:35:05 +02:00
|
|
|
|
|
|
|
if constexpr (Traits<T>::is_trivial()) {
|
|
|
|
TypedTransfer<T>::copy(slot(index), slot(index + 1), m_size - index - 1);
|
|
|
|
} else {
|
|
|
|
at(index).~T();
|
2020-02-25 14:49:47 +01:00
|
|
|
for (size_t i = index + 1; i < m_size; ++i) {
|
2019-08-12 10:35:05 +02:00
|
|
|
new (slot(i - 1)) T(move(at(i)));
|
|
|
|
at(i).~T();
|
|
|
|
}
|
2019-04-20 13:34:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
--m_size;
|
2018-10-13 01:17:36 +02:00
|
|
|
}
|
|
|
|
|
2020-12-21 23:21:09 -07:00
|
|
|
void remove(size_t index, size_t count)
|
|
|
|
{
|
|
|
|
if (count == 0)
|
|
|
|
return;
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(index + count > index);
|
|
|
|
VERIFY(index + count <= m_size);
|
2020-12-21 23:21:09 -07:00
|
|
|
|
|
|
|
if constexpr (Traits<T>::is_trivial()) {
|
|
|
|
TypedTransfer<T>::copy(slot(index), slot(index + count), m_size - index - count);
|
|
|
|
} else {
|
|
|
|
for (size_t i = index; i < index + count; i++)
|
|
|
|
at(i).~T();
|
|
|
|
for (size_t i = index + count; i < m_size; ++i) {
|
|
|
|
new (slot(i - count)) T(move(at(i)));
|
|
|
|
at(i).~T();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_size -= count;
|
|
|
|
}
|
|
|
|
|
2021-01-15 16:04:16 -07:00
|
|
|
template<typename U = T>
|
2021-04-24 16:51:31 +10:00
|
|
|
[[nodiscard]] bool try_insert(size_t index, U&& value)
|
2019-03-07 15:52:11 +01:00
|
|
|
{
|
2021-04-24 16:51:31 +10:00
|
|
|
if (index > size())
|
|
|
|
return false;
|
2019-03-07 15:52:11 +01:00
|
|
|
if (index == size())
|
2021-04-24 16:51:31 +10:00
|
|
|
return try_append(forward<U>(value));
|
|
|
|
if (!try_grow_capacity(size() + 1))
|
|
|
|
return false;
|
2019-04-20 13:34:37 +02:00
|
|
|
++m_size;
|
2020-01-19 12:15:43 +01:00
|
|
|
if constexpr (Traits<T>::is_trivial()) {
|
|
|
|
TypedTransfer<T>::move(slot(index + 1), slot(index), m_size - index - 1);
|
|
|
|
} else {
|
2020-02-25 14:49:47 +01:00
|
|
|
for (size_t i = size() - 1; i > index; --i) {
|
2020-01-19 12:15:43 +01:00
|
|
|
new (slot(i)) T(move(at(i - 1)));
|
|
|
|
at(i - 1).~T();
|
|
|
|
}
|
2019-03-07 15:52:11 +01:00
|
|
|
}
|
2021-01-15 16:04:16 -07:00
|
|
|
new (slot(index)) T(forward<U>(value));
|
2021-04-24 16:51:31 +10:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U = T>
|
|
|
|
void insert(size_t index, U&& value)
|
|
|
|
{
|
|
|
|
auto did_allocate = try_insert<U>(index, forward<U>(value));
|
|
|
|
VERIFY(did_allocate);
|
2019-03-07 15:52:11 +01:00
|
|
|
}
|
|
|
|
|
2021-01-15 16:04:16 -07:00
|
|
|
template<typename C, typename U = T>
|
2021-04-24 16:51:31 +10:00
|
|
|
[[nodiscard]] bool try_insert_before_matching(U&& value, C callback, size_t first_index = 0, size_t* inserted_index = nullptr)
|
2019-07-04 14:20:48 +02:00
|
|
|
{
|
2020-02-25 14:49:47 +01:00
|
|
|
for (size_t i = first_index; i < size(); ++i) {
|
2019-07-04 14:20:48 +02:00
|
|
|
if (callback(at(i))) {
|
2021-04-24 16:51:31 +10:00
|
|
|
if (!try_insert(i, forward<U>(value)))
|
|
|
|
return false;
|
2020-01-19 13:18:27 +01:00
|
|
|
if (inserted_index)
|
|
|
|
*inserted_index = i;
|
2021-04-24 16:51:31 +10:00
|
|
|
return true;
|
2019-07-04 14:20:48 +02:00
|
|
|
}
|
|
|
|
}
|
2021-04-24 16:51:31 +10:00
|
|
|
if (!try_append(forward<U>(value)))
|
|
|
|
return false;
|
2020-01-19 13:18:27 +01:00
|
|
|
if (inserted_index)
|
|
|
|
*inserted_index = size() - 1;
|
2021-04-24 16:51:31 +10:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename C, typename U = T>
|
|
|
|
void insert_before_matching(U&& value, C callback, size_t first_index = 0, size_t* inserted_index = nullptr)
|
|
|
|
{
|
|
|
|
auto did_allocate = try_insert_before_matching(forward<U>(value), callback, first_index, inserted_index);
|
|
|
|
VERIFY(did_allocate);
|
2019-07-04 14:20:48 +02:00
|
|
|
}
|
|
|
|
|
2019-04-20 13:34:37 +02:00
|
|
|
Vector& operator=(const Vector& other)
|
2018-11-13 01:36:31 +01:00
|
|
|
{
|
|
|
|
if (this != &other) {
|
|
|
|
clear();
|
2019-01-19 22:53:05 +01:00
|
|
|
ensure_capacity(other.size());
|
2019-08-07 15:25:34 +02:00
|
|
|
TypedTransfer<T>::copy(data(), other.data(), other.size());
|
|
|
|
m_size = other.size();
|
2018-11-13 01:36:31 +01:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-02-25 14:49:47 +01:00
|
|
|
template<size_t other_inline_capacity>
|
2020-01-04 10:57:30 +01:00
|
|
|
Vector& operator=(const Vector<T, other_inline_capacity>& other)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
ensure_capacity(other.size());
|
|
|
|
TypedTransfer<T>::copy(data(), other.data(), other.size());
|
|
|
|
m_size = other.size();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-04-24 16:51:31 +10:00
|
|
|
[[nodiscard]] bool try_append(Vector&& other)
|
2018-10-28 10:26:07 +01:00
|
|
|
{
|
2019-04-20 13:34:37 +02:00
|
|
|
if (is_empty()) {
|
|
|
|
*this = move(other);
|
2021-04-24 16:51:31 +10:00
|
|
|
return true;
|
2019-02-07 09:08:59 +01:00
|
|
|
}
|
2019-08-07 15:25:34 +02:00
|
|
|
auto other_size = other.size();
|
2019-04-20 13:34:37 +02:00
|
|
|
Vector tmp = move(other);
|
2021-04-24 16:51:31 +10:00
|
|
|
if (!try_grow_capacity(size() + other_size))
|
|
|
|
return false;
|
2019-08-07 15:25:34 +02:00
|
|
|
TypedTransfer<T>::move(data() + m_size, tmp.data(), other_size);
|
|
|
|
m_size += other_size;
|
2021-04-24 16:51:31 +10:00
|
|
|
return true;
|
2018-10-28 10:26:07 +01:00
|
|
|
}
|
|
|
|
|
2021-04-24 16:51:31 +10:00
|
|
|
void append(Vector&& other)
|
|
|
|
{
|
|
|
|
auto did_allocate = try_append(move(other));
|
|
|
|
VERIFY(did_allocate);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool try_append(const Vector& other)
|
2019-07-04 13:54:37 +02:00
|
|
|
{
|
2021-04-24 16:51:31 +10:00
|
|
|
if (!try_grow_capacity(size() + other.size()))
|
|
|
|
return false;
|
2019-08-07 15:25:34 +02:00
|
|
|
TypedTransfer<T>::copy(data() + m_size, other.data(), other.size());
|
|
|
|
m_size += other.m_size;
|
2021-04-24 16:51:31 +10:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void append(const Vector& other)
|
|
|
|
{
|
|
|
|
auto did_allocate = try_append(other);
|
|
|
|
VERIFY(did_allocate);
|
2019-07-04 13:54:37 +02:00
|
|
|
}
|
|
|
|
|
2020-11-21 18:12:35 +00:00
|
|
|
template<typename Callback>
|
|
|
|
Optional<T> first_matching(Callback callback)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < size(); ++i) {
|
|
|
|
if (callback(at(i))) {
|
|
|
|
return at(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
Optional<T> last_matching(Callback callback)
|
|
|
|
{
|
|
|
|
for (ssize_t i = size() - 1; i >= 0; --i) {
|
|
|
|
if (callback(at(i))) {
|
|
|
|
return at(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-03-18 20:50:39 +01:00
|
|
|
template<typename Callback>
|
2020-10-06 18:38:18 +02:00
|
|
|
bool remove_first_matching(Callback callback)
|
2019-03-18 20:50:39 +01:00
|
|
|
{
|
2020-02-25 14:49:47 +01:00
|
|
|
for (size_t i = 0; i < size(); ++i) {
|
2019-03-18 20:50:39 +01:00
|
|
|
if (callback(at(i))) {
|
|
|
|
remove(i);
|
2020-10-06 18:38:18 +02:00
|
|
|
return true;
|
2019-03-18 20:50:39 +01:00
|
|
|
}
|
|
|
|
}
|
2020-10-06 18:38:18 +02:00
|
|
|
return false;
|
2019-03-18 20:50:39 +01:00
|
|
|
}
|
|
|
|
|
2019-12-22 18:29:12 +01:00
|
|
|
template<typename Callback>
|
|
|
|
void remove_all_matching(Callback callback)
|
|
|
|
{
|
2020-02-25 14:49:47 +01:00
|
|
|
for (size_t i = 0; i < size();) {
|
2019-12-22 18:29:12 +01:00
|
|
|
if (callback(at(i))) {
|
|
|
|
remove(i);
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-15 16:04:16 -07:00
|
|
|
template<typename U = T>
|
|
|
|
ALWAYS_INLINE void unchecked_append(U&& value)
|
2018-10-28 10:26:07 +01:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY((size() + 1) <= capacity());
|
2021-01-15 16:04:16 -07:00
|
|
|
new (slot(m_size)) T(forward<U>(value));
|
2019-04-20 13:34:37 +02:00
|
|
|
++m_size;
|
2018-10-28 10:26:07 +01:00
|
|
|
}
|
|
|
|
|
2019-08-01 15:35:45 +02:00
|
|
|
template<class... Args>
|
2021-04-24 16:51:31 +10:00
|
|
|
[[nodiscard]] bool try_empend(Args&&... args)
|
2019-08-01 15:35:45 +02:00
|
|
|
{
|
2021-04-24 16:51:31 +10:00
|
|
|
if (!try_grow_capacity(m_size + 1))
|
|
|
|
return false;
|
2020-12-27 17:06:37 -05:00
|
|
|
new (slot(m_size)) T { forward<Args>(args)... };
|
2019-08-01 15:35:45 +02:00
|
|
|
++m_size;
|
2021-04-24 16:51:31 +10:00
|
|
|
return true;
|
2019-08-01 15:35:45 +02:00
|
|
|
}
|
|
|
|
|
2021-04-24 16:51:31 +10:00
|
|
|
template<class... Args>
|
|
|
|
void empend(Args&&... args)
|
|
|
|
{
|
|
|
|
auto did_allocate = try_empend(forward<Args>(args)...);
|
|
|
|
VERIFY(did_allocate);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] ALWAYS_INLINE bool try_append(T&& value)
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2021-04-24 16:51:31 +10:00
|
|
|
if (!try_grow_capacity(size() + 1))
|
|
|
|
return false;
|
2019-04-20 13:34:37 +02:00
|
|
|
new (slot(m_size)) T(move(value));
|
|
|
|
++m_size;
|
2021-04-24 16:51:31 +10:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE void append(T&& value)
|
|
|
|
{
|
|
|
|
auto did_allocate = try_append(move(value));
|
|
|
|
VERIFY(did_allocate);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] ALWAYS_INLINE bool try_append(const T& value)
|
|
|
|
{
|
|
|
|
return try_append(T(value));
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2020-04-27 18:52:40 +04:30
|
|
|
ALWAYS_INLINE void append(const T& value)
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2021-04-24 16:51:31 +10:00
|
|
|
auto did_allocate = try_append(T(value));
|
|
|
|
VERIFY(did_allocate);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U = T>
|
|
|
|
[[nodiscard]] bool try_prepend(U&& value)
|
|
|
|
{
|
|
|
|
return try_insert(0, forward<U>(value));
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2021-01-15 16:04:16 -07:00
|
|
|
template<typename U = T>
|
|
|
|
void prepend(U&& value)
|
2019-04-16 03:47:24 +02:00
|
|
|
{
|
2021-04-24 16:51:31 +10:00
|
|
|
auto did_allocate = try_insert(0, forward<U>(value));
|
|
|
|
VERIFY(did_allocate);
|
2019-04-16 03:47:24 +02:00
|
|
|
}
|
|
|
|
|
2021-04-24 16:51:31 +10:00
|
|
|
[[nodiscard]] bool try_prepend(Vector&& other)
|
2019-07-20 16:10:52 +02:00
|
|
|
{
|
|
|
|
if (other.is_empty())
|
2021-04-24 16:51:31 +10:00
|
|
|
return true;
|
2019-07-20 16:10:52 +02:00
|
|
|
|
|
|
|
if (is_empty()) {
|
|
|
|
*this = move(other);
|
2021-04-24 16:51:31 +10:00
|
|
|
return true;
|
2019-07-20 16:10:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto other_size = other.size();
|
2021-04-24 16:51:31 +10:00
|
|
|
if (!try_grow_capacity(size() + other_size))
|
|
|
|
return false;
|
2019-07-20 16:10:52 +02:00
|
|
|
|
2020-02-25 14:49:47 +01:00
|
|
|
for (size_t i = size() + other_size - 1; i >= other.size(); --i) {
|
2019-07-20 16:10:52 +02:00
|
|
|
new (slot(i)) T(move(at(i - other_size)));
|
|
|
|
at(i - other_size).~T();
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector tmp = move(other);
|
2019-08-07 11:53:21 +02:00
|
|
|
TypedTransfer<T>::move(slot(0), tmp.data(), tmp.size());
|
2019-07-20 16:10:52 +02:00
|
|
|
m_size += other_size;
|
2021-04-24 16:51:31 +10:00
|
|
|
return true;
|
2019-07-20 16:10:52 +02:00
|
|
|
}
|
|
|
|
|
2021-04-24 16:51:31 +10:00
|
|
|
void prepend(Vector&& other)
|
|
|
|
{
|
|
|
|
auto did_allocate = try_prepend(move(other));
|
|
|
|
VERIFY(did_allocate);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool try_prepend(const T* values, size_t count)
|
2020-11-21 21:45:36 +03:00
|
|
|
{
|
|
|
|
if (!count)
|
2021-04-24 16:51:31 +10:00
|
|
|
return true;
|
|
|
|
if (!try_grow_capacity(size() + count))
|
|
|
|
return false;
|
2020-11-21 21:45:36 +03:00
|
|
|
TypedTransfer<T>::move(slot(count), slot(0), m_size);
|
|
|
|
TypedTransfer<T>::copy(slot(0), values, count);
|
|
|
|
m_size += count;
|
2021-04-24 16:51:31 +10:00
|
|
|
return true;
|
2020-11-21 21:45:36 +03:00
|
|
|
}
|
|
|
|
|
2021-04-24 16:51:31 +10:00
|
|
|
void prepend(const T* values, size_t count)
|
|
|
|
{
|
|
|
|
auto did_allocate = try_prepend(values, count);
|
|
|
|
VERIFY(did_allocate);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool try_append(const T* values, size_t count)
|
2018-11-16 17:56:18 +01:00
|
|
|
{
|
2019-03-17 15:53:03 +01:00
|
|
|
if (!count)
|
2021-04-24 16:51:31 +10:00
|
|
|
return true;
|
|
|
|
if (!try_grow_capacity(size() + count))
|
|
|
|
return false;
|
2019-08-07 11:53:21 +02:00
|
|
|
TypedTransfer<T>::copy(slot(m_size), values, count);
|
2019-04-20 13:34:37 +02:00
|
|
|
m_size += count;
|
2021-04-24 16:51:31 +10:00
|
|
|
return true;
|
2018-11-16 17:56:18 +01:00
|
|
|
}
|
|
|
|
|
2021-04-24 16:51:31 +10:00
|
|
|
void append(const T* values, size_t count)
|
|
|
|
{
|
|
|
|
auto did_allocate = try_append(values, count);
|
|
|
|
VERIFY(did_allocate);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool try_grow_capacity(size_t needed_capacity)
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2019-04-20 13:34:37 +02:00
|
|
|
if (m_capacity >= needed_capacity)
|
2021-04-24 16:51:31 +10:00
|
|
|
return true;
|
|
|
|
return try_ensure_capacity(padded_capacity(needed_capacity));
|
2019-04-20 13:34:37 +02:00
|
|
|
}
|
|
|
|
|
2021-04-24 16:51:31 +10:00
|
|
|
void grow_capacity(size_t needed_capacity)
|
|
|
|
{
|
|
|
|
auto did_allocate = try_grow_capacity(needed_capacity);
|
|
|
|
VERIFY(did_allocate);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool try_ensure_capacity(size_t needed_capacity)
|
2019-04-20 13:34:37 +02:00
|
|
|
{
|
|
|
|
if (m_capacity >= needed_capacity)
|
2021-04-24 16:51:31 +10:00
|
|
|
return true;
|
2020-02-25 14:49:47 +01:00
|
|
|
size_t new_capacity = needed_capacity;
|
2019-04-20 13:34:37 +02:00
|
|
|
auto* new_buffer = (T*)kmalloc(new_capacity * sizeof(T));
|
2021-04-24 16:51:31 +10:00
|
|
|
if (new_buffer == nullptr)
|
|
|
|
return false;
|
2019-08-07 15:35:23 +02:00
|
|
|
|
|
|
|
if constexpr (Traits<T>::is_trivial()) {
|
|
|
|
TypedTransfer<T>::copy(new_buffer, data(), m_size);
|
|
|
|
} else {
|
2020-02-25 14:49:47 +01:00
|
|
|
for (size_t i = 0; i < m_size; ++i) {
|
2019-08-07 15:35:23 +02:00
|
|
|
new (&new_buffer[i]) T(move(at(i)));
|
|
|
|
at(i).~T();
|
|
|
|
}
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
2019-04-20 13:34:37 +02:00
|
|
|
if (m_outline_buffer)
|
|
|
|
kfree(m_outline_buffer);
|
|
|
|
m_outline_buffer = new_buffer;
|
|
|
|
m_capacity = new_capacity;
|
2021-04-24 16:51:31 +10:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ensure_capacity(size_t needed_capacity)
|
|
|
|
{
|
|
|
|
auto did_allocate = try_ensure_capacity(needed_capacity);
|
|
|
|
VERIFY(did_allocate);
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2020-05-03 18:38:16 +02:00
|
|
|
void shrink(size_t new_size, bool keep_capacity = false)
|
2018-11-01 13:39:28 +01:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(new_size <= size());
|
2019-03-07 16:49:04 +01:00
|
|
|
if (new_size == size())
|
2018-11-01 13:39:28 +01:00
|
|
|
return;
|
2019-03-07 16:49:04 +01:00
|
|
|
|
|
|
|
if (!new_size) {
|
2020-05-03 18:38:16 +02:00
|
|
|
if (keep_capacity)
|
|
|
|
clear_with_capacity();
|
|
|
|
else
|
|
|
|
clear();
|
2019-03-07 16:49:04 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-25 14:49:47 +01:00
|
|
|
for (size_t i = new_size; i < size(); ++i)
|
2019-07-24 09:33:26 +02:00
|
|
|
at(i).~T();
|
|
|
|
m_size = new_size;
|
|
|
|
}
|
|
|
|
|
2021-04-24 16:51:31 +10:00
|
|
|
[[nodiscard]] bool try_resize(size_t new_size, bool keep_capacity = false)
|
2019-07-24 09:33:26 +02:00
|
|
|
{
|
2021-04-24 16:51:31 +10:00
|
|
|
if (new_size <= size()) {
|
|
|
|
shrink(new_size, keep_capacity);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!try_ensure_capacity(new_size))
|
|
|
|
return false;
|
2019-07-24 09:33:26 +02:00
|
|
|
|
2020-02-25 14:49:47 +01:00
|
|
|
for (size_t i = size(); i < new_size; ++i)
|
2021-05-13 14:28:32 +02:00
|
|
|
new (slot(i)) T {};
|
2019-04-20 13:34:37 +02:00
|
|
|
m_size = new_size;
|
2021-04-24 16:51:31 +10:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void resize(size_t new_size, bool keep_capacity = false)
|
|
|
|
{
|
|
|
|
auto did_allocate = try_resize(new_size, keep_capacity);
|
|
|
|
VERIFY(did_allocate);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool try_resize_and_keep_capacity(size_t new_size)
|
|
|
|
{
|
|
|
|
return try_resize(new_size, true);
|
2018-11-01 13:39:28 +01:00
|
|
|
}
|
|
|
|
|
2020-05-03 18:38:16 +02:00
|
|
|
void resize_and_keep_capacity(size_t new_size)
|
|
|
|
{
|
2021-04-24 16:51:31 +10:00
|
|
|
auto did_allocate = try_resize_and_keep_capacity(new_size);
|
|
|
|
VERIFY(did_allocate);
|
2020-05-03 18:38:16 +02:00
|
|
|
}
|
|
|
|
|
2020-09-06 21:14:08 +02:00
|
|
|
using ConstIterator = SimpleIterator<const Vector, const T>;
|
|
|
|
using Iterator = SimpleIterator<Vector, T>;
|
|
|
|
|
|
|
|
ConstIterator begin() const { return ConstIterator::begin(*this); }
|
|
|
|
Iterator begin() { return Iterator::begin(*this); }
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2020-09-06 21:14:08 +02:00
|
|
|
ConstIterator end() const { return ConstIterator::end(*this); }
|
|
|
|
Iterator end() { return Iterator::end(*this); }
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2020-12-23 11:35:20 -07:00
|
|
|
template<typename TUnaryPredicate>
|
|
|
|
ConstIterator find_if(TUnaryPredicate&& finder) const
|
2019-08-04 19:21:08 +02:00
|
|
|
{
|
2020-12-23 11:35:20 -07:00
|
|
|
return AK::find_if(begin(), end(), forward<TUnaryPredicate>(finder));
|
2019-08-04 19:21:08 +02:00
|
|
|
}
|
|
|
|
|
2020-12-23 11:35:20 -07:00
|
|
|
template<typename TUnaryPredicate>
|
|
|
|
Iterator find_if(TUnaryPredicate&& finder)
|
2019-08-04 19:21:08 +02:00
|
|
|
{
|
2020-12-23 11:35:20 -07:00
|
|
|
return AK::find_if(begin(), end(), forward<TUnaryPredicate>(finder));
|
2019-08-04 19:21:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ConstIterator find(const T& value) const
|
|
|
|
{
|
2020-12-23 11:35:20 -07:00
|
|
|
return AK::find(begin(), end(), value);
|
2019-08-04 19:21:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Iterator find(const T& value)
|
|
|
|
{
|
2020-12-23 11:35:20 -07:00
|
|
|
return AK::find(begin(), end(), value);
|
2019-08-04 19:21:08 +02:00
|
|
|
}
|
|
|
|
|
2020-02-25 14:49:47 +01:00
|
|
|
Optional<size_t> find_first_index(const T& value)
|
2019-12-18 00:15:36 +03:00
|
|
|
{
|
2020-12-23 11:35:20 -07:00
|
|
|
if (const auto index = AK::find_index(begin(), end(), value);
|
|
|
|
index < size()) {
|
|
|
|
return index;
|
2019-12-18 00:15:36 +03:00
|
|
|
}
|
2020-02-25 14:49:47 +01:00
|
|
|
return {};
|
2019-12-18 00:15:36 +03:00
|
|
|
}
|
2019-12-22 18:29:12 +01:00
|
|
|
|
2019-03-16 13:24:39 +01:00
|
|
|
private:
|
2019-04-20 13:34:37 +02:00
|
|
|
void reset_capacity()
|
|
|
|
{
|
|
|
|
m_capacity = inline_capacity;
|
|
|
|
}
|
|
|
|
|
2020-02-25 14:49:47 +01:00
|
|
|
static size_t padded_capacity(size_t capacity)
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2020-02-25 14:49:47 +01:00
|
|
|
return max(static_cast<size_t>(4), capacity + (capacity / 4) + 4);
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2020-02-25 14:49:47 +01:00
|
|
|
T* slot(size_t i) { return &data()[i]; }
|
|
|
|
const T* slot(size_t i) const { return &data()[i]; }
|
2019-04-20 13:34:37 +02:00
|
|
|
|
2019-05-28 11:53:16 +02: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 13:34:37 +02:00
|
|
|
|
2020-02-25 14:49:47 +01:00
|
|
|
size_t m_size { 0 };
|
|
|
|
size_t m_capacity { 0 };
|
2019-04-20 13:34:37 +02:00
|
|
|
|
2020-03-08 11:57:24 +01:00
|
|
|
alignas(T) unsigned char m_inline_buffer_storage[sizeof(T) * inline_capacity];
|
2019-04-20 13:34:37 +02:00
|
|
|
T* m_outline_buffer { nullptr };
|
2018-10-10 11:53:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::Vector;
|