diff options
author | Claudius "keldu" Holeksa <mail@keldu.de> | 2024-05-29 21:16:06 +0200 |
---|---|---|
committer | Claudius "keldu" Holeksa <mail@keldu.de> | 2024-05-29 21:16:06 +0200 |
commit | 60d0f8da2b754d1deb0dbb59f6e6783ba4c692c4 (patch) | |
tree | c0d49736f035220640ed01cfb210d37e7bb254cb /modules/core | |
parent | 7b6e0ca99f8521e034452f0d0243a7f3e33843a9 (diff) |
Reworked id_map and trying to fix sycl launch
Diffstat (limited to 'modules/core')
-rw-r--r-- | modules/core/c++/id_map.hpp | 164 | ||||
-rw-r--r-- | modules/core/tests/core.cpp | 27 |
2 files changed, 0 insertions, 191 deletions
diff --git a/modules/core/c++/id_map.hpp b/modules/core/c++/id_map.hpp deleted file mode 100644 index 1df2178..0000000 --- a/modules/core/c++/id_map.hpp +++ /dev/null @@ -1,164 +0,0 @@ -#pragma once - -#include "id.hpp" -#include "error.hpp" - -#include <deque> - -namespace saw { -/** - * Fast random access id based container. - * - * Access - O(1) - * Insert - O(1) - * Erase - O(n) ? Dunno - */ -template<typename T> -class id_map final { -private: - /** - * Container which stores the primary data - */ - std::vector<T> data_; - /** - * Container which tracks free'd/fragmented elements within the - * main container - */ - std::deque<id<T>> free_ids_; - -private: - /** - * Tries to reduce top ids - */ - void reduce_free_ids() noexcept { - for(;;){ - /** - * If we have no free ids left, we also have nothing to reduce - */ - if(free_ids_.empty()){ - break; - } - - /** - * The front contains the highest free id. - */ - if((free_ids_.front().get_value() + 1) < data_.size()){ - break; - } - - /** - * Can this throw? - */ - free_ids_.pop_front(); - } - } -public: - /** - * Default constructor - */ - id_map() = default; - - SAW_FORBID_COPY(id_map); - SAW_DEFAULT_MOVE(id_map); - - /** - * Inserts an element into the container and returns either an id on success - * or an error on failure. - */ - error_or<id<T>> insert(T val) noexcept { - /// @todo Fix size_t and id base type - if(free_ids_.empty()){ - try { - size_t i = data_.size(); - data_.emplace_back(std::move(val)); - return saw::id<T>{i}; - } catch(std::exception& e) { - return make_error<err::out_of_memory>(); - } - } else { - auto f_id = std::move(free_ids_.back()); - free_ids_.pop_back(); - data_.at(f_id.get_value()) = std::move(val); - return f_id; - } - - exit(-1); - // Dummy return since this is not reachable - return make_error<err::critical>(); - } - - /** - * Erase a value at this id. - */ - error_or<void> erase(const id<T>& val) noexcept { - /** - * If id is bigger than the available vector then return an error. - */ - if(val.get_value() >= data_.size()){ - return make_error<err::not_found>("ID is too large"); - } - - /** - * Check if it's the highest ID. Then we can just try to reduce the highest - * IDs. - */ - if((val.get_value() + 1) == data_.size()){ - data_.pop_back(); - this->reduce_free_ids(); - if(data_.size()*2 <= data_.capacity()){ - try { - data_.shrink_to_fit(); - }catch(std::exception& e){ - return make_error<err::out_of_memory>(); - } - } - return void_t{}; - } - - /** - * Check if ID already exists with the free IDs. - * This would mean that a double free has occured. - */ - auto find_id = std::find(free_ids_.begin(), free_ids_.end(), val); - if(find_id != free_ids_.end()){ - return make_error<err::not_found>("ID value has already been freed"); - } - - /** - * Insert id into deque and sort it. - */ - try { - free_ids_.push_back(val); - } catch(std::exception& e){ - return make_error<err::out_of_memory>(); - } - std::stable_sort(free_ids_.begin(), free_ids_.end(), - [](const id<T>& left, const id<T>& right) -> bool { - return left.get_value() > right.get_value(); - } - ); - return void_t{}; - } - - /** - * Tries to find a value based on an id. - * Returns an error on failure and returns - * a value pointer on success. - */ - error_or<T*> find(const id<T>& val){ - if(val.get_value() >= data_.size()){ - return make_error<err::not_found>("ID is too large"); - } - - /** - * This can be removed technically if we are not in a debug state? - */ - auto find_id = std::find(free_ids_.begin(), free_ids_.end(), val); - if(find_id != free_ids_.end()){ - return make_error<err::not_found>("ID value has already been freed"); - } - - return &data_.at(val.get_value()); - } -}; -} diff --git a/modules/core/tests/core.cpp b/modules/core/tests/core.cpp index 48a25ea..b1ce741 100644 --- a/modules/core/tests/core.cpp +++ b/modules/core/tests/core.cpp @@ -1,6 +1,5 @@ #include "../c++/test/suite.hpp" #include "../c++/id.hpp" -#include "../c++/id_map.hpp" #include "../c++/string_literal.hpp" namespace { @@ -38,30 +37,4 @@ SAW_TEST("String Literal Append"){ SAW_EXPECT(c == "foobar", "CT String sum is not \"foobar\""); } - -SAW_TEST("ID Map Insert"){ - using namespace saw; - - struct foo {}; - - id_map<foo> map; - { - auto eoid = map.insert(foo{}); - SAW_EXPECT(eoid.is_value(), "First insert failed"); - - auto& id = eoid.get_value(); - - auto eoid_2 = map.insert(foo{}); - SAW_EXPECT(eoid_2.is_value(), "Second Insert failed"); - auto& id_2 = eoid_2.get_value(); - - SAW_EXPECT(id != id_2, "Shouldn't be equal"); - - auto eov = map.erase(id); - SAW_EXPECT(eov.is_value(), "Erase failed"); - - auto eov_2 = map.erase(id); - SAW_EXPECT(eov_2.is_error(), "This is a double free"); - } -} } |