mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 09:00:22 +00:00
88c8451973
Let's bring this class back, but without the confusing resize() API. A FixedArray<T> is simply a fixed-size array of T. The size is provided at run-time, unlike Array<T> where the size is provided at compile-time.
111 lines
2.5 KiB
C++
111 lines
2.5 KiB
C++
/*
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <AK/Iterator.h>
|
|
#include <AK/Span.h>
|
|
#include <AK/kmalloc.h>
|
|
|
|
namespace AK {
|
|
|
|
template<typename T>
|
|
class FixedArray {
|
|
public:
|
|
FixedArray() { }
|
|
explicit FixedArray(size_t size)
|
|
: m_size(size)
|
|
{
|
|
if (m_size != 0) {
|
|
m_elements = (T*)kmalloc(sizeof(T) * m_size);
|
|
for (size_t i = 0; i < m_size; ++i)
|
|
new (&m_elements[i]) T();
|
|
}
|
|
}
|
|
~FixedArray()
|
|
{
|
|
clear();
|
|
}
|
|
|
|
FixedArray(FixedArray const& other)
|
|
: m_size(other.m_size)
|
|
{
|
|
if (m_size != 0) {
|
|
m_elements = (T*)kmalloc(sizeof(T) * m_size);
|
|
for (size_t i = 0; i < m_size; ++i)
|
|
new (&m_elements[i]) T(other[i]);
|
|
}
|
|
}
|
|
|
|
FixedArray& operator=(FixedArray const& other)
|
|
{
|
|
FixedArray array(other);
|
|
swap(array);
|
|
return *this;
|
|
}
|
|
|
|
FixedArray(FixedArray&&) = delete;
|
|
FixedArray& operator=(FixedArray&&) = delete;
|
|
|
|
void clear()
|
|
{
|
|
if (!m_elements)
|
|
return;
|
|
for (size_t i = 0; i < m_size; ++i)
|
|
m_elements[i].~T();
|
|
kfree_sized(m_elements, sizeof(T) * m_size);
|
|
m_elements = nullptr;
|
|
m_size = 0;
|
|
}
|
|
|
|
size_t size() const { return m_size; }
|
|
T* data() { return m_elements; }
|
|
T const* data() const { return m_elements; }
|
|
|
|
T& operator[](size_t index)
|
|
{
|
|
VERIFY(index < m_size);
|
|
return m_elements[index];
|
|
}
|
|
|
|
T const& operator[](size_t index) const
|
|
{
|
|
VERIFY(index < m_size);
|
|
return m_elements[index];
|
|
}
|
|
|
|
bool contains_slow(T const& value) const
|
|
{
|
|
for (size_t i = 0; i < m_size; ++i) {
|
|
if (m_elements[i] == value)
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void swap(FixedArray& other)
|
|
{
|
|
::swap(m_elements, other.m_elements);
|
|
::swap(m_size, other.m_size);
|
|
}
|
|
|
|
using ConstIterator = SimpleIterator<FixedArray const, T const>;
|
|
using Iterator = SimpleIterator<FixedArray, T>;
|
|
|
|
ConstIterator begin() const { return ConstIterator::begin(*this); }
|
|
Iterator begin() { return Iterator::begin(*this); }
|
|
|
|
ConstIterator end() const { return ConstIterator::end(*this); }
|
|
Iterator end() { return Iterator::end(*this); }
|
|
|
|
private:
|
|
size_t m_size { 0 };
|
|
T* m_elements { nullptr };
|
|
};
|
|
|
|
}
|
|
|
|
using AK::FixedArray;
|