// ===================================================================================================================== // open-cpp-utils, an open-source cpp library with data structures that extend the STL. // Copyright (C) 2024 Medusa Slockbower // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // ===================================================================================================================== #ifndef OPEN_CPP_UTILS_OBJECT_POOL_H #define OPEN_CPP_UTILS_OBJECT_POOL_H #include #include "allocation.h" #include "optional.h" namespace open_cpp_utils { /** * \brief * \tparam T */ template class object_list { // Typedefs ============================================================================================================ public: using value_type = T; using pointer = T*; using const_pointer = const T*; using reference = T&; using const_reference = const T&; using uuid_type = uint64_t; class iterator; class const_iterator; private: using node = optional; using obj_alloc = std::vector; using free_stack = std::deque; // Functions =========================================================================================================== public: // Constructors & Destructor ------------------------------------------------------------------------------------------- object_list() = default; object_list(const object_list& other) = default; object_list(object_list&& other) = default; ~object_list() = default; // Modifiers ----------------------------------------------------------------------------------------------------------- size_t size() const { return data_.size(); } size_t capacity() const { return data_.capacity(); } // Modifiers ----------------------------------------------------------------------------------------------------------- void clear() { data_.clear(); freed_.clear(); } uuid_type insert(const_reference& value); void erase(uuid_type id); object_list& operator=(const object_list&) = default; object_list& operator=(object_list&&) = default; // Accessors ----------------------------------------------------------------------------------------------------------- reference operator[](uuid_type id) { assert(data_[id]()); return data_[id]; } const_reference operator[](uuid_type id) const { assert(data_[id]()); return data_[id]; } bool operator()(uuid_type id) const { return data_[id](); } iterator begin() { return { this, data_.begin() }; } iterator end() { return { this, data_.end() }; } const_iterator begin() const { return { this, data_.begin() }; } const_iterator end() const { return { this, data_.end() }; } // Variables =========================================================================================================== private: obj_alloc data_; free_stack freed_; public: class iterator { public: using obj_iterator = typename obj_alloc::iterator; iterator(object_list* list, obj_iterator start) : list_(list), it_(start) { while(it_ != list_->data_.end() && not (*it_)()) { ++it_; } } iterator& operator++() { ++it_; while(it_ != list_->data_.end() && not (*it_)()) { ++it_; } return *this; } iterator operator++(int) { iterator ret = this; ++it_; while(it_ != list_->data_.end() && not (*it_)()) { ++it_; } return ret; } reference operator*() { return *it_; } bool operator==(const iterator& other) const { return list_ == other.list_ && it_ == other.it_; } bool operator!=(const const_iterator& other) const { return list_ != other.list_ || it_ != other.it_; } uuid_type id() { return (&*it_) - list_->data_.data(); } // Convert to node pointer private: object_list* list_; obj_iterator it_; }; class const_iterator { public: using obj_iterator = typename obj_alloc::pointer; const_iterator(const object_list* list, obj_iterator start) : list_(list), it_(start) { } const_iterator& operator++() { ++it_; while(it_ != list_->data_.end() && not (*it_)()) { ++it_; } return *this; } const_iterator operator++(int) { const_iterator ret = this; ++it_; while(it_ != list_->data_.end() && not (*it_)()) { ++it_; } return ret; } const_reference operator*() { return *it_; } bool operator==(const const_iterator& other) const { return list_ == other.list_ && it_ == other.it_; } bool operator!=(const const_iterator& other) const { return list_ != other.list_ || it_ != other.it_; } uuid_type id() { return (&*it_) - list_->data_.data(); } // Convert to node pointer private: const object_list* list_; obj_iterator it_; }; }; template typename object_list::uuid_type object_list::insert(const_reference value) { if(freed_.empty()) { data_.push_back(value); return data_.size() - 1; } uuid_type id = freed_.back(); freed_.pop_back(); data_[id] = value; return id; } template void object_list::erase(uuid_type id) { data_[id].reset(); freed_.push_back(id); } } // open_cpp_utils #endif // OPEN_CPP_UTILS_OBJECT_POOL_H