# SparseArray

This class implements a sparse array, which is an array that allows gaps in the occupied indices. It is commonly used in ECS to manage components efficiently, allowing fast access and iteration over the components while avoiding the overhead of large contiguous memory allocations for unused indices.

```cpp
template <typename Component>
class SparseArray {
public:
    using value_type = std::optional<Component>; ///< Optional type for the component, allowing for uninitialized gaps.
    using reference = value_type&; ///< Reference to the value_type.
    using const_reference = const value_type&; ///< Constant reference to the value_type.
    using container_type = std::vector<value_type>; ///< Underlying container type.
    using size_type = typename container_type::size_type; ///< Size type used by the container.
    using iterator = typename container_type::iterator; ///< Iterator type for the container.
    using const_iterator = typename container_type::const_iterator; ///< Constant iterator type for the container.

    SparseArray() = default;
    explicit SparseArray(size_type size) : data(size, std::nullopt) {}

    SparseArray(const SparseArray&) = default;
    SparseArray(SparseArray&&) noexcept = default;
    ~SparseArray() = default;
    SparseArray& operator=(const SparseArray&) = default;
    SparseArray& operator=(SparseArray&&) noexcept = default;

    reference operator[](size_type idx);
    const_reference operator[](size_type idx) const;

    iterator begin();
    const_iterator begin() const;
    const_iterator cbegin() const;
    iterator end();
    const_iterator end() const;
    const_iterator cend() const;

    size_type size() const;

    reference addComponent(size_type pos);
    reference insertComponent(size_type pos, Component&& component);
    void removeComponent(size_type pos);
    size_type indexOf(const_reference comp) const;

    template <class... Params>
    reference emplaceComponent(size_type pos, Params&&... params);

    /**
     * @brief Removes the component at the specified position.
     * 
     * @param pos Index of the component to remove.
     */
    void remove(size_type pos) {
        if (pos < data.size()) {
            data[pos].reset();
        }
    }

    /**
     * @brief Ensures that the component at the specified position is initialized as nullopt.
     * 
     * @param pos Index of the component to initialize.
     */
    void add(size_type pos) {
        if (pos >= data.size()) {
            data.resize(pos + 1);
        }
        data[pos] = std::nullopt;
    }

    container_type data; ///< The underlying container that stores the components.
};
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://r-type-5.gitbook.io/r-type/development-guidelines/ecs/graphic/sparsearray.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
