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