-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathids_pool.hpp
More file actions
72 lines (58 loc) · 1.49 KB
/
Copy pathids_pool.hpp
File metadata and controls
72 lines (58 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* File: /lib/utils/ids_pool.hpp
*
* This file is a part of cpplib project which is distributed under MIT License.
* See file LICENSE for full license details.
*
* Copyright (c) 2020-present Nikita Zuev (V.Slavski!) <[email protected]>
*/
#ifndef CPPLIB__lib__utils__ids_pool__hpp
#define CPPLIB__lib__utils__ids_pool__hpp
#include <algorithm>
#include <iterator>
#include <vector>
#include <cpp/lib_concepts>
#include <cpp/lib_debug>
#include "../../lib/types.hpp"
#include "../../lib/literals.hpp"
namespace lib::utils {
// DECLARATION lib::utils::ids_pool
template< ::cpp::IntegralUnsigned T >
class ids_pool final {
public:
using value_type = T;
constexpr ids_pool() : count{} {}
/*constexpr */value_type retain() {
if ( count == ids.size() ) {
ids.emplace_back( true );
return (value_type)++count;
}
CPP_ASSERT( count < ids.size() );
auto it = ::std::find( ids.begin(), ids.end(), false );
*it = true;
++count;
return (value_type)( ::std::distance( ids.begin(), it ) + 1 );
}
constexpr bool release( value_type id ) {
if ( not contains( id ) )
return false;
--count;
ids[id - 1_sz] = false;
return true;
}
constexpr bool contains( value_type id ) const {
CPP_ASSERT( id > 0 );
return ids.size() >= id and ids[id - 1_sz];
}
constexpr usize size() const {
return count;
}
constexpr void clear() {
ids.clear();
count = 0;
}
private:
::std::vector<bool> ids;
usize count;
};
} // namespace lib::utils
#endif // CPPLIB__lib__utils__ids_pool__hpp