2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
#pragma once
|
|
|
|
|
2019-04-20 14:34:37 +03:00
|
|
|
#include <AK/Assertions.h>
|
2020-02-14 23:41:10 +03:00
|
|
|
#include <AK/Forward.h>
|
2020-09-06 22:14:08 +03:00
|
|
|
#include <AK/Iterator.h>
|
2020-02-25 16:49:47 +03:00
|
|
|
#include <AK/Optional.h>
|
2020-07-25 17:00:26 +03:00
|
|
|
#include <AK/Span.h>
|
2019-04-20 14:34:37 +03:00
|
|
|
#include <AK/StdLibExtras.h>
|
2019-06-29 20:14:03 +03:00
|
|
|
#include <AK/Traits.h>
|
2020-09-09 14:41:58 +03:00
|
|
|
#include <AK/TypedTransfer.h>
|
2019-04-20 14:34:37 +03:00
|
|
|
#include <AK/kmalloc.h>
|
2019-06-28 21:58:41 +03:00
|
|
|
|
2020-05-20 16:52:20 +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
|
2020-05-16 06:23:51 +03:00
|
|
|
# include <initializer_list>
|
2019-06-14 07:43:56 +03:00
|
|
|
#endif
|
|
|
|
|
2020-05-20 16:52:20 +03:00
|
|
|
#ifndef __serenity__
|
|
|
|
# include <new>
|
|
|
|
#endif
|
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
namespace AK {
|
|
|
|
|
2020-02-25 16:49:47 +03:00
|
|
|
template<typename T, size_t inline_capacity>
|
2019-04-20 14:34:37 +03:00
|
|
|
class Vector {
|
2018-10-10 12:53:07 +03:00
|
|
|
public:
|
2020-11-15 19:45:53 +03:00
|
|
|
using value_type = T;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-05-20 16:52:20 +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);
|
|
|
|
}
|
2020-05-20 16:52:20 +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) {
|
2020-02-25 16:49:47 +03:00
|
|
|
for (size_t i = 0; i < m_size; ++i) {
|
2019-04-20 14:34:37 +03: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 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
|
|
|
}
|
|
|
|
|
2020-02-25 16:49:47 +03:00
|
|
|
template<size_t other_inline_capacity>
|
2020-01-04 12:57:30 +03: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 17:00:26 +03:00
|
|
|
Span<T> span() { return { data(), size() }; }
|
|
|
|
Span<const T> span() const { return { data(), size() }; }
|
|
|
|
|
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) {
|
2020-02-25 16:49:47 +03:00
|
|
|
for (size_t i = 0; i < m_size; ++i) {
|
2019-04-20 14:34:37 +03: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 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()
|
|
|
|
{
|
2020-02-25 16:49:47 +03:00
|
|
|
for (size_t i = 0; i < m_size; ++i)
|
2019-04-20 14:34:37 +03:00
|
|
|
data()[i].~T();
|
|
|
|
m_size = 0;
|
2019-01-12 09:22:25 +03:00
|
|
|
}
|
|
|
|
|
2020-11-15 19:45:53 +03:00
|
|
|
template<typename V>
|
|
|
|
bool operator==(const V& other) const
|
2019-07-11 15:05:22 +03:00
|
|
|
{
|
2020-11-15 19:45:53 +03:00
|
|
|
if (m_size != other.size())
|
2019-07-11 15:05:22 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-08-15 19:38:24 +03:00
|
|
|
operator Span<T>() { return span(); }
|
|
|
|
operator Span<const T>() const { return span(); }
|
|
|
|
|
2018-11-03 02:31:42 +03:00
|
|
|
bool contains_slow(const T& value) const
|
|
|
|
{
|
2020-02-25 16:49:47 +03: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 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().
|
2020-01-25 14:14:59 +03:00
|
|
|
bool is_null() const { return false; }
|
2018-12-21 04:10:45 +03:00
|
|
|
bool is_empty() const { return size() == 0; }
|
2020-04-27 17:22:40 +03:00
|
|
|
ALWAYS_INLINE size_t size() const { return m_size; }
|
2020-02-25 16:49:47 +03:00
|
|
|
size_t 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
|
|
|
|
2020-04-27 17:22:40 +03:00
|
|
|
ALWAYS_INLINE const T& at(size_t i) const
|
2019-05-28 12:53:16 +03:00
|
|
|
{
|
2020-05-12 01:32:48 +03:00
|
|
|
ASSERT(i < m_size);
|
2019-05-28 12:53:16 +03:00
|
|
|
return data()[i];
|
|
|
|
}
|
2020-04-27 17:22:40 +03:00
|
|
|
ALWAYS_INLINE T& at(size_t i)
|
2019-05-28 12:53:16 +03:00
|
|
|
{
|
2020-05-12 01:32:48 +03:00
|
|
|
ASSERT(i < m_size);
|
2019-05-28 12:53:16 +03:00
|
|
|
return data()[i];
|
|
|
|
}
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2020-04-27 17:22:40 +03:00
|
|
|
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 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;
|
|
|
|
}
|
|
|
|
|
2020-02-25 16:49:47 +03:00
|
|
|
T take(size_t index)
|
2019-11-07 22:38:24 +03: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 21:24:25 +03:00
|
|
|
{
|
|
|
|
ASSERT(index < m_size);
|
|
|
|
swap(at(index), at(m_size - 1));
|
2020-07-02 11:51:46 +03:00
|
|
|
return take_last();
|
2020-01-15 21:24:25 +03:00
|
|
|
}
|
|
|
|
|
2020-02-25 16:49:47 +03:00
|
|
|
void remove(size_t 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();
|
2020-02-25 16:49:47 +03:00
|
|
|
for (size_t i = index + 1; i < m_size; ++i) {
|
2019-08-12 11:35:05 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-02-25 16:49:47 +03:00
|
|
|
void insert(size_t index, T&& value)
|
2019-03-07 17:52:11 +03:00
|
|
|
{
|
|
|
|
ASSERT(index <= size());
|
|
|
|
if (index == size())
|
|
|
|
return append(move(value));
|
2019-04-20 14:34:37 +03:00
|
|
|
grow_capacity(size() + 1);
|
|
|
|
++m_size;
|
2020-01-19 14:15:43 +03:00
|
|
|
if constexpr (Traits<T>::is_trivial()) {
|
|
|
|
TypedTransfer<T>::move(slot(index + 1), slot(index), m_size - index - 1);
|
|
|
|
} else {
|
2020-02-25 16:49:47 +03:00
|
|
|
for (size_t i = size() - 1; i > index; --i) {
|
2020-01-19 14:15:43 +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
|
|
|
}
|
|
|
|
|
2020-02-25 16:49:47 +03:00
|
|
|
void insert(size_t index, const T& value)
|
2019-05-17 05:59:56 +03:00
|
|
|
{
|
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>
|
2020-02-25 16:49:47 +03:00
|
|
|
void insert_before_matching(T&& value, C callback, size_t first_index = 0, size_t* inserted_index = nullptr)
|
2019-07-04 15:20:48 +03:00
|
|
|
{
|
2020-02-25 16:49:47 +03:00
|
|
|
for (size_t i = first_index; i < size(); ++i) {
|
2019-07-04 15:20:48 +03:00
|
|
|
if (callback(at(i))) {
|
|
|
|
insert(i, move(value));
|
2020-01-19 15:18:27 +03:00
|
|
|
if (inserted_index)
|
|
|
|
*inserted_index = i;
|
2019-07-04 15:20:48 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
append(move(value));
|
2020-01-19 15:18:27 +03:00
|
|
|
if (inserted_index)
|
|
|
|
*inserted_index = size() - 1;
|
2019-07-04 15:20:48 +03:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-02-25 16:49:47 +03:00
|
|
|
template<size_t other_inline_capacity>
|
2020-01-04 12:57:30 +03: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;
|
|
|
|
}
|
|
|
|
|
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>
|
2020-10-06 19:38:18 +03:00
|
|
|
bool remove_first_matching(Callback callback)
|
2019-03-18 22:50:39 +03:00
|
|
|
{
|
2020-02-25 16:49:47 +03:00
|
|
|
for (size_t i = 0; i < size(); ++i) {
|
2019-03-18 22:50:39 +03:00
|
|
|
if (callback(at(i))) {
|
|
|
|
remove(i);
|
2020-10-06 19:38:18 +03:00
|
|
|
return true;
|
2019-03-18 22:50:39 +03:00
|
|
|
}
|
|
|
|
}
|
2020-10-06 19:38:18 +03:00
|
|
|
return false;
|
2019-03-18 22:50:39 +03:00
|
|
|
}
|
|
|
|
|
2019-12-22 20:29:12 +03:00
|
|
|
template<typename Callback>
|
|
|
|
void remove_all_matching(Callback callback)
|
|
|
|
{
|
2020-02-25 16:49:47 +03:00
|
|
|
for (size_t i = 0; i < size();) {
|
2019-12-22 20:29:12 +03:00
|
|
|
if (callback(at(i))) {
|
|
|
|
remove(i);
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-27 17:22:40 +03:00
|
|
|
ALWAYS_INLINE 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
|
|
|
}
|
|
|
|
|
2020-04-27 17:22:40 +03:00
|
|
|
ALWAYS_INLINE void unchecked_append(const T& value)
|
2018-11-05 12:23:00 +03:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2020-04-27 17:22:40 +03:00
|
|
|
ALWAYS_INLINE void append(T&& value)
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2020-04-27 17:22:40 +03:00
|
|
|
ALWAYS_INLINE void append(const T& value)
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2019-07-04 14:54:37 +03:00
|
|
|
append(T(value));
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2019-11-07 12:35:13 +03:00
|
|
|
void prepend(T&& value)
|
|
|
|
{
|
|
|
|
insert(0, move(value));
|
|
|
|
}
|
|
|
|
|
2019-04-16 04:47:24 +03:00
|
|
|
void prepend(const T& value)
|
|
|
|
{
|
2019-11-07 12:35:13 +03:00
|
|
|
insert(0, value);
|
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);
|
|
|
|
|
2020-02-25 16:49:47 +03:00
|
|
|
for (size_t 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;
|
|
|
|
}
|
|
|
|
|
2020-02-25 16:49:47 +03:00
|
|
|
void append(const T* values, size_t 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
|
|
|
}
|
|
|
|
|
2020-02-25 16:49:47 +03:00
|
|
|
void grow_capacity(size_t 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));
|
|
|
|
}
|
|
|
|
|
2020-02-25 16:49:47 +03:00
|
|
|
void ensure_capacity(size_t needed_capacity)
|
2019-04-20 14:34:37 +03:00
|
|
|
{
|
|
|
|
if (m_capacity >= needed_capacity)
|
|
|
|
return;
|
2020-02-25 16:49:47 +03:00
|
|
|
size_t new_capacity = needed_capacity;
|
2019-04-20 14:34:37 +03:00
|
|
|
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 {
|
2020-02-25 16:49:47 +03:00
|
|
|
for (size_t i = 0; i < m_size; ++i) {
|
2019-08-07 16:35:23 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-05-03 19:38:16 +03:00
|
|
|
void shrink(size_t new_size, bool keep_capacity = false)
|
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) {
|
2020-05-03 19:38:16 +03:00
|
|
|
if (keep_capacity)
|
|
|
|
clear_with_capacity();
|
|
|
|
else
|
|
|
|
clear();
|
2019-03-07 18:49:04 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-25 16:49:47 +03:00
|
|
|
for (size_t i = new_size; i < size(); ++i)
|
2019-07-24 10:33:26 +03:00
|
|
|
at(i).~T();
|
|
|
|
m_size = new_size;
|
|
|
|
}
|
|
|
|
|
2020-05-03 19:38:16 +03:00
|
|
|
void resize(size_t new_size, bool keep_capacity = false)
|
2019-07-24 10:33:26 +03:00
|
|
|
{
|
|
|
|
if (new_size <= size())
|
2020-05-03 19:38:16 +03:00
|
|
|
return shrink(new_size, keep_capacity);
|
2019-07-24 10:33:26 +03:00
|
|
|
|
|
|
|
ensure_capacity(new_size);
|
2020-02-25 16:49:47 +03:00
|
|
|
for (size_t i = size(); i < new_size; ++i)
|
2019-07-24 10:33:26 +03:00
|
|
|
new (slot(i)) T;
|
2019-04-20 14:34:37 +03:00
|
|
|
m_size = new_size;
|
2018-11-01 15:39:28 +03:00
|
|
|
}
|
|
|
|
|
2020-05-03 19:38:16 +03:00
|
|
|
void resize_and_keep_capacity(size_t new_size)
|
|
|
|
{
|
|
|
|
return resize(new_size, true);
|
|
|
|
}
|
|
|
|
|
2020-09-06 22:14:08 +03: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 12:53:07 +03:00
|
|
|
|
2020-09-06 22:14:08 +03:00
|
|
|
ConstIterator end() const { return ConstIterator::end(*this); }
|
|
|
|
Iterator end() { return Iterator::end(*this); }
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2019-08-04 20:21:08 +03:00
|
|
|
template<typename Finder>
|
|
|
|
ConstIterator find(Finder finder) const
|
|
|
|
{
|
2020-02-25 16:49:47 +03:00
|
|
|
for (size_t i = 0; i < m_size; ++i) {
|
2019-08-04 20:21:08 +03:00
|
|
|
if (finder(at(i)))
|
|
|
|
return ConstIterator(*this, i);
|
|
|
|
}
|
|
|
|
return end();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Finder>
|
|
|
|
Iterator find(Finder finder)
|
|
|
|
{
|
2020-02-25 16:49:47 +03:00
|
|
|
for (size_t i = 0; i < m_size; ++i) {
|
2019-08-04 20:21:08 +03:00
|
|
|
if (finder(at(i)))
|
2020-09-06 22:14:08 +03:00
|
|
|
return Iterator { *this, i };
|
2019-08-04 20:21:08 +03:00
|
|
|
}
|
|
|
|
return end();
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstIterator find(const T& value) const
|
|
|
|
{
|
2020-09-06 22:49:59 +03:00
|
|
|
return find([&](auto& other) { return Traits<T>::equals(value, other); });
|
2019-08-04 20:21:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Iterator find(const T& value)
|
|
|
|
{
|
2020-09-06 22:49:59 +03:00
|
|
|
return find([&](auto& other) { return Traits<T>::equals(value, other); });
|
2019-08-04 20:21:08 +03:00
|
|
|
}
|
|
|
|
|
2020-02-25 16:49:47 +03:00
|
|
|
Optional<size_t> find_first_index(const T& value)
|
2019-12-18 00:15:36 +03:00
|
|
|
{
|
2020-02-25 16:49:47 +03:00
|
|
|
for (size_t i = 0; i < m_size; ++i) {
|
2020-09-06 22:49:59 +03:00
|
|
|
if (Traits<T>::equals(value, at(i)))
|
2019-12-18 00:15:36 +03:00
|
|
|
return i;
|
|
|
|
}
|
2020-02-25 16:49:47 +03:00
|
|
|
return {};
|
2019-12-18 00:15:36 +03:00
|
|
|
}
|
2019-12-22 20:29:12 +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;
|
|
|
|
}
|
|
|
|
|
2020-02-25 16:49:47 +03:00
|
|
|
static size_t padded_capacity(size_t capacity)
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2020-02-25 16:49:47 +03:00
|
|
|
return max(static_cast<size_t>(4), capacity + (capacity / 4) + 4);
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2020-02-25 16:49:47 +03:00
|
|
|
T* slot(size_t i) { return &data()[i]; }
|
|
|
|
const T* slot(size_t i) const { return &data()[i]; }
|
2019-04-20 14:34:37 +03:00
|
|
|
|
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
|
|
|
|
2020-02-25 16:49:47 +03:00
|
|
|
size_t m_size { 0 };
|
|
|
|
size_t m_capacity { 0 };
|
2019-04-20 14:34:37 +03:00
|
|
|
|
2020-03-08 13:57:24 +03:00
|
|
|
alignas(T) unsigned char 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;
|