Huge refactor: use util::hash_table instead of std::unordered everywhere

This commit is contained in:
Nikita Lisitsa 2024-06-03 14:36:39 +03:00
parent e79266d7e2
commit 22a57f91db
23 changed files with 53 additions and 61 deletions

View file

@ -1,8 +1,7 @@
#pragma once #pragma once
#include <psemek/app/events.hpp> #include <psemek/app/events.hpp>
#include <psemek/util/hash_table.hpp>
#include <unordered_set>
namespace psemek::app namespace psemek::app
{ {
@ -13,8 +12,8 @@ namespace psemek::app
bool focus = true; bool focus = true;
geom::point<int, 2> mouse = {0, 0}; geom::point<int, 2> mouse = {0, 0};
int wheel = 0; int wheel = 0;
std::unordered_set<mouse_button> mouse_button_down; util::hash_set<mouse_button> mouse_button_down;
std::unordered_set<keycode> key_down; util::hash_set<keycode> key_down;
}; };
inline void apply(event_state & state, resize_event const & event) inline void apply(event_state & state, resize_event const & event)

View file

@ -1,10 +1,10 @@
#pragma once #pragma once
#include <psemek/geom/vector.hpp> #include <psemek/geom/vector.hpp>
#include <psemek/util/hash_table.hpp>
#include <psemek/io/stream.hpp> #include <psemek/io/stream.hpp>
#include <string> #include <string>
#include <unordered_map>
namespace psemek::fonts namespace psemek::fonts
{ {
@ -25,7 +25,7 @@ namespace psemek::fonts
std::string name; std::string name;
geom::vector<int, 2> size; geom::vector<int, 2> size;
int baseline; int baseline;
std::unordered_map<char32_t, glyph_data> glyphs; util::hash_map<char32_t, glyph_data> glyphs;
float sdf_scale = 0.f; float sdf_scale = 0.f;
// Parse JSON font info generated by BMFont or msdf-bmfont // Parse JSON font info generated by BMFont or msdf-bmfont

View file

@ -3,8 +3,6 @@
#include <psemek/fonts/font.hpp> #include <psemek/fonts/font.hpp>
#include <psemek/fonts/bmfont.hpp> #include <psemek/fonts/bmfont.hpp>
#include <unordered_map>
namespace psemek::fonts namespace psemek::fonts
{ {

View file

@ -3,8 +3,6 @@
#include <psemek/fonts/font.hpp> #include <psemek/fonts/font.hpp>
#include <psemek/fonts/kerned_font.hpp> #include <psemek/fonts/kerned_font.hpp>
#include <unordered_map>
namespace psemek::fonts namespace psemek::fonts
{ {

View file

@ -2,6 +2,7 @@
#include <psemek/geom/box.hpp> #include <psemek/geom/box.hpp>
#include <psemek/geom/simplex.hpp> #include <psemek/geom/simplex.hpp>
#include <psemek/util/hash_table.hpp>
#include <vector> #include <vector>
#include <cstdint> #include <cstdint>
@ -170,7 +171,7 @@ namespace psemek::geom
return e; return e;
}; };
std::unordered_map<edge, Index> midpoints; util::hash_map<edge, Index> midpoints;
auto add_midpoint = [&](Index i0, Index i1) auto add_midpoint = [&](Index i0, Index i1)
{ {

View file

@ -7,12 +7,12 @@
#include <psemek/geom/affine_transform.hpp> #include <psemek/geom/affine_transform.hpp>
#include <psemek/geom/easing.hpp> #include <psemek/geom/easing.hpp>
#include <psemek/util/hstring.hpp> #include <psemek/util/hstring.hpp>
#include <psemek/util/hash_table.hpp>
#include <vector> #include <vector>
#include <string> #include <string>
#include <optional> #include <optional>
#include <variant> #include <variant>
#include <unordered_map>
namespace psemek::gfx namespace psemek::gfx
{ {
@ -20,7 +20,7 @@ namespace psemek::gfx
struct gltf_asset struct gltf_asset
{ {
using extra = std::variant<float, std::vector<float>, std::string>; using extra = std::variant<float, std::vector<float>, std::string>;
using extras_map = std::unordered_map<util::hstring, extra>; using extras_map = util::hash_map<util::hstring, extra>;
struct node struct node
{ {
@ -182,8 +182,8 @@ namespace psemek::gfx
std::vector<buffer> buffers; std::vector<buffer> buffers;
std::vector<light> lights; // KHR_lights_punctual std::vector<light> lights; // KHR_lights_punctual
std::unordered_map<std::string, std::size_t> node_index; // node by name util::hash_map<std::string, std::size_t> node_index; // node by name
std::unordered_map<std::string, std::size_t> material_index; // material by name util::hash_map<std::string, std::size_t> material_index; // material by name
struct animation_and_channel struct animation_and_channel
{ {

View file

@ -14,11 +14,9 @@
#include <psemek/util/assert.hpp> #include <psemek/util/assert.hpp>
#include <psemek/util/span.hpp> #include <psemek/util/span.hpp>
#include <psemek/util/hash_table.hpp>
#include <cstddef> #include <cstddef>
#include <type_traits>
#include <cmath>
#include <unordered_map>
namespace psemek::gfx namespace psemek::gfx
{ {
@ -348,7 +346,7 @@ namespace psemek::gfx
} }
using pose_ref = util::span<bone_transform<float> const>; using pose_ref = util::span<bone_transform<float> const>;
using pose_library = std::unordered_map<std::string_view, pose_ref>; using pose_library = util::hash_map<std::string_view, pose_ref>;
struct imported_mesh struct imported_mesh
{ {

View file

@ -1,11 +1,10 @@
#include <psemek/gfx/color.hpp> #include <psemek/gfx/color.hpp>
#include <psemek/util/hash_table.hpp>
#include <unordered_map>
namespace psemek::gfx namespace psemek::gfx
{ {
static std::unordered_map<std::string_view, color_rgba> const named_colors static util::hash_map<std::string_view, color_rgba> const named_colors
{ {
{"black", {0, 0, 0, 255}}, {"black", {0, 0, 0, 255}},
{"white", {255, 255, 255, 255}}, {"white", {255, 255, 255, 255}},

View file

@ -3,11 +3,9 @@
#include <psemek/gfx/array.hpp> #include <psemek/gfx/array.hpp>
#include <psemek/gfx/buffer.hpp> #include <psemek/gfx/buffer.hpp>
#include <psemek/util/hstring.hpp> #include <psemek/util/hstring.hpp>
#include <psemek/util/hash_table.hpp>
#include <psemek/io/memory_stream.hpp> #include <psemek/io/memory_stream.hpp>
#include <stdexcept>
#include <unordered_map>
namespace psemek::gfx namespace psemek::gfx
{ {
@ -75,7 +73,7 @@ namespace psemek::gfx
std::vector<gfx::texture_2d> textures_; std::vector<gfx::texture_2d> textures_;
std::vector<gfx::buffer> buffers_; std::vector<gfx::buffer> buffers_;
std::vector<std::unique_ptr<drawable_impl>> drawables_; std::vector<std::unique_ptr<drawable_impl>> drawables_;
std::unordered_map<util::hstring, mesh_data> meshes_; util::hash_map<util::hstring, mesh_data> meshes_;
}; };
gltf_mesh_impl::gltf_mesh_impl(gltf_asset const & asset, std::function<util::blob(std::string const &)> uri_loader) gltf_mesh_impl::gltf_mesh_impl(gltf_asset const & asset, std::function<util::blob(std::string const &)> uri_loader)

View file

@ -3,6 +3,7 @@
#include <psemek/geom/rotation.hpp> #include <psemek/geom/rotation.hpp>
#include <psemek/geom/translation.hpp> #include <psemek/geom/translation.hpp>
#include <psemek/util/to_string.hpp> #include <psemek/util/to_string.hpp>
#include <psemek/util/hash_table.hpp>
#include <psemek/util/exception.hpp> #include <psemek/util/exception.hpp>
#include <psemek/io/memory_stream.hpp> #include <psemek/io/memory_stream.hpp>
#include <psemek/log/log.hpp> #include <psemek/log/log.hpp>
@ -70,7 +71,7 @@ namespace psemek::gfx
throw util::exception(util::to_string("Unknown accessor component type: ", type)); throw util::exception(util::to_string("Unknown accessor component type: ", type));
} }
static std::unordered_set<std::string> supported_extensions = static util::hash_set<std::string> const supported_extensions =
{ {
"KHR_lights_punctual", "KHR_lights_punctual",
"KHR_materials_emissive_strength" "KHR_materials_emissive_strength"

View file

@ -27,10 +27,10 @@
#include <psemek/util/to_string.hpp> #include <psemek/util/to_string.hpp>
#include <psemek/util/hash.hpp> #include <psemek/util/hash.hpp>
#include <psemek/util/hash_table.hpp>
#include <psemek/util/exception.hpp> #include <psemek/util/exception.hpp>
#include <map> #include <map>
#include <unordered_map>
namespace psemek::gfx namespace psemek::gfx
{ {
@ -1046,7 +1046,7 @@ void main(){}
struct bin struct bin
{ {
std::unordered_map<std::tuple<std::uint32_t, material const *>, objects_bucket> buckets; util::hash_map<std::tuple<std::uint32_t, material const *>, objects_bucket> buckets;
geom::box<float, 3> bbox; geom::box<float, 3> bbox;
bool contains_near_clip = false; bool contains_near_clip = false;
float camera_separation; float camera_separation;

View file

@ -1,6 +1,7 @@
#include <psemek/log/log.hpp> #include <psemek/log/log.hpp>
#include <psemek/util/to_string.hpp> #include <psemek/util/to_string.hpp>
#include <psemek/util/exception.hpp> #include <psemek/util/exception.hpp>
#include <psemek/util/hash_table.hpp>
#include <boost/stacktrace/stacktrace.hpp> #include <boost/stacktrace/stacktrace.hpp>
#include <chrono> #include <chrono>
@ -9,7 +10,6 @@
#include <sstream> #include <sstream>
#include <iomanip> #include <iomanip>
#include <mutex> #include <mutex>
#include <unordered_map>
#include <thread> #include <thread>
#include <atomic> #include <atomic>
#include <vector> #include <vector>
@ -24,7 +24,7 @@ namespace psemek::log
std::atomic<std::size_t> max_thread_name_length = 3; std::atomic<std::size_t> max_thread_name_length = 3;
std::mutex thread_names_mutex; std::mutex thread_names_mutex;
std::unordered_map<std::thread::id, std::string> thread_names; util::hash_map<std::thread::id, std::string> thread_names;
std::mutex sinks_mutex; std::mutex sinks_mutex;
std::vector<std::unique_ptr<sink>> sinks; std::vector<std::unique_ptr<sink>> sinks;

View file

@ -2,9 +2,9 @@
#include <psemek/util/function.hpp> #include <psemek/util/function.hpp>
#include <psemek/util/array.hpp> #include <psemek/util/array.hpp>
#include <psemek/util/hash_table.hpp>
#include <psemek/geom/box.hpp> #include <psemek/geom/box.hpp>
#include <unordered_map>
#include <optional> #include <optional>
namespace psemek::pcg namespace psemek::pcg
@ -68,7 +68,7 @@ namespace psemek::pcg
private: private:
int const chunk_size_; int const chunk_size_;
generator_func generator_; generator_func generator_;
mutable std::unordered_map<geom::vector<int, N>, util::array<T, N>> chunks_; mutable util::hash_map<geom::vector<int, N>, util::array<T, N>> chunks_;
}; };

View file

@ -2,11 +2,11 @@
#include <psemek/util/function.hpp> #include <psemek/util/function.hpp>
#include <psemek/util/array.hpp> #include <psemek/util/array.hpp>
#include <psemek/util/hash_table.hpp>
#include <psemek/geom/vector.hpp> #include <psemek/geom/vector.hpp>
#include <psemek/geom/box.hpp> #include <psemek/geom/box.hpp>
#include <unordered_map>
#include <optional> #include <optional>
namespace psemek::pcg namespace psemek::pcg
@ -101,7 +101,7 @@ namespace psemek::pcg
private: private:
int const grid_size_; int const grid_size_;
mutable std::unordered_map<geom::vector<int, N>, geom::vector<T, N>> grid_; mutable util::hash_map<geom::vector<int, N>, geom::vector<T, N>> grid_;
generator_func gen_; generator_func gen_;
geom::vector<T, N> grid_at(geom::vector<int, N> const & c) const geom::vector<T, N> grid_at(geom::vector<int, N> const & c) const

View file

@ -1,12 +1,11 @@
#include <psemek/rs/registry.hpp> #include <psemek/rs/registry.hpp>
#include <psemek/util/to_string.hpp> #include <psemek/util/to_string.hpp>
#include <psemek/util/hash_table.hpp>
#include <map> #include <map>
#include <unordered_map>
#include <mutex> #include <mutex>
#include <atomic> #include <atomic>
#include <optional>
namespace psemek::rs namespace psemek::rs
{ {
@ -34,7 +33,7 @@ namespace psemek::rs
auto & name_map() auto & name_map()
{ {
static std::unordered_map<std::string_view, id> instance; static util::hash_map<std::string_view, id> instance;
return instance; return instance;
} }

View file

@ -7,11 +7,10 @@
#include <psemek/util/to_string.hpp> #include <psemek/util/to_string.hpp>
#include <psemek/util/type_name.hpp> #include <psemek/util/type_name.hpp>
#include <psemek/util/function.hpp> #include <psemek/util/function.hpp>
#include <psemek/util/hash_table.hpp>
#include <psemek/util/exception.hpp> #include <psemek/util/exception.hpp>
#include <exception>
#include <typeindex> #include <typeindex>
#include <unordered_map>
namespace psemek::ui::impl namespace psemek::ui::impl
{ {
@ -92,7 +91,7 @@ namespace psemek::ui::impl
impl->set_children_token(typed_value.children.subscribe([this, impl = impl.get()](std::vector<typename Type::element> const & children_values){ impl->set_children_token(typed_value.children.subscribe([this, impl = impl.get()](std::vector<typename Type::element> const & children_values){
impl->release_child_tokens(); impl->release_child_tokens();
std::unordered_map<key, std::unique_ptr<component>> child_by_key; util::hash_map<key, std::unique_ptr<component>> child_by_key;
auto old_children = impl->release_children(); auto old_children = impl->release_children();
{ {
@ -159,7 +158,7 @@ namespace psemek::ui::impl
} }
private: private:
std::unordered_map<std::type_index, util::function<std::unique_ptr<component>(std::unique_ptr<component>, std::any const &)>> type_factories_; util::hash_map<std::type_index, util::function<std::unique_ptr<component>(std::unique_ptr<component>, std::any const &)>> type_factories_;
}; };
} }

View file

@ -1,8 +1,9 @@
#pragma once #pragma once
#include <psemek/util/hash_table.hpp>
#include <typeindex> #include <typeindex>
#include <any> #include <any>
#include <unordered_map>
namespace psemek::util namespace psemek::util
{ {
@ -34,7 +35,7 @@ namespace psemek::util
void remove(); void remove();
private: private:
std::unordered_map<std::type_index, std::any> storage_; util::hash_map<std::type_index, std::any> storage_;
}; };
template <typename T> template <typename T>

View file

@ -6,6 +6,7 @@
#include <psemek/util/range.hpp> #include <psemek/util/range.hpp>
#include <psemek/util/hash.hpp> #include <psemek/util/hash.hpp>
#include <psemek/util/hstring.hpp> #include <psemek/util/hstring.hpp>
#include <psemek/util/hash_table.hpp>
#include <psemek/util/exception.hpp> #include <psemek/util/exception.hpp>
#include <cstdint> #include <cstdint>
@ -18,8 +19,6 @@
#include <functional> #include <functional>
#include <optional> #include <optional>
#include <experimental/type_traits>
namespace psemek::util namespace psemek::util
{ {
@ -710,17 +709,17 @@ namespace psemek::util
private: private:
std::vector<std::unique_ptr<ecs_detail::species_base>> species_; std::vector<std::unique_ptr<ecs_detail::species_base>> species_;
std::unordered_map<util::hstring, ecs_detail::species_handle> species_by_name_; util::hash_map<util::hstring, ecs_detail::species_handle> species_by_name_;
std::unordered_map<std::type_index, std::vector<util::function<void(void const *)>>> event_subscribers_; util::hash_map<std::type_index, std::vector<util::function<void(void const *)>>> event_subscribers_;
std::vector<util::function<void(species_handle)>> species_constructors_; std::vector<util::function<void(species_handle)>> species_constructors_;
std::vector<util::function<void(entity_handle)>> constructors_; std::vector<util::function<void(entity_handle)>> constructors_;
std::unordered_map<ecs_detail::species_handle, std::vector<util::function<void(entity_handle)>>> private_constructors_; util::hash_map<ecs_detail::species_handle, std::vector<util::function<void(entity_handle)>>> private_constructors_;
std::vector<util::function<void(entity_handle)>> destructors_; std::vector<util::function<void(entity_handle)>> destructors_;
std::unordered_map<ecs_detail::species_handle, std::vector<util::function<void(entity_handle)>>> private_destructors_; util::hash_map<ecs_detail::species_handle, std::vector<util::function<void(entity_handle)>>> private_destructors_;
}; };
template <typename ... Components> template <typename ... Components>

View file

@ -5,13 +5,13 @@
#include <psemek/util/to_string.hpp> #include <psemek/util/to_string.hpp>
#include <psemek/util/type_name.hpp> #include <psemek/util/type_name.hpp>
#include <psemek/util/hash_table.hpp>
#include <psemek/util/exception.hpp> #include <psemek/util/exception.hpp>
#include <string_view>
#include <stdexcept>
#include <type_traits> #include <type_traits>
#include <optional> #include <optional>
#include <unordered_map> #include <string_view>
#include <iostream>
namespace psemek::util namespace psemek::util
{ {
@ -104,7 +104,7 @@ namespace psemek::util
} \ } \
} \ } \
inline std::optional<name> BOOST_PP_CAT(name, _from_string)(::std::string_view str) { \ inline std::optional<name> BOOST_PP_CAT(name, _from_string)(::std::string_view str) { \
static const ::std::unordered_map<::std::string_view, name> map = { \ static const ::psemek::util::hash_map<::std::string_view, name> map = { \
BOOST_PP_SEQ_FOR_EACH(psemek_declare_enum_detail_from_string, name, values) \ BOOST_PP_SEQ_FOR_EACH(psemek_declare_enum_detail_from_string, name, values) \
}; \ }; \
auto it = map.find(str); if (it == map.end()) return std::nullopt; return it->second; \ auto it = map.find(str); if (it == map.end()) return std::nullopt; return it->second; \

View file

@ -1,10 +1,10 @@
#pragma once #pragma once
#include <psemek/util/functional.hpp> #include <psemek/util/functional.hpp>
#include <psemek/util/hash_table.hpp>
#include <psemek/util/at.hpp> #include <psemek/util/at.hpp>
#include <list> #include <list>
#include <unordered_map>
namespace psemek::util namespace psemek::util
{ {
@ -79,7 +79,7 @@ namespace psemek::util
// list_ contains non-removable items first, then all removable items // list_ contains non-removable items first, then all removable items
std::list<value_type> list_; std::list<value_type> list_;
iterator removable_begin_ = list_.end(); iterator removable_begin_ = list_.end();
std::unordered_map<Key, iterator> map_; util::hash_map<Key, iterator> map_;
iterator find_safe(Key const & key); iterator find_safe(Key const & key);
const_iterator find_safe(Key const & key) const; const_iterator find_safe(Key const & key) const;

View file

@ -1,12 +1,12 @@
#pragma once #pragma once
#include <psemek/util/hstring.hpp> #include <psemek/util/hstring.hpp>
#include <psemek/util/hash_table.hpp>
#include <vector> #include <vector>
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <cstdint> #include <cstdint>
#include <unordered_map>
#include <optional> #include <optional>
namespace psemek::util namespace psemek::util
@ -82,7 +82,7 @@ namespace psemek::util
private: private:
std::vector<object> objects_; std::vector<object> objects_;
std::unordered_map<util::hstring, Index> name_map_; util::hash_map<util::hstring, Index> name_map_;
}; };
} }

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <unordered_map> #include <psemek/util/hash_table.hpp>
#include <vector> #include <vector>
#include <cstdint> #include <cstdint>
@ -30,7 +31,7 @@ namespace psemek::util
} }
private: private:
std::unordered_map<T, index, Hash> value_to_index_; util::hash_map<T, index, Hash> value_to_index_;
std::vector<T> index_to_value_; std::vector<T> index_to_value_;
}; };

View file

@ -4,6 +4,7 @@
#include <psemek/random/generator.hpp> #include <psemek/random/generator.hpp>
#include <psemek/random/uniform_sphere.hpp> #include <psemek/random/uniform_sphere.hpp>
#include <psemek/gfx/pixmap.hpp> #include <psemek/gfx/pixmap.hpp>
#include <psemek/util/hash_table.hpp>
#include <psemek/io/file_stream.hpp> #include <psemek/io/file_stream.hpp>
using namespace psemek; using namespace psemek;
@ -198,7 +199,7 @@ std::optional<bool> parse_bool(std::string const & str)
int main(int argc, char * argv[]) int main(int argc, char * argv[])
{ {
std::unordered_map<std::string, std::string> values; util::hash_map<std::string, std::string> values;
values["dim"] = "2"; values["dim"] = "2";
values["sizex"] = "256"; values["sizex"] = "256";
values["sizey"] = "256"; values["sizey"] = "256";