Skip to content

Commit

Permalink
Simplify IdTracker to IdGenerator
Browse files Browse the repository at this point in the history
* Do not bother caching previously used Ids, just a simple increment will do
  • Loading branch information
SirBob01 committed Oct 23, 2024
1 parent 8f4e760 commit 6c6b22f
Show file tree
Hide file tree
Showing 17 changed files with 95 additions and 265 deletions.
2 changes: 1 addition & 1 deletion src/Dynamo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#include <Sound/Source.hpp>
#include <Utils/Allocator.hpp>
#include <Utils/Bits.hpp>
#include <Utils/IdTracker.hpp>
#include <Utils/IdGenerator.hpp>
#include <Utils/Log.hpp>
#include <Utils/Random.hpp>
#include <Utils/RingBuffer.hpp>
Expand Down
2 changes: 1 addition & 1 deletion src/Graphics/Material.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <Graphics/Shader.hpp>
#include <Utils/IdTracker.hpp>
#include <Utils/IdGenerator.hpp>

namespace Dynamo::Graphics {
/**
Expand Down
2 changes: 1 addition & 1 deletion src/Graphics/Mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <vector>

#include <Utils/IdTracker.hpp>
#include <Utils/IdGenerator.hpp>

namespace Dynamo::Graphics {
/**
Expand Down
2 changes: 1 addition & 1 deletion src/Graphics/Model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <Graphics/Material.hpp>
#include <Graphics/Mesh.hpp>
#include <Utils/IdTracker.hpp>
#include <Utils/IdGenerator.hpp>

namespace Dynamo::Graphics {
/**
Expand Down
2 changes: 1 addition & 1 deletion src/Graphics/Shader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <string>

#include <Utils/IdTracker.hpp>
#include <Utils/IdGenerator.hpp>

namespace Dynamo::Graphics {
/**
Expand Down
3 changes: 1 addition & 2 deletions src/Graphics/Vulkan/MeshSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ namespace Dynamo::Graphics::Vulkan {
}

// Register the allocation
Mesh mesh = _ids.generate();
Mesh mesh = IdGenerator<Mesh>::generate();
_allocations.insert(mesh, allocation);
return mesh;
}
Expand All @@ -94,6 +94,5 @@ namespace Dynamo::Graphics::Vulkan {
index_buffer.free(allocation.index_offset);
}
_allocations.remove(mesh);
_ids.discard(mesh);
}
} // namespace Dynamo::Graphics::Vulkan
3 changes: 1 addition & 2 deletions src/Graphics/Vulkan/MeshSet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <Graphics/Mesh.hpp>
#include <Graphics/Vulkan/Buffer.hpp>
#include <Utils/IdTracker.hpp>
#include <Utils/IdGenerator.hpp>
#include <Utils/SparseSet.hpp>

namespace Dynamo::Graphics::Vulkan {
Expand All @@ -29,7 +29,6 @@ namespace Dynamo::Graphics::Vulkan {
*
*/
class MeshSet {
IdTracker<Mesh> _ids;
SparseSet<Mesh, MeshAllocation> _allocations;

public:
Expand Down
4 changes: 1 addition & 3 deletions src/Graphics/Vulkan/ShaderSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ namespace Dynamo::Graphics::Vulkan {
Log::info("");

// Register the new shader
Shader shader = _ids.generate();
Shader shader = IdGenerator<Shader>::generate();
_modules.insert(shader, module);
return shader;
}
Expand All @@ -227,7 +227,6 @@ namespace Dynamo::Graphics::Vulkan {
ShaderModule &module = _modules.get(shader);
vkDestroyShaderModule(_device, module.handle, nullptr);
_modules.remove(shader);
_ids.discard(shader);
}

void ShaderSet::destroy() {
Expand All @@ -240,6 +239,5 @@ namespace Dynamo::Graphics::Vulkan {
vkDestroyShaderModule(_device, module.handle, nullptr);
}
_modules.clear();
_ids.clear();
}
} // namespace Dynamo::Graphics::Vulkan
1 change: 0 additions & 1 deletion src/Graphics/Vulkan/ShaderSet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ namespace Dynamo::Graphics::Vulkan {
*/
class ShaderSet {
VkDevice _device;
IdTracker<Shader> _ids;
SparseSet<Shader, ShaderModule> _modules;
std::unordered_map<DescriptorLayoutKey, VkDescriptorSetLayout, DescriptorLayoutKey::Hash> _descriptor_layouts;

Expand Down
1 change: 0 additions & 1 deletion src/Sound/Jukebox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include <portaudio.h>

#include <Utils/IdTracker.hpp>
#include <Utils/RingBuffer.hpp>

#include <Sound/Buffer.hpp>
Expand Down
1 change: 0 additions & 1 deletion src/Sound/Listener.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include <Math/Quaternion.hpp>
#include <Math/Vec3.hpp>
#include <Utils/IdTracker.hpp>
#include <Utils/SparseSet.hpp>

namespace Dynamo::Sound {
Expand Down
35 changes: 35 additions & 0 deletions src/Utils/IdGenerator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include <cstdint>

#define DYN_DEFINE_ID_TYPE(T) using T = struct T##_t *

static_assert(sizeof(void *) == sizeof(uintptr_t), "Pointer and its integer mode must be same size.");

namespace Dynamo {
/**
* @brief Generate typesafe handles.
*
* @tparam Id
*/
template <typename Id>
class IdGenerator {
static inline uintptr_t _counter = 0;

public:
/**
* @brief Generate an id.
*
* @return Id
*/
static inline Id generate() { return reinterpret_cast<Id>(_counter++); }

/**
* @brief Get the key of the id.
*
* @param id
* @return uintptr_t
*/
static inline uintptr_t key(Id id) { return reinterpret_cast<uintptr_t>(id); }
};
} // namespace Dynamo
119 changes: 0 additions & 119 deletions src/Utils/IdTracker.hpp

This file was deleted.

20 changes: 10 additions & 10 deletions src/Utils/SparseSet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <algorithm>
#include <vector>

#include <Utils/IdTracker.hpp>
#include <Utils/IdGenerator.hpp>
#include <Utils/Log.hpp>

namespace Dynamo {
Expand Down Expand Up @@ -41,7 +41,7 @@ namespace Dynamo {
* @brief Contains indices to _dense and _pool
*
*/
std::vector<int> _sparse;
std::vector<uintptr_t> _sparse;

public:
/**
Expand All @@ -66,14 +66,14 @@ namespace Dynamo {
* @return int Index position of the value (-1 on failure).
*/
inline int find(Id id) const {
unsigned key = IdTracker<Id>::get_index(id);
uintptr_t key = IdGenerator<Id>::key(id);
if (key >= _sparse.size()) {
return -1;
}

// Verify that the sparse and dense arrays are correlated
int index = _sparse[key];
int dense_size = _dense.size();
uintptr_t index = _sparse[key];
uintptr_t dense_size = _dense.size();
if (index == -1 || index >= dense_size || _dense[index] != id) {
return -1;
}
Expand Down Expand Up @@ -101,7 +101,7 @@ namespace Dynamo {
template <typename... Params>
inline void insert(Id id, Params... args) {
// Resize the sparse array or remove the existing item
unsigned key = IdTracker<Id>::get_index(id);
uintptr_t key = IdGenerator<Id>::key(id);
if (key >= _sparse.size()) {
_sparse.resize((key + 1) * 2, -1);
} else if (_sparse[key] != -1) {
Expand All @@ -127,20 +127,20 @@ namespace Dynamo {
* @param id Unique identifier of the object to be removed.
*/
inline void remove(Id id) {
unsigned key = IdTracker<Id>::get_index(id);
uintptr_t key = IdGenerator<Id>::key(id);
if (key >= _sparse.size()) {
return;
}

// Verify that the sparse and dense arrays are correlated
int index = _sparse[key];
int dense_size = _dense.size();
uintptr_t index = _sparse[key];
uintptr_t dense_size = _dense.size();
if (index == -1 || index >= dense_size || _dense[index] != id) {
return;
}

// Swap last element of dense and pool array to maintain contiguity
unsigned new_key = IdTracker<Id>::get_index(_dense.back());
uintptr_t new_key = IdGenerator<Id>::key(_dense.back());
std::swap(_pool.back(), _pool[index]);
_pool.pop_back();

Expand Down
19 changes: 19 additions & 0 deletions tests/src/Utils/IdGenerator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <Dynamo.hpp>
#include <catch2/catch_test_macros.hpp>

DYN_DEFINE_ID_TYPE(Id_0);
DYN_DEFINE_ID_TYPE(Id_1);

TEST_CASE("IdGenerator generate", "[IdGenerator]") {
Id_0 a0 = Dynamo::IdGenerator<Id_0>::generate();
Id_0 b0 = Dynamo::IdGenerator<Id_0>::generate();

Id_1 a1 = Dynamo::IdGenerator<Id_1>::generate();
Id_1 b1 = Dynamo::IdGenerator<Id_1>::generate();

REQUIRE(Dynamo::IdGenerator<Id_0>::key(a0) == 0);
REQUIRE(Dynamo::IdGenerator<Id_0>::key(b0) == 1);

REQUIRE(Dynamo::IdGenerator<Id_1>::key(a1) == 0);
REQUIRE(Dynamo::IdGenerator<Id_1>::key(b1) == 1);
}
Loading

0 comments on commit 6c6b22f

Please sign in to comment.