open-cpp-utils 0.0.1
Loading...
Searching...
No Matches
object_pool.h
1// =====================================================================================================================
2// open-cpp-utils, an open-source cpp library with data structures that extend the STL.
3// Copyright (C) 2024 Medusa Slockbower
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <https://www.gnu.org/licenses/>.
17// =====================================================================================================================
18
19#ifndef OPEN_CPP_UTILS_OBJECT_POOL_H
20#define OPEN_CPP_UTILS_OBJECT_POOL_H
21
22#include "dynarray.h"
23#include "optional.h"
24
25namespace open_cpp_utils
26{
27
32template<typename T>
34{
35// Typedefs ============================================================================================================
36
37public:
38 using value_type = T;
39 using pointer = T*;
40 using const_pointer = const T*;
41 using reference = T&;
42 using const_reference = const T&;
43
44 using uuid_type = uint64_t;
45
46private:
48
49
50// Functions ===========================================================================================================
51
52public:
53
54// Constructors & Destructor -------------------------------------------------------------------------------------------
55
56 object_list() = default;
57 object_list(const object_list& other) = default;
58 object_list(object_list&& other) = default;
59 ~object_list() = default;
60
61
62// Modifiers -----------------------------------------------------------------------------------------------------------
63
64 size_t size() const { return data_.size(); }
65 size_t capacity() const { return data_.capacity(); }
66
67
68// Modifiers -----------------------------------------------------------------------------------------------------------
69
70 void clear() { data_.clear(); freed_.clear(); }
71
72 uuid_type insert(const_reference& value);
73 void erase(uuid_type id);
74
75 object_list& operator=(const object_list&) = default;
76 object_list& operator=(object_list&&) = default;
77
78
79// Accessors -----------------------------------------------------------------------------------------------------------
80
81 reference operator[](uuid_type id) { assert(data_[id]()); return data_[id]; }
82 const_reference operator[](uuid_type id) const { assert(data_[id]()); return data_[id]; }
83 bool operator()(uuid_type id) const { return data_[id](); }
84
85 typename dynarray<node>::iterator begin() { return data_.begin(); }
86 typename dynarray<node>::iterator end() { return data_.end(); }
87
88
89// Variables ===========================================================================================================
90
91private:
92 dynarray<node> data_;
93 dynarray<uuid_type> freed_;
94};
95
96template<typename T>
97typename object_list<T>::uuid_type object_list<T>::insert(const_reference value)
98{
99 if(freed_.empty()) { data_.push_back(value); return data_.size() - 1; }
100
101 uuid_type id = freed_.back(); freed_.pop_back();
102 data_[id] = value;
103 return id;
104}
105
106template<typename T>
107void object_list<T>::erase(uuid_type id)
108{
109 data_[id].reset();
110 freed_.push_back(id);
111}
112
113} // open_cpp_utils
114
115#endif // OPEN_CPP_UTILS_OBJECT_POOL_H
Definition object_pool.h:34
Definition optional.h:28