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
16
#
if
!
_MSTD_HAS_CXX17
17
_MSTD_WARNING(
"this is only available for c++17 and greater!"
);
18
#
else
19
20
#
include
<
mstd
/
types
.
hpp
>
21
22
namespace
mstd
{
23
#
if
_MSTD_HAS_CXX20
24
template
<
unsigned_integral
IdT
>
25
#
else
26
template
<
class
IdT
,
std
::
enable_if_t
<
mstd
::
is_unsigned_integral_v
<
IdT
>,
bool
> >
27
#
endif
28
class
base_id_manager
{
29
public
:
30
using
id_type
=
IdT
;
31
32
static
_MSTD_CONSTEXPR17
id_type
max_ids
=
std
::
numeric_limits
<
id_type
>::
max
();
33
34
private
:
35
id_type
_nextId
= 0;
36
std
::
set
<
id_type
>
_removedIds
= {};
37
38
_MSTD_CONSTEXPR20
void
_update_removed_ids
() {
39
if
(
_removedIds
.
empty
()) {
return
; }
40
41
auto
last
=
std
::
prev
(
_removedIds
.
end
());
42
while
(*
last
==
_nextId
- 1) {
43
--
_nextId
;
44
_removedIds
.
erase
(*
last
);
45
46
if
(
_removedIds
.
empty
()) {
return
; }
47
48
last
=
std
::
prev
(
_removedIds
.
end
());
49
}
50
}
51
52
public
:
53
_MSTD_CONSTEXPR20
base_id_manager
()
noexcept
=
default
;
54
_MSTD_CONSTEXPR20
base_id_manager
(
const
base_id_manager
&
other
)
noexcept
=
default
;
55
_MSTD_CONSTEXPR20
base_id_manager
(
base_id_manager
&&
other
)
noexcept
=
default
;
56
_MSTD_CONSTEXPR20
~
base_id_manager
()
noexcept
=
default
;
57
58
_MSTD_CONSTEXPR20
base_id_manager
&
operator
=(
const
base_id_manager
&
other
)
noexcept
=
default
;
59
_MSTD_CONSTEXPR20
base_id_manager
&
operator
=(
base_id_manager
&&
other
)
noexcept
=
default
;
60
61
[[
nodiscard
]]
_MSTD_CONSTEXPR20
id_type
get_next_id
() {
62
if
(!
_removedIds
.
empty
()) {
63
const
id_type
id
= *
_removedIds
.
begin
();
64
_removedIds
.
erase
(
id
);
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
75
_MSTD_CONSTEXPR20
bool
return_id
(
id_type
id
) {
76
#
if
_MSTD_HAS_CXX20
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
84
_removedIds
.
insert
(
id
);
85
_update_removed_ids
();
86
return
true
;
87
}
88
89
_MSTD_CONSTEXPR20
void
reset
()
noexcept
{
90
_nextId
= 0;
91
_removedIds
.
clear
();
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
_MSTD_HAS_CXX17
#define _MSTD_HAS_CXX17
Definition
config.hpp:45
_MSTD_CONSTEXPR17
#define _MSTD_CONSTEXPR17
Definition
config.hpp:76
_MSTD_HAS_CXX20
#define _MSTD_HAS_CXX20
Definition
config.hpp:52
_MSTD_CONSTEXPR20
#define _MSTD_CONSTEXPR20
Definition
config.hpp:84
mstd
Definition
arithmetic_types.hpp:23
mstd
include
management
mstd
id_manager.hpp
Generated by
1.16.1