2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2021-11-10 10:55:37 +00:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2021-06-08 13:24:06 +00:00
|
|
|
* Copyright (c) 2021, the SerenityOS developers.
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-04-20 11:34:37 +00:00
|
|
|
#include <AK/Assertions.h>
|
2021-11-10 10:55:37 +00:00
|
|
|
#include <AK/Error.h>
|
2020-12-23 18:35:20 +00:00
|
|
|
#include <AK/Find.h>
|
2020-02-14 20:41:10 +00:00
|
|
|
#include <AK/Forward.h>
|
2020-09-06 19:14:08 +00:00
|
|
|
#include <AK/Iterator.h>
|
2020-02-25 13:49:47 +00:00
|
|
|
#include <AK/Optional.h>
|
2022-02-23 14:59:00 +00:00
|
|
|
#include <AK/ReverseIterator.h>
|
2020-07-25 14:00:26 +00:00
|
|
|
#include <AK/Span.h>
|
2019-04-20 11:34:37 +00:00
|
|
|
#include <AK/StdLibExtras.h>
|
2019-06-29 17:14:03 +00:00
|
|
|
#include <AK/Traits.h>
|
2020-09-09 11:41:58 +00:00
|
|
|
#include <AK/TypedTransfer.h>
|
2019-04-20 11:34:37 +00:00
|
|
|
#include <AK/kmalloc.h>
|
2021-09-19 22:25:22 +00:00
|
|
|
#include <initializer_list>
|
2019-06-14 04:43:56 +00:00
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
namespace AK {
|
|
|
|
|
2021-06-08 13:24:06 +00:00
|
|
|
namespace Detail {
|
|
|
|
|
|
|
|
template<typename StorageType, bool>
|
|
|
|
struct CanBePlacedInsideVectorHelper;
|
|
|
|
|
|
|
|
template<typename StorageType>
|
|
|
|
struct CanBePlacedInsideVectorHelper<StorageType, true> {
|
|
|
|
template<typename U>
|
2022-10-16 22:06:11 +00:00
|
|
|
static constexpr bool value = requires(U && u) { StorageType { &u }; };
|
2021-06-08 13:24:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename StorageType>
|
|
|
|
struct CanBePlacedInsideVectorHelper<StorageType, false> {
|
|
|
|
template<typename U>
|
2022-10-16 22:06:11 +00:00
|
|
|
static constexpr bool value = requires(U && u) { StorageType(forward<U>(u)); };
|
2021-06-08 13:24:06 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-02-25 13:49:47 +00:00
|
|
|
template<typename T, size_t inline_capacity>
|
2021-06-08 13:24:06 +00:00
|
|
|
requires(!IsRvalueReference<T>) class Vector {
|
|
|
|
private:
|
|
|
|
static constexpr bool contains_reference = IsLvalueReference<T>;
|
|
|
|
using StorageType = Conditional<contains_reference, RawPtr<RemoveReference<T>>, T>;
|
|
|
|
|
2022-01-08 22:46:38 +00:00
|
|
|
using VisibleType = RemoveReference<T>;
|
|
|
|
|
2021-06-08 13:24:06 +00:00
|
|
|
template<typename U>
|
|
|
|
static constexpr bool CanBePlacedInsideVector = Detail::CanBePlacedInsideVectorHelper<StorageType, contains_reference>::template value<U>;
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
public:
|
2021-06-08 10:46:32 +00:00
|
|
|
using ValueType = T;
|
2019-04-20 11:34:37 +00:00
|
|
|
Vector()
|
2019-03-17 14:53:03 +00:00
|
|
|
{
|
|
|
|
}
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2022-10-16 22:06:11 +00:00
|
|
|
Vector(std::initializer_list<T> list)
|
|
|
|
requires(!IsLvalueReference<T>)
|
2019-06-28 18:20:19 +00:00
|
|
|
{
|
|
|
|
ensure_capacity(list.size());
|
|
|
|
for (auto& item : list)
|
|
|
|
unchecked_append(item);
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
Vector(Vector&& other)
|
2019-04-20 11:34:37 +00:00
|
|
|
: m_size(other.m_size)
|
|
|
|
, m_capacity(other.m_capacity)
|
|
|
|
, m_outline_buffer(other.m_outline_buffer)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2019-04-20 11:34:37 +00:00
|
|
|
if constexpr (inline_capacity > 0) {
|
|
|
|
if (!m_outline_buffer) {
|
2022-11-03 07:03:52 +00:00
|
|
|
TypedTransfer<T>::move(inline_buffer(), other.inline_buffer(), m_size);
|
|
|
|
TypedTransfer<T>::delete_(other.inline_buffer(), m_size);
|
2019-04-20 11:34:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
other.m_outline_buffer = nullptr;
|
|
|
|
other.m_size = 0;
|
|
|
|
other.reset_capacity();
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 11:56:28 +00:00
|
|
|
Vector(Vector const& other)
|
2018-11-05 09:23:00 +00:00
|
|
|
{
|
2019-01-19 21:53:05 +00:00
|
|
|
ensure_capacity(other.size());
|
2021-06-08 13:24:06 +00:00
|
|
|
TypedTransfer<StorageType>::copy(data(), other.data(), other.size());
|
2019-08-07 13:25:34 +00:00
|
|
|
m_size = other.size();
|
2018-11-05 09:23:00 +00:00
|
|
|
}
|
|
|
|
|
2023-02-05 19:02:54 +00:00
|
|
|
explicit Vector(ReadonlySpan<T> other)
|
2022-10-16 22:06:11 +00:00
|
|
|
requires(!IsLvalueReference<T>)
|
2022-03-09 20:32:33 +00:00
|
|
|
{
|
|
|
|
ensure_capacity(other.size());
|
|
|
|
TypedTransfer<StorageType>::copy(data(), other.data(), other.size());
|
|
|
|
m_size = other.size();
|
|
|
|
}
|
|
|
|
|
2020-02-25 13:49:47 +00:00
|
|
|
template<size_t other_inline_capacity>
|
2021-06-08 11:56:28 +00:00
|
|
|
Vector(Vector<T, other_inline_capacity> const& other)
|
2020-01-04 09:57:30 +00:00
|
|
|
{
|
|
|
|
ensure_capacity(other.size());
|
2021-06-08 13:24:06 +00:00
|
|
|
TypedTransfer<StorageType>::copy(data(), other.data(), other.size());
|
2020-01-04 09:57:30 +00:00
|
|
|
m_size = other.size();
|
|
|
|
}
|
|
|
|
|
2021-06-08 11:50:36 +00:00
|
|
|
~Vector()
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2021-06-08 13:24:06 +00:00
|
|
|
Span<StorageType> span() { return { data(), size() }; }
|
2023-02-05 19:02:54 +00:00
|
|
|
ReadonlySpan<StorageType> span() const { return { data(), size() }; }
|
2020-07-25 14:00:26 +00:00
|
|
|
|
2021-06-08 13:24:06 +00:00
|
|
|
operator Span<StorageType>() { return span(); }
|
2023-02-05 19:02:54 +00:00
|
|
|
operator ReadonlySpan<StorageType>() const { return span(); }
|
2021-06-08 11:50:36 +00:00
|
|
|
|
|
|
|
bool is_empty() const { return size() == 0; }
|
|
|
|
ALWAYS_INLINE size_t size() const { return m_size; }
|
|
|
|
size_t capacity() const { return m_capacity; }
|
|
|
|
|
2022-02-05 15:35:23 +00:00
|
|
|
ALWAYS_INLINE StorageType* data()
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2021-06-08 11:50:36 +00:00
|
|
|
if constexpr (inline_capacity > 0)
|
|
|
|
return m_outline_buffer ? m_outline_buffer : inline_buffer();
|
|
|
|
return m_outline_buffer;
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2022-02-05 15:35:23 +00:00
|
|
|
ALWAYS_INLINE StorageType const* data() const
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2021-06-08 11:50:36 +00:00
|
|
|
if constexpr (inline_capacity > 0)
|
|
|
|
return m_outline_buffer ? m_outline_buffer : inline_buffer();
|
|
|
|
return m_outline_buffer;
|
|
|
|
}
|
|
|
|
|
2022-01-08 22:46:38 +00:00
|
|
|
ALWAYS_INLINE VisibleType const& at(size_t i) const
|
2021-06-08 11:50:36 +00:00
|
|
|
{
|
|
|
|
VERIFY(i < m_size);
|
2021-06-08 13:24:06 +00:00
|
|
|
if constexpr (contains_reference)
|
|
|
|
return *data()[i];
|
|
|
|
else
|
|
|
|
return data()[i];
|
2021-06-08 11:50:36 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 22:46:38 +00:00
|
|
|
ALWAYS_INLINE VisibleType& at(size_t i)
|
2021-06-08 11:50:36 +00:00
|
|
|
{
|
|
|
|
VERIFY(i < m_size);
|
2021-06-08 13:24:06 +00:00
|
|
|
if constexpr (contains_reference)
|
|
|
|
return *data()[i];
|
|
|
|
else
|
|
|
|
return data()[i];
|
2021-06-08 11:50:36 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 22:46:38 +00:00
|
|
|
ALWAYS_INLINE VisibleType const& operator[](size_t i) const { return at(i); }
|
|
|
|
ALWAYS_INLINE VisibleType& operator[](size_t i) { return at(i); }
|
2021-06-08 11:50:36 +00:00
|
|
|
|
2022-01-08 22:46:38 +00:00
|
|
|
VisibleType const& first() const { return at(0); }
|
|
|
|
VisibleType& first() { return at(0); }
|
2021-06-08 11:50:36 +00:00
|
|
|
|
2022-01-08 22:46:38 +00:00
|
|
|
VisibleType const& last() const { return at(size() - 1); }
|
|
|
|
VisibleType& last() { return at(size() - 1); }
|
2021-06-08 11:50:36 +00:00
|
|
|
|
2021-07-12 21:17:25 +00:00
|
|
|
template<typename TUnaryPredicate>
|
2022-10-16 22:06:11 +00:00
|
|
|
Optional<VisibleType&> first_matching(TUnaryPredicate const& predicate)
|
|
|
|
requires(!contains_reference)
|
2021-06-08 11:50:36 +00:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < size(); ++i) {
|
2021-07-12 21:17:25 +00:00
|
|
|
if (predicate(at(i))) {
|
2021-06-08 11:50:36 +00:00
|
|
|
return at(i);
|
|
|
|
}
|
2019-04-20 11:34:37 +00:00
|
|
|
}
|
2021-06-08 11:50:36 +00:00
|
|
|
return {};
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 16:26:00 +00:00
|
|
|
template<typename TUnaryPredicate>
|
2022-10-16 22:06:11 +00:00
|
|
|
Optional<VisibleType const&> first_matching(TUnaryPredicate const& predicate) const
|
|
|
|
requires(!contains_reference)
|
2022-04-04 16:26:00 +00:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < size(); ++i) {
|
|
|
|
if (predicate(at(i))) {
|
|
|
|
return Optional<VisibleType const&>(at(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-07-12 21:17:25 +00:00
|
|
|
template<typename TUnaryPredicate>
|
2022-10-16 22:06:11 +00:00
|
|
|
Optional<VisibleType&> last_matching(TUnaryPredicate const& predicate)
|
|
|
|
requires(!contains_reference)
|
2019-01-12 06:22:25 +00:00
|
|
|
{
|
2021-06-08 11:50:36 +00:00
|
|
|
for (ssize_t i = size() - 1; i >= 0; --i) {
|
2021-07-12 21:17:25 +00:00
|
|
|
if (predicate(at(i))) {
|
2021-06-08 11:50:36 +00:00
|
|
|
return at(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
2019-01-12 06:22:25 +00:00
|
|
|
}
|
|
|
|
|
2020-11-15 16:45:53 +00:00
|
|
|
template<typename V>
|
2021-06-08 11:56:28 +00:00
|
|
|
bool operator==(V const& other) const
|
2019-07-11 12:05:22 +00:00
|
|
|
{
|
2020-11-15 16:45:53 +00:00
|
|
|
if (m_size != other.size())
|
2019-07-11 12:05:22 +00:00
|
|
|
return false;
|
2021-06-08 13:24:06 +00:00
|
|
|
return TypedTransfer<StorageType>::compare(data(), other.data(), size());
|
2019-07-11 12:05:22 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 19:33:41 +00:00
|
|
|
template<typename V>
|
|
|
|
bool contains_slow(V const& value) const
|
2018-11-02 23:31:42 +00:00
|
|
|
{
|
2020-02-25 13:49:47 +00:00
|
|
|
for (size_t i = 0; i < size(); ++i) {
|
2022-01-08 22:46:38 +00:00
|
|
|
if (Traits<VisibleType>::equals(at(i), value))
|
2018-11-02 23:31:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-01-08 22:46:38 +00:00
|
|
|
bool contains_in_range(VisibleType const& value, size_t const start, size_t const end) const
|
2021-05-10 20:49:54 +00:00
|
|
|
{
|
|
|
|
VERIFY(start <= end);
|
|
|
|
VERIFY(end < size());
|
|
|
|
for (size_t i = start; i <= end; ++i) {
|
2022-01-08 22:46:38 +00:00
|
|
|
if (Traits<VisibleType>::equals(at(i), value))
|
2021-05-10 20:49:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-01-03 11:29:58 +00:00
|
|
|
#ifndef KERNEL
|
|
|
|
|
2021-06-08 11:50:36 +00:00
|
|
|
template<typename U = T>
|
2022-10-16 22:06:11 +00:00
|
|
|
void insert(size_t index, U&& value)
|
|
|
|
requires(CanBePlacedInsideVector<U>)
|
2021-06-08 11:50:36 +00:00
|
|
|
{
|
2021-11-10 10:55:37 +00:00
|
|
|
MUST(try_insert<U>(index, forward<U>(value)));
|
2021-06-08 11:50:36 +00:00
|
|
|
}
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2021-07-12 21:17:25 +00:00
|
|
|
template<typename TUnaryPredicate, typename U = T>
|
2022-10-16 22:06:11 +00:00
|
|
|
void insert_before_matching(U&& value, TUnaryPredicate const& predicate, size_t first_index = 0, size_t* inserted_index = nullptr)
|
|
|
|
requires(CanBePlacedInsideVector<U>)
|
2019-04-20 11:34:37 +00:00
|
|
|
{
|
2021-11-10 10:55:37 +00:00
|
|
|
MUST(try_insert_before_matching(forward<U>(value), predicate, first_index, inserted_index));
|
2019-04-20 11:34:37 +00:00
|
|
|
}
|
2021-06-08 11:50:36 +00:00
|
|
|
|
2021-06-12 11:24:45 +00:00
|
|
|
void extend(Vector&& other)
|
2019-04-20 11:34:37 +00:00
|
|
|
{
|
2021-11-10 10:55:37 +00:00
|
|
|
MUST(try_extend(move(other)));
|
2019-04-20 11:34:37 +00:00
|
|
|
}
|
2018-10-27 21:42:20 +00:00
|
|
|
|
2021-06-12 11:24:45 +00:00
|
|
|
void extend(Vector const& other)
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
2021-11-10 10:55:37 +00:00
|
|
|
MUST(try_extend(other));
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
2021-06-08 11:50:36 +00:00
|
|
|
|
2022-01-03 11:29:58 +00:00
|
|
|
#endif
|
|
|
|
|
2021-06-08 11:50:36 +00:00
|
|
|
ALWAYS_INLINE void append(T&& value)
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
2021-06-08 13:24:06 +00:00
|
|
|
if constexpr (contains_reference)
|
2021-11-10 10:55:37 +00:00
|
|
|
MUST(try_append(value));
|
2021-06-08 13:24:06 +00:00
|
|
|
else
|
2021-11-10 10:55:37 +00:00
|
|
|
MUST(try_append(move(value)));
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2022-10-16 22:06:11 +00:00
|
|
|
ALWAYS_INLINE void append(T const& value)
|
|
|
|
requires(!contains_reference)
|
2021-06-08 11:50:36 +00:00
|
|
|
{
|
2021-11-10 10:55:37 +00:00
|
|
|
MUST(try_append(T(value)));
|
2021-06-08 11:50:36 +00:00
|
|
|
}
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2022-01-03 11:29:58 +00:00
|
|
|
#ifndef KERNEL
|
2021-06-08 13:24:06 +00:00
|
|
|
void append(StorageType const* values, size_t count)
|
2021-06-08 11:50:36 +00:00
|
|
|
{
|
2021-11-10 10:55:37 +00:00
|
|
|
MUST(try_append(values, count));
|
2021-06-08 11:50:36 +00:00
|
|
|
}
|
2022-01-03 11:29:58 +00:00
|
|
|
#endif
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2021-06-08 11:50:36 +00:00
|
|
|
template<typename U = T>
|
2022-10-16 22:06:11 +00:00
|
|
|
ALWAYS_INLINE void unchecked_append(U&& value)
|
|
|
|
requires(CanBePlacedInsideVector<U>)
|
2021-06-08 11:50:36 +00:00
|
|
|
{
|
|
|
|
VERIFY((size() + 1) <= capacity());
|
2021-06-08 13:24:06 +00:00
|
|
|
if constexpr (contains_reference)
|
|
|
|
new (slot(m_size)) StorageType(&value);
|
|
|
|
else
|
|
|
|
new (slot(m_size)) StorageType(forward<U>(value));
|
2021-06-08 11:50:36 +00:00
|
|
|
++m_size;
|
|
|
|
}
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2021-11-24 23:18:46 +00:00
|
|
|
ALWAYS_INLINE void unchecked_append(StorageType const* values, size_t count)
|
|
|
|
{
|
|
|
|
if (count == 0)
|
|
|
|
return;
|
|
|
|
VERIFY((size() + count) <= capacity());
|
|
|
|
TypedTransfer<StorageType>::copy(slot(m_size), values, count);
|
|
|
|
m_size += count;
|
|
|
|
}
|
|
|
|
|
2022-01-03 11:29:58 +00:00
|
|
|
#ifndef KERNEL
|
2021-06-08 11:50:36 +00:00
|
|
|
template<class... Args>
|
2022-10-16 22:06:11 +00:00
|
|
|
void empend(Args&&... args)
|
|
|
|
requires(!contains_reference)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2021-11-10 10:55:37 +00:00
|
|
|
MUST(try_empend(forward<Args>(args)...));
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 11:50:36 +00:00
|
|
|
template<typename U = T>
|
2022-10-16 22:06:11 +00:00
|
|
|
void prepend(U&& value)
|
|
|
|
requires(CanBePlacedInsideVector<U>)
|
2019-01-14 01:49:30 +00:00
|
|
|
{
|
2021-11-10 10:55:37 +00:00
|
|
|
MUST(try_insert(0, forward<U>(value)));
|
2019-01-14 01:49:30 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 11:50:36 +00:00
|
|
|
void prepend(Vector&& other)
|
2019-11-07 19:38:24 +00:00
|
|
|
{
|
2021-11-10 10:55:37 +00:00
|
|
|
MUST(try_prepend(move(other)));
|
2019-11-07 19:38:24 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 13:24:06 +00:00
|
|
|
void prepend(StorageType const* values, size_t count)
|
2020-01-15 18:24:25 +00:00
|
|
|
{
|
2021-11-10 10:55:37 +00:00
|
|
|
MUST(try_prepend(values, count));
|
2021-06-08 11:50:36 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 11:29:58 +00:00
|
|
|
#endif
|
|
|
|
|
2021-06-08 11:50:36 +00:00
|
|
|
// FIXME: What about assigning from a vector with lower inline capacity?
|
|
|
|
Vector& operator=(Vector&& other)
|
|
|
|
{
|
|
|
|
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 (size_t i = 0; i < m_size; ++i) {
|
2021-06-08 13:24:06 +00:00
|
|
|
new (&inline_buffer()[i]) StorageType(move(other.inline_buffer()[i]));
|
|
|
|
other.inline_buffer()[i].~StorageType();
|
2021-06-08 11:50:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
other.m_outline_buffer = nullptr;
|
|
|
|
other.m_size = 0;
|
|
|
|
other.reset_capacity();
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-06-08 11:56:28 +00:00
|
|
|
Vector& operator=(Vector const& other)
|
2021-06-08 11:50:36 +00:00
|
|
|
{
|
|
|
|
if (this != &other) {
|
|
|
|
clear();
|
|
|
|
ensure_capacity(other.size());
|
2021-06-08 13:24:06 +00:00
|
|
|
TypedTransfer<StorageType>::copy(data(), other.data(), other.size());
|
2021-06-08 11:50:36 +00:00
|
|
|
m_size = other.size();
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t other_inline_capacity>
|
2021-06-08 11:56:28 +00:00
|
|
|
Vector& operator=(Vector<T, other_inline_capacity> const& other)
|
2021-06-08 11:50:36 +00:00
|
|
|
{
|
|
|
|
clear();
|
|
|
|
ensure_capacity(other.size());
|
2021-06-08 13:24:06 +00:00
|
|
|
TypedTransfer<StorageType>::copy(data(), other.data(), other.size());
|
2021-06-08 11:50:36 +00:00
|
|
|
m_size = other.size();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
clear_with_capacity();
|
|
|
|
if (m_outline_buffer) {
|
2021-07-11 11:23:27 +00:00
|
|
|
kfree_sized(m_outline_buffer, m_capacity * sizeof(StorageType));
|
2021-06-08 11:50:36 +00:00
|
|
|
m_outline_buffer = nullptr;
|
|
|
|
}
|
|
|
|
reset_capacity();
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear_with_capacity()
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < m_size; ++i)
|
2021-06-08 13:24:06 +00:00
|
|
|
data()[i].~StorageType();
|
2021-06-08 11:50:36 +00:00
|
|
|
m_size = 0;
|
2020-01-15 18:24:25 +00:00
|
|
|
}
|
|
|
|
|
2020-02-25 13:49:47 +00:00
|
|
|
void remove(size_t index)
|
2018-10-12 23:17:36 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(index < m_size);
|
2019-08-12 08:35:05 +00:00
|
|
|
|
2021-06-08 13:24:06 +00:00
|
|
|
if constexpr (Traits<StorageType>::is_trivial()) {
|
|
|
|
TypedTransfer<StorageType>::copy(slot(index), slot(index + 1), m_size - index - 1);
|
2019-08-12 08:35:05 +00:00
|
|
|
} else {
|
2021-06-08 13:24:06 +00:00
|
|
|
at(index).~StorageType();
|
2020-02-25 13:49:47 +00:00
|
|
|
for (size_t i = index + 1; i < m_size; ++i) {
|
2021-06-08 13:24:06 +00:00
|
|
|
new (slot(i - 1)) StorageType(move(at(i)));
|
|
|
|
at(i).~StorageType();
|
2019-08-12 08:35:05 +00:00
|
|
|
}
|
2019-04-20 11:34:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
--m_size;
|
2018-10-12 23:17:36 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 11:50:36 +00:00
|
|
|
void remove(size_t index, size_t count)
|
|
|
|
{
|
|
|
|
if (count == 0)
|
|
|
|
return;
|
|
|
|
VERIFY(index + count > index);
|
|
|
|
VERIFY(index + count <= m_size);
|
|
|
|
|
2021-06-08 13:24:06 +00:00
|
|
|
if constexpr (Traits<StorageType>::is_trivial()) {
|
|
|
|
TypedTransfer<StorageType>::copy(slot(index), slot(index + count), m_size - index - count);
|
2021-06-08 11:50:36 +00:00
|
|
|
} else {
|
|
|
|
for (size_t i = index; i < index + count; i++)
|
2021-06-08 13:24:06 +00:00
|
|
|
at(i).~StorageType();
|
2021-06-08 11:50:36 +00:00
|
|
|
for (size_t i = index + count; i < m_size; ++i) {
|
2021-06-08 13:24:06 +00:00
|
|
|
new (slot(i - count)) StorageType(move(at(i)));
|
|
|
|
at(i).~StorageType();
|
2021-06-08 11:50:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_size -= count;
|
|
|
|
}
|
|
|
|
|
2021-07-12 21:17:25 +00:00
|
|
|
template<typename TUnaryPredicate>
|
2022-04-12 17:21:05 +00:00
|
|
|
bool remove_first_matching(TUnaryPredicate const& predicate)
|
2021-06-08 11:50:36 +00:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < size(); ++i) {
|
2021-07-12 21:17:25 +00:00
|
|
|
if (predicate(at(i))) {
|
2021-06-08 11:50:36 +00:00
|
|
|
remove(i);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-12 21:17:25 +00:00
|
|
|
template<typename TUnaryPredicate>
|
2022-04-12 17:21:05 +00:00
|
|
|
bool remove_all_matching(TUnaryPredicate const& predicate)
|
2021-06-08 11:50:36 +00:00
|
|
|
{
|
2022-01-05 16:00:17 +00:00
|
|
|
bool something_was_removed = false;
|
2021-06-08 11:50:36 +00:00
|
|
|
for (size_t i = 0; i < size();) {
|
2021-07-12 21:17:25 +00:00
|
|
|
if (predicate(at(i))) {
|
2021-06-08 11:50:36 +00:00
|
|
|
remove(i);
|
2022-01-05 16:00:17 +00:00
|
|
|
something_was_removed = true;
|
2021-06-08 11:50:36 +00:00
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
2022-01-05 16:00:17 +00:00
|
|
|
return something_was_removed;
|
2021-06-08 11:50:36 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 16:00:28 +00:00
|
|
|
ALWAYS_INLINE T take_last()
|
2021-06-08 11:50:36 +00:00
|
|
|
{
|
|
|
|
VERIFY(!is_empty());
|
2021-06-08 13:24:06 +00:00
|
|
|
auto value = move(raw_last());
|
|
|
|
if constexpr (!contains_reference)
|
|
|
|
last().~T();
|
2021-06-08 11:50:36 +00:00
|
|
|
--m_size;
|
2021-06-08 13:24:06 +00:00
|
|
|
if constexpr (contains_reference)
|
|
|
|
return *value;
|
|
|
|
else
|
|
|
|
return value;
|
2021-06-08 11:50:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
T take_first()
|
|
|
|
{
|
|
|
|
VERIFY(!is_empty());
|
2021-06-08 13:24:06 +00:00
|
|
|
auto value = move(raw_first());
|
2021-06-08 11:50:36 +00:00
|
|
|
remove(0);
|
2021-06-08 13:24:06 +00:00
|
|
|
if constexpr (contains_reference)
|
|
|
|
return *value;
|
|
|
|
else
|
|
|
|
return value;
|
2021-06-08 11:50:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
T take(size_t index)
|
|
|
|
{
|
2021-06-08 13:24:06 +00:00
|
|
|
auto value = move(raw_at(index));
|
2021-06-08 11:50:36 +00:00
|
|
|
remove(index);
|
2021-06-08 13:24:06 +00:00
|
|
|
if constexpr (contains_reference)
|
|
|
|
return *value;
|
|
|
|
else
|
|
|
|
return value;
|
2021-06-08 11:50:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
T unstable_take(size_t index)
|
2020-12-22 06:21:09 +00:00
|
|
|
{
|
2021-06-08 11:50:36 +00:00
|
|
|
VERIFY(index < m_size);
|
2021-06-08 13:24:06 +00:00
|
|
|
swap(raw_at(index), raw_at(m_size - 1));
|
2021-06-08 11:50:36 +00:00
|
|
|
return take_last();
|
2020-12-22 06:21:09 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 23:04:16 +00:00
|
|
|
template<typename U = T>
|
2022-10-16 22:06:11 +00:00
|
|
|
ErrorOr<void> try_insert(size_t index, U&& value)
|
|
|
|
requires(CanBePlacedInsideVector<U>)
|
2019-03-07 14:52:11 +00:00
|
|
|
{
|
2021-04-24 06:51:31 +00:00
|
|
|
if (index > size())
|
2021-11-10 10:55:37 +00:00
|
|
|
return Error::from_errno(EINVAL);
|
2019-03-07 14:52:11 +00:00
|
|
|
if (index == size())
|
2021-04-24 06:51:31 +00:00
|
|
|
return try_append(forward<U>(value));
|
2021-11-10 10:55:37 +00:00
|
|
|
TRY(try_grow_capacity(size() + 1));
|
2019-04-20 11:34:37 +00:00
|
|
|
++m_size;
|
2021-06-08 13:24:06 +00:00
|
|
|
if constexpr (Traits<StorageType>::is_trivial()) {
|
|
|
|
TypedTransfer<StorageType>::move(slot(index + 1), slot(index), m_size - index - 1);
|
2020-01-19 11:15:43 +00:00
|
|
|
} else {
|
2020-02-25 13:49:47 +00:00
|
|
|
for (size_t i = size() - 1; i > index; --i) {
|
2021-06-08 13:24:06 +00:00
|
|
|
new (slot(i)) StorageType(move(at(i - 1)));
|
|
|
|
at(i - 1).~StorageType();
|
2020-01-19 11:15:43 +00:00
|
|
|
}
|
2019-03-07 14:52:11 +00:00
|
|
|
}
|
2021-06-08 13:24:06 +00:00
|
|
|
if constexpr (contains_reference)
|
|
|
|
new (slot(index)) StorageType(&value);
|
|
|
|
else
|
|
|
|
new (slot(index)) StorageType(forward<U>(value));
|
2021-11-10 10:55:37 +00:00
|
|
|
return {};
|
2021-04-24 06:51:31 +00:00
|
|
|
}
|
|
|
|
|
2021-07-12 21:17:25 +00:00
|
|
|
template<typename TUnaryPredicate, typename U = T>
|
2022-10-16 22:06:11 +00:00
|
|
|
ErrorOr<void> try_insert_before_matching(U&& value, TUnaryPredicate const& predicate, size_t first_index = 0, size_t* inserted_index = nullptr)
|
|
|
|
requires(CanBePlacedInsideVector<U>)
|
2019-07-04 12:20:48 +00:00
|
|
|
{
|
2020-02-25 13:49:47 +00:00
|
|
|
for (size_t i = first_index; i < size(); ++i) {
|
2021-07-12 21:17:25 +00:00
|
|
|
if (predicate(at(i))) {
|
2021-11-10 10:55:37 +00:00
|
|
|
TRY(try_insert(i, forward<U>(value)));
|
2020-01-19 12:18:27 +00:00
|
|
|
if (inserted_index)
|
|
|
|
*inserted_index = i;
|
2021-11-10 10:55:37 +00:00
|
|
|
return {};
|
2019-07-04 12:20:48 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-10 10:55:37 +00:00
|
|
|
TRY(try_append(forward<U>(value)));
|
2020-01-19 12:18:27 +00:00
|
|
|
if (inserted_index)
|
|
|
|
*inserted_index = size() - 1;
|
2021-11-10 10:55:37 +00:00
|
|
|
return {};
|
2021-04-24 06:51:31 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 10:55:37 +00:00
|
|
|
ErrorOr<void> try_extend(Vector&& other)
|
2018-10-28 09:26:07 +00:00
|
|
|
{
|
2021-11-28 16:24:53 +00:00
|
|
|
if (is_empty() && capacity() <= other.capacity()) {
|
2019-04-20 11:34:37 +00:00
|
|
|
*this = move(other);
|
2021-11-10 10:55:37 +00:00
|
|
|
return {};
|
2019-02-07 08:08:59 +00:00
|
|
|
}
|
2019-08-07 13:25:34 +00:00
|
|
|
auto other_size = other.size();
|
2019-04-20 11:34:37 +00:00
|
|
|
Vector tmp = move(other);
|
2021-11-10 10:55:37 +00:00
|
|
|
TRY(try_grow_capacity(size() + other_size));
|
2021-06-08 13:24:06 +00:00
|
|
|
TypedTransfer<StorageType>::move(data() + m_size, tmp.data(), other_size);
|
2019-08-07 13:25:34 +00:00
|
|
|
m_size += other_size;
|
2021-11-10 10:55:37 +00:00
|
|
|
return {};
|
2018-10-28 09:26:07 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 10:55:37 +00:00
|
|
|
ErrorOr<void> try_extend(Vector const& other)
|
2019-07-04 11:54:37 +00:00
|
|
|
{
|
2021-11-10 10:55:37 +00:00
|
|
|
TRY(try_grow_capacity(size() + other.size()));
|
2021-06-08 13:24:06 +00:00
|
|
|
TypedTransfer<StorageType>::copy(data() + m_size, other.data(), other.size());
|
2019-08-07 13:25:34 +00:00
|
|
|
m_size += other.m_size;
|
2021-11-10 10:55:37 +00:00
|
|
|
return {};
|
2021-04-24 06:51:31 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 10:55:37 +00:00
|
|
|
ErrorOr<void> try_append(T&& value)
|
2019-03-18 19:50:39 +00:00
|
|
|
{
|
2021-11-10 10:55:37 +00:00
|
|
|
TRY(try_grow_capacity(size() + 1));
|
2021-06-08 13:24:06 +00:00
|
|
|
if constexpr (contains_reference)
|
|
|
|
new (slot(m_size)) StorageType(&value);
|
|
|
|
else
|
|
|
|
new (slot(m_size)) StorageType(move(value));
|
2021-06-08 11:50:36 +00:00
|
|
|
++m_size;
|
2021-11-10 10:55:37 +00:00
|
|
|
return {};
|
2019-03-18 19:50:39 +00:00
|
|
|
}
|
|
|
|
|
2022-10-16 22:06:11 +00:00
|
|
|
ErrorOr<void> try_append(T const& value)
|
|
|
|
requires(!contains_reference)
|
2019-12-22 17:29:12 +00:00
|
|
|
{
|
2021-06-08 11:50:36 +00:00
|
|
|
return try_append(T(value));
|
2019-12-22 17:29:12 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 10:55:37 +00:00
|
|
|
ErrorOr<void> try_append(StorageType const* values, size_t count)
|
2018-10-28 09:26:07 +00:00
|
|
|
{
|
2021-11-06 20:12:16 +00:00
|
|
|
if (count == 0)
|
2021-11-10 10:55:37 +00:00
|
|
|
return {};
|
|
|
|
TRY(try_grow_capacity(size() + count));
|
2021-06-08 13:24:06 +00:00
|
|
|
TypedTransfer<StorageType>::copy(slot(m_size), values, count);
|
2021-06-08 11:50:36 +00:00
|
|
|
m_size += count;
|
2021-11-10 10:55:37 +00:00
|
|
|
return {};
|
2018-10-28 09:26:07 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 13:35:45 +00:00
|
|
|
template<class... Args>
|
2022-10-16 22:06:11 +00:00
|
|
|
ErrorOr<void> try_empend(Args&&... args)
|
|
|
|
requires(!contains_reference)
|
2019-08-01 13:35:45 +00:00
|
|
|
{
|
2021-11-10 10:55:37 +00:00
|
|
|
TRY(try_grow_capacity(m_size + 1));
|
2021-06-08 13:24:06 +00:00
|
|
|
new (slot(m_size)) StorageType { forward<Args>(args)... };
|
2019-08-01 13:35:45 +00:00
|
|
|
++m_size;
|
2021-11-10 10:55:37 +00:00
|
|
|
return {};
|
2019-08-01 13:35:45 +00:00
|
|
|
}
|
|
|
|
|
2021-04-24 06:51:31 +00:00
|
|
|
template<typename U = T>
|
2022-10-16 22:06:11 +00:00
|
|
|
ErrorOr<void> try_prepend(U&& value)
|
|
|
|
requires(CanBePlacedInsideVector<U>)
|
2021-04-24 06:51:31 +00:00
|
|
|
{
|
|
|
|
return try_insert(0, forward<U>(value));
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 10:55:37 +00:00
|
|
|
ErrorOr<void> try_prepend(Vector&& other)
|
2019-07-20 14:10:52 +00:00
|
|
|
{
|
|
|
|
if (other.is_empty())
|
2021-11-10 10:55:37 +00:00
|
|
|
return {};
|
2019-07-20 14:10:52 +00:00
|
|
|
|
|
|
|
if (is_empty()) {
|
|
|
|
*this = move(other);
|
2021-11-10 10:55:37 +00:00
|
|
|
return {};
|
2019-07-20 14:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto other_size = other.size();
|
2021-11-10 10:55:37 +00:00
|
|
|
TRY(try_grow_capacity(size() + other_size));
|
2019-07-20 14:10:52 +00:00
|
|
|
|
2020-02-25 13:49:47 +00:00
|
|
|
for (size_t i = size() + other_size - 1; i >= other.size(); --i) {
|
2021-06-08 13:24:06 +00:00
|
|
|
new (slot(i)) StorageType(move(at(i - other_size)));
|
|
|
|
at(i - other_size).~StorageType();
|
2019-07-20 14:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector tmp = move(other);
|
2021-06-08 13:24:06 +00:00
|
|
|
TypedTransfer<StorageType>::move(slot(0), tmp.data(), tmp.size());
|
2019-07-20 14:10:52 +00:00
|
|
|
m_size += other_size;
|
2021-11-10 10:55:37 +00:00
|
|
|
return {};
|
2019-07-20 14:10:52 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 10:55:37 +00:00
|
|
|
ErrorOr<void> try_prepend(StorageType const* values, size_t count)
|
2020-11-21 18:45:36 +00:00
|
|
|
{
|
2021-11-06 20:12:16 +00:00
|
|
|
if (count == 0)
|
2021-11-10 10:55:37 +00:00
|
|
|
return {};
|
|
|
|
TRY(try_grow_capacity(size() + count));
|
2021-06-08 13:24:06 +00:00
|
|
|
TypedTransfer<StorageType>::move(slot(count), slot(0), m_size);
|
|
|
|
TypedTransfer<StorageType>::copy(slot(0), values, count);
|
2020-11-21 18:45:36 +00:00
|
|
|
m_size += count;
|
2021-11-10 10:55:37 +00:00
|
|
|
return {};
|
2020-11-21 18:45:36 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 10:55:37 +00:00
|
|
|
ErrorOr<void> try_grow_capacity(size_t needed_capacity)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2019-04-20 11:34:37 +00:00
|
|
|
if (m_capacity >= needed_capacity)
|
2021-11-10 10:55:37 +00:00
|
|
|
return {};
|
2021-04-24 06:51:31 +00:00
|
|
|
return try_ensure_capacity(padded_capacity(needed_capacity));
|
2019-04-20 11:34:37 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 10:55:37 +00:00
|
|
|
ErrorOr<void> try_ensure_capacity(size_t needed_capacity)
|
2019-04-20 11:34:37 +00:00
|
|
|
{
|
|
|
|
if (m_capacity >= needed_capacity)
|
2021-11-10 10:55:37 +00:00
|
|
|
return {};
|
2021-06-08 13:24:06 +00:00
|
|
|
size_t new_capacity = kmalloc_good_size(needed_capacity * sizeof(StorageType)) / sizeof(StorageType);
|
2021-08-07 20:34:29 +00:00
|
|
|
auto* new_buffer = static_cast<StorageType*>(kmalloc_array(new_capacity, sizeof(StorageType)));
|
2021-04-24 06:51:31 +00:00
|
|
|
if (new_buffer == nullptr)
|
2021-11-10 10:55:37 +00:00
|
|
|
return Error::from_errno(ENOMEM);
|
2019-08-07 13:35:23 +00:00
|
|
|
|
2021-06-08 13:24:06 +00:00
|
|
|
if constexpr (Traits<StorageType>::is_trivial()) {
|
|
|
|
TypedTransfer<StorageType>::copy(new_buffer, data(), m_size);
|
2019-08-07 13:35:23 +00:00
|
|
|
} else {
|
2020-02-25 13:49:47 +00:00
|
|
|
for (size_t i = 0; i < m_size; ++i) {
|
2021-06-08 13:24:06 +00:00
|
|
|
new (&new_buffer[i]) StorageType(move(at(i)));
|
|
|
|
at(i).~StorageType();
|
2019-08-07 13:35:23 +00:00
|
|
|
}
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
2019-04-20 11:34:37 +00:00
|
|
|
if (m_outline_buffer)
|
2021-07-11 11:23:27 +00:00
|
|
|
kfree_sized(m_outline_buffer, m_capacity * sizeof(StorageType));
|
2019-04-20 11:34:37 +00:00
|
|
|
m_outline_buffer = new_buffer;
|
|
|
|
m_capacity = new_capacity;
|
2021-11-10 10:55:37 +00:00
|
|
|
return {};
|
2021-04-24 06:51:31 +00:00
|
|
|
}
|
|
|
|
|
2022-10-16 22:06:11 +00:00
|
|
|
ErrorOr<void> try_resize(size_t new_size, bool keep_capacity = false)
|
|
|
|
requires(!contains_reference)
|
2021-06-08 11:50:36 +00:00
|
|
|
{
|
|
|
|
if (new_size <= size()) {
|
|
|
|
shrink(new_size, keep_capacity);
|
2021-11-10 10:55:37 +00:00
|
|
|
return {};
|
2021-06-08 11:50:36 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 10:55:37 +00:00
|
|
|
TRY(try_ensure_capacity(new_size));
|
2021-06-08 11:50:36 +00:00
|
|
|
|
|
|
|
for (size_t i = size(); i < new_size; ++i)
|
2021-06-08 13:24:06 +00:00
|
|
|
new (slot(i)) StorageType {};
|
2021-06-08 11:50:36 +00:00
|
|
|
m_size = new_size;
|
2021-11-10 10:55:37 +00:00
|
|
|
return {};
|
2021-06-08 11:50:36 +00:00
|
|
|
}
|
|
|
|
|
2022-10-16 22:06:11 +00:00
|
|
|
ErrorOr<void> try_resize_and_keep_capacity(size_t new_size)
|
|
|
|
requires(!contains_reference)
|
2021-06-08 11:50:36 +00:00
|
|
|
{
|
|
|
|
return try_resize(new_size, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void grow_capacity(size_t needed_capacity)
|
|
|
|
{
|
2021-11-10 10:55:37 +00:00
|
|
|
MUST(try_grow_capacity(needed_capacity));
|
2021-06-08 11:50:36 +00:00
|
|
|
}
|
|
|
|
|
2021-04-24 06:51:31 +00:00
|
|
|
void ensure_capacity(size_t needed_capacity)
|
|
|
|
{
|
2021-11-10 10:55:37 +00:00
|
|
|
MUST(try_ensure_capacity(needed_capacity));
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 16:38:16 +00:00
|
|
|
void shrink(size_t new_size, bool keep_capacity = false)
|
2018-11-01 12:39:28 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(new_size <= size());
|
2019-03-07 15:49:04 +00:00
|
|
|
if (new_size == size())
|
2018-11-01 12:39:28 +00:00
|
|
|
return;
|
2019-03-07 15:49:04 +00:00
|
|
|
|
2021-11-06 20:12:16 +00:00
|
|
|
if (new_size == 0) {
|
2020-05-03 16:38:16 +00:00
|
|
|
if (keep_capacity)
|
|
|
|
clear_with_capacity();
|
|
|
|
else
|
|
|
|
clear();
|
2019-03-07 15:49:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-25 13:49:47 +00:00
|
|
|
for (size_t i = new_size; i < size(); ++i)
|
2021-06-08 13:24:06 +00:00
|
|
|
at(i).~StorageType();
|
2019-07-24 07:33:26 +00:00
|
|
|
m_size = new_size;
|
|
|
|
}
|
|
|
|
|
2022-10-16 22:06:11 +00:00
|
|
|
void resize(size_t new_size, bool keep_capacity = false)
|
|
|
|
requires(!contains_reference)
|
2021-04-24 06:51:31 +00:00
|
|
|
{
|
2021-11-10 10:55:37 +00:00
|
|
|
MUST(try_resize(new_size, keep_capacity));
|
2021-04-24 06:51:31 +00:00
|
|
|
}
|
|
|
|
|
2022-10-16 22:06:11 +00:00
|
|
|
void resize_and_keep_capacity(size_t new_size)
|
|
|
|
requires(!contains_reference)
|
2020-05-03 16:38:16 +00:00
|
|
|
{
|
2021-11-10 10:55:37 +00:00
|
|
|
MUST(try_resize_and_keep_capacity(new_size));
|
2020-05-03 16:38:16 +00:00
|
|
|
}
|
|
|
|
|
2022-11-26 19:32:33 +00:00
|
|
|
void shrink_to_fit()
|
|
|
|
{
|
|
|
|
if (size() == capacity())
|
|
|
|
return;
|
|
|
|
Vector new_vector;
|
|
|
|
new_vector.ensure_capacity(size());
|
|
|
|
for (auto& element : *this) {
|
|
|
|
new_vector.unchecked_append(move(element));
|
|
|
|
}
|
|
|
|
*this = move(new_vector);
|
|
|
|
}
|
|
|
|
|
2022-01-08 22:46:38 +00:00
|
|
|
using ConstIterator = SimpleIterator<Vector const, VisibleType const>;
|
|
|
|
using Iterator = SimpleIterator<Vector, VisibleType>;
|
2022-02-23 14:59:00 +00:00
|
|
|
using ReverseIterator = SimpleReverseIterator<Vector, VisibleType>;
|
2022-03-17 11:30:04 +00:00
|
|
|
using ReverseConstIterator = SimpleReverseIterator<Vector const, VisibleType const>;
|
2020-09-06 19:14:08 +00:00
|
|
|
|
|
|
|
ConstIterator begin() const { return ConstIterator::begin(*this); }
|
|
|
|
Iterator begin() { return Iterator::begin(*this); }
|
2022-02-23 14:59:00 +00:00
|
|
|
ReverseIterator rbegin() { return ReverseIterator::rbegin(*this); }
|
2022-03-17 11:30:04 +00:00
|
|
|
ReverseConstIterator rbegin() const { return ReverseConstIterator::rbegin(*this); }
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2020-09-06 19:14:08 +00:00
|
|
|
ConstIterator end() const { return ConstIterator::end(*this); }
|
|
|
|
Iterator end() { return Iterator::end(*this); }
|
2022-02-23 14:59:00 +00:00
|
|
|
ReverseIterator rend() { return ReverseIterator::rend(*this); }
|
2022-03-17 11:30:04 +00:00
|
|
|
ReverseConstIterator rend() const { return ReverseConstIterator::rend(*this); }
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2022-03-05 13:05:56 +00:00
|
|
|
ALWAYS_INLINE constexpr auto in_reverse()
|
|
|
|
{
|
|
|
|
return ReverseWrapper::in_reverse(*this);
|
|
|
|
}
|
|
|
|
|
2022-03-17 11:30:04 +00:00
|
|
|
ALWAYS_INLINE constexpr auto in_reverse() const
|
|
|
|
{
|
|
|
|
return ReverseWrapper::in_reverse(*this);
|
|
|
|
}
|
|
|
|
|
2020-12-23 18:35:20 +00:00
|
|
|
template<typename TUnaryPredicate>
|
|
|
|
ConstIterator find_if(TUnaryPredicate&& finder) const
|
2019-08-04 17:21:08 +00:00
|
|
|
{
|
2020-12-23 18:35:20 +00:00
|
|
|
return AK::find_if(begin(), end(), forward<TUnaryPredicate>(finder));
|
2019-08-04 17:21:08 +00:00
|
|
|
}
|
|
|
|
|
2020-12-23 18:35:20 +00:00
|
|
|
template<typename TUnaryPredicate>
|
|
|
|
Iterator find_if(TUnaryPredicate&& finder)
|
2019-08-04 17:21:08 +00:00
|
|
|
{
|
2020-12-23 18:35:20 +00:00
|
|
|
return AK::find_if(begin(), end(), forward<TUnaryPredicate>(finder));
|
2019-08-04 17:21:08 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 22:46:38 +00:00
|
|
|
ConstIterator find(VisibleType const& value) const
|
2019-08-04 17:21:08 +00:00
|
|
|
{
|
2020-12-23 18:35:20 +00:00
|
|
|
return AK::find(begin(), end(), value);
|
2019-08-04 17:21:08 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 22:46:38 +00:00
|
|
|
Iterator find(VisibleType const& value)
|
2019-08-04 17:21:08 +00:00
|
|
|
{
|
2020-12-23 18:35:20 +00:00
|
|
|
return AK::find(begin(), end(), value);
|
2019-08-04 17:21:08 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 22:46:38 +00:00
|
|
|
Optional<size_t> find_first_index(VisibleType const& value) const
|
2019-12-17 21:15:36 +00:00
|
|
|
{
|
2021-06-08 11:56:28 +00:00
|
|
|
if (auto const index = AK::find_index(begin(), end(), value);
|
2020-12-23 18:35:20 +00:00
|
|
|
index < size()) {
|
|
|
|
return index;
|
2019-12-17 21:15:36 +00:00
|
|
|
}
|
2020-02-25 13:49:47 +00:00
|
|
|
return {};
|
2019-12-17 21:15:36 +00:00
|
|
|
}
|
2019-12-22 17:29:12 +00:00
|
|
|
|
2023-04-10 16:27:16 +00:00
|
|
|
template<typename TUnaryPredicate>
|
|
|
|
Optional<size_t> find_first_index_if(TUnaryPredicate&& finder) const
|
|
|
|
{
|
|
|
|
auto maybe_result = AK::find_if(begin(), end(), finder);
|
|
|
|
if (maybe_result == end())
|
|
|
|
return {};
|
|
|
|
return maybe_result.index();
|
|
|
|
}
|
|
|
|
|
2021-09-16 04:54:50 +00:00
|
|
|
void reverse()
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < size() / 2; ++i)
|
|
|
|
AK::swap(at(i), at(size() - i - 1));
|
|
|
|
}
|
|
|
|
|
2019-03-16 12:24:39 +00:00
|
|
|
private:
|
2019-04-20 11:34:37 +00:00
|
|
|
void reset_capacity()
|
|
|
|
{
|
|
|
|
m_capacity = inline_capacity;
|
|
|
|
}
|
|
|
|
|
2020-02-25 13:49:47 +00:00
|
|
|
static size_t padded_capacity(size_t capacity)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2023-06-13 10:38:38 +00:00
|
|
|
return 4 + capacity + capacity / 4;
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 13:24:06 +00:00
|
|
|
StorageType* slot(size_t i) { return &data()[i]; }
|
|
|
|
StorageType const* slot(size_t i) const { return &data()[i]; }
|
2019-04-20 11:34:37 +00:00
|
|
|
|
2021-06-08 13:24:06 +00:00
|
|
|
StorageType* inline_buffer()
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
|
|
|
static_assert(inline_capacity > 0);
|
2021-06-08 13:24:06 +00:00
|
|
|
return reinterpret_cast<StorageType*>(m_inline_buffer_storage);
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
2021-06-08 13:24:06 +00:00
|
|
|
StorageType const* inline_buffer() const
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
|
|
|
static_assert(inline_capacity > 0);
|
2021-06-08 13:24:06 +00:00
|
|
|
return reinterpret_cast<StorageType const*>(m_inline_buffer_storage);
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
2019-04-20 11:34:37 +00:00
|
|
|
|
2021-06-08 13:24:06 +00:00
|
|
|
StorageType& raw_last() { return raw_at(size() - 1); }
|
|
|
|
StorageType& raw_first() { return raw_at(0); }
|
|
|
|
StorageType& raw_at(size_t index) { return *slot(index); }
|
|
|
|
|
2020-02-25 13:49:47 +00:00
|
|
|
size_t m_size { 0 };
|
2023-02-10 20:11:16 +00:00
|
|
|
size_t m_capacity { inline_capacity };
|
2019-04-20 11:34:37 +00:00
|
|
|
|
2022-03-26 16:59:33 +00:00
|
|
|
static constexpr size_t storage_size()
|
|
|
|
{
|
|
|
|
if constexpr (inline_capacity == 0)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return sizeof(StorageType) * inline_capacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr size_t storage_alignment()
|
|
|
|
{
|
|
|
|
if constexpr (inline_capacity == 0)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return alignof(StorageType);
|
|
|
|
}
|
|
|
|
|
|
|
|
alignas(storage_alignment()) unsigned char m_inline_buffer_storage[storage_size()];
|
2021-06-08 13:24:06 +00:00
|
|
|
StorageType* m_outline_buffer { nullptr };
|
2018-10-10 09:53:07 +00:00
|
|
|
};
|
|
|
|
|
2021-07-22 14:36:16 +00:00
|
|
|
template<class... Args>
|
|
|
|
Vector(Args... args) -> Vector<CommonType<Args...>>;
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2022-11-26 11:18:30 +00:00
|
|
|
#if USING_AK_GLOBALLY
|
2018-10-10 09:53:07 +00:00
|
|
|
using AK::Vector;
|
2022-11-26 11:18:30 +00:00
|
|
|
#endif
|