AK: Bring back FixedArray<T>

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.
This commit is contained in:
Andreas Kling 2021-07-11 17:16:13 +02:00
parent 846685fca2
commit 88c8451973
Notes: sideshowbarker 2024-07-18 09:19:35 +09:00
4 changed files with 145 additions and 0 deletions

111
AK/FixedArray.h Normal file
View file

@ -0,0 +1,111 @@
/*
* 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;

View file

@ -84,6 +84,9 @@ using OrderedHashMap = HashMap<K, V, KeyTraits, true>;
template<typename T>
class Badge;
template<typename T>
class FixedArray;
template<typename>
class Function;
@ -132,6 +135,7 @@ using AK::CircularDuplexStream;
using AK::CircularQueue;
using AK::DoublyLinkedList;
using AK::DuplexMemoryStream;
using AK::FixedArray;
using AK::FlyString;
using AK::Function;
using AK::HashMap;

View file

@ -21,6 +21,7 @@ set(AK_TEST_SOURCES
TestEndian.cpp
TestEnumBits.cpp
TestFind.cpp
TestFixedArray.cpp
TestFormat.cpp
TestGenericLexer.cpp
TestHashFunctions.cpp

View file

@ -0,0 +1,29 @@
/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibTest/TestSuite.h>
#include <AK/FixedArray.h>
#include <AK/String.h>
TEST_CASE(construct)
{
EXPECT(FixedArray<int>().size() == 0);
}
TEST_CASE(ints)
{
FixedArray<int> ints(3);
ints[0] = 0;
ints[1] = 1;
ints[2] = 2;
EXPECT_EQ(ints[0], 0);
EXPECT_EQ(ints[1], 1);
EXPECT_EQ(ints[2], 2);
ints.clear();
EXPECT_EQ(ints.size(), 0u);
}