diff --git a/libs/util/include/psemek/util/unique_sequential_storage.hpp b/libs/util/include/psemek/util/unique_sequential_storage.hpp new file mode 100644 index 00000000..8c643e6a --- /dev/null +++ b/libs/util/include/psemek/util/unique_sequential_storage.hpp @@ -0,0 +1,37 @@ +#pragma once + +#include +#include +#include + +namespace psemek::util +{ + + template > + struct unique_sequential_storage + { + using index = std::uint32_t; + + template + index insert(H && value) + { + if (auto it = value_to_index_.find(value); it != value_to_index_.end()) + return it->second; + + index result = index_to_value_.size(); + index_to_value_.push_back(value); + value_to_index_[std::forward(value)] = result; + return result; + } + + T const & get(index i) const + { + return index_to_value_[i]; + } + + private: + std::unordered_map value_to_index_; + std::vector index_to_value_; + }; + +}