RTYPE TECHNICAL DOCUMENTATION
  • Introduction
  • Classes
    • utils::InputMap
    • utils::Window
    • utils::constant::ButtonValue
    • network::Client
    • network::LockedQueue
    • network::Server
    • ecs::EnemyFactory
    • ecs::Engine
    • ecs::Entity
    • ecs::Event
    • ecs::Registry
    • ecs::SparseArray
    • ecs::World
    • ecs::WorldManager
    • ecs::component::Activable
    • ecs::component::Animated
    • ecs::component::Animated::AnimFrame
    • ecs::component::AttackAI
    • ecs::component::AttackAI::Action
    • ecs::component::AttackAI::AI
    • ecs::component::AttackAI::AI::Pattern
    • ecs::component::Controllable
    • ecs::component::Direction
    • ecs::component::Drawable
    • ecs::component::EntityType
    • ecs::component::Faction
    • ecs::component::FollowEntity
    • ecs::component::Health
    • ecs::component::Hitbox
    • ecs::component::MovementAI
    • ecs::component::MovementAI::AI
    • ecs::component::NetworkId
    • ecs::component::Parallax
    • ecs::component::Position
    • ecs::component::Projectile
    • ecs::component::Score
    • ecs::component::Shootable
    • ecs::component::Size
    • ecs::component::Text
    • ecs::component::textColor
    • ecs::component::Velocity
    • ecs::component::Weapon
    • audio::AudioManager
    • asset::AssetLoader
    • anim::Animation
  • Namespaces
    • utils
    • utils::constant
    • network
    • ecs
    • ecs::component
    • ecs::systems
    • audio
    • asset
    • anim
  • Modules
    • Input
  • Files
    • src
    • src/client
    • Animation.cpp
    • Animation.hpp
    • AssetLoader.cpp
    • AssetLoader.hpp
    • AudioManager.cpp
    • AudioManager.hpp
    • entrypoint.cpp
    • GetWorld.cpp
    • GetWorld.hpp
    • NetworkClient.cpp
    • NetworkClient.hpp
    • src/ecs
    • src/ecs/components
    • src/ecs/components/client
    • Activable.hpp
    • Animated.hpp
    • Controllable.hpp
    • Drawable.hpp
    • Hitbox.hpp
    • Parallax.hpp
    • Shootable.hpp
    • Text.hpp
    • src/ecs/components/server
    • AttackAI.cpp
    • AttackAI.hpp
    • FollowEntity.hpp
    • Projectile.hpp
    • Direction.hpp
    • EntityType.hpp
    • Faction.hpp
    • Health.hpp
    • MovementAI.cpp
    • MovementAI.hpp
    • NetworkId.hpp
    • Position.hpp
    • Score.hpp
    • Size.hpp
    • Velocity.hpp
    • Weapon.hpp
    • src/ecs/systems
    • src/ecs/systems/client
    • Animate.hpp
    • Draw.hpp
    • ExecuteOnce.hpp
    • HandleIncomingMessages.hpp
    • HandleParallaxBounds.hpp
    • HandleSFMLEvents.hpp
    • HandleSFMLKeys.hpp
    • HealthBar.hpp
    • MenuSelect.hpp
    • ScoreUpdate.hpp
    • SendDirection.hpp
    • src/ecs/systems/server
    • DeathUpdate.hpp
    • FollowEntitySystem.hpp
    • HandleIncomingMessage.hpp
    • PlayerHealthUpdate.hpp
    • PositionUpdate.hpp
    • ProjectileCollision.hpp
    • RunAttackAI.hpp
    • Waves.hpp
    • ManageClientEvents.hpp
    • Movement.hpp
    • PositionLogger.hpp
    • RunMovementAI.hpp
    • EnemyFactory.cpp
    • EnemyFactory.hpp
    • Engine.hpp
    • Entity.hpp
    • Event.hpp
    • LockedQueue.hpp
    • Registry.hpp
    • SparseArray.hpp
    • World.hpp
    • WorldManager.cpp
    • WorldManager.hpp
    • src/server
    • entrypoint.cpp
    • Server.cpp
    • Server.hpp
    • src/utils
    • Constant.hpp
    • InputMap.cpp
    • InputMap.hpp
    • Window.cpp
    • README.md
  • Pages
    • deprecated
  • GitHub
Powered by GitBook
On this page
  • Namespaces
  • Classes
  • Source code
  1. Files

SparseArray.hpp

Namespaces

Name

Classes

Name

class

Source code

/*
** EPITECH PROJECT, 2022
** RTYPE
** File description:
** SparseArray
*/

#pragma once

#include <exception>
#include <iostream>
#include <optional>
#include <vector>
#include "Constant.hpp"

namespace ecs
{
    template <typename Component> class SparseArray {
      public:
        using valueType = std::optional<Component>;
        using referenceType = valueType &;
        using constReferenceType = valueType const &;
        using container = std::vector<valueType>;
        using sizeType = typename container::size_type;
        using iterator = typename container::iterator;
        using constIterator = typename container::const_iterator;

      public:
        SparseArray() { _data.clear(); }
        SparseArray(SparseArray const &other) : _data(other._data) {}
        SparseArray(SparseArray &&other) noexcept : _data(std::move(other._data)) {}
        ~SparseArray() = default;

        SparseArray &operator=(SparseArray const &other)
        {
            for (auto &&i : other) {
                _data.insert(other);
            }
            return *this;
        }

        SparseArray &operator=(SparseArray &&other) noexcept
        {
            _data = std::move(other._data);
            return *this;
        }

        referenceType operator[](size_t idx) { return _data[idx]; }

        constReferenceType operator[](size_t idx) const { return _data[idx]; }

        iterator begin() { return _data.begin(); }

        constIterator begin() const { return _data.begin(); }

        constIterator cBegin() const { return _data.cbegin(); }

        iterator end() { return _data.end(); }

        constIterator end() const { return _data.end(); }

        constIterator cEnd() const { return _data.cend(); }

        sizeType size() const { return _data.size(); }

        referenceType insertAt(sizeType pos, Component const &c)
        {
            if (pos >= _data.size())
                _data.resize(pos + 1, std::nullopt);
            _data.at(pos) = c;
            return _data[pos];
        }

        referenceType insertAt(sizeType pos, Component &&c)
        {
            if (pos >= _data.size())
                _data.resize(pos + 1, std::nullopt);
            _data.at(pos) = c;
            return _data[pos];
        }

        template <class... Params> referenceType emplaceAt(sizeType pos, Params &&...args)
        {
            if (pos >= _data.size())
                _data.resize(pos + 1, std::nullopt);
            _data.at(pos) = Component(args...);
            return _data[pos];
        }

        void erase(sizeType pos)
        {
            if (pos < _data.size())
                if (_data[pos])
                    _data[pos] = std::nullopt;
        }

        sizeType getIndex(valueType const &c) const
        {
            std::size_t cpt = 0;
            if (!c.has_value())
                return utils::constant::npos;
            for (valueType iterator = _data.begin(); iterator != _data.end(); iterator++, cpt++)
                if (iterator.has_value() && iterator.value() == c.value()) {
                    return cpt;
                }
            return utils::constant::npos;
        }

      private:
        container _data;
    };
} // namespace ecs

Updated on 2022-11-13 at 17:21:37 +0100

PreviousRegistry.hppNextWorld.hpp

Last updated 2 years ago

ecs
ecs::SparseArray