group2 0.1.0
CSE 125 Group 2
Loading...
Searching...
No Matches
ParticlePool.hpp
Go to the documentation of this file.
1
3
4#pragma once
5
6#include <array>
7#include <concepts>
8#include <cstdint>
9
20template <typename T, uint32_t MaxN>
22{
23 std::array<T, MaxN> data{};
24 uint32_t count = 0;
25
27 T* spawn()
28 {
29 if (count >= MaxN)
30 return nullptr;
31 data[count] = T{};
32 return &data[count++];
33 }
34
36 void kill(uint32_t i) { data[i] = data[--count]; }
37
40 void update(auto fn)
41 {
42 for (uint32_t i = count; i-- > 0;)
43 if (!fn(data[i]))
44 kill(i);
45 }
46
48 [[nodiscard]] const T* rawData() const { return data.data(); }
49 [[nodiscard]] uint32_t liveCount() const { return count; }
50 [[nodiscard]] bool empty() const { return count == 0; }
51 [[nodiscard]] bool full() const { return count >= MaxN; }
52};
Fixed-capacity particle pool with O(1) swap-remove.
Definition ParticlePool.hpp:22
const T * rawData() const
Pointer to the contiguous live data for GPU upload.
Definition ParticlePool.hpp:48
std::array< T, MaxN > data
Definition ParticlePool.hpp:23
T * spawn()
Allocate a new slot (zero-initialised). Returns nullptr when full.
Definition ParticlePool.hpp:27
void update(auto fn)
Iterate all live particles.
Definition ParticlePool.hpp:40
bool full() const
Definition ParticlePool.hpp:51
uint32_t liveCount() const
Definition ParticlePool.hpp:49
bool empty() const
Definition ParticlePool.hpp:50
void kill(uint32_t i)
O(1) swap-remove. Does NOT preserve order.
Definition ParticlePool.hpp:36
uint32_t count
Definition ParticlePool.hpp:24