Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cth/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ target_compile_options(cth INTERFACE $<$<CXX_COMPILER_ID:MSVC>:/Zc:preprocessor>
cth_pkg_target_find_package(cth boost_pfr CONFIG REQUIRED)
target_link_libraries(cth INTERFACE Boost::pfr)

# cth/data/hash_map.hpp includes gtl/phmap.hpp, gtl installs no cmake package
cth_pkg_add_find_modules(cmake/Findgtl.cmake)
cth_pkg_target_find_package(cth gtl MODULE REQUIRED)
target_link_libraries(cth INTERFACE gtl::gtl)

target_link_libraries(cth INTERFACE $<$<AND:$<CXX_COMPILER_ID:GNU,Clang>,$<NOT:$<PLATFORM_ID:Windows>>>:stdc++exp>)

# tests
Expand Down
12 changes: 12 additions & 0 deletions cth/cmake/Findgtl.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# gtl installs only its headers and no cmake package, this module creates the target gtl::gtl
include(FindPackageHandleStandardArgs)

find_path(gtl_INCLUDE_DIR gtl/phmap.hpp)
find_package_handle_standard_args(gtl REQUIRED_VARS gtl_INCLUDE_DIR)

if(gtl_FOUND AND NOT TARGET gtl::gtl)
add_library(gtl::gtl INTERFACE IMPORTED)
target_include_directories(gtl::gtl SYSTEM INTERFACE "${gtl_INCLUDE_DIR}")
endif()

mark_as_advanced(gtl_INCLUDE_DIR)
4 changes: 2 additions & 2 deletions cth/incl/cth/algorithm/combine.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include "cth/data/hash_map.hpp"
#include "cth/io/log.hpp"
#include "cth/meta/ranges.hpp"

Expand All @@ -9,7 +10,6 @@
#include <queue>
#include <ranges>
#include <span>
#include <unordered_map>
#include <unordered_set>
#include <vector>

Expand All @@ -26,7 +26,7 @@ template<mta::range2d_over_cpt<CPT(std::equality_comparable)> Rng>
[[nodiscard]] auto unique_combine(Rng const& selections) {
using T = std::remove_cvref_t<mta::range2d_value_t<Rng>>;
using node_t = T const*;
using node_selection_map_t = std::unordered_map<node_t, size_t>;
using node_selection_map_t = dt::flat_hash_map<node_t, size_t>;
static constexpr node_t EMPTY_NODE = nullptr;

auto const selectionCount = std::ranges::size(selections);
Expand Down
16 changes: 16 additions & 0 deletions cth/incl/cth/data/hash_map.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once
#include <gtl/phmap.hpp>

namespace cth::dt {
/**
* @brief hash map without pointer stability, a rehash moves the elements
*/
template<class Key, class T, class... Args>
using flat_hash_map = gtl::flat_hash_map<Key, T, Args...>;

/**
* @brief hash set without pointer stability, a rehash moves the elements
*/
template<class Key, class... Args>
using flat_hash_set = gtl::flat_hash_set<Key, Args...>;
}
4 changes: 2 additions & 2 deletions cth/incl/cth/data/pool.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include "cth/data/hash_map.hpp"
#include "cth/io/log.hpp"
#include "cth/meta/concepts.hpp"

Expand All @@ -7,7 +8,6 @@
#include <functional>
#include <memory>
#include <ranges>
#include <unordered_set>
#include <vector>

namespace cth::dt {
Expand Down Expand Up @@ -113,7 +113,7 @@ class pool {
private:
void resetActive() {
if constexpr(HAS_RESET) {
std::unordered_set inactiveSet{std::from_range, _inactive};
flat_hash_set<T*> const inactiveSet{_inactive.begin(), _inactive.end()};

for(auto& stored : _storage)
if(!inactiveSet.contains(&stored))
Expand Down
4 changes: 2 additions & 2 deletions cth/incl/cth/data/thread_pool.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#pragma once
#include "cth/data/hash_map.hpp"
#include "cth/data/pool.hpp"

#include <cstddef>
#include <mutex>
#include <thread>
#include <unordered_map>

namespace cth::dt {

Expand Down Expand Up @@ -91,7 +91,7 @@ class thread_pool {
private:
mutable std::mutex _mtx;
pool_t _pool;
std::unordered_map<std::thread::id, T*> _bound{};
flat_hash_map<std::thread::id, T*> _bound{};

public:
/** total number of instances in the pool */
Expand Down
16 changes: 9 additions & 7 deletions cth/incl/cth/data/union_find.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#include "cth/data/poly_vector.hpp"

#include <cstddef>
#include <unordered_map>
#include <unordered_set>
#include <limits>
#include <vector>

namespace cth::dt {
/**
Expand Down Expand Up @@ -162,14 +162,16 @@ class union_find {
[[nodiscard]] constexpr std::vector<group_t> all() {
std::vector<group_t> result{};

std::unordered_map<index_type, size_t> groups{};
// group of each root, the roots are indices
constexpr size_t NO_GROUP = std::numeric_limits<size_t>::max();
std::vector<size_t> groups(size(), NO_GROUP);

for(index_type idx = 0; idx < size(); idx++) {
auto const parent = find(idx);
if(auto groupIt = groups.find(parent); groupIt != groups.end())
result[groupIt->second].emplace_back(idx);
auto& group = groups[find(idx)];
if(group != NO_GROUP)
result[group].emplace_back(idx);
else {
groups[parent] = result.size();
group = result.size();
result.emplace_back(1)[0] = idx;
}
}
Expand Down
6 changes: 3 additions & 3 deletions cth/tests/src/data/thread_pool_test.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include "cth/data/thread_pool.hpp"
#include "cth/constants.hpp"
#include "cth/data/hash_map.hpp"
#include "test.hpp"

#include <atomic>
#include <cstddef>
#include <latch>
#include <string>
#include <thread>
#include <unordered_set>
#include <vector>


Expand Down Expand Up @@ -157,7 +157,7 @@ DATA_TEST(thread_pool, distinct_ids_get_distinct_resources) {
EXPECT_TRUE(tp.exhausted());

// three owners hold three distinct resources
std::unordered_set<int*> refs{&a, &b, &c};
cth::dt::flat_hash_set<int*> refs{&a, &b, &c};
EXPECT_EQ(refs.size(), 3);
}

Expand Down Expand Up @@ -292,7 +292,7 @@ DATA_TEST(thread_pool, concurrent_acquire_gives_each_thread_a_distinct_resource)
EXPECT_TRUE(tp.exhausted());

// every thread received a distinct resource, none null
std::unordered_set<int*> unique{acquired.begin(), acquired.end()};
cth::dt::flat_hash_set<int*> unique{acquired.begin(), acquired.end()};
EXPECT_EQ(unique.size(), THREADS);
EXPECT_FALSE(unique.contains(nullptr));
}
Expand Down
5 changes: 3 additions & 2 deletions cth_net/src/websocket_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
#include "cth/net/exception.hpp"
#include "websocket_connection_state.hpp"

#include "cth/data/hash_map.hpp"

#include <hv/WebSocketServer.h>
#include <hv/hsocket.h>

#include <atomic>
#include <deque>
#include <mutex>
#include <unordered_map>
#include <utility>
#include <vector>

Expand All @@ -22,7 +23,7 @@ struct websocket_server::impl {
std::uint16_t boundPort{};

mutable std::mutex mutex;
std::unordered_map<hv::WebSocketChannel*, std::shared_ptr<dev::websocket_connection_state>>
dt::flat_hash_map<hv::WebSocketChannel*, std::shared_ptr<dev::websocket_connection_state>>
connections;
std::deque<std::shared_ptr<dev::websocket_connection_state>> accepted;
os::fence readyFence;
Expand Down
2 changes: 1 addition & 1 deletion lib/cth_cmake
1 change: 1 addition & 0 deletions vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"boost-pfr",
"glm",
"gtest",
"gtl",
"stb",
"asio",
"libhv"
Expand Down
Loading