Maipa's Standard Library Extension 1.5.6
mstd
Loading...
Searching...
No Matches
id_manager.hpp
1/*
2 * mstd - Maipa's Standard Library
3 *
4 * Licensed under the BSD 3-Clause License with Attribution Requirement.
5 * See the LICENSE file for details: https://github.com/MAIPA01/mstd/blob/main/LICENSE
6 *
7 * Copyright (c) 2025, Patryk Antosik (MAIPA01)
8 */
9
10#pragma once
11#ifndef _MSTD_ID_MANAGER_HPP_
12 #define _MSTD_ID_MANAGER_HPP_
13
14 #include <mstd/config.hpp>
15
17_MSTD_WARNING("this is only available for c++17 and greater!");
18 #else
19
20 #include <mstd/types.hpp>
21
22namespace mstd {
24 template<unsigned_integral IdT>
25 #else
26 template<class IdT, std::enable_if_t<mstd::is_unsigned_integral_v<IdT>, bool> >
27 #endif
29 public:
30 using id_type = IdT;
31
33
34 private:
37
39 if (_removedIds.empty()) { return; }
40
41 auto last = std::prev(_removedIds.end());
42 while (*last == _nextId - 1) {
43 --_nextId;
45
46 if (_removedIds.empty()) { return; }
47
49 }
50 }
51
52 public:
53 _MSTD_CONSTEXPR20 base_id_manager() noexcept = default;
56 _MSTD_CONSTEXPR20 ~base_id_manager() noexcept = default;
57
60
62 if (!_removedIds.empty()) {
63 const id_type id = *_removedIds.begin();
65 return id;
66 }
67
68 if (_nextId == max_ids) { return bad_id(); }
69
70 const id_type id = _nextId;
71 ++_nextId;
72 return id;
73 }
74
77 if (id == bad_id() || id >= _nextId || _removedIds.contains(id)) {
78 #else
79 if (id == bad_id() || id >= _nextId || _removedIds.find(id) != _removedIds.end()) {
80 #endif
81 return false;
82 }
83
86 return true;
87 }
88
89 _MSTD_CONSTEXPR20 void reset() noexcept {
90 _nextId = 0;
92 }
93
94 [[nodiscard]] static _MSTD_CONSTEXPR20 id_type bad_id() noexcept { return ~static_cast<id_type>(0); }
95
96 [[nodiscard]] static _MSTD_CONSTEXPR20 id_type last_id() noexcept { return max_ids - 1; }
97 };
98} // namespace mstd
99 #endif
100#endif
#define _MSTD_HAS_CXX17
Definition config.hpp:45
#define _MSTD_CONSTEXPR17
Definition config.hpp:76
#define _MSTD_HAS_CXX20
Definition config.hpp:52
#define _MSTD_CONSTEXPR20
Definition config.hpp:84
Definition arithmetic_types.hpp:23