Skip to main content

LinearLookupStrategy Class Template

Lookup strategy using a flat vector with linear scan. More...

Declaration

template <size_t TCapacity> class helios::ecs::strategies::LinearLookupStrategy<TCapacity> { ... }

Public Constructors Index

template <size_t TCapacity>
LinearLookupStrategy (const size_t capacity=TCapacity)

Constructs a strategy with pre-allocated capacity. More...

Public Member Functions Index

template <size_t TCapacity>
boolremove (const StrongId_t id)

Removes a strong ID using swap-and-pop. More...

template <size_t TCapacity>
booladd (const StrongId_t id)

Registers a strong ID. More...

template <size_t TCapacity>
boolhas (const StrongId_t id) const

Checks whether a strong ID is registered. More...

Private Member Attributes Index

template <size_t TCapacity>
std::vector< StrongId_t >strongIds_

Vector of registered strong IDs. More...

Description

Lookup strategy using a flat vector with linear scan.

LinearLookupStrategy stores strong IDs in a contiguous vector and performs linear scans for membership tests. Swap-and-pop is used for O(1) removal. Suitable for small registries where hash overhead is undesirable.

Template Parameters
TCapacity

Default initial capacity for pre-allocation.

See Also

EntityRegistry

See Also

HashedLookupStrategy

Definition at line 33 of file LinearLookupStrategy.ixx.

Public Constructors

LinearLookupStrategy()

template <size_t TCapacity>
helios::ecs::strategies::LinearLookupStrategy< TCapacity >::LinearLookupStrategy (const size_t capacity=TCapacity)
inline explicit

Constructs a strategy with pre-allocated capacity.

Parameters
capacity

The initial capacity to reserve.

Definition at line 47 of file LinearLookupStrategy.ixx.

47 explicit LinearLookupStrategy(const size_t capacity = TCapacity) {
48 strongIds_.reserve(capacity);
49 };

Public Member Functions

add()

template <size_t TCapacity>
bool helios::ecs::strategies::LinearLookupStrategy< TCapacity >::add (const StrongId_t id)
inline

Registers a strong ID.

Parameters
id

The strong ID to add.

Returns

True if inserted, false if already present.

Definition at line 86 of file LinearLookupStrategy.ixx.

86 [[nodiscard]] bool add(const StrongId_t id) {
87 if (has(id)) {
88 return false;
89 }
90 strongIds_.push_back(id);
91 return true;
92 }

Reference helios::ecs::strategies::LinearLookupStrategy< TCapacity >::has.

has()

template <size_t TCapacity>
bool helios::ecs::strategies::LinearLookupStrategy< TCapacity >::has (const StrongId_t id)
inline

Checks whether a strong ID is registered.

Parameters
id

The strong ID to test.

Returns

True if the ID is present.

Definition at line 101 of file LinearLookupStrategy.ixx.

101 [[nodiscard]] bool has(const StrongId_t id) const {
102 return std::ranges::find(strongIds_, id) != strongIds_.end();
103 }

Referenced by helios::ecs::strategies::LinearLookupStrategy< TCapacity >::add.

remove()

template <size_t TCapacity>
bool helios::ecs::strategies::LinearLookupStrategy< TCapacity >::remove (const StrongId_t id)
inline

Removes a strong ID using swap-and-pop.

Parameters
id

The strong ID to remove.

Returns

True if removed, false if not found.

Definition at line 58 of file LinearLookupStrategy.ixx.

58 [[nodiscard]] bool remove(const StrongId_t id) {
59
60 const auto it = std::ranges::find(strongIds_, id);
61
62 if (it == strongIds_.end()) {
63 return false;
64 }
65
66 if (it == strongIds_.end() - 1) {
67 strongIds_.pop_back();
68 return true;
69 }
70
71 const auto idx = std::distance(strongIds_.begin(), it);
72 const auto tmp = strongIds_.back();
73 strongIds_[idx] = tmp;
74 strongIds_.pop_back();
75
76 return true;
77 }

Private Member Attributes

strongIds_

template <size_t TCapacity>
std::vector<StrongId_t> helios::ecs::strategies::LinearLookupStrategy< TCapacity >::strongIds_

Vector of registered strong IDs.

Definition at line 38 of file LinearLookupStrategy.ixx.

38 std::vector<StrongId_t> strongIds_;

The documentation for this class was generated from the following file:


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.